Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12.  
  13. #define BUFFER_LENGHT 10
  14. #define NAME_PID_LEN 20
  15. #define MAX_SLEEP_TIME 10
  16.  
  17. #define PRINT(args...) \
  18. printf(args); \
  19. fflush (stdout); \
  20.  
  21.  
  22. int modeDetection (char* mode);
  23. int follower ();
  24. int streamer (char* fileFromName);
  25.  
  26. int main (int argc, char** argv)
  27. {
  28. if ((argc > 3) || (argc == 1))
  29. {
  30. printf ("wrong number of arguments\n");
  31. return 0;
  32. }
  33. else if (argc == 0)
  34. {
  35. printf ("something wrong with argv pointer\n");
  36. return 0;
  37. }
  38.  
  39. int mode = modeDetection (argv[1]);
  40. //printf ("mode = %d\n", mode);
  41. if (mode == 0) // streamer
  42. {
  43. if (argc == 2)
  44. {
  45. printf ("no file\n");
  46. return 0;
  47. }
  48.  
  49. streamer(argv[2]);
  50.  
  51. }
  52. else if (mode == 1) // follower
  53. {
  54. if (argc == 3)
  55. {
  56. printf ("I dont need any more arguments in this mode\n");
  57. return 0;
  58. }
  59.  
  60. follower ();
  61. }
  62. else
  63. {
  64. printf ("some mistake occured\n");
  65. return 0;
  66. }
  67.  
  68. return 0;
  69. }
  70.  
  71. //-----------------------------------------------------------------------------------------
  72.  
  73. int follower ()
  74. {
  75.  
  76.  
  77. char namePid [NAME_PID_LEN] = {};
  78. sprintf (namePid, "%d", getpid());
  79. mkfifo (namePid, 0644);
  80.  
  81. int fdFrom = open (namePid, O_RDONLY | O_NONBLOCK);
  82. if (fdFrom == -1)
  83. {
  84. printf ("some error occured with opening Fifo for RD_ONLY | O_NONBLOCK, pid = %s\n", namePid);
  85. return -1;
  86. }
  87.  
  88. int folQueFd = open ("followersqueue", O_RDWR);
  89. write (folQueFd, namePid, NAME_PID_LEN);
  90.  
  91. int sleepTime = 0;
  92. char buffer [BUFFER_LENGHT] = {};
  93. int readRetValue = 0;
  94.  
  95. //--------------------------Start of a critical section-----------------------
  96.  
  97. do
  98. {
  99. sleep (sleepTime);
  100. readRetValue = read(fdFrom, buffer, BUFFER_LENGHT);
  101. sleepTime++;
  102. } while ((readRetValue == 0) && (sleepTime <= MAX_SLEEP_TIME + 1));
  103.  
  104. //---------------------------Finish of a critical section-------------------------
  105.  
  106. if (readRetValue == 0)
  107. {
  108. printf ("file with name /'%s/' was not opened on the other side\n", namePid);
  109. return -1;
  110. }
  111.  
  112. fcntl(fdFrom, O_RDONLY);
  113.  
  114. while (readRetValue != 0)
  115. {
  116. write (STDOUT_FILENO, buffer, readRetValue);
  117. readRetValue = read (fdFrom, buffer, BUFFER_LENGHT);
  118. }
  119.  
  120. close (fdFrom);
  121. close (folQueFd);
  122.  
  123. unlink (namePid);
  124.  
  125. return 0;
  126. }
  127.  
  128. //------------------------------------------------------------------------------------------------------
  129.  
  130. int streamer (char* fileFromName)
  131. {
  132. int fdFrom = open (fileFromName, 0);
  133. if (fdFrom == -1)
  134. {
  135. printf ("file do not exists\n");
  136. return -1;
  137. }
  138.  
  139. int folQueFd = open ("followersqueue", O_RDWR);
  140. if (folQueFd == -1)
  141. {
  142. printf ("error with opening followersqueue\n");
  143. return -1;
  144. }
  145.  
  146. char followerName [NAME_PID_LEN] = {};
  147.  
  148. //--------------------Start of a critical section---------------------------
  149.  
  150. int folReadRet = read (folQueFd, followerName, NAME_PID_LEN);
  151. if (folReadRet == -1)
  152. {
  153. printf ("some error with getting followers pid from main pipe\n");
  154. printf ("read returned %d\n", folReadRet);
  155. return -1;
  156. }
  157.  
  158. //--------------------Finish of a critical section---------------------------
  159.  
  160.  
  161. int fdTo = open (followerName, O_WRONLY | O_NONBLOCK);
  162. if(fdTo < 0)
  163. {
  164. perror("fd on the other side has died\n");
  165. return -1;
  166. }
  167.  
  168.  
  169. fcntl (fdTo, O_WRONLY);
  170.  
  171. char buffer [BUFFER_LENGHT] = {};
  172. int readRetValue = 1;
  173. int writeRetValue = 0;
  174.  
  175. while (readRetValue != 0)
  176. {
  177. readRetValue = read (fdFrom, buffer, BUFFER_LENGHT);
  178. if (readRetValue != 0)
  179. {
  180. writeRetValue = write (fdTo, buffer, readRetValue);
  181. }
  182. }
  183.  
  184. close (fdTo);
  185. close (fdFrom);
  186.  
  187. close (folQueFd);
  188.  
  189. return 0;
  190. }
  191.  
  192. //------------------------------------------------------------------------------------------------------
  193.  
  194. int modeDetection (char* mode)
  195. {
  196. int val = 0;
  197. errno = 0;
  198. char* endptr = 0;
  199.  
  200. val = strtol (mode, &endptr, 10);
  201.  
  202. if (errno == ERANGE)
  203. {
  204. printf ("overflow of int\n");
  205. return -1;
  206. }
  207. else if (errno == EINVAL)
  208. {
  209. return -1;
  210. }
  211. else if (val < 0)
  212. {
  213. printf ("less then zero");
  214. return -1;
  215. }
  216. else if (*endptr != '\0')
  217. {
  218. printf ("I found not a digit\n");
  219. return -1;
  220. }
  221.  
  222. if ((val == 0) || (val == 1))
  223. return val;
  224. else
  225. return -1;
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement