Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <limits.h>
  5. #include <errno.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. #define FIFO_NAME "/tmp/moje_fifo5"
  12.  
  13. #define BUFFER_SIZE PIPE_BUF
  14. int main(int argc , char *argv[])
  15. {
  16. int res;
  17. int pipe_fd;
  18. int open_mode=O_RDONLY;
  19. char bufor[BUFFER_SIZE+1];
  20. int zczytane=0;
  21. char buf[20];
  22.  
  23. memset(bufor,'\0',sizeof(bufor));
  24. if(access(FIFO_NAME, F_OK)==-1)
  25. {
  26.  
  27. res=mkfifo(FIFO_NAME,0666);
  28.  
  29. if(res ==0) printf("Kolejka FIFO utworzona\n");
  30. else
  31. {
  32. printf("B??d %d - %s tworzenia kolejki %s\n",errno,strerror(errno),FIFO_NAME);
  33. exit(1);
  34. }
  35. }
  36. printf("Proces %d otwiera FIFO \n",getpid());
  37.  
  38.  
  39. pipe_fd=open(FIFO_NAME,open_mode);
  40. printf("proces %d warto?c %d\n", getpid(),pipe_fd);
  41.  
  42. sprintf(buf,"ls -l /proc/%d/fd",getpid());
  43. system(buf);
  44.  
  45. if(pipe_fd!=-1)
  46. {
  47. do{
  48. res=read(pipe_fd,bufor,BUFFER_SIZE);
  49. zczytane+=res;
  50. int z;
  51. char a = bufor[0];
  52. int ia = a - '0';
  53. char b = bufor[2];
  54. int ib = b - '0';
  55. int c = 0;
  56. if(bufor[1] == '+'){
  57. c = ia + ib;
  58. printf("%d",c);
  59. }
  60. if(bufor[1] == '/'){
  61. c = ia / ib;
  62. printf("%d",c);
  63. }
  64. if(bufor[1] == '-'){
  65. c = ia - ib;
  66. printf("%d",c);
  67. }
  68. if(bufor[1] == '*'){
  69. c = ia * ib;
  70. printf("%d",c);
  71. }
  72. printf(" to wynik dzialania : %s\n",bufor);
  73. }while(res>0);
  74.  
  75. close(pipe_fd);
  76. }
  77. else
  78. exit(1);
  79.  
  80. printf("proces %d zko?czony zczytano %d\n", getpid(),zczytane);
  81.  
  82.  
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement