-
Notifications
You must be signed in to change notification settings - Fork 3
/
process.c
71 lines (62 loc) · 1.7 KB
/
process.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
#include "headers.h"
/* Modify this file as needed*/
int remainingtime;
Process *shmCurrProcess;
int msg_Id = -1;
bool kill_me();
bool initializeMsgQueue();
void nextSecondWaiting(int *lastSecond)
{
while (*lastSecond == getClk()) ;
*lastSecond = getClk();
}
int main(int agrc, char *argv[])
{
initClk();
// TODO it needs to get the remaining time from somewhere
// remainingtime = ??;
int shm_Id = shmget(CONNKEY + 1, 40, 0666 | IPC_CREAT);
shmCurrProcess = (Process *)shmat(shm_Id, (void *)0, 0);
remainingtime = shmCurrProcess->remRunTime;
int last = -1;
shmCurrProcess->startingTime = getClk();
while (remainingtime > 0)
{
// printf("process with pid %d ran for 1 quanta , r = %d \n", getpid(), remainingtime - 1);
sleep(1);
remainingtime--;
// last =getClk();
// while (last==getClk());
// remainingtime -= 1;
// last=getClk();
shmCurrProcess->remRunTime = remainingtime;
}
initializeMsgQueue();
kill_me();
// printf("loool process send signal to finsish with id = %d , realId = %d \n", shmCurrProcess->id, shmCurrProcess->realID);
kill(getppid(), SIGUSR2);
return 0;
}
bool initializeMsgQueue()
{
if ((msg_Id = msgget(CLRPKEY, 0666 | IPC_CREAT)) == -1)
{
printf("failled to initialize msg queue");
return false;
}
return true;
}
bool kill_me()
{
struct message *msg = malloc(sizeof(struct message));
msg->pid = getpid();
if (msg_Id == -1)
{
return false;
}
// printf("i will send the pid = %d\n", msg->pid);
msgsnd(msg_Id, (void *)msg, sizeof(struct message), IPC_NOWAIT);
return true;
}
void SignalHandler(){
}