1. Course Code: CSE2005 Course Name: Operating System Lab
Faculty: Dr. Bhanu Chander Balusa Slot: L21+L22
Program-1:
Aim:
Consider that two threads of a process should access the critical section. Assume that the
critical section has a count variable with the initial value of 0. First thread increments the count
variable by one for five times and second thread decrements the count variable by one for five
times. Implement this job in C programming using Peterson’s solution of process
synchronization.
Background:
NAME: Mridul Jadon
REGNO: 20BCE1734
2. CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define TRUE 1
#define FALSE 0
// Let us create a global variable count to change it in threads
int count = 0;
int favoured;
int t1_wants_to_enter =FALSE;
int t2_wants_to_enter=FALSE;
void *increment(void *vargp)
{
// Store the value argument passed to this thread
int *myid = (int *)vargp;
t1_wants_to_enter=TRUE;
favoured=2;
while(t2_wants_to_enter==TRUE && favoured==2);
// Change global variable
for(int i=0;i<5;i++){
count++;
printf("Thread ID: %d, Count: %dn", *myid, count);
}
t1_wants_to_enter=FALSE;
// Print the argument and global variables
}
void *decrement(void *vargp)
{
// Store the value argument passed to this thread
int *myid = (int *)vargp;
t2_wants_to_enter=TRUE;
favoured=1;
while(t1_wants_to_enter==TRUE && favoured==1);
// Change global variable
for(int i=0;i<5;i++){
count--;
printf("Thread ID: %d, Count: %dn", *myid, count);
}
t2_wants_to_enter=FALSE;
// Print the argument and global variables
printf("Thread ID: %d, Count: %dn", *myid, count);
}
int main()
{
int i;
pthread_t tid1;
pthread_t tid2;
// Let us create three threads
pthread_create(&tid1, NULL, increment, (void *)&tid1);
pthread_create(&tid2, NULL, decrement, (void *)&tid2);
pthread_exit(NULL);
return 0;
}
3. OUTPUT:
Program-2:
Aim:
Consider that two threads of a process should access the critical section. Assume that the
critical section has a count variable with the initial value of 0. First thread increments the count
variable by one for five times and second thread decrements the count variable by one
4. for five times. Implement this job in C programming using Semaphore solution of process
synchronization.
Background:
CODE:
#include<pthread.h>
#include<stdio.h>
#include<semaphore.h>
#include<unistd.h>
void *fun1();
void *fun2();
int shared=1;
sem_t s;
int main()
{
sem_init(&s,0,1);
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, fun1, NULL);
pthread_create(&thread2, NULL, fun2, NULL);
5. pthread_join(thread1, NULL);
pthread_join(thread2,NULL);
printf("Final value of shared is %dn",shared);
}
void *fun1()
{
int x;
sem_wait(&s);
x=shared;
printf("Thread1 reads the value as %dn",x);
for(int i=0;i<5;i++){
x++;
}
printf("Local updation by Thread1: %dn",x);
sleep(1);
shared=x;
printf("Value of shared variable updated by Thread1 is: %dn",shared);
sem_post(&s);
}
void *fun2()
{
int y;
sem_wait(&s);
y=shared;
printf("Thread2 reads the value as %dn",y);
for(int i=0;i<5;i++){
y--;
}
printf("Local updation by Thread2: %dn",y);
sleep(1);
shared=y;
printf("Value of shared variable updated by Thread2 is: %dn",shared);
sem_post(&s);
}
OUTPUT: