Pipe System Call in Linux
Write a C program to run on Linux to do the following.
1. Parent process creates a pipe using the pipe() system call.
2. Parent process creates a child process.
3. Parent process sends a message "Hello from Parent" to the child.
4. Child process prints the message to the screen.
C Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int pid,pipe_ends[2];
char received_string[20];
pipe(pipe_ends);
pid=fork();
if(pid>0){
close(pipe_ends[0]);
write(pipe_ends[1], "Hello From Parent", 18);
close(pipe_ends[1]);
exit(0);
}
else{
close(pipe_ends[1]);
read(pipe_ends[0], received_string, 18);
printf("Message - %s\n", received_string);
close(pipe_ends[0]);
exit(0);
}
}
1. Parent process creates a pipe using the pipe() system call.
2. Parent process creates a child process.
3. Parent process sends a message "Hello from Parent" to the child.
4. Child process prints the message to the screen.
C Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int pid,pipe_ends[2];
char received_string[20];
pipe(pipe_ends);
pid=fork();
if(pid>0){
close(pipe_ends[0]);
write(pipe_ends[1], "Hello From Parent", 18);
close(pipe_ends[1]);
exit(0);
}
else{
close(pipe_ends[1]);
read(pipe_ends[0], received_string, 18);
printf("Message - %s\n", received_string);
close(pipe_ends[0]);
exit(0);
}
}
Comments
Post a Comment