Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6.  
  7. #define FIFO1 "my_fifo1"
  8. #define FIFO2 "my_fif02"
  9.  
  10. int silnia(int n)
  11. {
  12. int out=1;
  13. int i;
  14. for(i=n;i>1;i--)
  15. {
  16. out*=i;
  17. }
  18.  
  19. return out;
  20. }
  21.  
  22. void client1(const char *IN){
  23. FILE *fp;
  24. FILE *fp_out;
  25. char string[100];
  26.  
  27. printf("IN1: server - > client1 : %s\n", IN);
  28. printf("OUT2: client1 -> client2 : %s\n", FIFO2);
  29.  
  30. /* W petli pobieramy dane wpisane przez uzytkownika */
  31. while(1){
  32. fp = fopen(FIFO1, "r");
  33. fp_out = fopen(FIFO2, "w");
  34. fgets(string, 100, fp);
  35. int data = atoi(string);
  36. data = silnia(data);
  37. sprintf(string,"%d",data);
  38.  
  39. fputs(string, fp_out);
  40. fclose(fp);
  41. fclose(fp_out);
  42. }
  43. }
  44.  
  45. void client2(const char *IN){
  46. FILE *fp;
  47. char string[100];
  48.  
  49. printf("IN2: client1 -> client2 %s\n", IN);
  50.  
  51. /* W petli pobieramy dane wpisane przez uzytkownika */
  52. while(1){
  53. fp = fopen(FIFO2, "r");
  54. fgets(string, 100, fp);
  55. int data = atoi(string);
  56. printf("Silnia wynosi: %d\n\n",data);
  57. fclose(fp);
  58. }
  59. }
  60.  
  61. void server(const char *OUT){
  62. FILE *fp;
  63. char string[100];
  64.  
  65. printf("OUT1: server -> client1 : %s\n", OUT);
  66.  
  67.  
  68. /* W petli pobieramy dane od uzytkownika */
  69. while(1){
  70. fp = fopen(FIFO1, "w");
  71. fprintf(stdout, "Podaj liczbe: ");
  72. scanf("%s", string);
  73. /* Zapisujemy je do otwartego poprzednio deskryptora */
  74.  
  75. fputs(string, fp);
  76. fclose(fp);
  77. sleep(1);
  78. }
  79. }
  80.  
  81. int main(void){
  82.  
  83. /* UWAGA! Na prawa dostepu tworzonego pliku ma wplyw UMASK */
  84. umask(0);
  85. mkfifo(FIFO1, 0666); //0 z przodu oznacza notacje ósemkowa
  86. mkfifo(FIFO2, 0666); //0 z przodu oznacza notacje ósemkowa
  87.  
  88. /* rozdwajamy proces */
  89.  
  90.  
  91.  
  92. if(fork())
  93. {
  94. client1(FIFO1);
  95. }
  96. if(fork())
  97. {
  98. client2(FIFO2);
  99. }
  100. else
  101. {
  102. server(FIFO1);
  103. }
  104.  
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement