-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
158 lines (133 loc) · 3.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "headers.h"
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include"string.h"
#include <sys/wait.h>
#include <signal.h>
union Semun
{
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
ushort *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
void *__pad;
};
void up(int sem)
{
struct sembuf v_op;
v_op.sem_num = 0;
v_op.sem_op = 1;
v_op.sem_flg = !IPC_NOWAIT;
if (semop(sem, &v_op, 1) == -1)
{
perror("Error in up()");
exit(-1);
}
union Semun semun;
int VAL=semctl(sem, 0, GETVAL, semun);
//printf("semval after UUUP %d \n",VAL);
}
void down(int sem)
{
/*union Semun semun;
int VAL=semctl(sem, 0, GETVAL, semun);
printf("semval from down %d \n",VAL);*/
struct sembuf p_op;
p_op.sem_num = 0;
p_op.sem_op = -1;
p_op.sem_flg = !IPC_NOWAIT;
if (semop(sem, &p_op, 1) == -1)
{
perror("Error in down()");
exit(-1);
}
}
/* Modify this file as needed*/
int remainingtime;
int main(int agrc, char *argv[])
{
printf("you are in process %d \n",getpid());
initClk();
int shmid, pid,sempho1;
key_t memo=7893;
sempho1 = ftok("sem1", 'a');
// key_t sempho1=7894;
shmid = shmget(memo, 4096, IPC_CREAT | 0644);
if (shmid == -1)
{
perror("Error in create");
exit(-1);
}
void *shmaddr = shmat(shmid, (void *)0, 0);
union Semun semun;
int sem1 = semget(sempho1, 1, 0666 | IPC_CREAT);
if (sem1 == -1)
{
perror("Error in create sem");
exit(-1);
}
/*
semun.val = 0;
if (semctl(sem1, 0, SETVAL, semun) == -1)
{
perror("Error in semctl");
exit(-1);
}
*/
int VAL=semctl(sem1, 0, GETVAL, semun);
printf("semval %d \n",VAL);
down(sem1);
char runTime[10];
strcpy(runTime,(char *) shmaddr);
//TODO The process needs to get the remaining time from somewhere
remainingtime = atoi(runTime);
printf("runTime From process %d \n",remainingtime);
int prvtime=getClk();
while (remainingtime > 0)
{
if(getClk()!=prvtime)
{
printf("proc: getclk: %d",getClk());
printf("proc: prvTime: %d",prvtime);
if(getClk()!= prvtime +1){
prvtime=getClk();
}
else{
printf("current time from process: %d \n",getClk());
remainingtime--;
prvtime=getClk();
printf("process: remTime %d \n",remainingtime);
//Send the latest remaining time to the scheduler
char text[10];
sprintf(text, "%d", remainingtime);
strcpy((char *) shmaddr,text);
int VAL= semctl(sem1, 0, GETVAL, semun);
printf("semval after UUUP in function %d \n",VAL);
//if wasn't a zero, then it's already upped , no need to up it twice
if(VAL == 0){
up(sem1);
}
}
}
}
key_t FinsgedrocessSem = 7788;
int semFinish = semget(FinsgedrocessSem, 1, 0666 | IPC_CREAT);
if (semFinish == -1)
{
perror("Error in create sem FinsgedrocessSem");
exit(-1);
}
semun.val = 0;
if (semctl(semFinish, 0, SETVAL, semun) == -1)
{
perror("Error in semctl");
exit(-1);
}
destroyClk(false);
printf("TRMINAT process with run time %d \n",atoi(runTime));
//kill(getppid(), SIGUSR1);
up(semFinish);
shmdt(shmaddr);
return 0;
}