You are on page 1of 8

OS Lab Assignment – 05

Name : Sachin Mahendra Gaikwad


Roll No : 207941
Reg No : MC20151

1. Write a program to show the race condition.


Create two threads: one to increment the value of a shared variable and second to decrement
the value of shared variable. Both the threads are executed, so the final value of shared
variable should be same as its initial value. But due to race condition it would not be same.
#include<bits/stdc++.h>
using namespace std;
int shared = 1;
void fun1()
{
int x;
x = shared;
cout << "Thread1 reads the value of shared variable as " << x << endl;
x++;
cout << "Local updation by Thread1: " << x << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
shared = x;
cout << "Value of shared variable updated by Thread1 is: " << shared <<
endl;
}
void fun2()
{
int y;
y = shared;
cout << "Thread2 reads the value of shared variable as " << y << endl;
y--;
cout << "Local updation by Thread2: " << y << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
shared = y;
cout << "Value of shared variable updated by Thread2 is: " << shared <<
endl;
}
int main()
{
thread t1(fun1);
thread t2(fun2);
t1.join();
t2.join();
cout << "Final value of shared is " << shared << endl;
return 0;
}

Output :
2. Write a Program to avoid Race condition using Mutex Locks.
Create two threads: one to increment the value of a shared variable and second to decrement
the value of shared variable. Both the threads make use of locks so that only one of the
threads is executing in its critical section.
#include<bits/stdc++.h>
using namespace std;
int shared = 1;
mutex m;
void fun1()
{
int x;
cout << "Thread1 trying to acquire lock " << endl;
m.lock();
cout << "Thread1 acquired lock " << endl;
x = shared;
cout << "Thread1 reads the value of shared variable as " << x << endl;
x++;
cout << "Local updation by Thread1: " << x << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
shared = x;
cout << "Value of shared variable updated by Thread1 is: " << shared <<
endl;
m.unlock();
cout << "Thread1 released the lock " << endl;
}
void fun2()
{
int y;
cout << "Thread2 trying to acquire lock " << endl;
m.lock();
cout << "Thread2 acquired lock " << endl;
y = shared;
cout << "Thread2 reads the value of shared variable as " << y << endl;
y--;
cout << "Local updation by Thread2: " << y << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
shared = y;
cout << "Value of shared variable updated by Thread2 is: " << shared <<
endl;
m.unlock();
cout << "Thread2 released the lock " << endl;
}
int main()
{
thread t1(fun1);
thread t2(fun2);
t1.join();
t2.join();
cout << "Final value of shared is " << shared << endl;
return 0;
}

Output :
3. Write a Program to avoid Race condition using Semaphore.
Create two threads: one to increment the value of a shared variable and second to decrement
the value of shared variable. Both the threads make use of semaphore so that only one of the
threads is executing in its critical section.

Note : A semaphore is a signalling mechanism and a thread that is waiting on a semaphore


can be signalled by another thread. This is different than a mutex as the mutex can be
signalled only by the thread that called the wait function.
A semaphore uses two atomic operations, wait and signal for process synchronization.
The wait operation decrements the value of its argument S, if it is positive. If S is negative or
zero, then no operation is performed.
#include<bits/stdc++.h>
using namespace std;
int shared = 1;
int s = 1;
void wait(int &s)
{
while(s<=0);
s--;
}
void signal(int &s)
{
s++;
}
void fun1()
{
wait(s);
int x;
x = shared;
cout << "Thread1 reads the value of shared variable as " << x << endl;
x++;
cout << "Local updation by Thread1: " << x << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
shared = x;
cout << "Value of shared variable updated by Thread1 is: " << shared <<
endl;
signal(s);
}
void fun2()
{
wait(s);
int y;
y = shared;
cout << "Thread2 reads the value of shared variable as " << y << endl;
y--;
cout << "Local updation by Thread2: " << y << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
shared = y;
cout << "Value of shared variable updated by Thread2 is: " << shared <<
endl;
signal(s);
}
int main()
{
thread t1(fun1);
thread t2(fun2);
t1.join();
t2.join();
cout << "Final value of shared is " << shared << endl;
return 0;
}

Output :

You might also like