Program 7):
AIM: Write C programs to illustrate the following IPC mechanisms:
a. Pipes
b. Fifos
c. Message queues
d. Shared memory
a) PIPES
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()
{
int fd[2];
char data[100];
pid_t pid;
pipe(fd);
pid=fork();
if(pid==0)
{
close(fd[1]);
read(fd[0],data,100);
printf("Reading from pipe.....\n");
printf("%s",data);
}
else
{
close(fd[0]);
printf("Enter the string.....");
printf("\nWritng to pipe from parent...\n");
gets(data);
write(fd[1],data,100);
wait();
printf("\n");
}
return 0;
}
OUTPUT:
Reading from
pipe….
Venky
Enter the string
…..
Writing to pipe
from parent…
b) FIFO
Sender
#includestdio.h
#includefcntl.h
#includesysstat.h
#includeunistd.h
int main()
{
int fd=0;
char data[100];
int len=0;
int i=1;
unlink(datapipe);
mkfifo(datapipe,0660);
fd=open(datapipe,O_RDONLY);
if(fd==-1)
{
printf(Error creating pipe);
return 1;
}
printf(receiver.c);
while(1)
{
len=read(fd,data,100);
if(len=0){break;}
data[len]='0';
printf(“\nstring %d!%s--” ,i,data);
i++;
}
close(fd);
unlink(datapipe);
printf(n);
return 0;
}
Receiver
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<stdlib.h>
int main()
{
int fd1=0;
char data[100];int len=0;
fd1=open("datapipe",O_WRONLY);
if(fd1==-1)
{
printf("Error in accessing pipes:");
exit(0);
}
while(1)
{
len=read(0,data,100);
if(len<=0)
break;
write(fd1,data,len);
}
close(fd1);
printf("\n");
return 0;
}
OUTPUT:
Receiver.c
String 1 venky
No comments:
Post a Comment