Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

NAME: PRANAV CHAURASIA

REG. NO.: 18BCE0216


OS LAB ASSESSMENT-3
SLOT: L41 + L42
FACULTY: GERALDINE BESSIE MA’AM

1. Producer – Consumer Problem using Semaphores

CODE:

#include<stdio.h>
#include<sys/types.h>
#include<pthread.h>
#include<semaphore.h>
sem_t mutex;
sem_t full;
sem_t empty;
char buffer[100];

void *producer(void *arg)


{
int i,index=0;
for(i=0;i<26;i++)
{
sem_wait(&empty);
sem_wait(&mutex);

buffer[index]=i+64;
printf("Producer added %c to buffer ", buffer[index]);

sem_post(&full);
sem_post(&mutex);
if(++index==10)index=0;
}}

void *consumer(void *arg)


{
int i,index=0;
for(i=0;i<26;i++)
{
sem_wait(&full);
sem_wait(&mutex);

printf("Consumer consumed %c from buffer ",buffer[index]);

sem_post(&empty);
sem_post(&mutex);

if(++index==10) index=0;
}}

int main()
{
pthread_t tid1,tid2;
sem_init(&mutex,0,1);
sem_init(&empty,0,10); // 0 indicates its not shared memory but a buffer
sem_init(&full,0,0);

pthread_create(&tid1,NULL,producer,NULL);
pthread_create(&tid2,NULL,consumer,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);

sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);

return 0;
}

Output:
2. READER-WRITER Problem

CODE:

#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#define MAXTHREAD 10 /* define # readers */
void access_database(); /* prototypes */
void non_access_database();
void* reader(void*);
void* writer(void*);
sem_t q; /* establish que */
int rc = 0; /* number of processes reading or wanting to */
int wc = 0;
int write_request = 0;
int main()
{
pthread_t readers[MAXTHREAD],writerTh;
int index;
int ids[MAXTHREAD]; /* readers and initialize mutex, q and db-set them to 1
*/
sem_init (&q,0,1);
for(index = 0; index < MAXTHREAD; index ++)
{
ids[index]=index+1;
if(pthread_create(&readers[index],0,reader,&ids[index])!=0){
perror("Cannot create reader!");
exit(1);
}
}
if(pthread_create(&writerTh,0,writer,0)!=0){
perror("Cannot create writer");
exit(1);
}
pthread_join(writerTh,0);
sem_destroy (&q);
return 0;
}

void* reader(void*arg) /* readers function to read */


{
int index = *(int*)arg;
int can_read;
while(1){
can_read = 1;
sem_wait(&q);
if(wc == 0 && write_request == 0) rc++;
else can_read = 0;
sem_post(&q);
if(can_read) {
access_database();
printf("Thread %d reading\n", index);
sleep(index);
sem_wait(&q);
rc--;
sem_post(&q);
}
sched_yield();
}
return 0;
}
;
void* writer(void*arg) /* writer's function to write */
{
int can_write;
while(1){
can_write = 1;
non_access_database();
sem_wait (&q);
if(rc == 0) wc++;
else { can_write = 0; write_request = 1; }
sem_post(&q);
if(can_write) {
access_database();
printf("Writer is now writing...Number of readers: %d\n",rc);
sleep(3);
sem_wait(&q);
wc--;
write_request = 0;
sem_post(&q);
}
sched_yield();
}
return 0;
}
void access_database()
{
}
void non_access_database()
{
}

Output:
3. DINING PHILLOSOPHER Problem

CODE:

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>

#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N

int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];

void test(int phnum)


{
if (state[phnum] == HUNGRY
&& state[LEFT] != EATING
&& state[RIGHT] != EATING) {
// state that eating
state[phnum] = EATING;

sleep(2);

printf("Philosopher %d takes fork %d and %d\n",


phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is Eating\n", phnum + 1);

// sem_post(&S[phnum]) has no effect


// during takefork
// used to wake up hungry philosophers
// during putfork
sem_post(&S[phnum]);
}
}

// take up chopsticks
void take_fork(int phnum)
{

sem_wait(&mutex);

// state that hungry


state[phnum] = HUNGRY;

printf("Philosopher %d is Hungry\n", phnum + 1);

// eat if neighbours are not eating


test(phnum);

sem_post(&mutex);

// if unable to eat wait to be signalled


sem_wait(&S[phnum]);

sleep(1);
}

// put down chopsticks


void put_fork(int phnum)
{

sem_wait(&mutex);

// state that thinking


state[phnum] = THINKING;

printf("Philosopher %d putting fork %d and %d down\n",


phnum + 1, LEFT + 1, phnum + 1);
printf("Philosopher %d is thinking\n", phnum + 1);

test(LEFT);
test(RIGHT);

sem_post(&mutex);
}

void* philospher(void* num)


{

while (1) {

int* i = num;

sleep(1);

take_fork(*i);

sleep(0);

put_fork(*i);
}
}

int main()
{

int i;
pthread_t thread_id[N];

// initialize the semaphores


sem_init(&mutex, 0, 1);

for (i = 0; i < N; i++)

sem_init(&S[i], 0, 0);

for (i = 0; i < N; i++) {

// create philosopher processes


pthread_create(&thread_id[i], NULL,
philospher, &phil[i]);

printf("Philosopher %d is thinking\n", i + 1);


}

for (i = 0; i < N; i++)

pthread_join(thread_id[i], NULL);
}

Output:
4. BANKER’S Algorithm:

CODE:

#include <stdio.h>
int main()
{
int alloc[5][3],i,j;
int max[5][3];
int avail[3];
int n, m, k;
n = 5; // Number of processes
m = 3; // Number of resources
printf("\nEnter the allocation table for 5 processes in 5X3 matrix: \n ");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\nEnter max table(5X3):\n");
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\nEnter available resources:\n");
for(i=0;i<3;i++)
{
scanf("%d",&avail[i]);
}

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}

if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}}
}}
// P0, P1, P2, P3, P4 are the Process names here
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
return (0);
}

Output:

You might also like