Advertisement
Guest User

fifo #shf

a guest
Oct 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. // #shf
  2.  
  3. //ZAD. 1
  4.  
  5. //Program piszący
  6.  
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <wait.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #define FIFO1 "/tmp/fifo.1"
  17. extern int errno;
  18.  
  19. int main()
  20. {
  21.  
  22. if(mknod(FIFO1,0666|S_IFIFO, 0)<0){
  23. perror("nie mozna stworzyc fifo");
  24. }
  25.  
  26.  
  27. char buff[]="ala ma kota a kot ma ale";
  28. int fifo;
  29. int flagi=O_WRONLY /*| O_NDELAY*/;
  30. if ((fifo = open(FIFO1, flagi)) < 0)
  31. perror("nie moze otworzyc fifo1 do pisania");
  32. if (strlen(buff) != write(fifo,buff,strlen(buff)))
  33. perror("blad zapisu do fifo");
  34. return 0;
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41. //Program czytający
  42.  
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <wait.h>
  49. #include <errno.h>
  50. #include <string.h>
  51. #include <unistd.h>
  52.  
  53. extern int errno;
  54. #define FIFO1 "/tmp/fifo.1"
  55. int main()
  56. {
  57. char buff[1024];
  58.  
  59. int fifo;
  60. int flagi=O_RDONLY /*| O_NDELAY*/ ;
  61. if ((fifo = open(FIFO1, flagi)) < 0)
  62. perror("nie moze otworzyc fifo1 do pisania");
  63. int n=read(fifo,buff,sizeof(buff));
  64. if (n<=0)
  65. perror("blad zapisu do fifo");
  66. else{
  67. buff[n]='\0';
  68. write(1,buff,n);
  69. printf("\n");
  70. }
  71.  
  72. unlink(FIFO1);
  73. return 0;
  74. }
  75.  
  76.  
  77. //ZAD.2
  78. //KLIENT1
  79. Klient 1
  80.  
  81. #include <sys/types.h>
  82. #include <sys/stat.h>
  83. #include <fcntl.h>
  84. #include <unistd.h>
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87. #include <string.h>
  88. #include <wait.h>
  89. #include <errno.h>
  90. #define FIFO1 "/tmp/fifo.1" // do pisania dla klienta 1 i czytania dla klienta 2
  91. #define FIFO2 "/tmp/fifo.2" // do pisania dla klienta 2 i czytania dla klienta 1
  92. #define QUIT "\1"
  93. int pid_potomka,pid_rodzica,fifo_write;
  94.  
  95. void oblsluga_zakonczenia_procesow(int nr_sig)
  96. {
  97. if(getpid()==pid_potomka){
  98. char buff[]="\1";
  99. if (strlen(buff) != write(fifo_write,buff,strlen(buff)))
  100. perror("blad zapisu do fifo");
  101.  
  102. exit(0);
  103. }
  104. else if(getpid()==pid_rodzica){
  105. wait(NULL);
  106.  
  107.  
  108. unlink(FIFO1);
  109. unlink(FIFO2);
  110. exit(0);
  111. }
  112. }
  113.  
  114. void oblsluga_zakonczenia_potomka(int nr_sig)
  115. {
  116. unlink(FIFO1);
  117. unlink(FIFO2);
  118. exit(0);
  119. }
  120.  
  121.  
  122. int main()
  123. {
  124. signal(SIGINT,oblsluga_zakonczenia_procesow);
  125. if(mknod(FIFO1,S_IFIFO|0666,0)==-1){
  126. perror("nie mozna stworzyc fifo1");
  127. }
  128. if(mknod(FIFO2,S_IFIFO|0666,0)==-1){
  129. perror("nie mozna stworzyc fifo1");
  130. }
  131.  
  132. int childpid;
  133. if ((childpid = fork()) == -1)
  134. {
  135. perror("nie moge forknac");
  136. exit(1);
  137. }
  138. else
  139. if (childpid ==0 ) // POTOMEK PISZE
  140. {
  141. signal(SIGUSR1,oblsluga_zakonczenia_potomka);
  142. pid_potomka=getpid();
  143.  
  144. int fifo;
  145. if((fifo=open(FIFO1,O_WRONLY))<0){
  146. perror("Nie mozna otworzyc kolejki do pisania");
  147. }
  148. fifo_write=fifo;
  149. char buff[1024];
  150. while(1==1){
  151.  
  152. fgets(buff,sizeof(buff),stdin);
  153. if (strlen(buff) != write(fifo,buff,strlen(buff)))
  154. perror("blad zapisu do fifo");
  155.  
  156. }
  157. }
  158. else // RODZIC CZYTA
  159. {
  160. signal(SIGUSR1,SIG_IGN);
  161. pid_rodzica=getpid();
  162. int fifo;
  163.  
  164. if((fifo=open(FIFO2,O_RDONLY))<0){
  165. perror("Nie mozna otworzyc kolejki do czytania");
  166. }
  167. char buff[1024];
  168.  
  169. while(1==1){
  170. int n=read(fifo,buff,sizeof(buff));
  171. if (n<=0){
  172. perror("blad odczytu z fifo");
  173. }
  174. else{
  175. buff[n]='\0';
  176. if (strcmp(QUIT, buff)==0){
  177. printf("Klient do rozmowy sie odlaczyl\n");
  178. break;
  179. }
  180. printf("Klient:");
  181. printf("%s",buff);
  182. }
  183.  
  184. }
  185. kill(pid_potomka,SIGUSR1);
  186. wait(NULL);
  187. exit(0);
  188. }
  189.  
  190. exit(0);
  191. return 0;
  192. }
  193.  
  194.  
  195. //KLIENT2
  196. #include <sys/types.h>
  197. #include <sys/stat.h>
  198. #include <fcntl.h>
  199. #include <unistd.h>
  200. #include <stdio.h>
  201. #include <stdlib.h>
  202. #include <string.h>
  203. #include <wait.h>
  204. #include <errno.h>
  205. #define FIFO1 "/tmp/fifo.1" // do pisania dla klienta 1 i czytania dla klienta 2
  206. #define FIFO2 "/tmp/fifo.2" // do pisania dla klienta 2 i czytania dla klienta 1
  207. #define QUIT "\1"
  208. int pid_potomka,pid_rodzica,fifo_write;
  209.  
  210. void oblsluga_zakonczenia_procesow(int nr_sig)
  211. {
  212. if(getpid()==pid_potomka){
  213. char buff[]="\1";
  214. if (strlen(buff) != write(fifo_write,buff,strlen(buff)))
  215. perror("blad zapisu do fifo");
  216. exit(0);
  217. }
  218. else if(getpid()==pid_rodzica){
  219.  
  220. wait(NULL);
  221. exit(0);
  222. }
  223. }
  224.  
  225. void oblsluga_zakonczenia_potomka(int nr_sig)
  226. {
  227. exit(0);
  228. }
  229.  
  230.  
  231. int main()
  232. {
  233. signal(SIGINT,oblsluga_zakonczenia_procesow);
  234.  
  235.  
  236. int childpid;
  237. if ((childpid = fork()) == -1)
  238. {
  239. perror("nie moge forknac");
  240. exit(1);
  241. }
  242. else
  243. if (childpid ==0 ) // POTOMEK PISZE
  244. {
  245. signal(SIGUSR1,oblsluga_zakonczenia_potomka);
  246. pid_potomka=getpid();
  247.  
  248. int fifo;
  249.  
  250. if((fifo=open(FIFO2,O_WRONLY))<0){
  251. perror("Nie mozna otworzyc kolejki do pisania");
  252. }
  253. fifo_write=fifo;
  254. char buff[1024];
  255. while(1==1){
  256.  
  257. fgets(buff,sizeof(buff),stdin);
  258. if (strlen(buff) != write(fifo,buff,strlen(buff)))
  259. perror("blad zapisu do fifo");
  260.  
  261. }
  262. }
  263. else // RODZIC CZYTA
  264. {
  265. signal(SIGUSR1,SIG_IGN);
  266. pid_rodzica=getpid();
  267. int fifo;
  268. if((fifo=open(FIFO1,O_RDONLY))<0){
  269. perror("Nie mozna otworzyc kolejki do czytania");
  270. }
  271. char buff[1024];
  272. while(1==1){
  273. int n=read(fifo,buff,sizeof(buff));
  274. if (n<=0){
  275. perror("blad odczytu z fifo");
  276. sleep(10);
  277. }
  278. else{
  279. buff[n]='\0';
  280. if (strcmp(QUIT, buff)==0){
  281. printf("Klient do rozmowy sie odlaczyl\n");
  282. break;
  283. }
  284. printf("Klient:");
  285. printf("%s",buff);
  286. }
  287.  
  288. }
  289. kill(pid_potomka,SIGUSR1);
  290. wait(NULL);
  291. exit(0);
  292. }
  293.  
  294. exit(0);
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement