This document discusses process synchronization. It defines process synchronization as sharing system resources by processes to minimize inconsistent data. There are two types of processes: independent processes where execution of one does not affect others, and cooperative processes where execution of one can impact others. The critical section is a code segment accessed by multiple processes that needs synchronization to prevent inconsistent updates. Solutions to the critical section problem ensure mutual exclusion, progress, and bounded waiting. The document provides an example Java program to demonstrate the need for synchronization when multiple threads access a shared resource.
2. Objectives:
What is Process Synchronization
Types of Process
Critical Section
Solution of critical Section
Example of Synchronization in java
Monday, March
11, 2019
2
3. Process Synchronization
Process synchronization means sharing system resources by
processes in such a way that, Concurrent access to shared
data is handled thereby minimizing the chance of inconsistent
data.
Maintaining data consistency demands mechanisms to
ensure synchronized execution of cooperating process.
At times when more then one thread try to access a shared
resources, we need to ensure that resources will be used by
only one thread at a time. The process by which this is
achieved is called synchronization.
Monday, March
11, 2019
3
5. Critical Section
Critical Section is just a code segment which contain some variables
and data
Consider a system consists of ‘n’ processes.
Each process has segment of code called a critical section, in which the
process may be changing common variables, updating a table , writing a
file. When one process is executing in its critical section , no other
process can allowed to execute in its critical section .
Monday, March
11, 2019
5
6. Problem of Critical section
P1 P2
int x= shared; int y=shared;
X++; y--;
Sleep(1); Sleep(1);
Shared=x; shared=y;
Monday, March
11, 2019
6
7. Solution of Critical Section
Mutual Exclusion :
Mutual exclusion is assured as only one process can access the critical section at a
time.
Progress:
Progress is also assured , as a process outside the critical section does not block
other process from entering the critical section.
Bounded Waiting:
Bounded waiting is preserved as every process get a fair chance
Monday, March
11, 2019
7
8. Java Program of Non Synchronization
Monday, March
11, 2019
8
class First{
public void display(String msg, String ff){
System.out.println(msg);
try{
Thread.sleep(1000);
}
catch (InterruptedException nbn){
nbn.printStackTrace();
}
System.out.println(ff);
}
}
class Second extends Thread{
String msg,msg2;
First ff1;
Second(First ff2, String str, String str2){
ff1= ff2;
msg= str;
msg2= str2;
start();
}
public void run(){
ff1.display(msg,msg2); }}
public class Main {
public static void main(String[] args) {
First fnew = new First();
Second ss1= new Second(fnew,"Wellcome","in BSCS");
Second ss2= new Second(fnew,"Wellcome ","in Math");
}
}
10. Java program of Synchronize
Monday, March
11, 2019
10
class First{
public void display(String msg,String msg2){
System.out.println(msg);
try{
Thread.sleep(1000);}
catch (InterruptedException nbn)
{ nbn.printStackTrace(); }
System.out.println(msg2);
}}
class Second extends Thread{
String msg,msg2;
First ff1;
Second(First ff2, String str2,String str3){
ff1= ff2;
msg= str2;
msg2= str3;
start(); }
public void run(){
synchronized (ff1)
{
ff1.display(msg,msg2);
} }}
public class Main {
public static void main(String[] args) {
First fnew = new First();
Second ss1= new Second(fnew,"Wellcome","In Bscs");
Second ss2= new Second(fnew,"Wellcome ","In Math"); }}