0% found this document useful (0 votes)
340 views7 pages

R22 OS Lab Manual Final5!18!24

The document discusses various IPC mechanisms in C programming like pipes, FIFOs, message queues, and shared memory. It provides code examples to illustrate each mechanism. Pipes allow for one-way communication between related processes. FIFOs enable two-way communication between unrelated processes using a named pipe. Message queues support full duplex communication between processes by posting and retrieving messages. Shared memory achieves inter-process communication by sharing a piece of memory that must be synchronized between processes.

Uploaded by

227r1a6694
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)
340 views7 pages

R22 OS Lab Manual Final5!18!24

The document discusses various IPC mechanisms in C programming like pipes, FIFOs, message queues, and shared memory. It provides code examples to illustrate each mechanism. Pipes allow for one-way communication between related processes. FIFOs enable two-way communication between unrelated processes using a named pipe. Message queues support full duplex communication between processes by posting and retrieving messages. Shared memory achieves inter-process communication by sharing a piece of memory that must be synchronized between processes.

Uploaded by

227r1a6694
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/ 7

5.

Write C programs to illustrate the following IPC mechanisms


a)Pipes b)FIFOs c)MessageQueues d)SharedMemory

Pipes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
Int main()
{
int fd1[2];
intfd2[2];
charfixed_str[]="Welcome";
charinput_str[100];
pid_t p;
if(pipe(fd1)==-1)
{
fprintf(stderr,"pipe failed");
return1;
}
if(pipe(fd2)==-1)
{
fprintf(stderr,"pipe failed");
return1;
}
scanf("%s",input_str);
p=fork();
if(p<0){
fprintf(stderr,"forkfailed");
return1;
}
elseif(p>0){
charconcat_str[100];
close(fd1[0]);
write(fd1[1],input_str,strlen(input_str)+1);
close(fd1[1]);
wait(NULL);
close(fd2[1]);
read(fd2[0],concat_str,100);
printf("concatenated string %s\n",concat_str);
close(fd2[0]);
}
else{
close(fd1[1]);
char concat_str[100];
read(fd1[0],concat_str,100);
intk=strlen(concat_str);
int i;
for(i=0;i<strlen(fixed_str);i++)
concat_str[k++]=fixed_str[i];
concat_str[k]='\0';
close(fd1[0]);
close(fd2[0]);
write(fd2[1],concat_str,strlen(concat_str)+1);
close(fd2[1]);
exit(0);
}
}

OUTPUT
ubuntu@DESKTOP-B8CV2UR:~$
./a.outHi
concatenated string
HiWelcomeubuntu@DESKTOP-
B8CV2UR:~$

FIFOs
Fifo writer code

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
Int main()
{
int fd;
char
*myfifo="/home/ubuntu/myfifo";mkfi
fo(myfifo,0666);
chararr1[80],arr2[80];
while(1)
{
fd=open(myfifo,O_WRONLY);
fgets(arr2,80,stdin);
write(fd, arr2,strlen(arr2)+1);
close(fd);
fd=open(myfifo,O_RDONLY);
read(fd,arr1,sizeof(arr1));
printf("USer2: %s\n",arr1);
close(fd);
}
return0;
}
Fiforeaderscode

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int fd;
char *
myfifo="/home/ubuntu/myfifo";mkfifo
(myfifo,0666);
char str1[80],str2[80];
while(1)
{
fd=open(myfifo,O_RDONLY);
read(fd,str1,80);
printf("User1: %s\n",str1);
close(fd);
fd=open(myfifo,O_WRONLY);
fgets(str2,80,stdin);
write(fd,str2,strlen(str2)+1);
close(fd);
}
return0;
}

OUTPUT
//simultaneouslyexecutebothwriteandreadercodeintwoterminals
MessageQueues
//sendercode

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define MAX_TEXT 512
struct my_msg{
long int msg_type;
char some_text[MAX_TEXT];
};
int main()
{
int running=1;
int msgid;
struct my_msgsome_data;
char buffer[50];
msgid=msgget((key_t)14534,0666|IPC_CREAT);
if(msgid ==-1)
{
printf("Errorincreatingqueue\n");e
xit(0);
}

while(running)
{
printf("Enter some text:\n");
fgets(buffer,50,stdin);
some_data.msg_type=1;
strcpy(some_data.some_text,buffer);
if(msgsnd(msgid,(void*)&some_data, MAX_TEXT,0)==-1)
{
printf("Msgnotsent\n");
}
if(strncmp(buffer,"end",3)==0)
{
running=0;
}
}
}
//Receivercode
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct my_msg{
long int msg_type;
char some_text[BUFSIZ];
};
int main()
{
int running=1;
int msgid;
struct my_msgsome_data;
long int msg_to_rec=0;
msgid=msgget((key_t)12345,0666|IPC_CREAT);
while(running)
{
msg rcv(msgid,(void
*)&some_data,BUFSIZ,msg_to_rec,0);
printf("Data received: %s\n",some_data.some_text);
if(strncmp(some_data.some_text,"end",3)==0)
{
running=0;
}
}
msgctl(msgid,IPC_RMID,0);
}
OUTPUT
//ExecutionissameasasFIFO

SharedMemory
//Writer
code#include<stdlib.
h>#include<string.h
>#include<sys/shm.h
>#include<stdio.h>#i
nclude<unistd.h>int
main()
{
int i;
void *
shared_memory;charbu
ff[100];
intshmid;shmid=shmget((key_t)2345,1024,0666|IPC_
CREAT);printf("key of shared memory is
%d\n",shmid);shared_memory=shmat(shmid,NULL,0)
;printf("Process attached at
%p\n",shared_memory);printf("Enter some data to
write to shared memory\n");read(0,buff,100);
strcpy(shared_memory,buff);
printf("youwrote:%s\n",(char*)shared_memory);
}

//Readers Code
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
Int main(){
int i;
void* shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345,1024,0666);
printf("keyof sharedmemory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("processattacehedat%p\n",shared_memory);
printf("Datareadformsharedmemroyis%s\n",(char*)shared_memory);
}

OUTPUT

Viva questions:

a: What is IPC?
Ans: Inter Process Communication (IPC) is a mechanism that involves communication of one process
with another process

b: What are Pipes?


Ans: Pipes is used for communication between two related processes, the mechanism used in pipes is
half duplex i.e., only one way communication is possible

c: What is FIFO?
Ans: b. FIFO (First-In- First-Out) named pipe are meant for communication between unrelated
process, the mechanism used in FIFO is full duplex i.e., same single named pipe can be used for two-
way communication

d: What is Message Queue?


Ans: It support communication between two or more processes with full duplex capacity, the
processes will communicate with each other by posting a message and retrieving it out of the queue.

e: What is Shared Memory?


Ans: Communication between two or more processes is achieved through a shared piece of memory
among all processes, the shared memory needs to be protected from each other by synchronizing
access to all the processes.

You might also like