forked from DinaAdib/OS-Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclk.cpp
47 lines (41 loc) · 990 Bytes
/
clk.cpp
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
/* This file is done for you,
Probably you will not need to change anything
This file represents an emulated clock for the simulation purpose only
it is not a real part of operating system */
#include "headers.h"
int shmid;
/*clear the resources before exit*/
void cleanup(int x)
{
shmctl( shmid,IPC_RMID,NULL);
printf("Clock terminating \n");
raise(9);
}
/* this file represents the system clock for ease of calculations*/
int main() {
printf("Clock Starting with id %d\n",getpid());
signal(SIGINT,cleanup);
int clk=0;
//Create shared memory for one integer variable 4 bytes
shmid = shmget(SHKEY, 4, IPC_CREAT|0644);
if((long)shmid == -1)
{
perror("Error in create shm");
exit(-1);
}
int * shmaddr = (int*) shmat(shmid, (void *)0, 0);
if((long)shmaddr == -1)
{
perror("Error in attach in parent");
exit(-1);
}
else
{
*shmaddr = clk; /* initialize shared memory */
}
while(1)
{
sleep(1);
(*shmaddr)++;
}
}