Chapter - 2.2
Chapter - 2.2
Process Management
Lecture 2.2
1
Outline
• Inter process communication
• Race conditions
• Mutual exclusion
• Algorithms to avoid critical region problem
Disabling Interrupts
Lock Variables
Strict Alternation
Semaphores
Message passing
2
Inter Process Communication
• Process need to communicate with other processes.
• Three issues are there:
1- How one process can pass information to another process.
2- Making sure that two or more processes do not get into each
other’s way when engaging in critical activities.
3- Proper sequencing when dependencies are present.
3
Inter Process Communication…
Race Condition
• The situation where several processes access – and
manipulate shared data concurrently.
– (E.g, Main memory, printer spooler,…)
• The final value of the shared data depends upon which
process finishes last.
4
IPC: Race condition, print spooler
out=4
• If process wants to print a
in=7 file, it enter the file name
in spooler directory
Process A 4 abc
• Another process printer
5 prog.c daemon prints and
reads IN and remove their name
next_free_slot=7 6 file.n
CPU switches to B Assumption
7
A runs again • Slot 0-3 are printed out
put file in 7
• 4-6 are ready to be printed
update IN=8
• next_free_slot is 7
Process B
Spooler directory
reads IN and Process B will
next_free_slot=7 never receive
Put file in 7 5
any output
Update IN=8
Inter Process Communication…
The Critical-Section Problem
8
Inter Process Communication…
Mutual Exclusion
Algorithms to avoid critical region problem
• Disabling Interrupts
• Lock Variables
• Strict Alternation
• Semaphores
• Message passing
9
Disabling Interrupts
• Each process disable all interrupts just after entering its critical
region and re-enable them just before leaving it.
• Thus, once a process has disabled interrupts, it can update the
shared memory with out problem.
First Instruction in
Interrupt Interrupt Service Routine Task
Occurs (ISR) is executed resumes
ISR ends
while (true)
{
disable_interrupts();
critical_section();
enable_interrupts();
}
11
Disabling Interrupts…
• Drawbacks
12
Lock Variables
– Consider having a single shared variable X.
14
Strict Alternation
• the integer variable turn, initially 0, keeps track of whose
turn it is to enter the critical region and examine or update
the shared memory.
p0 p1
while (TRUE) { /* loop */ while (TRUE) { /*loop*/
while (turn != 1) while (turn != 0)
critical_region( ); critical_region( );
turn =1; turn =0;
noncritical_region( ); noncritical_region( );
} }
15
Strict Alternation…
• Problem:
– busy waiting: Continuously testing a variable
until some value appears, which wastes CPU time
• If P0 set turn 0, P1 will continuously check turn till it
becomes 1.
16
Sleep and wakeup
• is IPC primitives that block instead of wasting
CPU time when they are not allowed to enter
critical regions.
17
Sleep and wakeup…
The Producer-Consumer Problem (the bounded_buffer 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.
• Case 1: when the producer wants to put a new item in the
buffer, but it is already full.
– go to sleep to be awakened when the consumer has removed one or
more items.
20
The producer-consumer: problem
Race condition.
• The buffer is empty and the consumer has just read
count=0 .
• At that instant, the scheduler decides to stop running the
consumer temporarily and start running the producer.
• Case 1:
– Producer sees count = 0, insert item, inc. count =1, and it wakes up
the consumer. However, consumer is not asleep.
– Problem: wakeup signal is lost.
• Case 2:
– next when consumer runs, count =0 and go to sleep…
• Case 3:
– producer will fill buffer and will go to sleep. 21
– Both will sleep forever.
The producer-consumer: problem…
Problem
• The problem here is that a wakeup sent to a
process that is not (yet) sleeping is lost.
Solution
• Semaphore
22
Semaphore
• Semaphore was proposed by Dijkstra to manage
concurrent processes by using a simple integer value,
which is known as a semaphore.
23
Semaphore…
• Dijkstra suggested using an integer variable to count
the number of wakeups saved for future use.
• In his proposal, a new variable type, called a
semaphore, was introduced.
• A semaphore could have the value 0, indicating that no
wakeups were saved, or
• some positive value if one or more wakeups were
pending.
• Dijkstra proposed having two operations,
– down – sleep
– up - wakeup,
24
Semaphore…
TWO OPERATIONS
Case 1
• The down operation on a semaphore checks to see if the
value is greater than 0.
• If so, it decrements the value (i.e., uses up one stored
wakeup) and just continues.
• If the value is 0, the process is put to sleep without
completing the down for the moment.
Case 2
• The up operation increments the value of 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
25
complete its down.
Solving the producer consumer problem
using Semaphore
• Semaphores that are
initialized to 1 and used by
two or more processes to
ensure that only one of
them can enter its critical
region at the same time are
called binary semaphores.
• full is initially 0,
• empty is initially equal to the number of slots in the buffer,
• mutex is initially 1.
27
#define N 100 /* number of slots in the buffer */
typedef int semaphore; /* semaphores are a special kind*/
semaphore mutex = 1; /* controls access to critical region*/
semaphore empty = N; /* counts empty buffer slots */
semaphore full = 0; /* counts full buffer slots */
void prducer(void) {
int item;
while (TRUE) { /* TRUE is the constant 1 */
item =produce_item();/*generate something to put in the buffer*/
down(&empty); /* decrement empty count */
down(&mutex); /* enter critical region */
insertitem(item); /* put new item in buffer */
up(&mutex); /* leave critical region */
/* increment count of full slots */
28
up(&full);
void consumer(void)
{
int item;
while (TRUE) { /* infinite loop */
down(&full); /* decrement full count */
down(&mutex); /* enter critical region */
item = remove_item(); /* take item from buffer */
up(&mutex); /* leave critical region */
up(&empty); /* increment count of empty slots*/
consume_item(item); /* do something with the item */
29
Semaphore: Java implementation
Gentle reminder
• As we have seen, the producer-consumer problem is
an example of a multi-process synchronization
problem.
Solution
• The producer is to go to sleep if the buffer is full.
• The next time the consumer removes an item from the buffer, it
notifies the producer, who starts to fill the buffer again.
• In the same way, the consumer can go to sleep if it finds the
buffer to be empty.
• The next time the producer puts data into the buffer, it wakes up
the sleeping consumer. 31
Semaphore: Java implementation…
32
Semaphore, java…
import java.io.*;
import java.util.concurrent.Semaphore;
class Buffer {
int item;
//semConsumer is initialized to 0 to ensure put executes first
static Semaphore semConsumer=new Semaphore(0);
static Semaphore semProducer=new Semaphore(1);
//get item from buffer
public void get() {
try {
// Before consumer can consume an item, it must acquire a permit fromsemConsumer
semConsumer.acquire();
}
catch(InterruptedException e) {
System.out.println("Unable to enter CS!");
} 33
Semaphore, java…
//consumer consumes item
System.out.println("Consumer consumed item:"+ item);
//after consumer consumes item, it should release semProducer to notify producer
semProducer.release();
}
//put item in buffer
public void put(int item) {
try {
//Before producer can produce an item, it must acquire a permit from semProducer
semProducer.acquire();
}
catch(InterruptedException e) {
System.out.println("Unable to put item on buf");
34
}
Semaphore, java…
}
}
35
Semaphore, java…
//Producer class
class Producer implements Runnable {
Thread myThread;
Buffer b;
Producer(Buffer b) {
this.b=b;
myThread=new Thread(this, "Producer");
myThread.start();
}
public void run() {
for(int i=0; i<10;i++)
//producer puts item
b.put(i);
} 36
}
Semaphore, java…
//Consumer class
class Consumer implements Runnable {
Thread myThread;
Buffer b;
Consumer(Buffer b) {
this.b=b;
myThread=new Thread(this, "Consumer");
myThread.start();
}
public void run() {
for(int i=0; i<10;i++)
//consumer get item
b.get();
37
} }
Semaphore, java…
//main class
public class ProducerConsumer {
public static void main(String[] args) {
//creating buffer queue
Buffer b=new Buffer();
39
Sockets
• process sends/receives host or host or
client server
messages to/from its socket
controlled by
• socket analogous to door app developer
process process
– sending process gives message
out door socket socket
TCP with TCP with
– sending process relies on buffers, Internet buffers,
transport infrastructure on variables variables
other side of door which brings
message to socket at receiving
process controlled
by OS
40
Addressing processes
• to receive messages, • identifier includes both IP
process must have address and port numbers
identifier associated with process on
• host device has unique host.
32-bit IP address • Example port numbers:
• Q: does IP address of – HTTP server: 80
host on which process – Mail server: 25
runs suffice for identifying
• to send HTTP message to a
the process?
web server with IP
– A: No, many processes address:217.110.45.12, we
can be running on same use:
host
– 217.110.45.12:80 41
Socket Addresses
42
some of the well-known ports used by TCP
43
Socket programming
Goal: learn how to build client/server application that communicate using
sockets
Socket API
socket
• introduced in BSD4.1 UNIX,
1981 a host-local,
application-created,
• explicitly created, used, released OS-controlled interface (a
by apps “door”) into which
• client/server paradigm application process can
both send and
• two types of transport service receive messages to/from
via socket API: another application process
– reliable, byte stream-oriented
TCP
– unreliable datagram UDP
44
Socket programming using TCP
Client must contact server
• server process must first be running
• server must create socket (door) that welcomes client’s contact
Server Client
(running on hostid)
create socket,
port=x, for
incoming request:
ServerSocket servSock =new
ServerSocket(x)
write reply to
servSock read reply from
clientSocket
close
servSock
close
clientSocket 46
Stream terminology
keyboard monitor
• A stream is a sequence of
characters that flow into or out
inFromUser
of a process. input
stream
Client
Process
process
• An input stream is attached to
some input source for the
process, e.g., keyboard or
socket.
inFromServer
outToServer
output input
stream stream
47
Server-Socket programming
• The java.net package provides ServerSocket and
DatagramSocket objects for servers at the TCP/IP
socket level.
ListenSocket Method
The listenSocket method creates a ServerSocket
object with port number on which the server
program is going to listen for client
communications.
49
Server-Socket Programming cont’d…
51
Server-Socket Programming cont’d…
Socket link=null;
try
{
link = servSock.accept();
}
catch(IOException e)
{
System.out.println("Accept failed: 1234");
}
52
Server-Socket Programming cont’d…
53
Server-Socket Programming cont’d…
int numMessages = 0;
String message = in.readLine();
while (!message.equals("close")) {
System.out.println("Message received.");
numMessages++;
out.println("Message " + numMessages + ": " +
message);
message = in.readLine();
}
54
Lab: Server -Socket source code
import java.io.*;
import java.net.*;
public class Server {
private static ServerSocket servSock;
private static final int PORT=1234;
public static void main(String[] args)
throws IOException {
System.out.println("Openning port.....");
while(true)
{
listenSocket();
}
}
55
Lab: Server-Socket source code …
public static void listenSocket() {
try {
Create servSock=new ServerSocket(PORT);
server socket
at port 1234 } catch(IOException e) {
System.out.println("Unable to create
socket with port no:1234!");
System.exit(-1); }
Socket link=null;
try {
Create socket
connection
link=servSock.accept();
with the client
} catch(IOException e){
System.out.println("Accept failed:
Port 1234");
56
}
Lab: Server-Socket source code …
try {
Create input BufferedReader in=new BufferedReader(new
stream, attached
to socket
InputStreamReader(link.getInputStream(
)));
Create output
PrintWriter out=new
stream, attached PrintWriter(link.getOutputStream(),tru
to socket e);
int numMessages=0;
Read in line
from socket String message=in.readLine();
while(!message.equals("close")) {
System.out.println("Message
recieved.");
numMessages ++; 57
Lab: Server-Socket source code…
Write out line out.println("Message" + numMessages+
to socket ":" + message);
message=in.readLine();
}
}
catch(IOException e)
{
System.out.println("Message is not
recieved");
End of while loop,
loop back and wait for }
another client connection
}
} 58
Client-Socket Programming
59
Client-Socket Programming cont’d…
60
Client-Socket Programming cont’d…
listenSocket Method
The listenSocket method first creates a Socket object
with the IP address (“local host”) and port number (1234)
where the server program is listening for client connection
requests.
Socket link= new Socket(host,PORT);
63
Lab: Client-Socket Programming
import java.io.*;
import java.net.*;
public class Client {
private static InetAddress host;
private static final int PORT=1234;
public static void main(String[] args) throws
IOException {
Server is try {
local host=InetAddress.getLocalHost();
}
catch(UnknownHostException e)
{
System.out.println("Host id not found!");
System.exit(-1);
}
listenSocket();
}
64
Lab: Client-Socket Programming
public static void listenSocket() {
Socket link=null;
Create try {
client socket, link=new Socket(host,PORT);}
connect to server
catch(IOException e){
System.out.println("Unable to connect");
System.exit(-1);}
Create try {
input stream
attached to socket BufferedReader in=new BufferedReader(new
InputStreamReader(link.getInputStream()));
Create PrintWriter out=new
output stream PrintWriter(link.getOutputStream(),true);
attached to socket
67