0% found this document useful (0 votes)
10 views

OS LAB 4TH & 5TH WEEK PROGRAMS (2)

The document provides C programs to implement the Producer-Consumer problem using semaphores and various IPC mechanisms including Pipes, FIFO, Message Queues, and Shared Memory. It includes code snippets for each mechanism, demonstrating how to create and manage inter-process communication. The examples illustrate basic operations for producing and consuming data, as well as sending and receiving messages through different IPC methods.

Uploaded by

gundasreeja6
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)
10 views

OS LAB 4TH & 5TH WEEK PROGRAMS (2)

The document provides C programs to implement the Producer-Consumer problem using semaphores and various IPC mechanisms including Pipes, FIFO, Message Queues, and Shared Memory. It includes code snippets for each mechanism, demonstrating how to create and manage inter-process communication. The examples illustrate basic operations for producing and consuming data, as well as sending and receiving messages through different IPC methods.

Uploaded by

gundasreeja6
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/ 8

4.

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);
}

5. Write a C Program to illustrate the following IPC mechanisms


a) Pipes b) FIFO c) Message Queues d) Shared Memory
a) Pipes

#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>

#define MSG_KEY 1234


#define MSG_SIZE 1024
struct msgbuf {
long mtype; // Message type (must be > 0)
char mtext[MSG_SIZE]; // Message data
};

int main() {
int msgid;
struct msgbuf message;
key_t key;

key = MSG_KEY;

// Get the message queue


if ((msgid = msgget(key, 0666)) < 0) {
perror("msgget");
exit(1);
}

// Receive the message


if (msgrcv(msgid, &message, MSG_SIZE, 1, 0) < 0) {
perror("msgrcv");
exit(1);
}

printf("Received message: %s\n", message.mtext);

// Optionally, remove the message queue after receiving


// msgctl(msgid, IPC_RMID, NULL);

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>

#define SHM_SIZE 1024

int main() {
key_t key = 1234; // Same key as sender
int shmid;
char *shm_ptr;

// Get the shared memory segment ID


shmid = shmget(key, SHM_SIZE, 0666);
if (shmid < 0) {
perror("shmget");
exit(1);
}

// Attach the shared memory segment to the address space


shm_ptr = shmat(shmid, NULL, 0);
if (shm_ptr == (char *) -1) {
perror("shmat");
exit(1);
}

// Read the message from shared memory


printf("Received message: %s\n", shm_ptr);

// Detach the shared memory segment


if (shmdt(shm_ptr) == -1) {
perror("shmdt");
exit(1);
}

// Remove the shared memory segment (optional, done by receiver).


if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl");
exit(1);
}

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>

#define SHM_SIZE 1024

int main() {
key_t key = 1234; // Unique key for shared memory
int shmid;
char *shm_ptr;
char message[100];

// Create the shared memory segment


shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget");
exit(1);
}

// Attach the shared memory segment to the address space


shm_ptr = shmat(shmid, NULL, 0);
if (shm_ptr == (char *) -1) {
perror("shmat");
exit(1);
}

printf("Enter a message to send: ");


fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = 0; // Remove trailing newline

// Copy the message to shared memory


strcpy(shm_ptr, message);

printf("Message sent: %s\n", message);

// Detach the shared memory segment


if (shmdt(shm_ptr) == -1) {
perror("shmdt");
exit(1);
}

return 0;
}
OUTPUT

You might also like