fork download
  1. #include <unistd.h>
  2.  
  3. int main() {
  4. // create two pipes for communication
  5. int back[2];
  6. int forth[2];
  7. pipe(back);
  8. pipe(forth);
  9.  
  10. // make a copy of this process
  11. if(fork() == 0) {
  12. // in that copy...
  13. // set this process's stdin to be the read end of back
  14. dup2(back[0], 0);
  15. // and stdout to be the write end of forth
  16. dup2(forth[1], 1);
  17. // cleanup the now unnecessary pipe ends
  18. close(back[0]); close(back[1]);
  19. close(forth[0]); close(forth[1]);
  20. // replace this process with the cat program
  21. // it simply copies input from stdin to stdout
  22. execlp("cat", "cat", 0); // never returns
  23. }
  24. // ... and in the original process
  25. // write meow into the back pipe
  26. write(back[1], "meow", 5);
  27. //
  28. // set this process's stdin to be the read end of forth
  29. dup2(forth[0], 0);
  30. // and stdout to be the write end of back
  31. dup2(back[1], 1);
  32. // cleanup the now unnecessary pipe ends
  33. close(back[0]); close(back[1]);
  34. close(forth[0]); close(forth[1]);
  35. // replace this one with cat too
  36. execlp("cat", "cat", 0);
  37.  
  38. // now each of the cat programs reads from the other's stdout
  39. // and writes intp the other's stdin
  40. // and we dropped a "meow" in the pipes
  41. // do the math...
  42. }
  43.  
Time limit exceeded #stdin #stdout 5s 3780KB
stdin
Standard input is empty
stdout
Standard output is empty