Lecture05 Processes
Lecture05 Processes
I/O status information Outstanding I/O requests, assigned I/O devices, list of open files
...
Admit Release
New Ready Running Exit
Timeout
Wait-occurred
Wait-for-event
Blocked
• Process states
– New: process is created, but not yet admitted to the pool of executable
processes by the operating system
• Process Control Block created
• Program may not be loaded yet into main memory
– Ready: process is prepared for execution and waits to be assigned to a
processor
– Running: process is executed
– Blocked: a process waits for some event to occur (such as the reception of a
signal or the completion of an I/O operation)
– Exit: process is released from pool of executable processes, because it halted
or aborted
Suspended Processes
Timeout
Blocked Queue
Event Event Wait
Occurs
• Implementation:
– Using one “Ready” and one “Blocked” queue
– Weakness:
• When a particular event occurs, ALL processes waiting for this event have to
be transferred from the “Blocked” queue to the “Ready” queue
• Operating system has to look through all the entries in the Blocked queue to
select the right processes for transfer
Process Management
• Multiple Event Queues
Ready Queue Release
Admit Dispatch
Processor
Timeout
Event 1 Queue
Event 1 Event 1 Wait
Occurs
Event 2 Queue
Event 2 Event 2 Wait
Occurs
Event n Queue
Event n Event n Wait
Occurs
Process Creation
• Principle Events that lead to the creation of processes
– System boot
• When a system is initialisation, several processes are started
• Under Unix, these are background processes, called “daemons”, such
as sched (pid 0) and init (pid 1), and others such as email server, logon
server, inetd etc.
– An existing process spawns a child process
• e.g.: a server process (web server) may spawn a child process for each
request handled
• The init daemon waits for a user login and spawns a new shell
– User request to create a new process
• User types command in shell or selects widget in GUI to start a
program, this creates a new process with the shell as the parent
– A batch system takes on the next job in line (e.g. Jobs in a
printer queue)
Process Creation
• Assign unique process identifier to the new
process
• Allocate space for the process
– Allocate memory for process image (program,
data, stack)
• Initialise the Process Control Block
• Add process to the Ready queue
Process Hierarchies
• Unix
– Strict hierarchy between processes
• Parent process and all its child processes are associated
– Parent and child processes (and their descendants)
form a process group
– E.g.: The command shell is the parent of all the
processes started by a user from the shell prompt
(foreground and background processes)
• If a user sends a signal (e.g. SIGKILL) to a process group, it is
delivered to all the members
• Each process can catch such a signal (except SIGKILL).
Unix Process Hierarchy
Process Relationships
• Example Windows
– Windows does not have any concept of a process
hierarchy
– All processes are equal
– Process handle: when a process is created by a
“parent” process, the parent receives a process
handle, helps to control the “child”
– This handle can be transferred to other processes
(processes in Unix cannot disinherit their children)
Process Creation
• Unix:
– Process creation via fork() / exec()
– System call fork()
• Creates the exact clone of the calling process, the so-called
“child” process
– System call exec()
• Replaces the process image of this clone with the new
program that should be executed
– Process hierarchy in Unix
• After process creation, parent and child process
have their distinct address spaces
– some resources can be shared such as open files
fork()/exec()
. . .
int main( ... )
{
if ( (pid = fork()) == 0 )
{
/* child process */
printf ( "Child process id: %i\n", getpid() ) ;
/* replace parent process image with executing a new program */
err = execlp ( "/bin/ls", "ls", NULL ) ;
return 0 ;
}
fork() / exec()
Process Termination
• Events
– Normal exit (voluntary) / Error exit (voluntary)
• Regular completion of a process, with or without error code
• Process voluntarily executes the exit(errNo) system call to
indicate to the operating system that it has finished.
– Fatal error (involuntary)
• Uncatchable or uncaught
• Service errors, such as: no memory left for allocation, I/O error,
etc.
• Total time limit exceeded
• Arithmetic error, out-of-bounds memory access, etc.
– Killed by another process via the kernel (involuntary)
• The process receives a SIGKILL signal
• In some systems, the parent takes down all its children with it
Process Switching
• When the operating system switches to another
process for execution, it has to perform a context
switch
– The state of the old process must be stored in the Process
Control Block of the old process
– The state of the newly dispatched process must be
restored from the Process Control Block of the new
process
• Time consumed during context switch is overhead, the
system does no useful work while switching
• Switching Time is dependent on hardware support
Context Switch
Context Switch
• Steps to be taken during context switch
– Save CPU context (PC and other registers)
– Update process state information in PCB (“Ready” or
“Blocked”)
– Move PCB to appropriate queue
– Select another process for execution
• This decision is made by CPU scheduling algorithm
– Update process state information in the PCB of
selected process (“Running”)
– Restore CPU context to the values contained in the
new PCB
Context Switch
• Events that trigger a context switch
– Interrupts
• External asynchronous events, independent of the currently executing
process instructions
– Clock interrupt: operating system checks time elapsed, may block process
– I/O interrupt: I/O request fulfilled, operating system may unblock process
– Memory fault: operating system may block process, must wait for missing
page to be swapped into physical memory
– Exceptions
• Internal synchronous, involuntary, events
– Caused by instructions, operating system may terminate or recover process
– System Calls
• Internal synchronous, voluntary, events
– After service completed, operating system may either resume or block the
calling process, depending on I/O, priorities, etc.