Processes
Processes
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 1
Processes: The Process Model
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 2
Processes and programs
The difference between a process and a program:
Baking analogy:
o Recipe = Program
o Baker = Processor
o Ingredients = data
o Baking the cake = Process
Interrupt analogy
o The baker’s son runs in with a wounded hand
o First aid guide = interrupt code
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 3
Main OS Process-related Goals
Interleave the execution of existing processes to
maximize processor utilization
Provide reasonable response times
Allocate resources to processes
Support inter-process communication (and
synchronization) and user creation of processes
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 4
How are these goals achieved?
Schedule and dispatch processes for execution by the
processor
Implement a safe and fair policy for resource
allocation to processes
Respond to requests by user programs
Construct and maintain tables for each process
managed by the operating system
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 5
Process Creation
?When is a new process created
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 6
Process Termination
?When does a process terminate
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 7
Processes: outline
Basic concepts
Process states and structures
Process management
signals
Threads
Specific implementations
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 8
Process States
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 9
Process State Transitions
When do these
?transitions occur Running
Dispatch Release
Admit
New Ready Running Exit
Time-out
Event Event
Occurs Wait
Blocked
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 11
Scheduling: Single Blocked Queue
Ready Queue
Dispatch Release
Admit
Processor
Time-out
Event Wait
Event
Occurs
Blocked Queue
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 12
Scheduling: Multiple Blocked Queues
Ready Queue
Dispatch Release
Admit
Processor
Time-out
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 14
Process State Transition Diagram with Two
Suspend States
New
Admit Suspend
Admit
Activate Dispatch
Ready, Ready Running Exit
suspend
Suspend Time out
Event Event
Event
Occurs Wait
Occurs
Activate
Blocked, Blocked
suspend
Suspend Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon 15
Meisels
Process Management Operations
Process creation and termination
Process scheduling and dispatching
Process switching
Process synchronization and support for inter-
process communication
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 16
Process Table
Process image consists of program (code/text), data,
stack, and attributes
Control Attributes form the Process Control Block -
PCB
o Unique ID (may be an index into the PT)
o User ID; User group ID, Parent process ID
o process control information
o Processor state information
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 17
Process Control Information
Additional information needed by the operating
system to control and coordinate the various active
processes
o Execution state: see next slide…
o Scheduling-related information - state; priority;
scheduling info
o inter-process communication - signals; pipes
o Time of next alarm
o memory management - pointers to text/data/stack
segments
o resource ownership and utilization - open files
o Process relationships: Parent, process group…
o Environment variables
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 18
Processor State Information
Contents of processor registers
o General registers
o Program counter
o Program Status Word (PSW)
• condition codes
• mode (user/kernel)
• status register - interrupts disabled/enabled
o Stack pointers - user and kernel stacks
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 19
Process-State-Management
Process
Control
Block
Running
Ready
Blocked
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 20
Processes: outline
Basic concepts
Process states and structures
Process management
signals
Threads
Specific implementations
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 21
Process Creation
Assign a unique process identifier
Allocate space for the process
Initialize process control block
Set up appropriate linkage to the scheduling
queue:
o In the former example: add the PCB to the ready queue
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 22
Stop a running process
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 23
Process Context Switch
Save processor context, including program counter
and other registers
Update the process control block with the new state
and any accounting information
Move process control block to appropriate queue -
ready, blocked
Select another process for execution
Update the process control block of the process
selected
Update memory-management data structures
Restore context of selected process
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 24
Switching Processes
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 26
The Unix Process
fork system call:
o memory address space is “copied”
o parent receives pid of child (value of fork())
o child gets 0 (value of fork())
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 28
Executing a New Program (Unix)
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 29
Executing the ls command
User
code
Kernel
code
Basic concepts
Process states and structures
Process management
Signals
Threads
Specific implementations
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 31
31
Unix signals
A signal is a software interrupt
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 32
Handling signals
Upon receiving a signal the process can:
o Ignore it (not always…)
o Let the system take default action
o Catch it by a process' signal handler
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 33
More on Unix signals
kernel sets signal bits in the PCB upon receiving
signals (software interrupt)
Some Examples (predefined signal numbers):
o sigabrt - abort process (core dump)
o sigalrm - alarm clock (alarm, sleep, pause)
o sigsegv - segmentation violation (invalid address)
o sigkill – kill the process
o sigill - illegal instruction
Upon child process termination, the signal
SIGCHILD is sent to parent. If parent executes
wait(), it gets the exit code too
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 34
Signals: a simple example
int main(void) {
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
err_sys(“can’t catch SIGUSR1”);
if (signal(SIGUSR2, sig_usr) == SIG_ERR)
err_sys(“can’t catch SIGUSR2”)
for ( ; ; )
pause(); }
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 36
System Calls for Process Management
s is an error code
pid is a process ID
residual is the remaining time from the previous alarm
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 37
Terminated processes
If a child process terminates and the parent doesn’t
execute `wait’, the child becomes a zombie – it still
holds a PTE
An ancestor can receive the process exit code stored
in the PTE
Zombie entries can be erased by the kernel when an
ancestor executes a wait() system call
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 38
Processes: outline
Basic concepts
Process states and structures
Process management
Signals
Threads
Specific implementations
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 39 39
Threads
Need:
Multiprogramming within a single application
Using the same environment for performing
different tasks concurrently
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 40
Single and multithreaded processes
process
control
code data files process code data files
block
control
registers user/kernel stacks block registers registers registers
Thread
user user user
control
/kernel /kernel /kernel
blocks
stacks stacks stacks
thread
thread thread thread
single threaded multithreaded
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 41
The Thread Model
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 42
Processes Threads
The basic unit of CPU scheduling - threads:
o program counter; register set; stack space
Peer threads share resources like code section and data
section
a process is created with a single thread
multi-threaded tasks (processes) can have one thread running
while another is blocked
Good for applications that require sharing a common buffer by
server threads
A word processor can use three threads
Updating the display (WYSIWYG)
Interacting with the user (keyboard & mouse)
Dealing with i/o to the disk
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 43
Processes Threads
Multithreading in different operating systems:
Operating systems support multiple threads of
execution within a single process
Older UNIX systems supported multiple user processes
but only one thread per process; new Unix systems
have multiple threads.
Windows NT supports multiple threads
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 44
The Benefits of Threads
Takes less time to create a new thread than a
process
Less time to terminate a thread than a process
Less time to switch between two threads within the
same process
Threads within the same process share memory and
files --> they can communicate without invoking the
kernel
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 45
Creation time: process vs. thread
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 46
More on Threads
Per-thread dynamic storage for local variables
Access to process' memory and resources
o all threads of a process share these
Suspending a process suspends all process threads
since all threads share the same PTE
Termination of a process, terminates all threads within
the process
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 47
Issues of threads
Fork – should all threads be inherited?
If so, and a parent thread was blocked on keyboard
read, would the corresponding child thread be in the
same state?
What if one thread closes a file while the other is still
reading it?
Which threads should receive signals?
…
User User
space space
Runtime
system Process table Process Threads
table table
Threads table
Operating Systems, 2013, Meni Adler, Michael Elhadad & 49
Amnon Meisels
User-Level Threads
All thread management is done by the application
The kernel is not aware of the existence of threads
Thread switching does not require kernel mode
privileges (and is thus faster)
Scheduling is application specific (can thus be more
efficient)
System calls by threads block the process
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 50
User-level Threads - Problems
Blocking read – all other threads are blocked!
o In Unix, use “select” - if data not in buffer, switch to another
thread
Page fault – all other threads are blocked!
Time limit– cannot handle clock interrupts PER
THREAD! Need other method e.g, thread_yield
Stack growth fault – kernel is not aware of which
thread’s stack caused the fault!
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 51
Kernel-level Threads
Kernel maintains context information for the process
and the threads
Kernel can schedule different threads of the same
process to different processors
Switching between threads requires the kernel
Kernel threads can simplify context switch of system
functions
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 52
Multi-cores:
Chip Multi-Threading (CMT)
Multiple cores on the same silicon die
On-core L1 cache
External L2 cache (may be either split or joined)
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 53
Execution on single-core vs. multi-core
Pipelining permits instruction-level parallelism (ILP)
Multi-core permits both ILP and Thread-Level-Parallelism
(TLP) on same CPU
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 55
Processes: outline
Basic concepts
Process states and structures
Process management
Signals
Threads
Specific implementations
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 56 56
Solaris 2-8: A Combined Approach for Threads
Thread creation, scheduling and synchronization can be done
in user space
Multiple user-level threads are mapped onto some (smaller or
equal) number of kernel-level threads
In Unix Solaris, a kernel thread into which user threads can be
mapped is called LWP (light-weight process) and an API is
provided to map a user thread to a LWP
Some kernel threads have no associated LWP
A user thread may be bound to a LWP for quick response
Many-to-many model
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 57
Threads in Solaris
58
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon
Threads & LWP Structure
Threads
Threads library
LWPs
Kernel - OS
Scheduler
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 59
Threads in Unix-Solaris
thr_create create thread
thr_join causes the calling thread to wait until target thread is finished
thr_exit destroy calling thread
thr_suspend suspend target thread
thr_continue make suspended thread active
thr_setconcurrency set desired number threads active at the same time to a new parameter
thr_getconcurrency get current concurrency level
thr_setprio set thread relative priority
thr_getprio get relative priority of the thread
thr_yield causes the current thread to yield its execution in favor of another
thread with the same or greater priority
thr_kill kill a thread
thr_keycreate allocate a key that locates data specific to each thread in the process
thr_min_stack amount of space needed to execute a null thread
thr_setspecific binds thread-specific value to the key
thr get-specific gets thread-specific value of the key
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 60
Threads in POSIX
The principal POSIX thread calls.
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 61
Threads – Sharing options (Linux)
;Pid = clone(function, stack_ptr, sharing_flags, arg)
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 62
Windows – Processes and Threads
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 63
Windows: jobs, processes, threads
Operating Systems, 2013, Meni Adler, Michael Elhadad & Amnon Meisels 66