Repository navigation
Expand file tree
/
Copy pathprocess.c
More file actions
71 lines (65 loc) · 1.56 KB
/
Copy pathprocess.c
File metadata and controls
71 lines (65 loc) · 1.56 KB
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"
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "ipc.h"
#include <sys/msg.h>
/* Modify this file as needed*/
int stopped = 0;
FILE *errorFile;
void open_logs();
void close_logs();
void sigStopHandler(int signum)
{
stopped = 1;
}
void logError(const char *errorMsg)
{
open_logs();
fprintf(errorFile, "At time %d: %s\n", getClk(), errorMsg);
close_logs();
}
void open_logs()
{
errorFile = fopen("error.log", "a");
}
void close_logs()
{
fclose(errorFile);
}
void sigContHandler(int signum)
{
stopped = 0;
}
int main(int agrc, char *argv[])
{
initClk();
// key_t key = ftok(IPC_FILE, KEY);
// int mqid = msgget(key, 0666);
// TODO it needs to get the remaining time from somewhere
// remainingtime = ??;
if (agrc < 2)
{
fprintf(stderr, "Usage: process.out <remaining>\n");
return 1;
}
int remainingtime = atoi(argv[1]); // sent by scheduler
// connect signal with its func
signal(SIGUSR1, sigContHandler);
signal(SIGUSR2, sigStopHandler);
while (remainingtime > 0)
{
sleep(1); // simulate 1 second CPU time
if (stopped)
continue; // if stopped, skip decreasing remaining time
remainingtime--; // decrease remaining time
}
logError("Process completed execution");
// TerminationMsg msg;
// msg.mtype = MSG_PROCESS_FINISHED;
// msg.pid = getpid();
// // msg.processID = atoi(argv[2]);
// msgsnd(mqid, &msg, sizeof(TerminationMsg) - sizeof(long), 0);
destroyClk(false);
return 0;
}