Professional Documents
Culture Documents
Hawassa University
• User process
Scheduling of process
Interprocess communication
5
Process…
The process control block is the key tool that enables the
OS to support multiple processes and to provide for
multiprocessing.
6
Process Creation
Events which cause process creation:
System initialization- When an operating system is booted several
processes are created. These includes:
•Foreground processes- are processes that interact with users and
perform work for them.
•Background processes (daemons)- Processes that stay in the
background to handle some activity .Like a process that accept
incoming email which sleeps most of the day but suddenly
springing to life when email arrives
Execution of a process creation– new processes can be created after
boot time as well.
A user request to create a new process
Initiation of a batch job- Users can submit batch jobs to the system and
OS creates a new process if there is a resource 7
Process Termination
After a process has been created and does whatever its job is
sooner or later the this process will terminate.
The process will terminate due to one of the following
condition:
Normal exit (voluntary) - When processes terminate because
they have done their work, Example, in word processors
when a user clicked the close menu or the close button.
Error exit (voluntary)- error caused by the process due to a
program bug. Like division by zero
Fatal error (involuntary)- when a process discovers a fatal
error. E.g trying to read data from an un existing file.
Killed by another process (involuntary)-when one process
executes a system call telling the operating system to kill
some other process.
8
Process State
Process may be in one of two states
• Running
• Not-running
A small dispatcher program is used to switch the processor
from one process to another.
Two State Process Model
9
9
Cont.…
A Five-State Model
A dispatcher program is used to switch the processor from one
process to another.
12
Queening diagram of 5 state model
Cont.…
Process description and control example
14
Context Switching
Context Switching:
• Process of saving the context of one process and loading the
context of other process is known as Context Switching.
• In simple term it is like loading and unloading of process
from running state to ready state.
When does Context switching happen?
1. When a high priority process comes to ready state,
compared to priority of running process.
2. Interrupt Occurs
3. User and Kernel mode switch: (It is not necessary though)
4. Preemptive CPU scheduling used.
15
Threads
A thread defines a single sequential execution stream with in a
process.
Threads are different modules that are presented in a process
Thread is a light weighted process.
A basic unit of CPU execution which consists program
counter, thread id, stack and set of registers.
16
Cont…
17
Cont..
Threads within the same process share memory and files,
they can communicate with each other without invoking
the kernel but not in process.
Termination of a process, terminates all threads within the
process.
Multithreading
• Operating system supports multiple threads of execution
within a single process.
• MS-DOS supports a single level thread.
• Windows, Solaris, and Linux OS support multiple threads.
18
Thread state
Just like process a thread can have the following states
New
Ready
Running
Blocked
Terminated
19
Thread Usage
Parallel processing
20
Thread implementation
Three kinds of thread implementation
Hybrid implementation
21
Cont.…
User-Level Threads
• All thread management is done by the application programs.
• The kernel is not aware of the existence of threads.
• Implement in user-level libraries, rather than via systems
calls.
• Thread switching does not need to call operating system and
to cause interrupt to the kernel.
• In User-Level Threads, threads library contains codes for:
– creating and destroying threads
– Passing messages and data between threads
– Scheduling thread execution
– Saving and restoring thread context
22
Cont.…
23
Cont.…
User-Level Threads
• Advantages:
– Thread switching do not require operating system.
– Thread scheduling is application specific, without
disturbing the underlying OS scheduler.
– ULTs can run on any OS, need no change to the underlying
kernel to support ULT – use threads library.
• Disadvantages:
– If one thread blocks, the whole process blocks
– Cannot take advantage of multiprocessing – 1 process to 1
processor at a time, i.e. only 1 thread running at a time
24
Cont…
Kernel-Level Threads
In this method, the kernel knows about and manages the
threads.
No runtime system is needed in this case. Instead of thread table
in each process, the kernel has a thread table that keeps track of
all threads in the system.
Operating Systems kernel provides system call to create and
manage threads.
Kernel maintains context information for the process and the
threads.
Scheduling is done on a thread basis.
All calls that blocks threads are implemented as system call
25
Cont…
26
Cont…
Kernel-Level Threads - This approach overcome the
two principal drawbacks of ULT approach
• The kernel can simultaneously schedule threads on
different processors.
• If one thread of a process is blocked it can schedule
another thread of the same process.
The disadvantage
• Transfer of control from one thread to another thread
within the same process requires mode switching,
which is done by the kernel (busy the OS).
27
Cont.…
User level thread kernel level thread
Implemented by user and OS managed threads.
managed by run-time system OS knows about and manages
(user-level library). the thread.
OS is not aware of existence of System calls create and manage
threads. threads.
Creation of thread switching Slower to create and manage
between thread and
Run on specific operating
synchronizing thread are all
system.
done via procedural call.
Fast to create and manage.
Can run on any operating
system
28
Cont…
Hybrid Approaches
• Combining advantages of user level threads with kernel level
threads
• Thread creation done in the user space
• Bulk of scheduling and synchronization of threads within
application.
29
Thread
Hybrid approaches -Advantage
• Multiple threads within the same application
can run concurrently on a number of
processors.
• Blocking system calls need not block the
entire process.
30
Thread VS Process
As we mentioned earlier that in many respect threads operate in the
same way as that of processes. Some of the similarities and
differences are:
Similarities
Like processes threads share CPU and only one thread active (running)
at a time.
And like process, if one thread is blocked, another thread can run.
31
Cont.…
Differences
Process Thread
Process means any program is in Thread means segment of a process.
execution. Thread is called light weight process.
Process is called heavy weight process. Thread switching does not require to
Process switching uses interface in call a operating system and cause an
operating system. interrupt to the kernel.
If one process is blocked then it will Second thread in the same task
not affect the execution of other could not run, while one server
process. thread is blocked.
Process takes more time to create and Thread takes less time to create and
terminate. terminate.
Process is isolated. Threads share memory.
32
Interprocess Communication(IPC)
Processes executing concurrently in the operating system may be
either independent processes or cooperating processes
Independent process
• Cannot affect or be affected by the other processes executing in the
system
• Does not share data with any other process is independent
Cooperating
• Can affect or be affected by the other processes executing in the system
• Any process that shares data with other processes is a cooperating
process
33
Cont…
Processes need to communicate with other processes. for
instance, the output of the first process need to pass to the
second one.
There are three things here to be considered in IPC:
• How one process can pass information to another?
• How to make sure two or more processes do not get into conflict?
• How to sequence when dependencies are present between
processes? (proper sequencing when dependencies are present).
The last two concept can be applied for thread also
IPC facility provides a mechanism to allow processes to
communicate and synchronize their actions.
34
Cont..
Processes can communicate through:
1. Shared memory
• A region of memory shared by cooperating processes is
established.
• Processes exchange information by write and read data from
the shared memory.
• Processes communicate with each other with resorting to
shared variables.
2. Message passing
• Communication takes place by means of messages
exchanged between cooperating process.
• Processes communicate with each other without resorting to
shared variables.
35
Cont...
Communication
Models
36
Cont…
Message-Passing Systems
• Message system – processes communicate with each other
without resorting to shared variables.
Mailbox
Process P1 Process P2
M
40
Cont…
Indirect Communication
• Mailbox sharing
– P1, P2, and P3 share mailbox A
– P1, sends; P2 and P3 receive
– Who gets the message?
• Solutions
– Allow a link to be associated with at most two
processes
– Allow only one process at a time to execute a receive
operation
– Allow the system to select randomly the receiver.
Sender is notified who the receiver was.
41
Cont.…
Synchronization
• Computation speedup
o Multiprocessing environment
• Modularity
o Dividing the same function into pieces
• Convenience
o an individual user may work on several task at the same time
within a multiprocessor.
Problems in Concurrency
The sharing global resources
• Sharing of global resources safely is difficult. If two processes both
make use of a global variable and both perform read and write on
that variable, then the order in which various read and write are
executed is critical. 46
Cont..
Optimal allocation of resources
• It is difficult for the operating system to manage the
allocation of resources optimally.
Locating programming errors
• It is very difficult to locate a programming error because
reports are usually not deterministic and reproducible.
Locking the channel
• It may be inefficient for the operating system to simply lock
the channel and prevents its use by other processes.
47
Cont..
What design and management issues are raised by the existence
of concurrency?
The OS must be able to keep track of the various processes
The OS must allocate and deal locate various resources for each
active process like:
– Processor time
– Memory
– Files
– I/O devices
OS must protect the data and physical resources of each process
against unintended interference by other processes.
The functioning of a process, and the output it produces, must be
independent of the speed at which its execution is carried out
relative to the speed of other concurrent processes.
48
Cont..
Advantages of Concurrency:
Running of multiple applications- it enable to run multiple
applications at the same time.
Better resource utilization –It enables that the resources that are
unused by one application can be used for other applications.
Better average response time –Without concurrency, each
application has to be run to completion before the next one can
be run.
Better performance –It enables the better performance by the
operating system.
• When one application uses only the processor and another
application uses only the disk drive then the time to run both
applications concurrently to completion will be shorter than the
time to run each application consecutively.
Reduced waiting, time response time or turnaround time. 49
Cont..
Drawback of Concurrency:
It is required to coordinate multiple applications through
additional mechanisms.
Additional performance overheads and complexities in operating
systems are required for switching among applications.
Sometimes running too many applications concurrently leads to
severely degraded performance.
It is required to protect multiple applications from one another.
50
Race condition
Processes that are working together may share some common
storage that each one can read and write.
A race condition occurs when two or more processes are
reading or writing some shared data, and the final result
depends on who runs precisely.
A situation in which multiple threads or processes read and
write a shared data item, and the final result depends on the
relative timing of their execution.
This multiple access is not been properly controlled.
• Print spooler
The mechanism that prohibits or protect two processes from
accessing shared memory or file at the same time is mutual
exclusion.
51
Critical Region
Part of program where the shared memory accessed
A section of code within a process that requires access to shared
resources, and that must not be executed while another process
is in a corresponding section of code.
52
Critical Region
There are a number of potential solutions to avoid processes
enter the critical section at the same time
But the solution should meet all the below requirements
No two processes should be simultaneously in their critical
sections.
No assumption may be made about the relative speed of the
processes.
No process running outside its critical section may block other
processes.
No process should wait forever to enter its critical section.
53
Mutual Exclusion
The requirement that when one process is in a critical section that
accesses shared resources, no other process may be in a critical
section that accesses any of those shared resources.
54
Cont..
56
Cont…
In mutual exclusion there is:-
Disabling interrupts
Lock variable
Strict alternation
Peterson’s solutions
Sleep and Wakeup
The Producer-Consumer Problem
Semaphore
Monitor
57
Disable Interrupts
• Each process disable all interrupts just after entering its
critical region and re-enable them just before leaving it.
• With interrupts disable , no clock interrupts can occur
– so it can examine and update the shared memory
without fear
• It is not attractive solution.
while (true) {
/* disable interrupts */;
/* critical section */;
/* enable interrupts */;
/* remainder */;
}
58
Cont…
• Once a process has disabled interrupts, it can examine and
update the shared memory without fear that any other
process will intervene.
• Limitations:
– Doesn’t work for multiprocessing system.
– unwise to give user processes the power to turn off interrupts.
60
Strict alternation
The integer variable turn, initially 0,-
Keeps track of whose turn it is to enter the critical regions
and examine or update the shared memory
61
Peterson’s solution
In 1981, G.L. Peterson discovered a much simpler way to
achieve mutual exclusion.
62
Sleep and Wakeup
• Busy waiting causes:
– Wasting CPU time
– Priority inversion problem.
• The sleep and wakeup solution intends to resolve the busy
waiting problem.
• Sleep is a system call that causes the caller to block or be
suspended until another process wakes it up.
• The wakeup call has one parameter, the process to be
awakened (wakeup).
63
The Producer-Consumer Problem
• Two processes share a common, fixed-size buffer. One
of them, the producer, puts information into the buffer,
and the other one, the consumer, takes it out.
• Problem
– The producer wants to put a new item in the buffer while the
buffer is full.
• Solution
– The producer to go to sleep, to be awakened when the
consumer has removed one or more items.
64
Cont…
• Problem
– The consumer wants to remove an item from the buffer while the
buffer is empty.
• Solution
– Consumer goes to sleep until the producer puts something in the
buffer and wakes it up.
• To keep track of the number of items in the buffer, we will need
a variable, count.
• If the maximum number of items the buffer can hold is N, the
producer’s code will first test to see if count is N. If it is, the
producer will go to sleep; if it is not, the producer will add an
item and increment count
65
Cont…
else
66
Cont…
67
Cont…
Race condition may occur – both sleep forever
– The buffer is empty and the consumer has just read count to see if it is
0.
– At that instant, the scheduler decides to stop running the consumer
temporarily and start running the producer.
– The producer inserts an item in the buffer, increments count, and
notices that it is now 1.
– Reasoning that count was just 0, and thus the consumer must be
sleeping, the producer calls wakeup to wake the consumer up.
– the consumer is not yet logically asleep, so the wakeup signal is lost
– When the buffer is full, producer goes to sleep
BOTH SLEEP FOREVER
– A quick fix is to add a status bit to indicate whether a process is asleep
or awake.
68
Semaphore
Dijkstra (1965) – suggested using an integer variable to count the
number of walkups saved for the future use.
The variable is called semaphore.
A semaphore = 0 , if no wake up saved or some positive value if
one or more wakeups were pending .
There are two operations on a semaphore
Down (sleep)
Up(wakeup)
69
Cont.
1. Down operation
Equivalent to sleep
If the value of the semaphore is greater than 0, it uses one
saved wakeup and continues
At the same time the value of the semaphore is decremented
by one
If the value is 0, its goes to sleep without completing the
operation.
Once a semaphore operation has started , no other process can
access the semaphore until the operation has completed or
blocked.
• It solve synchronization problems and race conditions.
70
Cont…
2. Up
Increments the semaphore.
If one or more processes were sleeping on that semaphore,
unable to complete an earlier down operation, one of them
is chosen by the system (e.g., at random) and is allowed to
complete its down.
after an up on a semaphore with processes sleeping on it,
the semaphore will still be 0, but there will be one fewer
process sleeping on it.
If one or more processes is waiting on this semaphore, wake
up one process
• Reading more about semaphore-increment/decrement-waking
up/sleeping are indivisible atomic operation.
71
Cont…
Solving the Producer-Consumer Problem using Semaphores
Tewodros A.
72
Cont…
Solving the Producer-Consumer Problem using Semaphores
73
Monitors
• Monitor is a collection of procedures, variables, and data
structures that are all grouped together in a special kind of
module or package.
• Processes may call the procedures in a monitor whenever they
want to, but they cannot directly access the monitor’s internal
data structures from procedures declared outside the monitor
74
Cont…
• When a process calls a monitor procedure, the first few
instructions of the procedure will check to see, if any other
process is currently active within the monitor.
• If so, the calling process will be suspended until the other
process has left the monitor.
• If no other process is using the monitor, the calling process may
enter.
75
DEADLOCK
76
DEADLOCK
Deadlock can be defined as the permanent blocking of a set of
processes that either compete for system resources or communicate
with each other.
A set of processes is deadlocked when each process in the set is
blocked awaiting an event that can only be triggered by another
blocked process in the set.
The cause of deadlocks:
◦ Each process needing what another process has. This results from sharing
resources such as memory, devices, locks, links.
77
Cont.…
All deadlocks involve conflicting needs for resources by two or more
processes.
When a process requests for the resource that is been held another
process which needs another resource to continue, but is been held
by the first process, then it is called a deadlock.
81
Cont.…
• One way to achieve this goal is to require all processes to request
all their resources before starting execution
If everything is available, the process will be allocated whatever it
needs and can run to completion.
If one or more resources are busy, nothing will be allocated and the
process would just wait.
3. Attacking the no preemption condition
• Resources can’t be preempted. A resource can be released by the
process holding it after that process has completed its task.
4. Attacking the circular wait condition
• circular wait can be eliminated in several ways:
o a process is entitled only to a single resource at any moment
o provide a global numbering of all the resources, all requests must be
made in numerical order
82
1-
83
83