OPERATING SYSTEMS
CS3500 – CHAP - 3
PROF. SUKHENDU DAS,
DEPTT. OF COMPUTER SCIENCE AND ENGG., IIT MADRAS, CHENNAI – 600036.
Email: [email protected]
URL: //www.cse.iitm.ac.in/~vplab/os.html
https://sites.google.com/smail.iitm.ac.in/3500-os/
Aug. – 2022.
PROCESS MANAGEMENT
Outline
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
IPC in Shared-Memory Systems
IPC in Message-Passing Systems
Communication in Client-Server Systems
Remote Procedure Call
What is A PROCESS?
An operating system executes a variety of programs that run as a
process.
Process – a program in execution; process execution must
progress in sequential fashion. No parallel execution of
instructions of a single 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 Process In Memory
What is A PROCESS? (Cont.)
Program ≠ Process
Program is passive entity stored on disk (executable file); process is active
Program becomes process when an executable file is 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
Memory Layout of a C Program
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
5 State Process Chart
execution
7 State Process Transition Diagram
Diagram Courtesy :https://www.geeksforgeeks.org/states-of-a-process-in-operating-systems/
Process Control Block (PCB)
Information associated with each process (also called task control block - TCB)
Process state
Process number
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
What is a Thread*?
So far, process has a single thread of execution
Consider having multiple program counters per
process
Multiple locations can execute at once
Multiple threads of control -> threads
Must then have storage for thread details, multiple
program counters in PCB
*To be covered in detail in later chapters.
Process Scheduling
Process scheduler selects among available
processes for next execution on CPU core
Goal -- Maximize CPU use, quickly switch
processes onto CPU core
Maintains scheduling queues of processes
Ready queue – set of all processes
residing in main memory, ready and
waiting to execute
Wait queues – set of processes waiting
for an event (i.e., I/O)
Processes migrate among the various
queues
CPU Switch From Process to Process
A context switch occurs when the CPU switches from one process to
another.
Operations on Processes
System must provide mechanisms for:
Process creation
Process termination
Process Creation
Parent process create 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
Parent process calls wait()waiting for the child to terminate
A Tree of Processes in Linux
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
If no parent waiting (did not invoke wait()) process is a zombie
If parent terminated without invoking wait(), process is an orphan
Interprocess Communication
Processes within a system
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
(a) Shared memory. (b) Message passing.
Producer-Consumer Problem
Paradigm for cooperating processes:
producer process produces information that is consumed by a consumer process
Two variations:
unbounded-buffer places no practical limit on the size of the buffer:
Producer never waits
Consumer waits if there is no buffer to consume
bounded-buffer assumes that there is a fixed buffer size
Producer must wait if all buffers are full
Consumer waits if there is no buffer to consume
IPC- Shared Memory
An area of memory shared among the processes that
wish to communicate
The communication is under the control of the users
processes not the operating system.
Major issues is to provide mechanism that will allow
the user processes to synchronize their actions when
they access shared memory.
Synchronization to be covered in detail later.
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.
The shared buffer is implemented as a circular array with two logical pointers: in
and out.
The buffer is empty when in == out; the buffer is full when
((in + 1) % BUFFER SIZE) == out
Producer Process – Shared Memory Consumer Process – Shared Memory
item next_consumed;
item next_produced;
while (true) {
while (true) { while (in == out)
; /* do nothing */
/* produce an item in next produced */ next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */ /* consume the item in next consumed
buffer[in] = next_produced; */
in = (in + 1) % BUFFER_SIZE; }
}
What about Filling all the Buffers?
Suppose that we wanted to provide a solution to the consumer-producer
problem that fills all the buffers.
We can do so by having an integer counter that keeps track of the
number of full buffers.
Initially, counter is set to 0.
The integer counter is incremented by the producer after it produces a
new buffer.
The integer counter is and is decremented by the consumer after it
consumes a buffer.
Producer Consumer
while (true) { while (true) {
/* produce an item in next produced */ while (counter == 0)
; /* do nothing */
while (counter == BUFFER_SIZE) next_consumed = buffer[out];
; /* do nothing */ out = (out + 1) % BUFFER_SIZE;
buffer[in] = next_produced; counter--;
in = (in + 1) % BUFFER_SIZE; /* consume the item in next consumed
counter++; */
} }
Race Condition Consider this execution interleaving with “count = 5”;
initially:
S0: producer execute register1 = counter
counter++ could be implemented
as
{register1 = 5}
register1 = counter S1: producer execute register1 = register1 + 1
register1 = register1 + 1 {register1 = 6}
counter = register1
S2: consumer execute register2 = counter
counter-- could be implemented as {register2 = 5}
register2 = counter S3: consumer execute register2 = register2 – 1
register2 = register2 - 1
counter = register2
{register2 = 4}
S4: producer execute counter = register1
{counter = 6 }
Question – why was there no
race condition in the first S5: consumer execute counter = register2
solution (where at most N – 1)
buffers can be filled?
{counter = 4}
IPC – Message Passing
Processes communicate with each other without
resorting to shared variables
IPC facility provides two operations:
send(message)
receive(message)
The message size is either fixed or variable
Message Passing
If processes P and Q wish to communicate, they need
to:
Establish a communication link between them
Exchange messages via send/receive
Implementation issues: Physical:
Shared memory
How are links established? Hardware bus
Can a link be associated with more than two Network
processes? Logical:
How many links can there be between every pair Direct or indirect
of communicating processes? Synchronous or asynchronous
What is the capacity of a link? Automatic or explicit buffering
Is the size of a message that the link can
accommodate fixed or variable?
Is a link unidirectional or bi-directional?
Pipes
Acts as a conduit allowing two processes to communicate
Issues:
Is communication unidirectional or bidirectional?
In the case of two-way communication, is it half or full-duplex?
Must there exist a relationship (i.e., parent-child) between the communicating
processes?
Can the pipes be used over a network?
Ordinary pipes – cannot be accessed from outside the process that created it.
Typically, a parent process creates a pipe and uses it to communicate with a child process
that it created.
Producer writes to one end (the write-end of the pipe)
Consumer reads from the other end (the read-end of the pipe)
Named pipes – can be accessed without a parent-child
relationship. Communication is bidirectional
Communications in Client-Server Systems
Sockets
Remote Procedure Calls
Sockets
A socket is defined as an endpoint for communication
Concatenation of IP address and port – a number
included at start of message packet to differentiate
network services on a host
The socket 161.25.19.8:1625 refers to port 1625 on
host 161.25.19.8
Communication consists between a pair of sockets
All ports below 1024 are well known, used for
standard services
Special IP address 127.0.0.1 (loopback) to refer to
system on which process is running Socket Communication
Remote Procedure Calls
Remote procedure call (RPC) abstracts procedure calls between processes on
networked systems
Again uses ports for service differentiation
Stubs – client-side proxy for the actual procedure on the server
The client-side stub locates the server and marshalls the parameters
The server-side stub receives this message, unpacks the marshalled parameters, and
performs the procedure on the server
Data representation handled via External Data Representation (XDL) format to
account for different architectures
Big-endian and little-endian
Remote communication has more failure scenarios than local
Messages can be delivered exactly once rather than at most once
OS typically provides a rendezvous (or matchmaker) service to connect client and
server
Execution of RPC