代写代考 COMP30023 – Computer Systems

COMP30023 – Computer Systems
Operating systems:
Process Lifetime and Threads
© University of Melbourne 2021

Copyright By PowCoder代写 加微信 powcoder

• What is a process
• Multi-programming paradigm
• User vs. system processes
© University of Melbourne

• Process creation and termination
• Process states • Threads
© University of Melbourne

Process Creation
Four principal events that cause processes to be created:
1. System initialisation.
2. Execution of a process creation system call by a running process.
3. A user request to create a new process.
4. Initiation of a batch job.
© University of Melbourne

Process Creation
Four principal events that cause processes to be created:
1. System initialisation.
2. Execution of a process creation system call by a running process.
3. A user request to create a new process.
4. Initiation of a batch job.
© University of Melbourne

Process Creation
Four principal events that cause processes to be created:
1. System initialisation.
2. Execution of a process creation system call by a running process.
3. A user request to create a new process.
4. Initiation of a batch job.
© University of Melbourne

Process Creation
Four principal events that cause processes to be created:
1. System initialisation.
2. Execution of a process creation system call by a running process.
3. A user request to create a new process.
4. Initiation of a batch job.
© University of Melbourne

Example: process creation / control
• fork() creates a new process; copy of the parent process
• fork() returns pid value (each process has a process id)
• execve() is used after a fork to replace one of the two processes virtual memory space with a new program;
© University of Melbourne

Process Termination
Typical conditions which terminate a process: 1. Normalexit(voluntary).
2. Errorexit(voluntary).
3. Fatalerror(involuntary).
4. Killedbyanotherprocess(involuntary).
© University of Melbourne

Example: process exit
• exit() terminates a process
• A parent may wait for a child process to terminate: wait provides the process id of a terminated child so that the parent can tell which child terminated; wait3 allows the parent to collect performance statistics about the child
© University of Melbourne

Process States (1)
Three states a process may be in:
1. Running (actually using the CPU at that instant).
2. Ready (runnable; temporarily stopped while another process is running).
3. Blocked (unable to run until some external event happens).
© University of Melbourne

Process States (2)
© University of Melbourne

Process States (3)
The lowest layer of a process-structured operating system handles interrupts and scheduling. Above that layer are sequential processes.
© University of Melbourne

Interrupts
TB 5-5 How an interrupt happens
© University of Melbourne

I/O Device Interrupt
The steps in starting an I/O device and getting an interrupt.
© University of Melbourne

Interrupts
• When a hardware device needs attention from the CPU, e.g. because it has finished carrying out its current command and is ready to receive its next command, it generates a signal to interrupt the CPU.
• Asynchronous with the currently executing process
• When an interrupt occurs, the CPU’s hardware takes the values in the program counter and program status word registers (and, on some kinds of machines, the stack pointer register), and saves them in privileged memory locations reserved for this purpose.
• It then replaces them with new values.
• The replacement PSW will put the CPU into kernel mode. The replacement PC will cause execution to resume at the start of the interrupt handler, code that is part of the kernel.
© University of Melbourne

Interrupt vector and handler
Interrupt vector: address of the interrupt handler
The interrupt handler must
– savetherestofthestatusofthecurrentprocess,
– servicetheinterrupt,
– restorewhatitsaved,and
– executeareturnfrominterruptorsimilarinstructiontorestore whatever the hardware saved when the interrupt occurred (i.e. the PC, the PSW).
© University of Melbourne

Pseudo-interrupts
• True interrupts come from hardware devices outside the CPU, pseudo-interrupts from the CPU itself.
• User programs may generate pseudo-interrupts inadvertently, e.g. by executing divide-by-zero: such events are usually called exceptions. Some exceptions cause process termination.
• Users can generate pseudo-interrupts intentionally by executing a special instruction for system calls (e.g., CTRL-C)
• Catch interrupts with trap command
© University of Melbourne

Process Table
• One entry per process
• Contains state information to resume a process
• Fields include information about:
– process management (e.g., registers, program counter, program status word)
– memory management – file management
© University of Melbourne

Threads (1)
• “Lightweight process”
• Thread definition: a sequential execution stream within the
• Threads are the basic unit of CPU utilization – including the program counter, register set and stack space.
© University of Melbourne

Threads (2)
Threads can communicate with each other without invoking the kernel – threads share global variables and dynamic memory.
• Shared by threads:
– addressspaceandmemory–codeanddatasections;contentsof memory (global variables, heap); open files; child processes; signal and signal handlers;
• Threads own copy:
– programcounter;registers;stack(localvariables,functioncall stack); state (running/waiting etc.).
© University of Melbourne

Thread Usage: Text Editor
• Figure 2-7. A word processor with three threads.
© University of Melbourne

Threads vs. Processes
A process has one container but may have more than one thread, and each thread can perform computations (almost) independently of the other threads in the process.
There is reduced overhead than when using multiple processes
– less time to create a new thread;
– less time to terminate;
– less time to switch between threads;
– less time to communicate between threads.
© University of Melbourne

Threads vs. Processes
© University of Melbourne

Pthreads (1)
• A POSIX standard API for thread creation and synchronisation. Most UNIX systems support it.
– allfunctionsstartwithpthread
– includepthread.h
– allthreadshaveanidoftypepthread_t
POSIX: Portable Operating System Interface
© University of Melbourne

Pthreads (2)
• Figure 2-14. Some of the Pthreads function calls.
TB 2-14. Some of the pthread function calls
© University of Melbourne

Pthreads (3)
• Global variables are shared across threads. – threadswitchescouldoccuratanypoint
– thus,anotherthreadcouldmodifyshareddataatanytime
– consequently,thereisaneedtosynchronizethreads–ifnot, problems could arise.
© University of Melbourne

Implementing Threads in User Space
TB 2-16 (a) (b)
(a) A user-level threads package. (b) A threads package managed by the kernel.
© University of Melbourne

And finally…
• Multi-threading is extremely powerful, but it comes with a significant challenge: Concurrency
• If you are running multiple threads with a shared memory/data store modelling how they interact becomes critical, otherwise the following can happen:
– Raceconditions–wheretheoutputisdependentonthe sequence/timing of events
– Deadlock–eachthreadiswaitingforanotherthreadtocompletea task
• Requires locks, synchronization, and careful analysis
© University of Melbourne

• Processes and their lifetime • Interrupts
© University of Melbourne

Next lecture
• Interprocess communication
• Scheduling processes on CPU
© University of Melbourne

Acknowledgement
• The slides were prepared by based on material developed previously by: , , , , , and .
• Some of the images included in the notes were supplied as part of the teaching resources accompanying the text books listed in lecture 1.
– (Andalso)Figures3.10,4.1ofModernOperatingConcepts
• References: TB 1.3.4, 2.1, 2.2
© University of Melbourne

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com