Professional Documents
Culture Documents
Instr: Yusuf Altunel IKU Department of Computer Engineering (212) 498 42 10 y.altunel@iku.edu.tr
1
Content
2.1 2.2 2.3 2.4 2.5 Processes Threads Interprocess communication Classical IPC problems Scheduling
A process will perform its computation There is a need for timing mechanism
to prevent some processes
making the CPU too much busy
Processes
The Process Model
a. Multiprogramming of four programs b. Conceptual model of 4 independent, sequential processes c. Only one program active at any instant
4
Process Creation
Principal events result in process creation 1. System initialization 2. User request to create a new process 3. Initiation of a batch job
Process Termination
Conditions terminate processes: 1. Normal exit
voluntary voluntary involuntary involuntary
6
Process Hierarchies
Parent creates a child process,
child processes can create their own process Forms a hierarchy
UNIX calls this a "process group"
Windows
has no process hierarchy concept all processes are treated equally
7
Process States
Running
using the CPU
Ready
no CPU is available Ready to run, but temporarily stopped to let another process run
Blocked
Cannot run even if the CPU is available Unable to run until some external event happens
8
Scheduler
Lowest layer of process-structured OS handles interrupts, scheduling Starts and stops the processes
when it is necessary
9
Process Implementation
Process Table
Maintained by OS
to implement the processes
Memory Management
Pointer to text segment Pointer to data segment
File Management
Root directory Working directory File descriptions User ID Group ID
Interrupt Handling
Threads
Comparing Thread vs. Process
(a) Three processes each has one thread (b) One process with three threads
13
Cons
When thread is blocked
e.g., waiting for I/O or a page fault to be handled
Kernel space
Pros
When thread is blocked Having the chance to execute the next
Cons
Switching is slower
19
Interprocess Communication
Processes need
to communicate with others
Example:
UNIX command to
Concatenate three files (Process 1) Select all lines containing the word tree (Process 2)
cat chapter1 chapter2 chapter3 | grep tree The first process should
concatenate three files and send the resulting file
to the second process
Second process
will get the concatenated files and finds those lines including the word tree
20
Race Conditions
Processes often share a common storage
In main memory or a shared file
Race condition
Two or more processes ready To read or write a shared data The final result depends on who runs precisely when
21
Mutual Exclusion
Critical region
part of a program where shared area is to be accessed
in=4
Shared area
Shared file Shared memory Variable etc.
Slot 7 ...
Shared Area
No two processes are ever at the critical region at the same time
out=7
Mutual exclusion:
Shared area
must be prevented against access by more than one process
at the same time
22
2.
No assumptions made
about speeds or numbers of CPUs
3.
4.
No process must
wait forever to enter its critical region
23
Critical Regions
25
Re-enable them
just before leaving CR
Problem:
The lock variable itself is shared Access to lock variable
creates another Race Condition
27
Not Good
Similar problems to previous solutions Keeps the CPU busy If the waiting process is slower
the first process may not allow the second process to enter CR
28
Algorithm:
The process entering CR
Sets the array element
Process 0 element 0; Process 1 element 1, etc.
Set turn to its process number At leaving CR, reset the array element
Algorithm
Use a shared variable flag When flag is 0
any process may set it to 1
using TSL instructions
When flag is 1
any process wants to enter CR should wait
30
WAKEUP:
System call
causes a blocked process to be awakened and start execution
Semaphores
Counts number of wakeups
saved for future use
Atomic operations:
Checking the value, changing it, and possibly going to sleep
If the value is 0
Up
increments the value of the semaphore If one or more processes were sleeping on that semaphore
one of them is chosen by the system (e.g., at random) and is allowed to complete its down. the semaphore will still be 0, but there will be one fewer process sleeping on it
33
Each process
does a mutex down
just before entering its critical region
and an up
just after leaving it
Mutexes
A mutex is a variable
can be in one of two states:
unlocked (0) locked (1)
only 1 bit is required to represent it, but in practice an integer often is used
Process Scheduling
Operating system (OS) must decide
when more than one process is runnable
Scheduler:
OS part to decide which process is run
Scheduling algorithm:
The algorithm to decide
which process will run first
Efficiency
Keep the CPU busy as much as possible
Response time
Minimize the response time for interactive users
Turnaround
Minimize the time batch users must wait for output
Throughput
Maximize the number of jobs processed per hour
36
Scheduling
Scheduling Algorithms
First-Come First-Served Shortest Job First Shortest Remaining Time Next Three level scheduling Round Robin Scheduling Priority Scheduling Multiple Queues Shortest Process Next Guaranteed Scheduling Lottery Scheduling Fair-Share Scheduling
38
FirstFirst-Come First-Served First The simplest algorithm Processes use the CPU
in the order they request it.
There is a single process queue. When the first job enters the system
it is started immediately allowed to run as long as it wants to.
40
43
Each process
assigned a time interval
called quantum
Current Process
Next Process
Priority Scheduling
Each process assigned a priority The processes with highest in priority run first To prevent higher priority processes to run infinitively
Decrease the priority at each clock tick
It is often convenient to
Group processes into priority classes Apply
priority scheduling between classes and round robin within each class
45
Multiple Queues
To decrease the swap processes Create priority queues
Assign
1 quanta 2 quanta 4 quanta 8 quanta ... to to to to the the the the processes processes processes processes in in in in 1st priority queue 2nd priority queue 3rd priority queue 4th priority queue
Guaranteed Scheduling
If there are n processes
each will receive about 1/n of the CPU power.
Keep track of
how much CPU each process has had since its creation.
Then compute
the amount of CPU used by each process
A ratio of 0.5:
a process has only had half of what it should have had,
A ratio of 2.0:
a process has had twice as much as it was entitled to.
Lottery Scheduling
Give processes lottery tickets To schedule
a lottery ticket is chosen at random, schedule the process holding that ticket
Example
For 50 lotteries per second each process will get
20 msec of CPU time on average
FairFair-Share Scheduling
To consider who is the process owner Example: round robin, equal priorities,
user 1 has 9 processes user 2 has 1 process user 1 will get
90% of the CPU and
Scheduling Threads
When several processes each have multiple threads
we have two levels of parallelism present: processes and threads.
50
KernelKernel-Level Threads
End of Chapter 2
Processes and Threads
53