0% found this document useful (0 votes)
21 views

Lecture05 Processes

The document discusses processes in operating systems. A process consists of code, data, and context. The OS maintains process information in a process table and process control block. Processes can be in different states like running, ready, blocked, and transitions between these states are managed by the OS scheduler.

Uploaded by

Daniel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lecture05 Processes

The document discusses processes in operating systems. A process consists of code, data, and context. The OS maintains process information in a process table and process control block. Processes can be in different states like running, ready, blocked, and transitions between these states are managed by the OS scheduler.

Uploaded by

Daniel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Processes

CS3008 Operating Systems


Lecture 05
Process
• Definitions:
– A process is a program in execution
– The entity that can be assigned to and executed on a
processor
• Processes are a fundamental concept of an operating
system
– As a unit of execution, it enables the concurrent execution
of multiple programs on a system
• CPU switches between processes
– All the running software on a computer system, including
the operating system itself, is organised as a set of
processes
Process
• A process consists of three basic
components
– An executable program code
– Associated data needed by the
program
– Execution context
• Information needed by the
operating system to manage a
process
– Process ID
– CPU registers, Program counter
– Stack content
• Execution context essential for
process switching
• Operating system maintains a
process list of all processes
currently in the system
Process Image
• Allocation of a process in memory max
stack Stack segment
– Process Image
• Each process runs in its own virtual
memory address space that
consists of
– Stack segment: used for function
and system calls
– Data segment: variables, both Data segment
static and dynamic allocation on
the heap
heap
– Text segment: the program code,
usually read-only
• Invoking the same program data
multiple times results in the
creation of separate processes, program code Text segment
each with its distinct virtual 0
address space and environment
Process Control Structures
• Process Table: holds
one entry per process
– Process Control Block
• Process Control Block
– Describes a single
process and its current
state
• Process Image
– Memory allocation for
process
Process Control Block
Process Identifier A unique identifier associated with the process

Process State The state of the process, when it was interrupted

Priority Priority level relative to other processes

Program counter Address of next instruction to be executed

Memory pointers Pointers to program code and data

Context data Register content

I/O status information Outstanding I/O requests, assigned I/O devices, list of open files

Accounting information Used processor time, time limits etc.

...

• The Process Control Block (PCB) contains all the


information necessary to suspend a process and
later resume its execution
Process Control Block
• Process identification
– Process ID
– Identifier of creating process and user
• Processor state information
– CPU registers: user-visible registers, control and status registers, stack
pointers, program counter
• Process control information
– CPU scheduling information: process state, priority, scheduling-related
information
– Process privileges: access privileges to memory, allowed instructions
– Memory management information: information about the virtual
memory assigned to this process (segment, paging tables, various
control registers)
– Accounting information: time limits, accumulated CPU time etc.
– I/O status information: resource ownership, open files, allocated I/O
devices
Process Dispatch and Process States
• Processes are “dispatched” for execution by the
operating system
– Dispatcher
• OS function that allocates CPU to processes, switches CPU
from one process to the next
• At any time, a process will be in a particular state
– Waiting for I/O
– Currently executing
– Ready to be executed, with all resources assigned
– Swapped out
– Etc.
Process States
• Each process is an independent entity
– Has its own process counter (PC), own internal state
• Many processes run on a system, some of them may
interact
• Example:
– Processes “cat” and “grep”
$ cat textfile1 textfile2 textfile3 | grep “some pattern”

– The first process, “cat” concatenates and outputs three


files to stdout
– There is a pipe established between the two processes
– The second process, “grep” reads from stdin and prints out
those lines that contain the pattern “some pattern”
Process State
$ cat textfile1 textfile2 textfile3 | grep “some pattern”

• Both processes are started at the same time, run


concurrently and compete for CPU time
• No particular sequence guaranteed how processes are
scheduled:
– Process “grep” may be executed first, before process “cat”
can produce an output
– Process “grep” is ready to run, but has to wait for input, it
will block
– This gives process “cat” an opportunity to gain execution
time on the CPU in order to process its input and produce
output
Process State
1. Process blocked for I/O
Wait-for-event Running 2. 2. Dispatcher schedules another
1. Dispatch process
Timeout 3. Dispatcher schedules this process
Blocked Ready 4. Input becomes available, blocked
3.
process made ready
4.
Event-occurred

• We can distinguish three basic process states


– Running: actually using the CPU
– Ready: being runnable, temporarily stopped (time-out) to
let another process execute
– Blocked/Waiting: unable to run until some external event
happens, such as I/O completion
Process States
State Transitions
• Transition Running – Blocked:
– Operating systems discovers that process cannot continue right
now
• Unix: process is automatically blocked by dispatcher
• Other systems: processes may execute a system call such as “pause”
to set themselves into blocked state
• Transition Running – Ready – Running:
– Performed by dispatcher, without process knowing
– When the time slice of a process expires, it is set into Ready
state
– Process is transferred back into Running state according to some
scheduling policy (performance vs fairness)
• Transition Blocked – Ready
– Process is transferred into Ready state, when external event
occurs
State Transitions
• Scheduling / dispatching a process
– A process in “Ready” state is chosen and transferred into the
“Running” state:
• Program code loaded, the execution continues according to the information
held in the PCB
• Suspending a process:
– transfer from state “Running” into “Ready”
• Process is “preempted”:
– Process has reached a timeout (maximum allotted time for execution)
– Process of a higher priority is scheduled
• Process gives up processor voluntarily before timeout (background process)
– Transfer from state “Running” into “Blocked”
• Process makes a request to operating system in the form of a system service
call and has to wait for the call to be completed
– E.g. request to access a file or shared section of virtual memory
– E.g. I/O operation that must be completed
– E.g. Inter-process communication: Process communicates with another process and
waits for response
Process List Structures
Five-State Process Model
Dispatch

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

• Processes will be swapped out to disk to free physical memory


• Swapped out processes become suspended
– I/O operation needed to swap them in
– Two new states: Blocked-Suspended, Ready-Suspended
Process Scheduling / Dispatch Queues
• Operating system maintains a set of queues to
manage process scheduling
– Job queue: set of all processes in the system
– Ready queue: set of all processes ready for execution
• Memory allocated for process image
– Queues for waiting processes
• Either one Blocked queue or different queues for processes
waiting for particular events
– E.g.: Device queue: processes that wait for an I/O device to
become available
• Processes migrate between these queues
Process Management
Ready Queue Release
Admit Dispatch
Processor

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 ) ;

fprintf ( stderr, "Child execution: %d\n, err ) ;


exit ( err ) ;
}
else if ( pid > 0 )
{
/* parent process */
printf ( "Parent process id %d\n", getpid() ) ;
/* parent will wait for child process to complete */
child = waitpid ( pid, &status, 0) ;
}

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.

You might also like