Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include "pipe_local.h"
  2.  
  3. int main() {
  4.  
  5. int privatefifo, dummyfifo, publicfifo, n, done;
  6. struct message msg;
  7. FILE *fin;
  8. static char buffer[PIPE_BUF];
  9.  
  10. /*creating the PUBLIC fifo*/
  11. mknod(PUBLIC, S_IFIFO | 0666, 0);
  12.  
  13. /*
  14. Server process opens the PUBLIC fifo in write mode to make sure that
  15. the PUBLIC fifo is associated with atleast one WRITER process. As a
  16. result it never receives EOF on the PUBLIC fifo. The server process
  17. will block any empty PUBLIC fifo waiting for additional messages to
  18. be written. This technique saves us from having to close and reopen
  19. the public FIFO every time a client process finishes its activities.
  20. */
  21.  
  22. if( (publicfifo = open(PUBLIC, O_RDONLY)) < 0 ||
  23. (dummyfifo = open(PUBLIC, O_WRONLY | O_NDELAY)) < 0) {
  24. perror(PUBLIC);
  25. exit(1);
  26. }
  27.  
  28. /*Read the message from PUBLIC fifo*/
  29. while(read(publicfifo, &msg, sizeof(msg)) > 0) {
  30.  
  31. n=0;
  32. done=0;
  33.  
  34. do {
  35. if((privatefifo = open(msg.fifo_name, O_WRONLY|O_NDELAY)) == -1) {
  36. sleep(5);
  37. }
  38. else {
  39. fin = popen(msg.cmd_line, "r");
  40. write(privatefifo,"\n",1);
  41.  
  42. while((n= read(fileno(fin), buffer, PIPE_BUF)) > 0) {
  43. write(privatefifo, buffer, n);
  44. memset(buffer, 0x0, PIPE_BUF);
  45. }
  46.  
  47. pclose(fin);
  48. close(privatefifo);
  49. done = 1;
  50. }
  51. }while(n++ < 5 && !done);
  52.  
  53. if(!done) {
  54. perror("Not accessed the private fifo\n");
  55. exit(1);
  56. }
  57.  
  58. }
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement