CH 02
CH 02
Chapter 2
Processes
The Process Model
• Process is an abstraction of a running program
Figure 1.(b) Conceptual model of four independent, sequential processes (each running
independently)
The Process Model (3)
• Logically, the first two states are similar. In both case is willing to
run
• only in the second one, there is temporarily no CPU available for
it.
Process States
• Each process entry contains information about the process that must
be saved when the process is switched from running and ready state
it can be restarted later as if it had never been stopped.
Figure 5 Skeleton of what the lowest level of the operating system does when an
interrupt occurs.
Threads
• A threads are a way for a program to fork (or split) itself into two
or more simultaneously (or pseudo-simultaneously) running
tasks.
Figure 7. The first column lists some items shared by all threads in a process. The
second one lists some items private to each thread.
InterProcess Communication (IPC)
• Process frequently need to communicate with other processes in
well-structured way not using interruption.
•Example: Shell pipeline
•3 issues
− How one process can pass information to another
− Making sure that 2 or more processes do not get into each
other’s way when engaging in critical activities
− Proper sequencing when dependencies are present: process A
produce data and process B print it B must wait A has
produced data
Race Conditions
• In some OS, processes that are working together may share some
common storage that each one can read and write.
3. No process running outside its critical region may block other processes.
• Each process disable all interrupts just after entering its critical
region and re-enable them just before leaving it.
Disabling interrupts
•Lots of disadvantages:
− Give the user processes the power to turn off the interrupts. Problem: if the
user turn on the interrupts and never turn them again end of the system
− In multiprogramming system with multiple CPU. Disabling interrupts
affects only the CPU that executed the disable instructions. The other CPU will
continue running and can access the shared memory.
•Disadvantages
− CPU time wastage
− Violates condition 3
Strict Alternation
Figure10. solution to the critical region problem. (a) Process 0. (b) Process 1.
In both cases, be sure to note the semicolons terminating the while statements.
Peterson’s Solution
{ ...
Figure 12. Entering and leaving a critical region using the TSL instruction.
Tanenbaum & Woodhull, Operating
Systems: Design and Implementation, (c)
2006 Prentice-Hall, Inc. All rights reserved.
The Producer-Consumer Problem
• Two classes of processes
– Producers, which produce times and insert them into a buffer.
– Consumers, which remove items and consume them.
...
The Producer-Consumer Problem
...
Figure 2-13. The producer-consumer problem
with a fatal race condition
Semaphore
• The solution to both of these shortcomings is to remove the
restriction to a binary variable and define a generalized or
counting variable.
...
Figure 2-14. The producer-consumer problem using semaphores.
The Producer-Consumer Problem
...
Figure 2-14. The producer-consumer problem
using semaphores.
Monitors
• When the semaphore’s ability to count is not needed, a simplified
version of the semaphore, called mutex, is sometimes used.
• Mutexes are good only for managing mutual exclusion to some
shared resources or piece of code.
• A mutex is a variable that can be in one of two states: unlocked
or locked.
• If the buffer were completely full, the producer would block, with
mutex set to 0.
• Consequently, the next time the consumer tried to access the
buffer, it would do a down on mutex, now 0 and block too.
Monitors
• Problem: Both processes would stay blocked forever and no
more work would ever be done. This unfortunate situation is
called a deadlock.
• Solution: To make it easer, new approach to deal with it called
monitor in 1973 and 1974.
• A monitor is a collection of procedures, variables, and data
structures that are all grouped together in a special kind of
module or package.
Monitors
...
• What algorithm do you use for access to the shared resource (the
forks)?
...
...
Figure 20. A solution to the dining philosophers problem.
The Dining Philosophers Problem
...
– Reader-priority readers/writers.
The Readers and Writers Problem
...
Figure 21. A solution to the readers and writers problem.
The Readers and Writers Problem
...
• The part of the OS that makes the choice is called the scheduler;
• Consider the old days, when using batch system, with one input in
the form of card images on a magnetic tape, the scheduling
algorithm was simple: just run the next job on the tape.
Figure 22. Bursts of CPU usage alternate with periods of waiting for I/O.
(a) A CPU-bound process. (b) An I/O-bound process.
When to Schedule
• When scheduling is absolutely required:
• In both these cases the process that had most recently been running
becomes unready, so another must be chosen to run next.
When to Schedule
• When scheduling usually done (though not absolutely required)
1. When a new process is created. It make sense to reevaluate priorities at this
time. In some cases the parent may be able to request a different priority for
its child
2. When an I/O interrupt occurs. This usually means that an I/O device has
now completed its work. So some process that was blocked waiting for I/O
may now be ready to run.
(a)
Figure 28. (a) Possible scheduling of user-level threads with a 50-msec process
quantum and threads that run 5 msec per CPU burst.
Thread Scheduling
• Kernel-level threads
•kernel picks particular thread to run
•Kernel does not have to take into account which process the
thread belongs to, but it can if it wants to
•The thread is given a quantum and is forceably suspended if it
exceeds the quantum
•Since there are no clock interrupts to multiprogram threads A1
continue running as long as it wants to.