Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions OS-Scheduler/OS-Scheduler/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,53 @@

/* Modify this file as needed*/
int remainingtime;
union Semun
{
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};

void down(int sem)
{
struct sembuf op;

op.sem_num = 0;
op.sem_op = -1;
op.sem_flg = !IPC_NOWAIT;

if (semop(sem, &op, 1) == -1)
{
perror("Error in down()");
exit(-1);
}
}

void up(int sem)
{
struct sembuf op;

op.sem_num = 0;
op.sem_op = 1;
op.sem_flg = !IPC_NOWAIT;

if (semop(sem, &op, 1) == -1)
{
perror("Error in up()");
exit(-1);
}
}
int main(int agrc, char *argv[])
{
int sem1 = semget('5', 1, 0666 | IPC_CREAT);
int sem2 = semget('6', 1, 0666 | IPC_CREAT);

if (sem1 == -1 || sem2 == -1)
{
perror("Error in create sem");
exit(-1);
}
initClk();
key_t key = ftok("key", 'r'); // shared memory to get the remain time of the running process
int runPshmid = shmget(key, 4, 0666 | IPC_CREAT);
Expand All @@ -30,14 +74,16 @@ int main(int agrc, char *argv[])
while (currentTime == getClk()) // wait till the next clk;
if (remainingTime == 0)
break;

down(sem2);
(*runPshmadd)--;
remainingTime = (*runPshmadd);
up(sem1);
printf("process%d:%d\n",getpid(), currentTime);
// printf("%d",getpid());
kill(getppid(),SIGCONT);
raise(SIGSTOP);
usleep(100); //wait 10microseconds
kill(getppid(),SIGCONT);
// kill(getppid(),SIGCONT);
// raise(SIGSTOP);
// usleep(100); //wait 10microseconds
// kill(getppid(),SIGCONT);
currentTime = getClk(); // update the time
}

Expand Down
Loading