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

Chapter 2

Uploaded by

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

Chapter 2

Uploaded by

Mohammed Ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 2

CHAPTER 2:
PROCESSES &
THREADS
Chapter 2

PROCESSES AND THREADS


2

• Processes
• Threads
• Scheduling
Chapter 2

WHAT IS A PROCESS?
3

• Code, data, and stack


• Usually (but not always) has its own address space

• 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

4 THE PROCESS MODEL


Single PC Multiple PCs • Multiprogramming of four programs
(CPU’s point of view) (process point of view) • Conceptual model
A • 4 independent processes
• Processes run sequentially
B A C D
B • Only one program active at any
C instant!
B • That instant can be very short…
D

D
C
B
A
Time
Chapter 2

WHEN IS A PROCESS CREATED?


5

• Processes can be created in two ways


• System initialization: one or more processes created when the OS starts up
• Execution of a process creation system call: something explicitly asks for a
new process

• System calls can come from


• User request to create a new process (system call executed from user shell)
• Already running processes
• User programs
• System daemons
PROCESS CREATION

• 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

WHEN DO PROCESSES END?


8

• Conditions that terminate processes can be


• Voluntary
• Involuntary

• 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

• Parent creates a child process


• Child processes can create their own children

• Forms a hierarchy
• UNIX calls this a “process group”
• If a process exits, its children are “inherited” by the exiting process’s
parent

• Windows has no concept of process hierarchy


• All processes are created equal
Chapter 2

12 PROCESS STATES

• Process in one of 5 states


Created
• Created
1 • Ready
• Running
Ready
2 • Blocked
• Exit
5
3 • Transitions between states
Blocked
Running 1 - Process enters ready queue
(waiting)
4 2 - Scheduler picks this process
3 - Scheduler picks a different process

7 4 - Process waits for event (such as I/O)


6 5 - Event occurs
7
Exit 6 - Process exits
7 - Process ended by another process
Chapter 2

13 PROCESSES IN THE OS

• Two “layers” for processes


• Lowest layer of process-structured OS handles interrupts, scheduling
• Above that layer are sequential processes
• Processes tracked in the process table
• Each process has a process table entry
Processes

0 1 … N-2 N-1

Scheduler
Chapter 2

WHAT’S IN A PROCESS TABLE ENTRY?


14

Process management File management


May be Registers Root directory
stored Program counter Working (current) directory
on stack CPU status word File descriptors
Stack pointer User ID
Process state Group ID
Priority / scheduling parameters
Process ID
Parent process ID
Memory management
Signals
Pointers to text, data, stack
Process start time
or
Total CPU usage
Pointer to page table
Chapter 2

WHAT HAPPENS ON A TRAP/INTERRUPT?


15

1. Hardware saves program counter (on stack or in a special register)


2. Hardware loads new PC, identifies interrupt
3. Assembly language routine saves registers
4. Assembly language routine sets up stack
5. Assembly language calls C to run service routine
6. Service routine calls scheduler
7. Scheduler selects a process to run next (might be the one interrupted…)
8. Assembly language routine loads PC & registers for the selected process
Chapter 2

16 THREADS: “PROCESSES” SHARING MEMORY

• Process == address space


• Thread == program counter / stream of instructions
• Two examples
• Three processes, each with one thread
• One process with
Process three threads
1 Process 2 Process 3 Process 1

User
space

Threads Threads
System Kernel Kernel
space
Chapter 2

PROCESS & THREAD INFORMATION


17

Per process items


Address space
Open files
Child processes
Signals & handlers
Accounting info
Global variables

Per thread items Per thread items Per thread items


Program counter Program counter Program counter
Registers Registers Registers
Stack & stack pointer Stack & stack pointer Stack & stack pointer
State State State
Chapter 2

THREADS & STACKS


18

Thread 1 Thread 2 Thread 3

User space

Process
Thread 1’s
stack

Thread 3’s
stack

Thread 2’s
stack
Kernel

=> Each thread has its own stack!


Chapter 2

19 WHY USE THREADS?

• Allow a single application to do When in the Course of human events, it


becomes necessary for one people to
We hold these truths to be self-evident,
that all men are created equal, that they
destructive of these ends, it is the Right
of the People to alter or to abolish it,

many things at once


dissolve the political bands which have are endowed by their Creator with and to institute new Government,
connected them with another, and to certain unalienable Rights, that among laying its foundation on such principles
assume among the powers of the earth, these are Life, Liberty and the pursuit and organizing its powers in such form,
the separate and equal station to which of Happiness.--That to secure these as to them shall seem most likely to
the Laws of Nature and of Nature's God rights, Governments are instituted effect their Safety and Happiness.
entitle them, a decent respect to the among Men, deriving their just powers Prudence, indeed, will dictate that

• Simpler programming model opinions of mankind requires that they


should declare the causes which impel
them to the separation.
from the consent of the governed, --
That whenever any Form of
Government becomes
Governments long established should
not be changed for light and transient
causes; and accordingly all

• 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

MULTITHREADED WEB SERVER


20

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

THREE WAYS TO BUILD A SERVER


21

• 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

You might also like