#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BUFFER_SIZE 25
#define READ_END 0
#define WRITE_END 1
int main(void)
{
char write_msg[BUFFER_SIZE] = "HaZa CS is AweSome";
char read_msg[BUFFER_SIZE];
int fd[2];
pid_t
pid;
if(pipe(fd) == -1){
fprintf(stderr,"Pipe
Failed");
return 1;
}
pid
= fork(); //forking a child process
if(pid < 0){ //error occured
fprintf(stderr,"Fork
Failed");
return 1;
}
else if(pid>0){ //Parent
Process
close(fd[READ_END]); //Close the
unused end of the pipe
write(fd[WRITE_END],write_msg,strlen(write_msg)+1);
close(fd[WRITE_END]); //Close the
write end
}
else{ // CHILD PROCESS //
close(fd[WRITE_END]); // Close the
unused end of the pipe
read(fd[READ_END],read_msg,BUFFER_SIZE);//Read from the pipe
printf("\nRead Message: %s\n",read_msg);
close(fd[READ_END]); //Close the
write end
}
return 0;
}
No comments
Post a Comment