You are on page 1of 33

Process Concept

An operating system executes a variety of


programs:
 Batch system – jobs
 Time-shared systems – user programs or tasks
Textbook uses the terms job and process
almost interchangeably
Process – a program in execution; Program in
memory.
A process includes:
 program counter
 stack
 data section
Process address space

Process Address Space


• Address space ⇒ the set of accessible addresses +
state associated with them:
• For a 32-bit processor there are 232 = 4 billion
addresses (4GB)
Physical Address Extension technique is used to
extend memory of system
Stack : Local Variables and During function calls
(LIFO)
Heap : Dynamic memory ….
Data : Static, Global(initialised or Default initialised) (code)
Text : Program code

For Better processing, there are very small


memories in CPU which can get
data/instruction from RAM/Cache
After registers we have cache memory, which
are faster but less faster than registers. CPU

Operating System Concepts 3


Operating System Concepts
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 process
terminated: The process has finished execution
Diagram of Process State
Process Control Block (PCB)
Information associated with each process
Process Id
Parent Id
User Id
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
Parallelism is the quality of occurring at the same time.
Two events are parallel if they occur at the same time, and two tasks are parallel
if they are performed at the same time.

Concurrency is an illusion of parallelism. Thus, two tasks are concurrent if there


is an illusion that they are being performed in parallel, whereas, in reality, only
one of them may be performed at any time.

In an OS, concurrency is obtained by interleaving operation of processes on the


CPU, which creates the illusion that these processes are operating at the same
time.

Parallelism is obtained by using multiple CPUs, as in a multiprocessor system,


and operating different processes on these CPUs

Operating System Concepts


Process Scheduling Queues
 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
Job queue
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
Medium-term scheduler To reduce multiprogramming
degree to reduce contention for CPU. Swapping is done
Schedulers (Cont.)
Short-term scheduler is invoked very frequently
(milliseconds)  (must be fast)
Long-term scheduler is invoked very infrequently
(seconds, minutes)  (may be slow)
The long-term scheduler controls the degree of
multiprogramming
Processes can be described as either:
I/O-bound process – spends more time doing I/O
than computations, many short CPU bursts
CPU-bound process – spends more time doing
computations; few very long CPU bursts
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
Context-switch time is overhead; the system
does no useful work while switching
Time dependent on hardware support
Context Save, Scheduling, and Dispatching
An OS contains two processes P1 and P2, with P2 having a higher priority than P1.
Let P2 be blocked on an I/O operation and let P1 be running. The following actions
take place when the I/O completion event occurs for the I/O operation of P2:
1.The context save function is performed for P1 and its state is changed to ready.
2.Using the I/O Status field of PCBs, the event handler finds that the I/O operation
was initiated by P2, so it changes the state of P2 from blocked to ready.
3.Scheduling is performed. P2 is selected because it is the highest-priority ready
process.
4.P2’s state is changed to running and it is dispatched.
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 may wait until children terminate
Process Creation (Cont.)
Address space
Child duplicate of parent
Child has a program load 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
RAM

Fra Page Page Fra Page Fram


me No No me No e No
No No
Page Table
1 11
10 1P 1 10
2 12
14 2P 2 14
3 17
3 16
16 3P
4 20
4 19
19 4P 5 23
5 21
21 5P 6 24
6 26
26 6P 7 35
7 31
31 7P
PCB Parent Values PCB Child Values
PID=CHILD PID PID=0
Operating System Concepts
Operating System Concepts
Process Termination
Voluntary Termination: Process executes last statement and
asks the operating system to delete it (exit) and sends status to
parent exit(0)
 Output data from child to parent
 Process’ resources are deallocated by operating system
Involuntary Termination: Parent may terminate execution of
children processes kill(pid, signal)
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 Abnormal conditions during operation
 If parent is exiting
 Some operating system do not allow child to continue if its parent
terminates
 All children terminated - cascading termination
Wait()
Called in Parent process Wait(&status) is used to collect
Parent goes to block State status from children on exit()
Until one of its children exits Pid=wait();
If no children then -1 is return
Zombies
When a process terminates, it becomes what is known as a Zombie or a defunct process
Particular process is no longer executing;
however, the process control block in the operating system will still continue to exist.
Why it is continue…..
so that the parent process can read the child’s exit status through the wait system call stored
in PCB by Child process.
Till it is read process stays in Memory and known as Zombie process
Remove by wait system call or by reaper Process

Operating System Concepts


Cooperating Processes
Independent process cannot affect or be affected by
the execution of another process. Any process that
does not share data with any other process is
independent.
Cooperating process can affect or be affected by the
execution of another process. Shares data with others
Advantages of process cooperation
Information sharing
Computation speed-up
Modularity
Convenience
Interprocess Communication (IPC)
 Mechanism for processes to communicate and to synchronize their actions
Cooperating processes require an interprocess communication (IPC) mechanism that
will allow them to exchange data and information.

