-
Notifications
You must be signed in to change notification settings - Fork 1
/
so_sources.c
74 lines (68 loc) · 2.48 KB
/
so_sources.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <math.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include "libs/utilities.h"
#include "libs/initialConfig.h"
#include "libs/ipc_utilities.h"
/*
*Handler di SIGALRM or other signals;
**/
void handle_signal(int signal);
int main(int argc, char const* argv[])
{
struct message msgToSend;
struct condivisa* sharedStruct = (struct condivisa*)malloc(sizeof(struct condivisa));
struct sigaction sa;
signal(SIGALRM, handle_signal);
signal(SIGILL, handle_signal);
int msgId = 0, shmId = 0;
msgToSend.xSource = atoi(argv[1]);
msgToSend.ySource = atoi(argv[2]);
msgToSend.xDestination = 0;
msgToSend.yDestination = 0;
msgId = getMsgId(MESSAGE_QUEUE_KEY);
/*Create a shared memory area */
shmId = getSharedMemoryId(sharedStruct, SHM_KEY);
/*Attach the shared memory to a pointer */
sharedStruct = shmat(shmId, NULL, 0);
msgToSend.mtype = TYPE_DESTINATION;
int msg_length = sizeof(msgToSend.xSource) * 4;
int randrow = 0;
int randcol = 0, randNum;
alarm(getRandomNumberInRange(1, 5));
while (1) {
do {
generateRowAndCol(&randrow, &randcol, SO_HEIGHT, SO_WIDTH);
} while (sharedStruct->cityMap[randrow][randcol].tipoCella == HOLE || (msgToSend.xSource == randrow && msgToSend.ySource == randcol));
msgToSend.xDestination = randrow;
msgToSend.yDestination = randcol;
/*now sending the message */
msgsnd(msgId, &msgToSend, msg_length, 0);
printf(ANSI_COLOR_MAGENTA "SO_SOURCE" ARROWRIGHT ANSI_COLOR_YELLOW " (PID=%d): Q_id=%d: type=%ld |xSource= %d - ySource= %d| " ARROWRIGHT " |xDestination= %d - yDestination= %d| SENT\n",
getpid(), msgId, msgToSend.mtype, msgToSend.xSource, msgToSend.ySource, msgToSend.xDestination, msgToSend.yDestination);
pause();
}
return 0;
}
void handle_signal(int signal)
{
if (signal == SIGALRM) {
printf(ANSI_COLOR_MAGENTA "SO_SOURCE" ARROWRIGHT ANSI_COLOR_BLUE " SIGALARM received waking up and sending message...\n");
alarm(getRandomNumberInRange(1, 5));
}
else if (signal == SIGILL) {
printf(ANSI_COLOR_MAGENTA "SO_SOURCE" ARROWRIGHT ANSI_COLOR_BLUE " SIGILL received waking up and sending message...\n");
alarm(getRandomNumberInRange(1, 5));
}
}