SlideShare a Scribd company logo
PROCESSES & THREADS
PROCESSES AND THREADS
• Processes
• Threads
• Scheduling
• Interprocess communication
• Classical IPC problems
WHAT IS A PROCESS?
• 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!
3
THE PROCESS MODEL
• Multiprogramming of four
programs
• Conceptual model
• 4 independent processes
• Processes run sequentially
• Only one program active at any
instant!
• That instant can be very short…
4
A
C
D
Single PC
(CPU’s point of view)
A
B
C D
Multiple PCs
(process point of view)
B
B
A
B
C
D
Time
WHEN IS A PROCESS CREATED?
• 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
5
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
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
WHEN DO PROCESSES END?
• Conditions that terminate processes can be
• Voluntary
• Involuntary
• Voluntary
• Normal exit
• Error exit
• Involuntary
• Fatal error (only sort of involuntary)
• Killed by another process
8
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
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
PROCESS HIERARCHIES
• 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
11
PROCESS STATES
• Process in one of 5 states
• Created
• Ready
• Running
• Blocked
• Exit
• Transitions between states
1 - Process enters ready queue
2 - Scheduler picks this process
3 - Scheduler picks a different process
4 - Process waits for event (such as I/O)
5 - Event occurs
6 - Process exits
7 - Process ended by another process
12
Blocked
(waiting)
Created
Exit
Ready
Running
1
5
4
3
2
7
7
6
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
13
Scheduler
0 1 N-2 N-1
…
Processes
WHAT’S IN A PROCESS TABLE
ENTRY?
14
File management
Root directory
Working (current) directory
File descriptors
User ID
Group ID
Memory management
Pointers to text, data, stack
or
Pointer to page table
Process management
Registers
Program counter
CPU status word
Stack pointer
Process state
Priority / scheduling parameters
Process ID
Parent process ID
Signals
Process start time
Total CPU usage
May be
stored
on stack
WHAT HAPPENS ON A
TRAP/INTERRUPT?
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
15
THREADS: “PROCESSES” SHARING MEMORY
• Process == address space
• Thread == program counter / stream of instructions
• Two examples
• Three processes, each with one thread
• One process with three threads
16
Kernel Kernel
Threads
Threads
System
space
User
space
Process 1 Process 2 Process 3 Process 1
PROCESS & THREAD INFORMATION
17
Per process items
Address space
Open files
Child processes
Signals & handlers
Accounting info
Global variables
Per thread items
Program counter
Registers
Stack & stack pointer
State
Per thread items
Program counter
Registers
Stack & stack pointer
State
Per thread items
Program counter
Registers
Stack & stack pointer
State
THREADS & STACKS
18
Kernel
Process
Thread 1 Thread 2 Thread 3
Thread 1’s
stack
Thread 3’s
stack
Thread 2’s
stack
User space
=> Each thread has its own stack!
WHY USE THREADS?
• Allow a single application to
do many things at once
• Simpler programming model
• 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
• Example: word processor
• Thread to read from keyboard
• Thread to format document 19
Kernel
MULTITHREADED WEB SERVER
20
Kernel
Network
connection
Dispatcher
thread
Worker
thread
Web page
cache
while(TRUE) {
getNextRequest(&buf);
handoffWork(&buf);
}
while(TRUE) {
waitForWork(&buf);
lookForPageInCache(&buf,&page);
if(pageNotInCache(&page)) {
readPageFromDisk(&buf,&page);
}
returnPage(&page);
}
THREE WAYS TO BUILD A SERVER
• 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
21
IMPLEMENTING THREADS
22
Kernel
Run-time
system
Thread
table
Process
table
Kernel
Thread
Process
Thread
table
Process
table
User-level threads
+ No need for kernel support
- May be slower than kernel threads
- Harder to do non-blocking I/O
Kernel-level threads
+ More flexible scheduling
+ Non-blocking I/O
- Not portable
PRODUCER-CONSUMER PROBLEM
• Paradigm for cooperating processes, producer
process produces information that is consumed by a
consumer process
BOUNDED-BUFFER – SHARED-
MEMORY SOLUTION
• Shared data
• #define BUFFER_SIZE 10
• typedef struct {
• . . .
• } item;
• item buffer[BUFFER_SIZE];
• int in = 0;
• int out = 0;
• Solution is correct, but can only use BUFFER_SIZE-1
elements
BOUNDED-BUFFER – PRODUCER
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
BOUNDED BUFFER – CONSUMER
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
/* consume the item in next consumed */
}
26
SCHEDULING
• What is scheduling?
• Goals
• Mechanisms
• Scheduling on batch systems
• Scheduling on interactive systems
• Other kinds of scheduling
• Real-time scheduling
27
WHY SCHEDULE PROCESSES?
• Bursts of CPU usage alternate with periods of I/O wait
• Some processes are CPU-bound: they don’t make many I/O
requests
• Other processes are I/O-bound and make many kernel requests
28
CPU bound
I/O bound
CPU bursts I/O waits
Total CPU usage
Total CPU usage
Time
WHEN ARE PROCESSES
SCHEDULED?
• At the time they enter the system
• Common in batch systems
• Two types of batch scheduling
• Submission of a new job causes the scheduler to run
• Scheduling only done when a job voluntarily gives up
the CPU (i.e., while waiting for an I/O request)
• At relatively fixed intervals (clock interrupts)
• Necessary for interactive systems
• May also be used for batch systems
• Scheduling algorithms at each interrupt, and picks the next
process from the pool of “ready” processes
29
Scheduling goals
• All systems
• Fairness: give each process a fair share of the CPU
• Enforcement: ensure that the stated policy is carried out
• Balance: keep all parts of the system busy
• Batch systems
• Throughput: maximize jobs per unit time (hour)
• Turnaround time: minimize time users wait for jobs
• CPU utilization: keep the CPU as busy as possible
• Interactive systems
• Response time: respond quickly to users’ requests
• Proportionality: meet users’ expectations
• Real-time systems
• Meet deadlines: missing deadlines is a system failure!
• Predictability: same type of behavior for each time slice
30

