Untitled


SUBMITTED BY: antoineh1

DATE: May 8, 2016, 10:07 p.m.

FORMAT: Text only

SIZE: 5.2 kB

HITS: 3601

  1. //Pierwszy program
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <sys/wait.h>
  10. #include <time.h>
  11. int main(int argc,char **argv)
  12. {
  13. char bufor[50];
  14. int file_read,file_open,file_open_p,file_read_p;
  15. srand(time(0));
  16. if (argc != 3)
  17. {
  18. printf ("%s <file name>", argv[0]);
  19. return 1;
  20. }
  21. mkfifo("potok",0640);
  22. file_open=open(argv[2],O_WRONLY | O_CREAT,0640);
  23. if (file_open==-1)
  24. {
  25. perror("write_only_file error");
  26. exit(EXIT_FAILURE);
  27. }
  28. file_read=open(argv[1],O_RDONLY,0640);
  29. if (file_read==-1)
  30. {
  31. perror("read_only_file error");
  32. exit(EXIT_FAILURE);
  33. }
  34. switch (fork())
  35. {
  36. case -1:
  37. perror("fork error");
  38. exit(EXIT_FAILURE);
  39. case 0:
  40. file_open_p=open("potok",O_RDONLY,0640);
  41. if (file_open_p==-1)
  42. {
  43. perror("write_p error");
  44. exit(EXIT_FAILURE);
  45. }
  46. while(read(file_open_p,&bufor,25)>0)
  47. {
  48. write(STDOUT_FILENO, &bufor, 25);
  49. write(file_open,&bufor,25);
  50. }
  51. close(file_open);
  52. close(file_open_p);
  53. printf("\n");
  54. _exit(EXIT_SUCCESS);
  55. break;
  56. default:
  57. file_read_p=open("potok",O_WRONLY | O_CREAT,0640);
  58. if (file_read_p==-1)
  59. {
  60. perror("read_p error");
  61. exit(EXIT_FAILURE);
  62. }
  63. while(read(file_read,&bufor,50)>0)
  64. {
  65. sleep(rand()%3);
  66. write(file_read_p,&bufor,50);
  67. }
  68. close(file_read);
  69. close(file_read_p);
  70. wait(NULL);
  71. exit(EXIT_SUCCESS);
  72. break;
  73. };
  74. return 0;
  75. }
  76. //Drugi program w dwoch osobnyach plikach do uruchamiania na dwoch konsolach
  77. //producent
  78. #include <sys/types.h>
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <unistd.h>
  82. #include <string.h>
  83. #include <fcntl.h>
  84. #include <sys/stat.h>
  85. #include <sys/wait.h>
  86. #include <time.h>
  87. int main(int argc,char **argv)
  88. {
  89. char bufor[50];
  90. int file_read,file_read_p;
  91. srand(time(0));
  92. if (argc != 2)
  93. {
  94. printf ("%s <file name>", argv[0]);
  95. return 1;
  96. }
  97. file_read=open(argv[1],O_RDONLY,0640);
  98. if (file_read==-1)
  99. {
  100. perror("read_only_file error");
  101. exit(EXIT_FAILURE);
  102. }
  103. switch (fork())
  104. {
  105. case -1:
  106. perror("fork error");
  107. exit(EXIT_FAILURE);
  108. case 0:
  109. file_read_p=open("potok",O_WRONLY | O_CREAT,0640);
  110. if (file_read_p==-1)
  111. {
  112. perror("read_p error");
  113. exit(EXIT_FAILURE);
  114. }
  115. while(read(file_read,&bufor,50)>0)
  116. {
  117. sleep(rand()%3);
  118. write(file_read_p,&bufor,50);
  119. }
  120. close(file_read);
  121. close(file_read_p);
  122. wait(NULL);
  123. exit(EXIT_SUCCESS);
  124. break;
  125. default:;
  126. };
  127. return 0;
  128. }
  129. //konsument
  130. #include <sys/types.h>
  131. #include <stdio.h>
  132. #include <stdlib.h>
  133. #include <unistd.h>
  134. #include <string.h>
  135. #include <fcntl.h>
  136. #include <sys/stat.h>
  137. #include <sys/wait.h>
  138. #include <time.h>
  139. int main(int argc,char **argv)
  140. {
  141. char bufor[50];
  142. int file_open,file_open_p;
  143. srand(time(0));
  144. if (argc != 2)
  145. {
  146. printf ("%s <file name>", argv[0]);
  147. return 1;
  148. }
  149. file_open=open(argv[1],O_WRONLY | O_CREAT,0640);
  150. if (file_open==-1)
  151. {
  152. perror("write_only_file error");
  153. exit(EXIT_FAILURE);
  154. }
  155. switch (fork())
  156. {
  157. case -1:
  158. perror("fork error");
  159. exit(EXIT_FAILURE);
  160. case 0:
  161. file_open_p=open("potok",O_RDONLY,0640);
  162. if (file_open_p==-1)
  163. {
  164. perror("write_p error");
  165. exit(EXIT_FAILURE);
  166. }
  167. while(read(file_open_p,&bufor,25)>0)
  168. {
  169. write(STDOUT_FILENO, &bufor, 25);
  170. write(file_open,&bufor,25);
  171. }
  172. close(file_open);
  173. close(file_open_p);
  174. printf("\n");
  175. _exit(EXIT_SUCCESS);
  176. break;
  177. default:
  178. break;
  179. };
  180. return 0;
  181. }
  182. /makefile nie wiem do konca czy dobry
  183. Fifo: zadanie5.c
  184. gcc zadanie5.c -o Fifo.o && ./Fifo.o wejscie.txt wyjscie.txt
  185. clean: potok
  186. rm potok
  187. Potok:
  188. mkfifo -m 644 potok
  189. ProdKons: producent.c
  190. gcc producent.c -o Prod.o && ./Prod.o wejscie1.txt
  191. gcc konsument.c -o Kons.o && ./Kons.o wyjscie1.txt
  192. cleano:
  193. rm -f *.o

comments powered by Disqus