Guest User

Untitled

a guest
Oct 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5.  
  6. int main(void) {
  7. int n;
  8. int fd[2];
  9. pid_t pid;
  10. char line[1024];
  11.  
  12. if(pipe(fd) < 0) {
  13. fprintf(stderr, "pipe error: %s\n", strerror(errno));
  14. exit(-1);
  15. }
  16.  
  17. if((pid = fork()) < 0) {
  18. fprintf(stderr, "fork error: %s\n", strerror(errno));
  19. exit(-1);
  20. }
  21. else if(pid > 0) { /* Parent */
  22. close(fd[1]);
  23. n = read(fd[0], line, 1024);
  24. write(STDOUT_FILENO, line, n);
  25. }
  26. else { /* Child */
  27. close(fd[0]);
  28. write(fd[1], "Hello World\n", 12);
  29. }
  30.  
  31. return 0;
  32. }
Add Comment
Please, Sign In to add comment