More Related Content

Similar to Processes, Threads.pptx (20)

PPTX
Operating Systems Process Management.pptx
Sivakumar M
 
PPT
Processes this has stuff about processes and deifntions.ppt
SLowe7
 
PPTX
Processes
RaviRaj339
 
PPTX
Basic concept of process
Nabin Dahal
 
PDF
Ch3 processes
Welly Dian Astika
 
PPTX
Unit 2_OS process management
JayeshGadhave1
 
PPTX
operating system process management with example
salihan090918
 
PPTX
Os unit 3 , process management
Arnav Chowdhury
 
PPTX
Thread
Syed Zaid Irshad
 
PPTX
UNIT-2-PROCESS MANAGEMENT in opeartive system.pptx
nagarajans87
 
PPT
08 operating system support
Bitta_man
 
PPT
08 operating system support
dilip kumar
 
PPTX
2Chapter Two- Process Management(2) (1).pptx
jamsibro140
 
PPTX
UNIT 2 OS.pptx Introduction of Operating System
DevPatel62412
 
PPTX
Operating system 18 process creation and termination
Vaibhav Khanna
 
PPT
08 operating system support
Sher Shah Merkhel
 
PPTX
Processes and operating systems
RAMPRAKASHT1
 
PPTX
opearating system notes mumbai university.pptx
ssuser3dfcef
 
PPTX
Computer organization and aChapter 2.pptx
gadisaAdamu
 
Operating Systems Process Management.pptx
Sivakumar M
 
Processes this has stuff about processes and deifntions.ppt
SLowe7
 
Processes
RaviRaj339
 
Basic concept of process
Nabin Dahal
 
Ch3 processes
Welly Dian Astika
 
Unit 2_OS process management
JayeshGadhave1
 
operating system process management with example
salihan090918
 
Os unit 3 , process management
Arnav Chowdhury
 
UNIT-2-PROCESS MANAGEMENT in opeartive system.pptx
nagarajans87
 
08 operating system support
Bitta_man
 
08 operating system support
dilip kumar
 
2Chapter Two- Process Management(2) (1).pptx
jamsibro140
 
UNIT 2 OS.pptx Introduction of Operating System
DevPatel62412
 
Operating system 18 process creation and termination
Vaibhav Khanna
 
08 operating system support
Sher Shah Merkhel
 
Processes and operating systems
RAMPRAKASHT1
 
opearating system notes mumbai university.pptx
ssuser3dfcef
 
