COMP30023 – Computer Systems
© University of Melbourne 2021
Copyright By PowCoder代写 加微信 powcoder
Operating systems:
Process Lifetime and Threads
• What is a process
• Multi-programming paradigm
• User vs. system processes
© University of Melbourne 2
• Process creation and termination
• Process states
© University of Melbourne
Four principal events that cause processes to be
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.
Process Creation
© University of Melbourne 4
Four principal events that cause processes to be
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.
Process Creation
© University of Melbourne 5
Four principal events that cause processes to be
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.
Process Creation
© University of Melbourne 6
Four principal events that cause processes to be
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.
Process Creation
© University of Melbourne 7
• 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;
Example: process creation / control
© University of Melbourne
Typical conditions which terminate a process:
1. Normal exit (voluntary).
2. Error exit (voluntary).
3. Fatal error (involuntary).
4. Killed by another process (involuntary).
Process Termination
© University of Melbourne 10
• 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
Example: process exit
© University of Melbourne
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
Process States (1)
© University of Melbourne 13
Process States (2)
© University of Melbourne 14
The lowest layer of a process-structured operating system
handles interrupts and scheduling. Above that layer are
sequential processes.
Process States (3)
© University of Melbourne 16
Interrupts
© University of Melbourne 18
TB 5-5 How an interrupt happens
The steps in starting an I/O device
and getting an interrupt.
I/O Device Interrupt
© University of Melbourne 19
• 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.
Interrupts
© University of Melbourne
Interrupt vector: address of the interrupt handler
The interrupt handler must
– save the rest of the status of the current process,
– service the interrupt,
– restore what it saved, and
– execute a return from interrupt or similar instruction to restore
whatever the hardware saved when the interrupt occurred (i.e. the
PC, the PSW).
Interrupt vector and handler
© University of Melbourne
• 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
Pseudo-interrupts
© University of Melbourne
• 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 23
Process Table
• “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.
Threads (1)
© University of Melbourne
Threads can communicate with each other without invoking
the kernel – threads share global variables and dynamic
• Shared by threads:
– address space and memory – code and data sections; contents of
memory (global variables, heap); open files; child processes; signal
and signal handlers;
• Threads own copy:
– program counter; registers; stack (local variables, function call
stack); state (running/waiting etc.).
Threads (2)
© University of Melbourne
• Figure 2-7. A word processor with three
Thread Usage: Text Editor
© University of Melbourne 26
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.
Threads vs. Processes
© University of Melbourne
© University of Melbourne 29
Threads vs. Processes
• A POSIX standard API for thread creation and
synchronisation. Most UNIX systems support it.
– all functions start with pthread
– include pthread.h
– all threads have an id of type pthread_t
POSIX: Portable Operating System Interface
Pthreads (1)
© University of Melbourne
• Figure 2-14. Some of the Pthreads function
© University of Melbourne
Pthreads (2)
TB 2-14. Some of the pthread function calls
• Global variables are shared across threads.
– thread switches could occur at any point
– thus, another thread could modify shared data at any time
– consequently, there is a need to synchronize threads – if not,
problems could arise.
Pthreads (3)
© University of Melbourne
Implementing Threads
in User Space
© University of Melbourne
(a) A user-level threads package. (b) A threads package managed by the kernel.
• 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:
– Race conditions – where the output is dependent on the
sequence/timing of events
– Deadlock – each thread is waiting for another thread to complete a
• Requires locks, synchronization, and careful analysis
And finally…
© University of Melbourne
• Processes and their lifetime
• Interrupts
© University of Melbourne
• Interprocess communication
• Scheduling processes on CPU
Next lecture
© University of Melbourne
• 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.
– (And also) Figures 3.10, 4.1 of Modern Operating Concepts
• References: TB 1.3.4, 2.1, 2.2
Acknowledgement
© University of Melbourne
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com