There are two fundamental models of interprocess communication:


(1)Shared memory
(2)Message passing.

Operating System Concepts


Producer Consumer Problem
Step 1 − A producer process produces information that is consumed by the consumer
process.
Step 2 − It uses the shared memory concept.
Step 3 − If we want to allow producer and consumer processes to run concurrently, we have
to provide a buffer of items which can be filled by producer and empty the buffer by
consumer.
Step 4 − This buffer will reside in a region of memory which is shared by producer and
consumer process
Step 5 − A producer can produce one item while the consumer is consuming another item.

Step 6 − The producer and consumer must be synchronised, so that the consumer does not try
to consume an item which is not yet produced.
There are two types of buffer which are as follows −
•Unbounded buffer − It has no limit on the size of the buffer. The consumer may have to
wait for new items, but the producer can always produce new items.

•Bounder buffer − It assumed a fixed buffer size. In this case, the consumer must wait
if the buffer is empty, and the producer must wait if the buffer is full.

Operating System Concepts


#define BUFFER_SIZE 10

typedef struct{
……. }item;

item buffer[BUFFER_SIZE]; /Shared Area


int in = 0;
int out = 0;
item nextProduced;
while (true) {
I* produce an item in nextProduced *I
while ( ((in + 1) % BUFFER_SIZE) == out) ;
I* do nothing *I
buffer[in] = nextProduced;
item nextConsumed;
in = (in + 1) % BUFFER_SIZE;
while (true) {
}
while (in == out) ; II do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
I* consume the item in nextConsumed *I
}

Operating System Concepts


Message-Passing Systems

Two operations: send(message) and receive(message).


Messages sent by a process can be of either fixed or variable size.

If processes P and Q want to communicate, they must send messages to and


receive messages from each other; a communication link must exist between them.

Here are several methods for logically implementing a link and the send () / receive()
operations:
•Direct or indirect communication
•Synchronous or asynchronous communication
•Automatic or explicit buffering
Direct Communication :
send(P, message) -Send a message to process P.
receive (Q, message)-Receive a message from process Q.
Link is establish between process P and Q
Symmetry in addressing; both the sender process and the receiver process must name
the other to communicate.
Asymmetry in addressing. only the sender names the recipient; the recipient is not
required to name the sender.
send(P, message) -Send a message to process P.
receive(id, message) - Receive a message from any process;
Operating System Concepts
indirect communication
• The messages are sent to and received from mailboxes, or ports.
• A mailbox can be viewed abstractly as an object into which messages can be
placed by processes and from which messages can be removed.
• Each mailbox has a unique identification. For example, POSIX message queues
use an integer value to identify a mailbox.
send (A, message) -Send a message to mailbox A.
receive (A, message)-Receive a message from mailbox A.

Operating System Concepts


Synchronization Communication between processes takes place through calls
to send() and receive () primitives.
Message passing may be either blocking or nonblocking also known as
synchronous and asynchronous.
Blocking send. The sending process is blocked until the message is received
by the receiving process or by the mailbox.
Nonblocking send. The sending process sends the message and resumes
operation.
Blocking receive. The receiver blocks until a message is available.
Nonblocking receive. The receiver retrieves either a valid message or a null.

Buffering Whether communication is direct or indirect, messages exchanged by


communicating processes reside in a temporary queue. Basically, such queues can be
implemented in three ways:
Zero capacity. The queue has a maximum length of zero; thus, the link cannot have any
messages waiting in it. In this case, the sender must block until the recipient receives the
message.
Bounded capacity. The queue has finite length n; thus, at most n messages
can reside in it.
Unbounded capacity. The queue's length is potentially infinite; thus, any
number of messages can wait in it.

Operating System Concepts


Inter-Process Communication In UNIX – Like Systems

Signals

Shared Memory

Pipes

Message Queues

Sockets

Operating System Concepts


Linux Architecture

Operating System Concepts


Sender Receiver
#include <iostream>
#include <iostream>
#include <sys/ipc.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdio.h>
using namespace std;
using namespace std;
int main()
int main()
{
{
// ftok to generate unique key
// ftok to generate unique key
key_t key = ftok(“MCA1",22);
key_t key = ftok(“MCA1",22);
// shmget returns an identifier in shmid
// shmget returns an identifier in shmid
int shmid = shmget(key,1024,0666);
int shmid = shmget(key,1024,0666|IPC_CREAT);
// shmat to attach to shared memory
// shmat to attach to shared memory
char *str = (char*) shmat(shmid,(void*)0,0);
char *str = (char*) shmat(shmid,(void*)0,0);
printf("Data read from memory: %s\n",str);
cout<<"Write Data : ";
gets(str);
//detach from shared memory
shmdt(str);
printf("Data written in memory: %s\n",str);
// destroy the shared memory
//detach from shared memory
shmctl(shmid,IPC_RMID,NULL);
shmdt(str);
return 0;
return 0;
}
}
Operating System Concepts

You might also like