Computer organization and aChapter 2.pptx
gadisaAdamu
 

More from SKUP1 (20)

PPTX
serial_busses_i2c.pptx
SKUP1
 
PPTX
DESIGN PATTERN.pptx
SKUP1
 
PPTX
INTER PROCESS COMMUNICATION (IPC).pptx
SKUP1
 
PPTX
DATA STRUCTURES AND LINKED LISTS IN C.pptx
SKUP1
 
PPTX
C-Programming File-handling-C.pptx
SKUP1
 
PPTX
C-Programming Control statements.pptx
SKUP1
 
PPTX
Finite State Machine.ppt.pptx
SKUP1
 
PPTX
FUNCTIONS IN C.pptx
SKUP1
 
PPTX
cprogramming strings.pptx
SKUP1
 
PPTX
UNIONS IN C.pptx
SKUP1
 
PPTX
OPERATORS IN C.pptx
SKUP1
 
PPTX
cprogramming Structures.pptx
SKUP1
 
PPTX
C-Programming Function pointers.pptx
SKUP1
 
PPTX
POINTERS.pptx
SKUP1
 
PPTX
STACKS AND QUEUES.pptx
SKUP1
 
PPTX
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
SKUP1
 
PPTX
C MEMORY MODEL​.pptx
SKUP1
 
PPTX
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
SKUP1
 
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
PPTX
COMPILATION PROCESS IN C.pptx
SKUP1
 
serial_busses_i2c.pptx
SKUP1
 
DESIGN PATTERN.pptx
SKUP1
 
INTER PROCESS COMMUNICATION (IPC).pptx
SKUP1
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
SKUP1
 
C-Programming File-handling-C.pptx
SKUP1
 
C-Programming Control statements.pptx
SKUP1
 
Finite State Machine.ppt.pptx
SKUP1
 
FUNCTIONS IN C.pptx
SKUP1
 
cprogramming strings.pptx
SKUP1
 
UNIONS IN C.pptx
SKUP1
 
OPERATORS IN C.pptx
SKUP1
 
cprogramming Structures.pptx
SKUP1
 
C-Programming Function pointers.pptx
SKUP1
 
POINTERS.pptx
SKUP1
 
STACKS AND QUEUES.pptx
SKUP1
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
SKUP1
 
C MEMORY MODEL​.pptx
SKUP1
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
SKUP1
 
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 
COMPILATION PROCESS IN C.pptx
SKUP1
 
Ad

Recently uploaded (20)

PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
cybersecurityandthe importance of the that
JayachanduHNJc
 
PDF
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
cybersecurityandthe importance of the that
JayachanduHNJc
 
Air -Powered Car PPT by ER. SHRESTH SUDHIR KOKNE.pdf
SHRESTHKOKNE
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
Ad

