write a program to create a child process. Send the command to child process and execute there and return the result to the parent by using IPC using pipes.
main()
{
char *one[3],*two[2];
int ret;
one[0]=”ls”;
one[1]=”-il”;
one[2]=(char*)0;
two[0]=”sort”;
two[1]=(char*)0;
ret=join(one,two);
printf(“ join returned %d\n”,ret);
exit(0);
}
int join (com1,com2)
char *com1[],*com2[]
{
int p[2],status;
switch(fork())
{
case -1:
perror(“error”);
case 0:
break;
default:
wait(&status);
return (status);
}
if (pipe(p) < 0)
perror(“pipe call in join “);
switch(fork())
{
case -1:
perror(“ 2nd fork call in join “);
case 0:
close(1);
dup(p[1]);
close(p[0]);
close(p[1]);
execl(“/bin/ls”,com1[0],com1[1],com1[2]);
perror(“ 1st execvp call in join”);
default:
close(0);
dup(p[0]);
close(p[0]);
close(p[1]);
execl(“/bin/sort”,com2[0],com2[1]);
perror(“2nd execvp call in join”);
}
}
6. Write a program to establish the message communication between the child and parent process using pipes.
#include<stdio.h>
main()
{
int pp[2],pc[2],j,pid;
char msg1[20];
char msg2[20];
char msg3[20];
pipe(pp);
pipe(pc);
pid=fork();
if (pid==0)
{
close(pp[1]);
close(pc[0]);
write(pc[1],”hello daddy”,12);
read (pp[0],msg2,12);
printf(“%s\n”,msg2);
write(pc[1],”Thank you papa”,14);
}
else
{
close(pp[0]);
close(pc[1]);
read(pc[0],msg1,12);
printf(“%s\n”,msg1);
write(pp[1],”hello baby”,12);
read(pc[0],msg3,14);
printf(“%s\n”,msg3);
}
}
0 Comments