Using C, write a program that will create two child processes with one child process then using “pipe” to send “i love you” to another child process.
Program to pass value "I Love you" from one to another childProcess
#include<stdio.h>
#include<unistd.h>
int main() {
int
pipefds[2];
int
returnstatus;
int pid;
int pid2;
char
writemessages[1][20]={"I Love You"};
char
readmessage[20];
returnstatus =
pipe(pipefds);
if (returnstatus == -1)
{
printf("Unable to create
pipe\n");
return 1;
}
pid = fork();
pid2 =
fork();
// Child
process-2
if (pid == 0) {
read(pipefds[0],
readmessage, sizeof(readmessage));
printf("Child Process -2 -
Reading from pipe – Message 1 is %s\n", readmessage);
} if(pid2 == 0)
{ // Child
process-1
printf("Child Process- 1 - Writing to
pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1],
writemessages[0], sizeof(writemessages[0]));
}
return 0;
}
Output:
Child Process- 1 - Writing to pipe - Message 1 is I Love You
Child Process -2 - Reading from pipe – Message 1 is I Love You
Get Answers For Free
Most questions answered within 1 hours.