Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/time.h>
- #include <time.h>
- #include <stdbool.h>
- struct Cards {
- int card1;
- int card2;
- char selectedGame[20];
- };
- struct Message {
- long mtype;
- struct Cards cards;
- };
- int main() {
- int status;
- printf("Program started running\n");
- srand(time(NULL));
- // Card decks
- int deck[32];
- int parentCards[10];
- int child1Cards[12];
- int child2Cards[10];
- // Shuffle cards
- int i;
- for (i = 0; i < 32; ++i) {
- deck[i] = -1;
- }
- for (i = 0; i < 10; ++i) {
- bool insertedCard = false;
- while (!insertedCard) {
- int r = rand()%32;
- if (deck[r] == -1) {
- deck[r] = 0;
- parentCards[i] = r + 1;
- insertedCard = true;
- }
- }
- }
- for (i = 0; i < 12; ++i) {
- bool insertedCard = false;
- while (!insertedCard) {
- int r = rand()%32;
- if (deck[r] == -1) {
- deck[r] = 0;
- child1Cards[i] = r + 1;
- insertedCard = true;
- }
- }
- }
- for (i = 0; i < 10; ++i) {
- bool insertedCard = false;
- while (!insertedCard) {
- int r = rand()%32;
- if (deck[r] == -1) {
- deck[r] = 0;
- child2Cards[i] = r + 1;
- insertedCard = true;
- }
- }
- }
- key_t kulcs = ftok("/tmp", 1);
- int uzenetsor = msgget( kulcs, 0600 | IPC_CREAT );
- pid_t child1=fork(); //forks make a copy of variables
- if (child1 < 0){perror("The fork calling was not succesful\n"); exit(1);}
- if (child1 > 0) //the parent process, it can see the returning value of fork - the child1 variable!
- {
- printf("Parent program started running\n");
- printf("Parent cards: ");
- for (i = 0; i < 10; ++i) {
- printf("%i ", parentCards[i]);
- }
- printf("\n");
- printf("Child1 cards: ");
- for (i = 0; i < 12; ++i) {
- printf("%i ", child1Cards[i]);
- }
- printf("\n");
- printf("Child2 cards: ");
- for (i = 0; i < 10; ++i) {
- printf("%i ", child2Cards[i]);
- }
- printf("\n");
- waitpid(child1, &status, 0);
- //waits the end of child1 process PID number=child1, the returning value will be in status
- //0 means, it really waits for the end of child1 process - the same as wait(&status)
- printf("Parent program finished running.\n");
- }
- else //child1 process
- {
- pid_t child2=fork(); //forks make a copy of variables
- if (child2 > 0) {
- printf("Child1 process started\n");
- // Selecting 2 cards
- int child1Card1 = rand()%12;
- int child1Card2 = rand()%12;
- while (child1Card1 == child1Card2) {
- child1Card2 = rand()%12;
- }
- char selectedGame[20];
- int r = rand()%6;
- switch (r) {
- case 0:
- strcpy(selectedGame, "Passz");
- break;
- case 1:
- strcpy(selectedGame, "Piros passz");
- break;
- case 2:
- strcpy(selectedGame, "40 száz");
- break;
- case 3:
- strcpy(selectedGame, "Ulti");
- break;
- case 4:
- strcpy(selectedGame, "Betli");
- break;
- case 5:
- strcpy(selectedGame, "Durtmars");
- break;
- }
- struct Cards cards = {};
- cards.card1 = child1Card1;
- cards.card2 = child1Card2;
- strcpy(cards.selectedGame, selectedGame);
- struct Message msg = { 70, cards};
- msgsnd(uzenetsor, &msg, sizeof(msg.cards), 0);
- printf("Child1 sent message: %s game, with cards %i and %i\n", msg.cards.selectedGame, msg.cards.card1, msg.cards.card2);
- }
- if (child2 < 0){perror("The fork calling was not succesful\n"); exit(1);}
- if (child2 == 0) //the parent process, it can see the returning value of fork - the child2 variable!
- {
- printf("Child2 process started\n");
- struct Message msg;
- msgrcv(uzenetsor, &msg, sizeof(msg), 70, 0);
- printf("Child2 received message: %s game, with cards %i and %i\n", msg.cards.selectedGame, msg.cards.card1, msg.cards.card2);
- status = msgctl( uzenetsor, IPC_RMID, NULL );
- printf("Child2 process finished\n");
- exit(0);
- }
- waitpid(child2, &status, 0);
- printf("Child1 process finished\n");
- exit(0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement