CSE3102 Lab 2 and 3
CSE3102 Lab 2 and 3
Santosh, Tangail-1902
Faculty of Engineering
Department of Computer Science and Engineering
Course Title: Operating System Lab
Course code: CSE3102
Experiment No: 02
Experiment Name: producer and consumer problem using semaphore & Peterson’s
solution for critical section problem.
Submitted By: Submitted to:
A semaphore S is an integer variable that can be accessed only through two standard
operations : wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the signal() operation
increases its value by 1.
To solve this problem we need two counting semaphores -Full and Empty.Full keeps
track of number of items in the buffer at any given time and Empty keeps track of
number of unoccupied slots.
Algorithm:
Step 1: initialize mutex with 1, which is used to provide mutual exclusion to a critical
section of the code.
Step 2: initialize empty semaphore with an initial value is equal to the size of the
buffer.
Step 3: initialize the full semaphore with an initialize value of 0.The full semaphore
is used to keep track of number of filled slot in the buffer.
Step 4: now create two thread one for producer and one for consumer. Step 5: both
will run for infinite period of time.
Source Code:
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<semaphore.h>
#include<stdbool.h>
#include<windows.h>
#define Buffer_size 10
int buffer[Buffer_size];
int in=0;
int out=0;
sem_t mutex;
sem_t full;
sem_t empty1;
do
{
item++;
sem_wait(&empty1);
sem_wait(&mutex);
//add data in buffer
buffer[in]=item;
printf("Producer item %d\n",item);
in = (in+1)%Buffer_size;
sem_post(&mutex);
sem_post(&full);
Sleep(rand()%2);
}while(true);
}
out=(out+1)%Buffer_size;
sem_post(&mutex);
sem_post(&empty1);
Sleep(rand()%2);
}while(true);
}
int main()
{
pthread_t prod,con;
sem_init(&mutex,0,1);
sem_init(&empty1,0,Buffer_size);
sem_init(&full,0,0);
pthread_create(&prod,NULL,producer,NULL);
pthread_create(&con, NULL, consumer, NULL);
pthread_join(prod,NULL);
pthread_join(con,NULL);
sem_destroy(&mutex);
sem_destroy(&empty1);
sem_destroy(&full);
return 0;
}
Output:
Experiment no: 02
Name of experiment: Write a C program to Implement Peterson’s solution for
critical selection problem.
Description:
Peterson’s solution is a classic solution to the critical selection problem.The critical
selection problem ensures that no two processes change or modify a resource’s value
simultaneously.There are three sections except for the critical selection :entry
section,exit section and the reminder section.
Here we create two threads for denoting two process.When one process turns to
create selection another remains humble and lets that to enter in the critical section.
Algorithm:
Step 01:Initialize flag[0] and flag[1] equal to 0;
Step 02: Create two threads by using pthread_create;
Step 03: If Pi and Pj both are true ,then both are tries to enter critical section;
Step 04: For process Pi turn=j;
Step 05: Check a condition while((flag[1-other]==1)&&(turn==1-other)) if
condition false it leads us to the critical section;
Step 06: When Pi goes to the exit section ,Pj leads the critical section ;
Sources Code:
#include <stdio.h>
#include <pthread.h>
void *work(void *s);
int flag[2];
int turn, val=0;
void lock_init()
{ flag[0] = flag[1] = 0;
turn = 0;
}
void lock(int other)
{ flag[other] = 1;
turn = 1 - other;
while ((flag[1-other]==1)&&(turn==1-other));
}
void unlock(int other)
{ flag[other] = 0; }
void *work(void *s)
{ int i = 0;
int other = (int *)s;
printf("Thread : %d\n", other);
lock(other);
for (i=0;i<100000;i++) val++;
unlock(other);}
void main()
{
pthread_t t1, t2;
val = 0;
lock_init();
pthread_create(&t1, NULL, work, (void *)0);
pthread_create(&t2, NULL, work, (void *)1);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("Final Value: %d\n", val);
}
Output:
Mawlana Bhashani Science and Technology University
Santosh, Tangail-1902
Faculty of Engineering
Department of Computer Science and Engineering
Course Tittle: Operating System Lab
Course code: CSE3102
Experiment No: 03
Experiment Name: Solving dining-philosophers problem, Reader & Writer
problem using semaphore in C.
Submitted By: Submitted To:
Description:
The Dining Philosopher Problem states that K philosophers seated around a
circular table with one chopstick between each pair of philosophers. There is one
chopstick between each philosopher. A philosopher may eat if he can pick up the
two chopsticks adjacent to him. One chopstick may be picked up by any one of its
adjacent followers but not both. There are three states of the
philosopher: THINKING, HUNGRY, and EATING. Here there are two
semaphores: Mutex and a semaphore array for the philosophers. Mutex is used such
that no two philosophers may access the pickup or putdown at the same time. The
array is used to control the behavior of each philosopher. But, semaphores can
result in deadlock due to programming errors.
Algorithm:
1. The philosopher is instructed to think till the left fork is available, when it is
available, hold it.
2. The philosopher is instructed to think till the right fork is available, when it is
available, hold it.
3. The philosopher is instructed to eat when both forks are available.
4. then, put the right fork down first
5. then, put the left fork down next
6. repeat from the beginning.
Problem:
1. Deadlock: If every philosophers pick a chopstick each then nobody can eat.
2. Starvation: If a philosophers do not eat for a long time they might die
because of starvation.
Source code:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
#include<windows.h>
sem_t room,chopstick[5];
void * philosopher(void *);
void eat(int);
int main()
{
int i,a[5];
pthread_t tid[5];
sem_init(&room,0,4);
for(i=0;i<5;i++)
sem_init(&chopstick[i],0,1);
for(i=0;i<5;i++){
a[i]=i;
pthread_create(&tid[i],NULL,philosopher,(void *)&a[i]);
}
for(i=0;i<5;i++) pthread_join(tid[i],NULL);
}
void * philosopher(void * num)
{
int phil=*(int *)num;
sem_wait(&room);
printf("\nPhilosopher %d has entered room",phil);
sem_wait(&chopstick[phil]);
sem_wait(&chopstick[(phil+1)%5]);
eat(phil);
Sleep(2);//for windows
printf("\nPhilosopher %d has finished eating",phil);
sem_post(&chopstick[(phil+1)%5]);
sem_post(&chopstick[phil]);
sem_post(&room);
}
void eat(int phil)
{
printf("\nPhilosopher %d is eating",phil);
}
Output:
Experiment no: 04
Description:
To solve this situation, a writer should get exclusive access to an object when a writer
is accessing the object, no reader or writer may access it. However, multiple readers
can access the object at the same time.
Here, we will make the use of two semaphores and one integer variables to
implement this.
• mutex, a semaphore (initialized to 1) which is used to ensure mutual exclusion
when readcount is updated.
wrt, is a semaphore (initialized to 1) common to both reader and writer process.
• readcount , an integer variable (initialize to 0) that keeps track of how many
processes are currently reading object.
Source code:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
int counter=0;
void *writer(void *wno)
{
counter++;
sem_wait(&wrt);
cnt = cnt*2;
printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);
}
void *reader(void *rno)
{
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt);
}
pthread_mutex_unlock(&mutex);
printf("Reader %d: read cnt as %d and shared data\n",*((int *)rno),cnt);
pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0) {
sem_post(&wrt); // If this is the last reader, it will wake up the writer.
}
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {1,2,3,4,5,6,7,8,9,10}; //Just used for numbering the producer and
consumer
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}
Output: