diff --git a/OS-Scheduler/OS-Scheduler/process.c b/OS-Scheduler/OS-Scheduler/process.c index 4f928b1..79b662d 100644 --- a/OS-Scheduler/OS-Scheduler/process.c +++ b/OS-Scheduler/OS-Scheduler/process.c @@ -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); @@ -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 } diff --git a/OS-Scheduler/OS-Scheduler/scheduler.c b/OS-Scheduler/OS-Scheduler/scheduler.c index 90d5b11..93e5f46 100644 --- a/OS-Scheduler/OS-Scheduler/scheduler.c +++ b/OS-Scheduler/OS-Scheduler/scheduler.c @@ -1,6 +1,5 @@ #include "headers.h" -#include - +#include void RR(int quantum); void HPF(); void SRTN(); @@ -12,6 +11,7 @@ double TotalWTA, TotalWait, TotalRun, TotalTime; double WtaT[500]; struct Process *min = NULL; + int numofProcesses; // total number of processes comming from generator int recievedProcessesNum = 0; // number of arrived processes int terminatedProcessesNum = 0; // total number of terminated processes @@ -25,16 +25,92 @@ int *sigshmaddr; int runPshmid; // shared memory for the remain time of the running process int *runPshmadd; double idealtime = 0; +int semm1; +int semm2; +FILE*pf; +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); + } +} +void clearsemaphores() +{ + if(semctl(semm1, 0, IPC_RMID) == -1) { + perror("Error removing semaphore"); + exit(EXIT_FAILURE); + } + if(semctl(semm2, 0, IPC_RMID) == -1) { + perror("Error removing semaphore"); + exit(EXIT_FAILURE); + } else { + printf("Semaphore removed successfully\n"); + } +} //-------------------------------main function---------------------------// int main(int argc, char *argv[]) { + union Semun semun; + + semm1 = semget('5', 1, 0666 | IPC_CREAT); + semm2 = semget('6', 1, 0666 | IPC_CREAT); + // printf("%d\n",semm2); + if (semm1 == -1 || semm2 == -1) + { + perror("Error in create sem"); + exit(-1); + } + + semun.val = 0; /* initial value of the semaphore, Binary semaphore */ + if (semctl(semm1, 0, SETVAL, semun) == -1) + { + perror("Error in semctl"); + exit(-1); + } + if (semctl(semm2, 0, SETVAL, semun) == -1) + { + perror("Error in semctl"); + exit(-1); + } TotalTime = TotalWTA = TotalWait = TotalRun = 0; FILE *schedulerlog = fopen("scheduler.log", "w"); FILE *schedulerperf = fopen("scheduler.perf", "w"); fclose(schedulerperf); fclose(schedulerlog); - + pf=fopen("scheduler.log","w"); + if(pf==NULL) + {printf("error in opening file");} initClk(); printf("hi from scheduler\n"); @@ -70,21 +146,20 @@ int main(int argc, char *argv[]) SRTN(); break; } - + // upon termination release the clock resources. // destroyClk(true); perf(); - execl("./image_generator.out","image_generator.out",NULL); + } //-----------------------------processes operation functions--------------------// int forkProcess(struct Process *p) { p->pid = 0; - p->lastStopTime = getClk(); + p->lastStopTime=getClk(); p->waitingTime = 0; p->remainingTime = p->runtime; - p->starttime=-1; p->pid = fork(); if (p->pid == 0) @@ -113,33 +188,29 @@ void contProcess(struct Process *p) (*runPshmadd) = p->remainingTime; kill(p->pid, SIGCONT); } + //------------Output Function-------------// -void Outp(int op, int time, struct Process *p) -{ +void Outp(int op, int time, struct Process *p) { FILE *schedulerlog = fopen("scheduler.log", "a"); - if (op == 0) - { + if (op == 0) { fprintf(schedulerlog, "At time %d process %d started arr %d total %d remain %d wait %d", time, p->id, p->arrival_time, p->runtime, p->remainingTime, time - p->arrival_time - p->runtime + p->remainingTime); } - if (op == 1) - { + if (op == 1) { fprintf(schedulerlog, "At time %d process %d resumed arr %d total %d remain %d wait %d", time, p->id, p->arrival_time, p->runtime, p->remainingTime, time - p->arrival_time - p->runtime + p->remainingTime); } - if (op == 2) - { + if (op == 2) { fprintf(schedulerlog, "At time %d process %d stopped arr %d total %d remain %d wait %d", time, p->id, p->arrival_time, p->runtime, p->remainingTime, time - p->arrival_time - p->runtime + p->remainingTime); } - if (op == 3) - { + if (op == 3) { double x = p->finishtime - p->arrival_time; fprintf(schedulerlog, "At time %d process %d finished arr %d total %d remain %d wait %d TA %d WTA %.2lf", time, p->id, @@ -157,8 +228,7 @@ void Outp(int op, int time, struct Process *p) //-------------scheduler.perf output---------// -void perf() -{ +void perf() { double std = 0; TotalWTA /= numofProcesses; TotalWait /= numofProcesses; @@ -170,46 +240,44 @@ void perf() fprintf(schedulerperf, "CPU Utilization = %.2lf%%\n", TotalRun); fprintf(schedulerperf, "Avg WTA = %.2lf\n", TotalWTA); fprintf(schedulerperf, "Avg Waiting = %.2lf\n", TotalWait); - for (int i = 1; i <= numofProcesses; ++i) - { + for (int i = 1; i <= numofProcesses; ++i) { std += (WtaT[i] - TotalWTA) * (WtaT[i] - TotalWTA); } std = squareRoot(std); fprintf(schedulerperf, "Std WTA = %.2lf\n", std); fclose(schedulerperf); + } //-------------Square root function to calculate std-----------' -double squareRoot(double n) -{ +double squareRoot(double n) { double x = n; double y = 1; double epsilon = 0.000001; - while (x - y > epsilon) - { + while (x - y > epsilon) { x = (x + y) / 2; y = n / x; } return x; } + //------------------------ RR algorithm -----------------------------------// void RR(int quantum) { printf("executing RR algorithm\n"); + // printf("%d\n",semm2); int remainQuantum = quantum; recievedProcesses = (struct Process *)malloc(numofProcesses * sizeof(struct Process)); struct Process *recv; int currentTime; - int prevtime = 0; - bool firsttime = 1; - while (getClk() == 0) - ; - + int prevtime=0; + while (getClk() == 0); + while (terminatedProcessesNum < numofProcesses) { currentTime = getClk(); @@ -220,28 +288,29 @@ void RR(int quantum) { recv = &recievedProcesses[recievedProcessesNum]; forkProcess(recv); - kill(recv->id, SIGCONT); + kill(recv->id,SIGCONT); raise(SIGSTOP); - usleep(100); // wait 10microseconds - kill(recv->id, SIGCONT); + usleep(100); //wait 10microseconds + kill(recv->id,SIGCONT); enqueueInCQ(&readyQueue, recv); - TotalRun += recv->runtime; recievedProcessesNum++; } - // start of the program - if (currentTime != prevtime) + // start of the program + if(currentTime!=prevtime) { - if (!RunningProcess) - idealtime++; - prevtime = currentTime; - currentTime = getClk(); - // get remainig time & reduce the remaining quantum + prevtime=currentTime; + currentTime=getClk(); + // get remainig time & reduce the remaining quantum if (RunningProcess != NULL && RunningProcess->remainingTime > 0 && remainQuantum > 0) { - kill(RunningProcess->pid, SIGCONT); - raise(SIGSTOP); - usleep(100); // wait 10microseconds - kill(RunningProcess->id, SIGCONT); + printf("scheduler:%d\n",currentTime); + // printf("%d\n",semm2); + up(semm2); + down(semm1); + // kill(RunningProcess->pid,SIGCONT); + // raise(SIGSTOP); + // usleep(100); //wait 10microseconds + // kill(RunningProcess->id,SIGCONT); RunningProcess->remainingTime = (*runPshmadd); remainQuantum -= 1; } @@ -254,13 +323,11 @@ void RR(int quantum) RunningProcess = dequeueOFCQ(&readyQueue); contProcess(RunningProcess); RunningProcess->state = running; - if (RunningProcess->starttime == -1) - { - Outp(0, currentTime, RunningProcess); - RunningProcess->starttime = currentTime; - } - else - Outp(1, currentTime, RunningProcess); + fprintf(pf,"At time %d process %d started arr %d total %d ramain %d wait %d\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->remainingTime,RunningProcess->waitingTime); + } + else + { + idealtime++; } } // running process checks @@ -269,11 +336,15 @@ void RR(int quantum) // noyify process termination if (RunningProcess->remainingTime == 0) { + // printf("process with id=%d terminated with %d\n", RunningProcess->id, RunningProcess->remainingTime); RunningProcess->state = terminated; terminatedProcessesNum++; - RunningProcess->finishtime = currentTime; - Outp(3, currentTime, RunningProcess); + int TA=currentTime-RunningProcess->arrival_time; + double WTA=(TA/(double)RunningProcess->runtime); + fprintf(pf,"At time %d process %d finished arr %d total %d ramain 0 wait %d TA %.d WTA %.2f\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->waitingTime,TA,WTA); RunningProcess = NULL; + // printf("the process terminated\n"); + // } // switch processes else @@ -281,7 +352,10 @@ void RR(int quantum) RunningProcess->state = ready; // change state to ready enqueueInCQ(&readyQueue, RunningProcess); stopProcess(RunningProcess); // send stop signal to this process - Outp(2, currentTime, RunningProcess); + fprintf(pf,"At time %d process %d stopped arr %d total %d remain %d wait %d\n", + currentTime, RunningProcess->id, RunningProcess->arrival_time, + RunningProcess->runtime, RunningProcess->remainingTime, + RunningProcess->waitingTime); RunningProcess = NULL; // printf("stopped\n"); } @@ -293,34 +367,44 @@ void RR(int quantum) RunningProcess->state = running; (*runPshmadd) = RunningProcess->remainingTime; contProcess(RunningProcess); - if (RunningProcess->remainingTime == RunningProcess->runtime) - Outp(0, currentTime, RunningProcess); - else - Outp(1, currentTime, RunningProcess); + fprintf(pf,"At time %d process %d started arr %d total %d ramain %d wait %d\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->remainingTime,RunningProcess->waitingTime); } + else + { + idealtime++; + } } } } - idealtime--; + // double CPUUtilization=(getClk()-idealtime)\(double)(getClk()); + // CPUUtilization=round(CPUUtilization*100)/100; + // int sum=0 + // for (int i = 0; i < numofProcesses; i++) + // { + // sum+=recievedProcesses[i]. + // } + + // double AWTAT= + // fclose(pf); + } //---------------------HPF--------------------------------------------- void HPF() { - printf("executing HPF algorithm\n"); - - struct priority_Queue readyQueue2; - initializePriorityQueue(&readyQueue2); + printf("executing HPF algorithm\n"); + + struct priority_Queue readyQueue2; + initializePriorityQueue(&readyQueue2); recievedProcesses = (struct Process *)malloc(numofProcesses * sizeof(struct Process)); struct Process *recv; int currentTime; - int prevtime = 1; - bool firsttime = 1; - while (getClk() == 0) - ; - + int prevtime=1; + bool firsttime=1; + while (getClk() == 0); + while (terminatedProcessesNum < numofProcesses) { currentTime = getClk(); @@ -331,35 +415,37 @@ void HPF() { recv = &recievedProcesses[recievedProcessesNum]; forkProcess(recv); - kill(recv->id, SIGCONT); + kill(recv->id,SIGCONT); raise(SIGSTOP); - usleep(100); // wait 10microseconds - kill(recv->id, SIGCONT); - priority_enqueue(&readyQueue2, recv, 0); + usleep(100); //wait 10microseconds + kill(recv->id,SIGCONT); + priority_enqueue(&readyQueue2,recv,0); recievedProcessesNum++; } - if (currentTime == 1 && !priority_isempty(&readyQueue2) && firsttime) + if(currentTime==1&&!priority_isempty(&readyQueue2)&&firsttime) { - firsttime = 0; + firsttime=0; RunningProcess = priority_dequeue(&readyQueue2); contProcess(RunningProcess); RunningProcess->state = running; // fprintf(pf,"At time %d process %d started arr %d total %d ramain %d wait %d\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->remainingTime,RunningProcess->waitingTime); - Outp(0, currentTime, RunningProcess); + Outp(0,currentTime,RunningProcess); + } - // start of the program - if (currentTime != prevtime) + // start of the program + if(currentTime!=prevtime) { - prevtime = currentTime; - currentTime = getClk(); - // get remainig time & reduce the remaining quantum + prevtime=currentTime; + currentTime=getClk(); + // get remainig time & reduce the remaining quantum if (RunningProcess != NULL && RunningProcess->remainingTime > 0) { - kill(RunningProcess->pid, SIGCONT); + kill(RunningProcess->pid,SIGCONT); raise(SIGSTOP); - usleep(100); // wait 10microseconds - kill(RunningProcess->id, SIGCONT); + usleep(100); //wait 10microseconds + kill(RunningProcess->id,SIGCONT); RunningProcess->remainingTime = (*runPshmadd); + } // running new process if (RunningProcess == NULL) @@ -370,7 +456,8 @@ void HPF() contProcess(RunningProcess); RunningProcess->state = running; // fprintf(pf,"At time %d process %d started arr %d total %d ramain %d wait %d\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->remainingTime,RunningProcess->waitingTime); - Outp(0, currentTime, RunningProcess); + Outp(0,currentTime,RunningProcess); + } else { @@ -378,23 +465,24 @@ void HPF() } } // running process checks - else if (RunningProcess->remainingTime == 0) + else if (RunningProcess->remainingTime == 0 ) { // noyify process termination + + // printf("process with id=%d terminated with %d\n", RunningProcess->id, RunningProcess->remainingTime); + RunningProcess->state = terminated; + terminatedProcessesNum++; + int TA=currentTime-RunningProcess->arrival_time; + double WTA=(TA/(double)RunningProcess->runtime); + // fprintf(pf,"At time %d process %d finished arr %d total %d ramain 0 wait %d TA %.d WTA %.2f\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->waitingTime,TA,WTA); + RunningProcess->finishtime=currentTime; + Outp(3,currentTime,RunningProcess); + RunningProcess = NULL; + printf("the process terminated\n"); + - // printf("process with id=%d terminated with %d\n", RunningProcess->id, RunningProcess->remainingTime); - RunningProcess->state = terminated; - terminatedProcessesNum++; - int TA = currentTime - RunningProcess->arrival_time; - double WTA = (TA / (double)RunningProcess->runtime); - // fprintf(pf,"At time %d process %d finished arr %d total %d ramain 0 wait %d TA %.d WTA %.2f\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->waitingTime,TA,WTA); - RunningProcess->finishtime = currentTime; - Outp(3, currentTime, RunningProcess); - RunningProcess = NULL; - printf("the process terminated\n"); - - // - + // + if (!priority_isempty(&readyQueue2)) { printf("Context switching\n"); @@ -402,14 +490,16 @@ void HPF() RunningProcess->state = running; (*runPshmadd) = RunningProcess->remainingTime; contProcess(RunningProcess); - + + // fprintf(pf,"At time %d process %d started arr %d total %d ramain %d wait %d\n",currentTime,RunningProcess->id,RunningProcess->arrival_time,RunningProcess->runtime,RunningProcess->remainingTime,RunningProcess->waitingTime); - Outp(0, currentTime, RunningProcess); + Outp(0,currentTime,RunningProcess); + } else { idealtime++; - } + } } } } @@ -417,44 +507,38 @@ void HPF() //---------------------------------------------------------------// //------------------SRTN Algorithm----------------// -void SRTN() -{ +void SRTN() { printf("executing SRTN algorithm\n"); - recievedProcesses = (struct Process *)malloc(numofProcesses * sizeof(struct Process)); + recievedProcesses = (struct Process *) malloc(numofProcesses * sizeof(struct Process)); struct Process *recv; struct priority_Queue readyQueue; int currentTime; int prevtime = 0; bool firsttime = 1; - while (getClk() == 0) - ; + while (getClk() == 0); - while (terminatedProcessesNum < numofProcesses) - { + while (terminatedProcessesNum < numofProcesses) { currentTime = getClk(); usleep(100); // if all processes are terminated int val = msgrcv(msgqid, &recievedProcesses[recievedProcessesNum], sizeof(struct Process), 0, IPC_NOWAIT); - if (val != -1) - { + if (val != -1) { recv = &recievedProcesses[recievedProcessesNum]; forkProcess(recv); kill(recv->id, SIGCONT); raise(SIGSTOP); - usleep(100); // wait 10microseconds + usleep(100); //wait 10microseconds kill(recv->id, SIGCONT); recv->starttime = -1; priority_enqueue(&readyQueue, recv, 1); - TotalRun += recv->runtime; + TotalRun+=recv->runtime; recievedProcessesNum++; } // start of the program - if (currentTime != prevtime) - { - if (RunningProcess && RunningProcess->remainingTime == 0) - { + if (currentTime != prevtime) { + if (RunningProcess && RunningProcess->remainingTime == 0) { // notify process termination RunningProcess->finishtime = currentTime; Outp(3, currentTime, RunningProcess); @@ -462,30 +546,22 @@ void SRTN() terminatedProcessesNum++; RunningProcess = NULL; } - if (RunningProcess == NULL) - { - if (!priority_isempty(&readyQueue)) - { + if (RunningProcess == NULL) { + if (!priority_isempty(&readyQueue)) { RunningProcess = priority_dequeue(&readyQueue); RunningProcess->state = running; (*runPshmadd) = RunningProcess->remainingTime; contProcess(RunningProcess); - if (RunningProcess->starttime == -1) - { + if (RunningProcess->starttime == -1) { RunningProcess->starttime = currentTime; Outp(0, currentTime, RunningProcess); - } - else - { + } else { Outp(1, currentTime, RunningProcess); } } - } - else if (!priority_isempty(&readyQueue)) - { + } else if (!priority_isempty(&readyQueue)) { min = priority_peek(&readyQueue); - if (min->remainingTime < RunningProcess->remainingTime) - { + if (min->remainingTime < RunningProcess->remainingTime) { Outp(2, currentTime, RunningProcess); RunningProcess->state = ready; priority_enqueue(&readyQueue, RunningProcess, 1); @@ -495,28 +571,23 @@ void SRTN() RunningProcess->state = running; (*runPshmadd) = RunningProcess->remainingTime; contProcess(RunningProcess); - if (RunningProcess->starttime == -1) - { + if (RunningProcess->starttime == -1) { RunningProcess->starttime = currentTime; Outp(0, currentTime, RunningProcess); - } - else - { + } else { Outp(1, currentTime, RunningProcess); } } } // get remaining time - if (RunningProcess != NULL && RunningProcess->remainingTime > 0) - { + if (RunningProcess != NULL && RunningProcess->remainingTime > 0) { kill(RunningProcess->pid, SIGCONT); raise(SIGSTOP); - usleep(100); // wait 10microseconds + usleep(100); //wait 10microseconds kill(RunningProcess->id, SIGCONT); RunningProcess->remainingTime--; } - else if (!RunningProcess && terminatedProcessesNum != numofProcesses) - { + else if (!RunningProcess && terminatedProcessesNum != numofProcesses){ idealtime++; } prevtime = currentTime;