OS LAB 4TH & 5TH WEEK PROGRAMS (2)
OS LAB 4TH & 5TH WEEK PROGRAMS (2)
Write a C program to implement the Producer – Consumer problem using semaphores using
UNIX/LINUX system calls.
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3: exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int fd[2], n;
char buffer[100];
pid_t p;
pipe(fd);
p=fork();
if(p>0)
{
printf("Passing value to child\n");
write(fd[1], "hello\n",6);
}
else
{
printf("Child received data\n");
n=read(fd[0],buffer,100);
write(1,buffer,n);
}
return 0;
}
OUTPUT
b) FIFO
receiver.c
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int res,n;
char buffer[100];
res=open("fifo1",O_RDONLY);
n=read(res,buffer,100);
printf("Reader process having PID %d started\n",getpid());
printf("Data received by receiver %d is: %s\n",getpid(),buffer);
}
sender.c
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int res,n;
res=open("fifo1",O_WRONLY);
write(res,"Message",7);
printf("Sender process having PID %d sent the data\n",getpid());
}
OUTPUT
c) Message Queues
sender.c
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#define MAX_TEXT 50
struct my_msg{
long int msg_type;
char some_text[MAX_TEXT];
};
int main()
{
int running=1;
int msgid;
struct my_msg some_data;
char buffer[20];
msgid=msgget((key_t)12345,0666|IPC_CREAT);
if(msgid==-1)
{
printf("error in creating queue\n");
}
while(running);
{
printf("Enter some text\n");
fgets(buffer,20,stdin);
some_data.msg_type=1;
strcpy(some_data.some_text,buffer);
if(msgsnd(msgid,(void *)&some_data,MAX_TEXT,0)==-1)
{
printf("msg not sent\n");
}
if(strncmp(buffer,"end",3)==0)
{
running=0;
}
}
return 0;
}
receiver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
int main() {
int msgid;
struct msgbuf message;
key_t key;
key = MSG_KEY;
return 0;
}
OUTPUT
d) Shared Memory
reader.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
key_t key = 1234; // Same key as sender
int shmid;
char *shm_ptr;
return 0;
}
writer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
key_t key = 1234; // Unique key for shared memory
int shmid;
char *shm_ptr;
char message[100];
return 0;
}
OUTPUT