Processes, Threads.pptx

  • 2. PROCESSES AND THREADS • Processes • Threads • Scheduling • Interprocess communication • Classical IPC problems
  • 3. WHAT IS A PROCESS? • 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! 3
  • 4. THE PROCESS MODEL • Multiprogramming of four programs • Conceptual model • 4 independent processes • Processes run sequentially • Only one program active at any instant! • That instant can be very short… 4 A C D Single PC (CPU’s point of view) A B C D Multiple PCs (process point of view) B B A B C D Time
  • 5. WHEN IS A PROCESS CREATED? • 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 5
  • 6. 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
  • 7. 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
  • 8. WHEN DO PROCESSES END? • Conditions that terminate processes can be • Voluntary • Involuntary • Voluntary • Normal exit • Error exit • Involuntary • Fatal error (only sort of involuntary) • Killed by another process 8
  • 9. 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
  • 10. 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
  • 11. PROCESS HIERARCHIES • 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 11
  • 12. PROCESS STATES • Process in one of 5 states • Created • Ready • Running • Blocked • Exit • Transitions between states 1 - Process enters ready queue 2 - Scheduler picks this process 3 - Scheduler picks a different process 4 - Process waits for event (such as I/O) 5 - Event occurs 6 - Process exits 7 - Process ended by another process 12 Blocked (waiting) Created Exit Ready Running 1 5 4 3 2 7 7 6
  • 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 13 Scheduler 0 1 N-2 N-1 … Processes
  • 14. WHAT’S IN A PROCESS TABLE ENTRY? 14 File management Root directory Working (current) directory File descriptors User ID Group ID Memory management Pointers to text, data, stack or Pointer to page table Process management Registers Program counter CPU status word Stack pointer Process state Priority / scheduling parameters Process ID Parent process ID Signals Process start time Total CPU usage May be stored on stack
  • 15. WHAT HAPPENS ON A TRAP/INTERRUPT? 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 15
  • 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 three threads 16 Kernel Kernel Threads Threads System space User space Process 1 Process 2 Process 3 Process 1
  • 17. PROCESS & THREAD INFORMATION 17 Per process items Address space Open files Child processes Signals & handlers Accounting info Global variables Per thread items Program counter Registers Stack & stack pointer State Per thread items Program counter Registers Stack & stack pointer State Per thread items Program counter Registers Stack & stack pointer State
  • 18. THREADS & STACKS 18 Kernel Process Thread 1 Thread 2 Thread 3 Thread 1’s stack Thread 3’s stack Thread 2’s stack User space => Each thread has its own stack!
  • 19. WHY USE THREADS? • Allow a single application to do many things at once • Simpler programming model • 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 • Example: word processor • Thread to read from keyboard • Thread to format document 19 Kernel
  • 20. MULTITHREADED WEB SERVER 20 Kernel Network connection Dispatcher thread Worker thread Web page cache while(TRUE) { getNextRequest(&buf); handoffWork(&buf); } while(TRUE) { waitForWork(&buf); lookForPageInCache(&buf,&page); if(pageNotInCache(&page)) { readPageFromDisk(&buf,&page); } returnPage(&page); }
  • 21. THREE WAYS TO BUILD A SERVER • 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 21
  • 22. IMPLEMENTING THREADS 22 Kernel Run-time system Thread table Process table Kernel Thread Process Thread table Process table User-level threads + No need for kernel support - May be slower than kernel threads - Harder to do non-blocking I/O Kernel-level threads + More flexible scheduling + Non-blocking I/O - Not portable
  • 23. PRODUCER-CONSUMER PROBLEM • Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process
  • 24. BOUNDED-BUFFER – SHARED- MEMORY SOLUTION • Shared data • #define BUFFER_SIZE 10 • typedef struct { • . . . • } item; • item buffer[BUFFER_SIZE]; • int in = 0; • int out = 0; • Solution is correct, but can only use BUFFER_SIZE-1 elements
  • 25. BOUNDED-BUFFER – PRODUCER item next_produced; while (true) { /* produce an item in next produced */ while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; }
  • 26. BOUNDED BUFFER – CONSUMER item next_consumed; while (true) { while (in == out) ; /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; /* consume the item in next consumed */ } 26
  • 27. SCHEDULING • What is scheduling? • Goals • Mechanisms • Scheduling on batch systems • Scheduling on interactive systems • Other kinds of scheduling • Real-time scheduling 27
  • 28. WHY SCHEDULE PROCESSES? • Bursts of CPU usage alternate with periods of I/O wait • Some processes are CPU-bound: they don’t make many I/O requests • Other processes are I/O-bound and make many kernel requests 28 CPU bound I/O bound CPU bursts I/O waits Total CPU usage Total CPU usage Time
  • 29. WHEN ARE PROCESSES SCHEDULED? • At the time they enter the system • Common in batch systems • Two types of batch scheduling • Submission of a new job causes the scheduler to run • Scheduling only done when a job voluntarily gives up the CPU (i.e., while waiting for an I/O request) • At relatively fixed intervals (clock interrupts) • Necessary for interactive systems • May also be used for batch systems • Scheduling algorithms at each interrupt, and picks the next process from the pool of “ready” processes 29
  • 30. Scheduling goals • All systems • Fairness: give each process a fair share of the CPU • Enforcement: ensure that the stated policy is carried out • Balance: keep all parts of the system busy • Batch systems • Throughput: maximize jobs per unit time (hour) • Turnaround time: minimize time users wait for jobs • CPU utilization: keep the CPU as busy as possible • Interactive systems • Response time: respond quickly to users’ requests • Proportionality: meet users’ expectations • Real-time systems • Meet deadlines: missing deadlines is a system failure! • Predictability: same type of behavior for each time slice 30