0% found this document useful (0 votes)
47 views8 pages

University of Engineering and Management Kolkata Operating System Project Project Report Submitted by

This document is a project report submitted by two students for their Operating Systems course. It details the development of a program using synchronized threads in Java to increment a counter value and print it without data corruption. The program uses flags and wait/notify methods to ensure only one thread can access the shared counter at a time. The report includes an introduction, theory section explaining the need for synchronization, flowchart, source code, and conclusion on the use of synchronization in Java.

Uploaded by

Saurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

University of Engineering and Management Kolkata Operating System Project Project Report Submitted by

This document is a project report submitted by two students for their Operating Systems course. It details the development of a program using synchronized threads in Java to increment a counter value and print it without data corruption. The program uses flags and wait/notify methods to ensure only one thread can access the shared counter at a time. The report includes an introduction, theory section explaining the need for synchronization, flowchart, source code, and conclusion on the use of synchronization in Java.

Uploaded by

Saurav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIVERSITY OF ENGINEERING AND MANAGEMENT

KOLKATA

Operating System Project

Project Report Submitted By:

 Sanjeet Mahato (Roll number: 25)


 Deepak Kumar (Roll number: 84)

Department of Computer Science Engineering


Section: 3A

Under the Guidance of:


Prof. Sukanya Roy and Prof. Satyam Raha
CERTIFICATION

This is to certify that the project entitled synchronized two thread


counter has been successfully completed by Sanjeet Mahato and
Deepak Kumar of B.Tech, stream CSE under the guidance of Prof.
Sukanya Roy and Prof. Satyam Raha.

____________________
Signature of Examiner
ACKNOWLEDGEMENT

I wish to express my sincere gratitude to Prof. Sukanya Roy and Prof.


Satyam Raha for providing an opportunity to do my project work on
synchronised two thread counter. I sincerely thank Prof. Sukalyan
Goswami for his guidance and encouragement in carrying out this
project work.
Thank you very much.
INTRODUCTION

Multi-thread applications are useful to execute more than one


operations simultaneously. However, if more than one threads
are accessing a shared resource, there is a high risk of data
corruption or unexpected result. This article explains the
reason for the unexpected results and the possible solution to
avoid it.

Even though threads are considered to be executing


simultaneously, actually they are executed one after another
using a time-sharing mechanism of the underlying operating
system.

SOFTWARE USED

Here we have used Java to Code the program using Javac Compiler.

THEORY

In this thread T1 is increasing the value of the counter element by


one. At the same time, thread T2 is printing the value of the counter
element. Both threads are executing these operations till counter value
reaches 40. So ideally the counter must be increased and printed only
once for each counter value. Same counter value should not be printed
more than once and counter value must not be incremented without
printing. This is achieved be using synchronized threads.
Without synchronization ideal situation is not achieved.
Flowchart

SOURCE CODE
class X{
int i;
boolean printed=false; // Start be printing the given number
boolean incremented=true; // Just to allow printing to start first

X(int i){
this.i=i;
}

synchronized void inc(){


while(incremented){
try {
wait();
} catch (InterruptedException e) {

System.out.println("ex");
}
}
i++;
incremented=true;
printed=false;
notifyAll();
}
synchronized void print(){
while(printed){
try {
wait();
}
catch (InterruptedException e){

System.out.println("ext");
}
}
System.out.println(i);
printed=true;
incremented=false;
notifyAll();
}
}

class T1 extends Thread{


X a;

T1(X a){
this.a=a;
}

public void run(){


while(a.i<40){
a.inc();
}
}
}

class T2 extends Thread{


X a;

T2(X a){
this.a=a;
}

public void run(){


while(a.i<40){
a.print();
}
}

}
public class os_pro {
public static void main(String[] args){
X a = new X(0);
T1 thread1 = new T1(a);
T2 thread2 = new T2(a);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

OUTPUT
CONCLUSION

We use Synchronization in Java whenever we want only thread


to access particular resource at a particular time. If more than 1
thread tries to accessing same resource at a same time then it
will allow only the first thread to access that resource. Once
completed then only second thread can use that resource.
We use Synchronization in Java to avoid dead locks. Dead
lock is the condition in java when 2 threads were accessing
same resource and they stuck in a way that not the first thread
can be completed nor the second thread can be completed.

BIBLIOGRAPHY

 https://www.quora.com
 https://en.wikipedia.org/wiki/Synchronization_(computer_science)

You might also like