0% found this document useful (0 votes)
32 views14 pages

Assignment 6

Uploaded by

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

Assignment 6

Uploaded by

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

Assignment 6

Name: Atharav Gaikwad


Reg. no: 22BCE8897
Lab slot: L53+54

A. IPC USING SEMAPHORE – PRODUCER AND CONSUMER


PROBLEM

PROGRAM:

#define BUFFER_SIZE 20
typedef int semaphore;
semaphore full=0;
semaphore empty=BUFFER_SIZE;
typedef struct
{
int year;
int roll;
}
item;
item buffer[BUFFER_SIZE];
int counter=0;
int in=0;
int out=0;
main()
{
int choice;
int year1,roll1;
int i;
while(1)
{
printf("\n 1.producer");
printf("\n 2.consumers");
printf("\n 3.display");
printf("\n 4.exit");
printf("\n enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(((in+1)%BUFFER_SIZE)!=out)
{
printf("enter the year");

scanf("%d",&year1);
printf("enter the roll no");
scanf("%d",&roll1);
wait(empty);
buffer[in].year=year1;
buffer[in].roll=roll1;
signal(full);
in=(in+1)%BUFFER_SIZE;
counter ++;
}
break;
case 2:
if(in!=out)
{
wait(full);
printf("\n the year is %d",buffer[out].year);
printf("\n the roll no is %d",buffer[out].roll);
signal(empty);
out=(out+1)%BUFFER_SIZE;
counter --;
}
if(in<=out)
printf("\n buffer is empty");
break;
case 3:
for(i=out;i<in;i++)
{
printf("\n the year is %d",buffer[i].year);
printf("\n the roll no is %d",buffer[i].roll);
}
if(in<=out)
printf("buffer is empty");
break;
case 4:
break;}
if(in<=out)
printf("buffer is empty");
break;
case 4:
break;
exit(1);
}
}
}
signal(int temp)
{
temp++;
}
wait(int temp1)
{
temp1--;
}
OUTPUT
B. IPC USING SEMAPHORE – READERS AND WRITERS
PROBLEM

PROGRAM:

#include<stdlib.h>
#include<stdio.h>
static int mutex;
int wrt,readcount,ch;
int buffer[50];
int i=0,n;
void wait(int*);
void signal(int*);
void print();

void reader()
{
wait(&mutex);
readcount++;
if(readcount==1)
wait(&wrt);
signal(&mutex);
for(n=0;n<=i;n++)
printf("\n Reader Reads :%d",buffer[n]);
wait(&mutex);
readcount--;
if(readcount==0)
signal(&wrt);
signal(&mutex);
}

void writer()
{
wait(&wrt);
printf("\n Enter the Integer to write:");
scanf("%d",&buffer[++i]);
printf("\n Writer Writes: %d",buffer[i]);
signal(&wrt);
}
void wait(int *x)
{

while(*x<=0);
*x=*x-1;
}

void signal(int *y)


{
*y=*y+1;
}
int main()
{
clrscr();
printf("Enter the stored value:");
scanf("%d",&buffer[0]);
mutex=1;
wrt=1;
readcount=0;
do
{
printf("\n \n READER-WRITER PROBLEM \n\n 1.Reader \n 2.Writer \n 3. Read
and
Write \n 4.Write and Read \n 5.Exit \n\n Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
reader();
break;
case 2:
writer();
break;
case 3:
reader();
writer();
break;
case 4:
writer();
reader();
break;
case 5:
exit(0);
}
}
while(ch<6);
}

OUTPUT:
C. IPC USING SEMAPHORE – DINING
PHILOSOPHER PROBLEM

PROGRAM:

#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
D. IPC USING PIPES

PROGRAM:

#include<stdio.h>
#include<unistd.h>

int main() {
int pipefds[2];
int returnstatus;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);

if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}

printf("Writing to pipe - Message 1 is %s\n", writemessages[0]);


write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 1 is %s\n", readmessage);
printf("Writing to pipe - Message 2 is %s\n", writemessages[0]);

write(pipefds[1], writemessages[1], sizeof(writemessages[0]));


read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 2 is %s\n", readmessage);
return 0;
}
OUTPUT

You might also like