Chapter 2
Chapter 2
CHAPTER 2:
PROCESSES &
THREADS
Chapter 2
• Processes
• Threads
• Scheduling
Chapter 2
WHAT IS A PROCESS?
3
• Program state
• CPU registers
• Program counter (current location in the code)
• Stack pointer
• Only one process can be running in the CPU at any given time!
Chapter 2
D
C
B
A
Time
Chapter 2
• Parent process creates children processes, which, in turn create other processes,
forming a tree of processes
• Generally, process identified and managed via a process identifier (pid)
• Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources
• Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
6
Spring 2018
PROCESS CREATION (CONT.)
• Address space
• Child duplicate of parent
• Child has a program loaded into it
• UNIX examples
• fork() system call creates new process
• exec() system call used after a fork() to replace the process’ memory
space with a new program
7
Spring 2018
Chapter 2
• Voluntary
• Normal exit
• Error exit
• Involuntary
• Fatal error (only sort of involuntary)
• Killed by another process
PROCESS TERMINATION
• Process executes last statement and then asks the operating system to delete
it using the exit() system call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
• Parent may terminate the execution of children processes using the abort()
system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting and the operating systems does not allow a child to continue if
its parent terminates
9
Spring 2018
PROCESS TERMINATION
• Some operating systems do not allow a child to exist if its parent has terminated. If a
process terminates, then all its children must also be terminated.
• cascading termination. All children, grandchildren, etc. are terminated.
• The termination is initiated by the operating system.
• The parent process may wait for termination of a child process by using the wait()system
call. The call returns status information and the pid of the terminated process
pid = wait(&status);
• If no parent waiting (did not invoke wait()) process is a zombie
• If parent terminated without invoking wait , process is an orphan
10
Spring 2018
Chapter 2
PROCESS HIERARCHIES
11
• Forms a hierarchy
• UNIX calls this a “process group”
• If a process exits, its children are “inherited” by the exiting process’s
parent
12 PROCESS STATES
13 PROCESSES IN THE OS
0 1 … N-2 N-1
Scheduler
Chapter 2
User
space
Threads Threads
System Kernel Kernel
space
Chapter 2
User space
Process
Thread 1’s
stack
Thread 3’s
stack
Thread 2’s
stack
Kernel
• Less waiting
• Threads are faster to create or
destroy
• No separate address space
• Overlap computation and I/O
• Could be done without threads,
but it’s harder
Kernel
• Example: word processor
• Thread to read from keyboard
• Thread to format document
• Thread to write to disk
Chapter 2
Dispatcher while(TRUE) {
thread getNextRequest(&buf);
Worker handoffWork(&buf);
thread }
while(TRUE) {
waitForWork(&buf);
lookForPageInCache(&buf,&page);
if(pageNotInCache(&page)) {
Kernel
readPageFromDisk(&buf,&page);
Web page }
cache returnPage(&page);
Network
}
connection
Chapter 2
• Thread model
• Parallelism
• Blocking system calls
• Single-threaded process: slow, but easier to do
• No parallelism
• Blocking system calls
• Finite-state machine
• Each activity has its own state
• States change when system calls complete or interrupts occur
• Parallelism
• Nonblocking system calls
• Interrupts