Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. /*
  2.  
  3. * ex9a.c
  4. *
  5. * Created on: Jun 17, 2018
  6.  
  7.  
  8.  
  9. Authors: joshuaad N yisraelwa
  10.  
  11. DESCRIPTION:
  12.  
  13. The prog' has three processes:
  14. 1) process father that creates a shared memory
  15. 2) child proces a
  16. 3) child process b
  17. the two of the children has to get a random numbers in a loop
  18. the frist that get a prime number is the winner.
  19.  
  20. 1st the winner blocks the other brother from accessing to the shaed memory
  21. 2nd has to kill his brother
  22. 3rd to store the prime num in the shared memory
  23. 4th uses the SIGUSR1 signal. once the father get the SIGUSR1 he prints
  24. the prime number and quit.
  25.  
  26. races cases are impossible due to semaphoresem_t method being used.
  27.  
  28.  
  29. */
  30. //============================//
  31. // ---INCLUDE--- //
  32. //============================//
  33. #include <sys/types.h>
  34. #include <sys/ipc.h>
  35. #include <sys/shm.h>
  36. #include <sys/wait.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include <semaphore.h>
  43. #include <fcntl.h>
  44.  
  45. //----------------------------//
  46. // ---GLOBALS--- //
  47. //----------------------------//
  48. int shm_id ;
  49. int *shm_ptr;
  50. sem_t* mutex;
  51. //============================//
  52. // ---PROTOTYPES--- //
  53. //============================//
  54. void runProcess();
  55. void runSonA(int *shm_ptr);
  56. void runSonB(int *shm_ptr);
  57. void sigHandler();
  58. int isPrime(int num);
  59. //=========================================//
  60. // --MAIN FUNCTION-- //
  61. //=========================================//
  62. int main()
  63. {
  64.  
  65. signal(SIGUSR1,sigHandler);
  66. runProcess();
  67.  
  68. return 0;
  69. }
  70. //================================================//
  71. // ---PROCESS--- //
  72. //================================================//
  73. void runProcess()
  74. {
  75. key_t key ;
  76. if ((key = ftok("/tmp", 'x')) == -1)
  77. {
  78. perror("ftok() failed") ;
  79. exit(EXIT_FAILURE) ;
  80. }
  81. //-------------------------------------//
  82.  
  83. shm_id = shmget ( key, sizeof(int)*3, IPC_CREAT |IPC_EXCL | 0600 ) ;
  84. if (shm_id == -1)
  85. {
  86. printf("%d",key);
  87. perror("shmget() failed\n") ;
  88. exit(EXIT_FAILURE) ;
  89. }
  90.  
  91.  
  92. mutex=sem_open("/mymutex",O_CREAT,0644,1);
  93. if(mutex==SEM_FAILED)
  94. {
  95. perror("sem_open() failed\n") ;
  96. }
  97.  
  98.  
  99. shm_ptr = (int *) shmat ( shm_id, NULL, 0);
  100. if (shm_ptr == (int *)-1)
  101. {
  102. perror( "shmat failed" ) ;
  103. exit( EXIT_FAILURE ) ;
  104. }
  105. //-------------------------------------//
  106. srand(time(NULL));
  107.  
  108. pid_t sonB;
  109. pid_t sonA;
  110.  
  111.  
  112.  
  113. //-------------------------------------------------//
  114.  
  115. sonA=fork();
  116. if(sonA)//the father iteration
  117. {
  118. sonB=fork();
  119. }
  120.  
  121.  
  122. if(!sonA)
  123. {
  124. shm_ptr[1]=(int)getpid();
  125. runSonA(shm_ptr);
  126.  
  127. }
  128. if(!sonB)
  129. {
  130. shm_ptr[2]=(int)getpid();
  131. runSonB(shm_ptr);
  132. }
  133.  
  134. wait(NULL);
  135. wait(NULL);
  136.  
  137. }
  138. //================================================//
  139. // ---SON A--- //
  140. //================================================//
  141. void runSonA(int *shm_ptr)
  142. {
  143. sem_t* s;
  144. s =sem_open("/mymutex", 0);
  145. sem_post(s);
  146. int num=2;
  147.  
  148. pid_t pidbrother;
  149.  
  150.  
  151. while(1)
  152. {
  153.  
  154. num=rand();
  155.  
  156. if(isPrime(num))
  157. {
  158.  
  159.  
  160.  
  161. sem_wait(s);
  162. shm_ptr[0]=num;
  163.  
  164. printf("Hey there I won, I'm son a\n");//test
  165. sem_close(s);
  166. pidbrother=(pid_t)shm_ptr[2];
  167. kill(pidbrother,SIGUSR1);
  168. sem_unlink("/mutex" );
  169.  
  170.  
  171.  
  172. // kill(getpid(),0);
  173.  
  174. exit(EXIT_SUCCESS);
  175. }
  176.  
  177. }
  178.  
  179. }
  180. //================================================//
  181. // ---SON B--- //
  182. //================================================//
  183. void runSonB(int *shm_ptr)
  184. {
  185. sem_t* s;
  186. s =sem_open("/mymutex", 0);
  187. sem_post(s);
  188.  
  189. int num=2;
  190. pid_t pidbrother=(pid_t)shm_ptr[1];
  191.  
  192. while(1)
  193. {
  194. num=rand();
  195. if(isPrime(num))
  196. {
  197. sem_wait(s);
  198. shm_ptr[0]=num;
  199. printf("%s","Hey there I won, I'm son b\n");//test
  200.  
  201. sem_close(s);
  202. kill(pidbrother,SIGUSR1);
  203. sem_unlink("/mutex" );
  204.  
  205.  
  206.  
  207.  
  208. exit(EXIT_SUCCESS);
  209. }
  210.  
  211. }
  212.  
  213.  
  214.  
  215. }
  216. //=========================================//
  217. // --SIGNAL HANDLER-- //
  218. //=========================================//
  219. void sigHandler()
  220. {
  221. printf("%d is a prime\n",shm_ptr[0]);
  222. shmdt(shm_ptr) ;
  223.  
  224. if (shmctl(shm_id,IPC_RMID, NULL) == -1)
  225. {
  226. perror( "shmctl failed" ) ;
  227. exit( EXIT_FAILURE ) ;
  228. }
  229.  
  230. exit( EXIT_SUCCESS) ;
  231. }
  232. //=========================================//
  233. // ---IS PRIME--- //
  234. //=========================================//
  235. int isPrime(int num)
  236. {
  237.  
  238. int i=0;
  239. int flag=1;
  240. for(i=2; i<=num/2; ++i)
  241. {
  242. // condition for nonprime number
  243. if(num%i==0)
  244. {
  245. flag=0;
  246. return flag;
  247. }
  248. }
  249. return flag;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement