Created
December 14, 2012 20:02
-
-
Save anilmuppalla/4288190 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /***** File: pipe.c *****/ | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <sys/wait.h> | |
| #include<stdlib.h> | |
| int main(int argc, char *argv[]) | |
| { int p[2]; | |
| int i, pid, status; | |
| char buffer[20]; | |
| pipe(p); /* setting up the pipe */ | |
| if ((pid = fork()) == 0) | |
| /* in child */ | |
| { close(p[1]); /* child closes p[1] */ | |
| while ((i = read(p[0], buffer, 8)) != 0) | |
| { buffer[i] = '\0'; /* string terminator */ | |
| printf("%d chars :%s: received by child\n", i, buffer); | |
| } | |
| close(p[0]); | |
| _exit(0); /* child terminates */ | |
| } | |
| /* in parent */ | |
| close(p[0]); | |
| /* parent writes p[1] */ | |
| write(p[1], "Hello there, from me?extra", sizeof("Hello there, from me?")); | |
| write(p[1], "How are you? - Ercal", sizeof("How are you? - Ercal")); | |
| close(p[1]); /* finished writing p[1] */ | |
| while (wait(&status)!=pid); /* waiting for pid */ | |
| if (status == 0) printf("child finished\n"); | |
| else printf("child failed\n"); | |
| return(0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment