Module 2A
Module 2A
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Syllabus
Operating System Concepts – 8th Edition 2 Silberschatz, Galvin and Gagne ©2009
Outline
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server Systems
Operating System Concepts – 8th Edition 3 Silberschatz, Galvin and Gagne ©2009
Objectives
To introduce the notion of a process -- a program in execution, which forms the basis of all
computation
To describe the various features of processes, including scheduling, creation and termination,
and communication
Operating System Concepts – 8th Edition 4 Silberschatz, Galvin and Gagne ©2009
Process Concept
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
A process includes:
program counter
stack
data section
Operating System Concepts – 8th Edition 5 Silberschatz, Galvin and Gagne ©2009
The Process
Multiple parts
The program code, also called text section
Current activity including program counter, processor registers
Stack containing temporary data
Function parameters, return addresses, local variables
Data section containing global variables
Heap containing memory dynamically allocated during run time
Program is passive entity, process is active
Program becomes process when executable file loaded into memory
Execution of program started via GUI mouse clicks, command line entry of its name, etc
One program can be several processes
Consider multiple users executing the same program
Operating System Concepts – 8th Edition 6 Silberschatz, Galvin and Gagne ©2009
Process in Memory
Operating System Concepts – 8th Edition 7 Silberschatz, Galvin and Gagne ©2009
Process State
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution
Operating System Concepts – 8th Edition 8 Silberschatz, Galvin and Gagne ©2009
Diagram of Process State
Operating System Concepts – 8th Edition 9 Silberschatz, Galvin and Gagne ©2009
Process Control Block (PCB)
Information associated with each process
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
Operating System Concepts – 8th Edition 10 Silberschatz, Galvin and Gagne ©2009
Process Control Block (PCB)
Operating System Concepts – 8th Edition 11 Silberschatz, Galvin and Gagne ©2009
CPU Switch From Process to Process
Operating System Concepts – 8th Edition 12 Silberschatz, Galvin and Gagne ©2009
Process Scheduling
Maximize CPU use, quickly switch processes onto CPU for time sharing
Process scheduler selects among available processes for next execution on CPU
Maintains scheduling queues of processes
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory, ready and waiting to execute
Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
Operating System Concepts – 8th Edition 13 Silberschatz, Galvin and Gagne ©2009
Ready Queue And Various
I/O Device Queues
Operating System Concepts – 8th Edition 14 Silberschatz, Galvin and Gagne ©2009
Representation of Process Scheduling
Operating System Concepts – 8th Edition 15 Silberschatz, Galvin and Gagne ©2009
Schedulers
Long-term scheduler (or job scheduler) – selects which processes should be brought into the
ready queue
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and
allocates CPU
Sometimes the only scheduler in a system
Operating System Concepts – 8th Edition 16 Silberschatz, Galvin and Gagne ©2009
Schedulers (Cont.)
Short-term scheduler is invoked very frequently (milliseconds) ⇒ (must be fast)
Operating System Concepts – 8th Edition 17 Silberschatz, Galvin and Gagne ©2009
Process Scheduling Diagram
Operating System Concepts – 8th Edition 18 Silberschatz, Galvin and Gagne ©2009
Long Term Scheduler
-Long-term scheduler controls the programs that are selected within the system for processing. In this,
programs are found during a queue and therefore the best job is chosen as per the need it selects the
processes from the job pool and these processes are loaded into memory so as to execute.
- It provides restraint on the degree of multi-programming.
* If the amount of memory is too limited, the degree of multiprogramming will be limited because fewer processes
will t in memory.
Advantages of Long-term Scheduler:
• Better system performance: Long-term scheduling allows the operating system to select the best process to be
executed, which can improve overall system performance.
• Better resource utilisation: By selecting the most e cient processes, long-term scheduling can help maximize
the use of system resources.
• Improved response time: Long-term scheduling can help reduce the response time of the system by selecting the
best process to be executed.
Disadvantages of Long-term Scheduler:
• Delayed execution: Long-term scheduling can delay the execution of a process, as it must wait until a suitable
process is available.
• Increased overhead: The long-term scheduler can increase the overhead of the system, as it requires additional
processing power and resources to select the best process.
Operating System Concepts – 8th Edition 19 Silberschatz, Galvin and Gagne ©2009
fi
ffi
Medium-Term Scheduler
A medium-term scheduler is called a process swapping scheduler as it is a part of swapping. Through this
scheduler, processes are removed from memory. Medium-term schedulers cut down the degree of multi-
programming. In this scheduler, if a process requests I/O, it can be suspended and it cannot make any progress toward
the completion of the suspended process. During this condition, to get rid of the method from memory and make space
for other processes, the suspended process is moved to the auxiliary storage. This process is named swapping, and
therefore the process is claimed to be swapped out or unrolled. Swapping could also be necessary to enhance the
process mix.
Advantages of Medium-term Scheduler:
• Improved resource utilization: Medium-term scheduling can improve resource utilization by temporarily removing
processes from memory when they are not needed.
• Better response time: Medium-term scheduling can improve response time by removing inactive or less critical
processes from memory.
Disadvantages of Medium-term Scheduler:
• Increased overhead: Medium-term scheduling can increase the overhead of the system, as it requires additional
processing power and resources to manage the swapping of processes in and out of memory.
• Reduced system performance: Medium-term scheduling can temporarily reduce system performance during the
swapping of processes in and out of memory
Operating System Concepts – 8th Edition 20 Silberschatz, Galvin and Gagne ©2009
Short-Term Scheduling
Short-term scheduler’s major objective is to make sure that the CPU is constantly utilized e ectively and
e ciently.
-The short-term scheduler operates by continuously keeping track of the status of all the system’s processes. The
scheduler chooses a process from the ready queue when it is prepared to run and allows the CPU to do it. The process
then continues to operate until it either completes its work or runs into an I/O activity that blocks it.
The short-term scheduler can employ a variety of scheduling methods, each of which has advantages and
disadvantages of its own. Several well-liked algorithms include:
• First-Come, First-Served (FCFS): This method simply carries out the operations in the order in which the system
receives them.
• Shortest Job First (SJF): This algorithm chooses the process to run next based on its predicted runtime.
• Priority Scheduling: This algorithm gives each process a priority rating and chooses the process with the highest
rating to run next.
• Round Robin: This algorithm cycles around the ready queue, giving each process a chance to run, allotting each
process a xed time slice (referred to as a time quantum).
Advantage of STS
-Reduce process waiting time
-Improve CPU utilisation and system performance with the right scheduling algorithm and suitable time quantum.
Operating System Concepts – 8th Edition 21 Silberschatz, Galvin and Gagne ©2009
ffi
fi
ff
Difference b/w Long-Term and Medium-Term
In a long-term scheduler, the process are selected from the While in this, process can be revived in the
job pool and these processes’ time-sharing are loaded into memory as well as process execution can
memory in order to execute. also be carried out.
Long-term scheduler is can be or can’t be a part of a time While the medium-term scheduler is
sharing system. if it is then it is nominal in the time-sharing always in a time-sharing medium-term
system. system.
Operating System Concepts – 8th Edition 22 Silberschatz, Galvin and Gagne ©2009
Addition of Medium Term Scheduling
Operating System Concepts – 8th Edition 23 Silberschatz, Galvin and Gagne ©2009
Context Switch
When CPU switches to another process, the system must save the state of the old process and load the
saved state for the new process via a context switch.
Context-switch time is overhead; the system does no useful work while switching
The more complex the OS and the PCB -> longer the context switch
Operating System Concepts – 8th Edition 24 Silberschatz, Galvin and Gagne ©2009
Process Creation
Parent process create children processes, which, in turn create other processes, forming a tree of
processes
Resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
Operating System Concepts – 8th Edition 25 Silberschatz, Galvin and Gagne ©2009
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
Operating System Concepts – 8th Edition 26 Silberschatz, Galvin and Gagne ©2009
Process Creation
Operating System Concepts – 8th Edition 27 Silberschatz, Galvin and Gagne ©2009
C Program Forking Separate Process
#include <sys/types.h>
#include <studio.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child */
wait (NULL);
printf ("Child Complete");
}
return 0;
}
Operating System Concepts – 8th Edition 28 Silberschatz, Galvin and Gagne ©2009
A Tree of Processes on Solaris
Operating System Concepts – 8th Edition 29 Silberschatz, Galvin and Gagne ©2009
Process Termination
Process executes last statement and asks the operating system to delete it (exit)
Output data from child to parent (via wait)
Process’ resources are deallocated by operating system
Operating System Concepts – 8th Edition 30 Silberschatz, Galvin and Gagne ©2009
Interprocess Communication
Processes within a system may be independent or cooperating
Cooperating process can affect or be affected by other processes, including sharing data
Reasons for cooperating processes:
Information sharing
Computation speedup
Modularity
Convenience
Cooperating processes need interprocess communication (IPC)
Two models of IPC
Shared memory
Message passing
Operating System Concepts – 8th Edition 31 Silberschatz, Galvin and Gagne ©2009
Communications Models
Operating System Concepts – 8th Edition 32 Silberschatz, Galvin and Gagne ©2009
Cooperating Processes
Independent process cannot affect or be affected by the execution of another process
Operating System Concepts – 8th Edition 33 Silberschatz, Galvin and Gagne ©2009
Producer-Consumer Problem
Operating System Concepts – 8th Edition 34 Silberschatz, Galvin and Gagne ©2009
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
Operating System Concepts – 8th Edition 35 Silberschatz, Galvin and Gagne ©2009
Bounded-Buffer – Producer
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) ==
out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Operating System Concepts – 8th Edition 36 Silberschatz, Galvin and Gagne ©2009
Bounded Buffer – Consumer
while (true) {
while (in == out)
; // do nothing -- nothing
to consume
Operating System Concepts – 8th Edition 37 Silberschatz, Galvin and Gagne ©2009
Interprocess Communication –
Message Passing
Operating System Concepts – 8th Edition 38 Silberschatz, Galvin and Gagne ©2009
Implementation Questions
How are links established?
Can a link be associated with more than two processes?
How many links can there be between every pair of communicating processes?
What is the capacity of a link?
Is the size of a message that the link can accommodate fixed or variable?
Is a link unidirectional or bi-directional?
Operating System Concepts – 8th Edition 39 Silberschatz, Galvin and Gagne ©2009
Direct Communication
Processes must name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
Operating System Concepts – 8th Edition 40 Silberschatz, Galvin and Gagne ©2009
Indirect Communication
Messages are directed and received from mailboxes (also referred to as ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Operating System Concepts – 8th Edition 41 Silberschatz, Galvin and Gagne ©2009
Indirect Communication
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Operating System Concepts – 8th Edition 42 Silberschatz, Galvin and Gagne ©2009
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 arbitrarily the receiver. Sender is notified who the receiver was.
Operating System Concepts – 8th Edition 43 Silberschatz, Galvin and Gagne ©2009
Thank you
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009