Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <sys/ipc.h>
  9. #include <sys/msg.h>
  10. #include <sys/time.h>
  11. #include <time.h>
  12. #include <stdbool.h>
  13.  
  14. struct Cards {
  15. int card1;
  16. int card2;
  17. char selectedGame[20];
  18. };
  19.  
  20. struct Message {
  21. long mtype;
  22. struct Cards cards;
  23. };
  24.  
  25. int main() {
  26. int status;
  27. printf("Program started running\n");
  28. srand(time(NULL));
  29.  
  30. // Card decks
  31. int deck[32];
  32. int parentCards[10];
  33. int child1Cards[12];
  34. int child2Cards[10];
  35.  
  36. // Shuffle cards
  37. int i;
  38. for (i = 0; i < 32; ++i) {
  39. deck[i] = -1;
  40. }
  41. for (i = 0; i < 10; ++i) {
  42. bool insertedCard = false;
  43. while (!insertedCard) {
  44. int r = rand()%32;
  45. if (deck[r] == -1) {
  46. deck[r] = 0;
  47. parentCards[i] = r + 1;
  48. insertedCard = true;
  49. }
  50. }
  51. }
  52.  
  53. for (i = 0; i < 12; ++i) {
  54. bool insertedCard = false;
  55. while (!insertedCard) {
  56. int r = rand()%32;
  57. if (deck[r] == -1) {
  58. deck[r] = 0;
  59. child1Cards[i] = r + 1;
  60. insertedCard = true;
  61. }
  62. }
  63. }
  64.  
  65. for (i = 0; i < 10; ++i) {
  66. bool insertedCard = false;
  67. while (!insertedCard) {
  68. int r = rand()%32;
  69. if (deck[r] == -1) {
  70. deck[r] = 0;
  71. child2Cards[i] = r + 1;
  72. insertedCard = true;
  73. }
  74. }
  75. }
  76.  
  77. key_t kulcs = ftok("/tmp", 1);
  78. int uzenetsor = msgget( kulcs, 0600 | IPC_CREAT );
  79.  
  80. pid_t child1=fork(); //forks make a copy of variables
  81. if (child1 < 0){perror("The fork calling was not succesful\n"); exit(1);}
  82. if (child1 > 0) //the parent process, it can see the returning value of fork - the child1 variable!
  83. {
  84. printf("Parent program started running\n");
  85. printf("Parent cards: ");
  86. for (i = 0; i < 10; ++i) {
  87. printf("%i ", parentCards[i]);
  88. }
  89. printf("\n");
  90. printf("Child1 cards: ");
  91. for (i = 0; i < 12; ++i) {
  92. printf("%i ", child1Cards[i]);
  93. }
  94. printf("\n");
  95. printf("Child2 cards: ");
  96. for (i = 0; i < 10; ++i) {
  97. printf("%i ", child2Cards[i]);
  98. }
  99. printf("\n");
  100. waitpid(child1, &status, 0);
  101. //waits the end of child1 process PID number=child1, the returning value will be in status
  102. //0 means, it really waits for the end of child1 process - the same as wait(&status)
  103. printf("Parent program finished running.\n");
  104.  
  105. }
  106. else //child1 process
  107. {
  108. pid_t child2=fork(); //forks make a copy of variables
  109. if (child2 > 0) {
  110. printf("Child1 process started\n");
  111. // Selecting 2 cards
  112. int child1Card1 = rand()%12;
  113. int child1Card2 = rand()%12;
  114. while (child1Card1 == child1Card2) {
  115. child1Card2 = rand()%12;
  116. }
  117. char selectedGame[20];
  118. int r = rand()%6;
  119. switch (r) {
  120. case 0:
  121. strcpy(selectedGame, "Passz");
  122. break;
  123. case 1:
  124. strcpy(selectedGame, "Piros passz");
  125. break;
  126. case 2:
  127. strcpy(selectedGame, "40 száz");
  128. break;
  129. case 3:
  130. strcpy(selectedGame, "Ulti");
  131. break;
  132. case 4:
  133. strcpy(selectedGame, "Betli");
  134. break;
  135. case 5:
  136. strcpy(selectedGame, "Durtmars");
  137. break;
  138. }
  139. struct Cards cards = {};
  140. cards.card1 = child1Card1;
  141. cards.card2 = child1Card2;
  142. strcpy(cards.selectedGame, selectedGame);
  143. struct Message msg = { 70, cards};
  144. msgsnd(uzenetsor, &msg, sizeof(msg.cards), 0);
  145. printf("Child1 sent message: %s game, with cards %i and %i\n", msg.cards.selectedGame, msg.cards.card1, msg.cards.card2);
  146. }
  147. if (child2 < 0){perror("The fork calling was not succesful\n"); exit(1);}
  148. if (child2 == 0) //the parent process, it can see the returning value of fork - the child2 variable!
  149. {
  150. printf("Child2 process started\n");
  151. struct Message msg;
  152. msgrcv(uzenetsor, &msg, sizeof(msg), 70, 0);
  153. printf("Child2 received message: %s game, with cards %i and %i\n", msg.cards.selectedGame, msg.cards.card1, msg.cards.card2);
  154. status = msgctl( uzenetsor, IPC_RMID, NULL );
  155. printf("Child2 process finished\n");
  156. exit(0);
  157.  
  158. }
  159. waitpid(child2, &status, 0);
  160. printf("Child1 process finished\n");
  161. exit(0);
  162. }
  163. return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement