-
-
Save itsashis4u/e00d461bbe843a7c4c59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
oS Lab 24th Jan 2014- Program 3 2 child process | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
pid_t PID; | |
int i; | |
PID=getpid(); | |
printf("Before fork() the process id = %d \n",PID); | |
printf("Process id of the parent = %d \n",getppid()); | |
PID=fork(); | |
if(PID==0) | |
{ | |
printf("This is Child process with id = %d \n",getpid()); | |
printf("Child1 process has been started.\n"); | |
sleep(6); | |
execl("/bin/echo","echo","hello child1",(char *)0); | |
sleep(5); | |
} | |
else | |
{ | |
PID=fork(); | |
printf("This is Parent process with id = %d \n",getpid()); | |
wait(0); | |
if(PID==0) | |
{ | |
printf("This is Child process with id = %d \n",getpid()); | |
sleep(4); | |
printf("Child2 process has been started.\n"); | |
sleep(10); | |
execl("/bin/echo","echo","hello child2",(char *)0); | |
sleep(5); | |
} | |
else | |
{ | |
sleep(5); | |
printf("This is Parent process with id = %d \n",getpid()); | |
wait(0); | |
} | |
sleep(10); | |
printf("Parent2 process has been resumed.\n"); | |
} | |
sleep(10); | |
printf("Parent2 process has been terminated.\n"); | |
sleep(5); | |
printf("Parent1 process has been resumed.\n"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Program-2 | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
pid_t PID; | |
PID=getpid(); | |
printf("Before fork() the process id = %d \n",PID); | |
printf("Process id of the parent = %d \n",getppid()); | |
PID=fork(); | |
if(PID==0) | |
{ | |
printf("This is Child process with id = %d \n",getpid()); | |
sleep(6); | |
execl("/bin/echo","echo","hello",(char *)0); | |
sleep(5); | |
} | |
else | |
{ | |
printf("This is Parent process with id = %d \n",getpid()); | |
wait(0); | |
} | |
printf("Parent process has been resumed.\n"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include <malloc.h> | |
typedef struct execinterval Execinterval; | |
struct execinterval | |
{ | |
int process; | |
int start; | |
int end; | |
}; | |
struct process | |
{ | |
int brt; //burst time | |
int art; //arrival time | |
int rmt; //remaining time | |
int priority; // priority | |
int wt; //waiting time | |
int tnt; //turnaround time | |
int status; // 1 : ready | |
// 2 : executed | |
// 0 : completed | |
// -1: notready | |
int rc; // run count : no. of times process is executed(no.of exec intervals) | |
Execinterval ei[10]; //ei[i].start : start time of ith run | |
//ei[i].end : end time of ith run | |
}; | |
typedef struct process Process; | |
struct readyqueue | |
{ | |
int front; | |
int rear; | |
int arr[20]; | |
}; | |
typedef struct readyqueue Readyqueue; | |
struct ptyarr | |
{ | |
int itm; | |
int pty; | |
}; | |
typedef struct ptyarr Ptyarr; | |
struct priorityqueue | |
{ | |
int size; | |
Ptyarr arr[20]; | |
}; | |
typedef struct priorityqueue Priorityque; | |
Readyqueue* initialize(); | |
int insert(Readyqueue *rq, int item); | |
int delete(Readyqueue *rq); | |
Priorityque* pqinitialize(); | |
int pqinsert(Priorityque *pq, int item, int priority); | |
int pqdelete(Priorityque *pq); | |
void pqdisplay(Priorityque *pq); | |
void compWaiting(Process P[], int n); | |
void compTurnaround(Process P[], int n); | |
void execint(Process P[], int n, Execinterval ei[],int *nit); | |
void printGanttchart(Execinterval ei[]); | |
void printTable(Process P[],int n, int choice); | |
void Getdata(Process P[], int n); | |
void fcfs(Process P[], int n); | |
void sjf(Process P[], int n); | |
void sjfnp(Process P[], int n); | |
void sjfp(Process P[],int n); | |
void priority(Process P[], int n); | |
void prioritynp(Process P[], int n); | |
void priorityp(Process P[],int n); | |
void roundrobin(Process P[],int n,int ts); | |
void Refreshsystem(Process P[], int n); | |
main() | |
{ | |
int ch=0,n,ts; | |
Process P[20]; | |
do | |
{ | |
switch(ch) | |
{ | |
case 0: | |
printf("\n0.MENU"); | |
printf("\n1.Getting BurstTime and ArrivalTime"); | |
printf("\n2.FirstComeFirstServed"); | |
printf("\n3.ShortestJobFirst"); | |
printf("\n4.RoundRobin"); | |
printf("\n5.Priority"); | |
printf("\n6.EXIT"); | |
break; | |
case 1: | |
printf("Enter the no of processes:"); | |
scanf("%d",&n); | |
Getdata(P,n); | |
break; | |
case 2: | |
printf("FIRST COME FIRST SERVED SCHEDULING"); | |
fcfs(P,n); | |
break; | |
case 3: | |
printf("SHORTEST JOB FIRST SCHEDULING"); | |
sjf(P,n); | |
break; | |
case 4: | |
printf("ROUND ROBIN SCHEDULING"); | |
printf("Enter the time slice:"); | |
scanf("%d",&ts); | |
roundrobin(P,n,ts); | |
break; | |
case 5: | |
printf("PRIORITY SCHEDULING"); | |
priority(P,n); | |
break; | |
case 6: | |
break; | |
} //end switch | |
printf("\nEnter your choice:"); | |
scanf("%d",&ch); | |
}while(ch<6); | |
} | |
void Getdata(Process P[], int n) | |
{ | |
int i; | |
int ch; | |
printf("Priority: enter 1 for yes 0 for no :"); | |
scanf("%d",&ch); | |
if(ch == 0) { | |
for(i=1;i<=n;i++) { | |
printf("\nEnter The ArrivalTime for Process p%d = ",i); | |
scanf("%d",&P[i].art); | |
printf("\nEnter The BurstTime for Process p%d = ",i); | |
scanf("%d",&P[i].brt); | |
} | |
} | |
else if (ch == 1) { | |
for(i=1;i<=n;i++) { | |
printf("\nEnter The ArrivalTime for Process p%d = ",i); | |
scanf("%d",&P[i].art); | |
printf("\nEnter The BurstTime for Process p%d = ",i); | |
scanf("%d",&P[i].brt); | |
printf("\nEnter The Priority for Process p%d = ",i); | |
scanf("%d",&P[i].priority); | |
} | |
} | |
else { | |
printf("invalid choice\n"); | |
} | |
} | |
void Refreshsystem(Process P[], int n) | |
{ | |
int i; | |
for(i=1;i<=n;i++) { | |
P[i].rmt = P[i].brt; | |
P[i].status=-1; | |
P[i].rc=0; | |
} | |
} | |
Readyqueue* initialize() | |
{ | |
Readyqueue *rq; | |
rq=(Readyqueue*) malloc (sizeof(Readyqueue)); | |
rq->front=-1; | |
rq->rear=-1; | |
return rq; | |
} | |
int insert(Readyqueue *rq, int item) | |
{ | |
//Queue size =20 | |
//printf("\ninsert: front=%d rear=%d\n",rq->front,rq->rear); | |
if((rq->rear+1)%20 == rq->front) { | |
printf("\nqueue is filled\n"); | |
return -1; | |
} | |
if (rq->front == -1 ) { | |
rq->front=0; | |
rq->rear=0; | |
} | |
else { | |
rq->rear=(rq->rear+1)%20; | |
} | |
rq->arr[rq->rear]=item; | |
return 0; | |
} | |
int delete(Readyqueue *rq) | |
{ | |
int item=-1; | |
//printf("\n delete: front=%d rear=%d\n",rq->front,rq->rear); | |
if(rq->front == -1 && rq->rear == -1) { | |
//printf("\nqueue is empty\n"); | |
return -1; | |
} | |
item=rq->arr[rq->front]; | |
if(rq->front == rq->rear) { | |
rq->front = -1; | |
rq->rear = -1; | |
} | |
else { | |
rq->front=(rq->front+1)%20; | |
} | |
return item; | |
} | |
Priorityque* pqinitialize() | |
{ | |
Priorityque* pq; | |
pq = (Priorityque*) malloc(sizeof(Priorityque)); | |
pq->size = 0; | |
return pq; | |
} | |
int pqinsert(Priorityque *pq, int item, int priority) | |
{ | |
int i,parent,temp; | |
if (pq->size >= 20) { | |
//printf("queue is filled"); | |
return -1; | |
} | |
pq->arr[pq->size].itm = item; | |
pq->arr[pq->size].pty = priority; | |
i=pq->size; | |
while(i > 0) { | |
parent=(i-1)/2; | |
if(pq->arr[i].pty < pq->arr[parent].pty) { | |
temp=pq->arr[i].pty; | |
pq->arr[i].pty=pq->arr[parent].pty; | |
pq->arr[parent].pty=temp; | |
temp=pq->arr[i].itm; | |
pq->arr[i].itm=pq->arr[parent].itm; | |
pq->arr[parent].itm=temp; | |
} // end if | |
i=parent; | |
} // end while | |
pq->size=pq->size+1; | |
return 0; | |
} | |
int pqdelete(Priorityque *pq) | |
{ | |
int i,item,temp,smallest,l,r; | |
if (pq->size == 0) { | |
//printf("queue is empty"); | |
return -1; | |
} | |
item=pq->arr[0].itm; | |
pq->arr[0].itm=pq->arr[pq->size-1].itm; | |
pq->arr[0].pty=pq->arr[pq->size-1].pty;; | |
pq->size = pq->size -1; | |
i=0; | |
do { | |
l=2*i+1; | |
r=2*i+1+1; | |
smallest=i; | |
if(l < pq->size && pq->arr[l].pty < pq->arr[smallest].pty) { | |
smallest=l; | |
} | |
if(r < pq->size && pq->arr[r].pty < pq->arr[smallest].pty) { | |
smallest=r; | |
} | |
if (smallest == i) | |
break; | |
temp=pq->arr[i].pty; | |
pq->arr[i].pty=pq->arr[smallest].pty; | |
pq->arr[smallest].pty=temp; | |
temp=pq->arr[i].itm; | |
pq->arr[i].itm=pq->arr[smallest].itm; | |
pq->arr[smallest].itm=temp; | |
i=smallest; | |
} while(l >= pq->size && r >= pq->size); // end while | |
return item; | |
} | |
void pqdisplay(Priorityque *pq) | |
{ | |
int i,item,temp,smallest,l,r; | |
if (pq->size == 0) { | |
printf("\nqueue is empty"); | |
return; | |
} | |
printf("\n"); | |
for (i=0;i<pq->size;i++) { | |
printf("%d\t%d\n",pq->arr[i].itm,pq->arr[i].pty); | |
} | |
printf("\n"); | |
} | |
//Computation of Waiting time | |
void compWaiting(Process P[], int n) | |
{ | |
int i,j; | |
for(i=1;i<=n;i++) { | |
P[i].wt=P[i].ei[0].start-P[i].art; //arrival time - start time | |
for (j=1;j<P[i].rc;j++) { | |
P[i].wt=P[i].ei[j].start-P[i].ei[j-1].end; // start of (j)th run - end of (j-1)th run | |
} | |
} | |
} | |
//Computation of TurnaroundTime | |
void compTurnaround(Process P[], int n) | |
{ | |
int i; | |
for(i=1;i<=n;i++) { | |
P[i].tnt=P[i].ei[P[i].rc-1].end-P[i].art; // end time of final run - arrival time | |
} | |
} | |
//Compute Execution Interval | |
void compExecInt(Process P[], int n, Execinterval Ei[],int *nit) | |
{ | |
int i,j,k=0,temp; | |
for(i=1;i<=n;i++) { | |
for (j=0; j <P[i].rc;j++) { | |
Ei[k].process=i; // process id | |
Ei[k].start=P[i].ei[j].start; // process start time | |
Ei[k].end=P[i].ei[j].end; // process end time | |
k++; | |
} | |
} | |
*nit=k; //nit: no. of interval | |
printf("run count =%d\n",*nit); | |
//Sort execution interval with start time (bubble sort) | |
for(i=0;i<k-1;i++) { | |
for(j=k-1;j>i;j--) { | |
if (Ei[j].start < Ei[j-1].start) { | |
temp=Ei[j].process; | |
Ei[j].process=Ei[j-1].process; | |
Ei[j-1].process=temp; | |
temp=Ei[j].start; | |
Ei[j].start=Ei[j-1].start; | |
Ei[j-1].start=temp; | |
temp=Ei[j].end; | |
Ei[j].end=Ei[j-1].end; | |
Ei[j-1].end=temp; | |
} | |
} | |
} | |
} | |
//Gantt Chart | |
void printGanttChart(Execinterval Ei[],int nit) | |
{ | |
int i,j; | |
printf("\nGANTT CHART\n"); | |
for(j=0;j<Ei[0].start;j++) { | |
printf("0 "); | |
} | |
printf(" "); | |
for(i=0;i<nit-1;i++) | |
{ | |
for(j=Ei[i].start;j<Ei[i].end;j++) { | |
printf("P%d ",Ei[i].process); | |
} | |
printf(" "); | |
for(j=Ei[i].end;j<Ei[i+1].start;j++) { | |
printf("0 "); | |
} | |
printf(" "); | |
} | |
for(j=Ei[i].start;j<Ei[i].end;j++) { | |
printf("P%d ",Ei[i].process); | |
} | |
printf(" "); | |
} | |
//Print Table | |
void printTable(Process P[],int n, int choice) | |
{ | |
int i; | |
float avwt=0; // avwt: average waiting time | |
float avtnt=0; // avtnt: average turn around time | |
if (choice == 0) { | |
printf("\nProcess\tArrivalTimt\tBurstTime\tRemainingTime\tRun Count\tStatus\n"); | |
for(i=1;i<=n;i++) { | |
printf("P%d\t",i); | |
printf("%d\t\t",P[i].art); | |
printf("%d\t\t",P[i].brt); | |
printf("%d\t\t",P[i].rmt); | |
printf("%d\t\t",P[i].rc); | |
printf("%d\n",P[i].status); | |
} | |
return; | |
} | |
else if (choice == 1) { | |
printf("\nProcess\tArrivalTimt\tBurstTime\tWaitingTime\tTurnaroundTime\n"); | |
for(i=1;i<=n;i++) { | |
printf("P%d\t",i); | |
printf("%d\t\t",P[i].art); | |
printf("%d\t\t",P[i].brt); | |
printf("%d\t\t",P[i].wt); | |
printf("%d\n",P[i].tnt); | |
} | |
} | |
else if (choice == 2) { | |
printf("\nProcess\tArrivalTimt\tBurstTime\tPriority\tRun Count\tStatus\n"); | |
for(i=1;i<=n;i++) { | |
printf("P%d\t",i); | |
printf("%d\t\t",P[i].art); | |
printf("%d\t\t",P[i].brt); | |
printf("%d\t\t",P[i].priority); | |
printf("%d\t\t",P[i].rc); | |
printf("%d\n",P[i].status); | |
} | |
return; | |
} | |
else if (choice == 3) { | |
printf("\nProcess\tArrivalTimt\tBurstTime\tPriority\tWaitingTime\tTurnaroundTime\n"); | |
for(i=1;i<=n;i++) { | |
printf("P%d\t",i); | |
printf("%d\t\t",P[i].art); | |
printf("%d\t\t",P[i].brt); | |
printf("%d\t\t",P[i].priority); | |
printf("%d\t\t",P[i].wt); | |
printf("%d\n",P[i].tnt); | |
} | |
} | |
//Average waiting time and turn around time | |
for(i=1;i<=n;i++) | |
{ | |
avwt=avwt+P[i].wt; | |
avtnt=avtnt+P[i].tnt; | |
} | |
avwt=avwt/n; | |
avtnt=avtnt/n; | |
printf("\nAverage Waiting Time=%f",avwt); | |
printf("\nAverage Turnaround time=%f",avtnt); | |
} | |
void fcfs(Process P[], int n) | |
{ | |
int i,j,k,temp; | |
int t=0; // t : time counter | |
int pid; // process id | |
Readyqueue *rque; // ready queue | |
rque=initialize(); | |
Execinterval Ei[50]; // execution interval | |
int nit=0; //total no.of intervals | |
int pcrstat = 0; // processor status :empty processor | |
Refreshsystem(P,n); | |
printTable(P,n,0); | |
while(1) | |
{ | |
// insert the process arrivet into ready queue | |
for (i=1;i<=n;i++) { | |
if(P[i].status == -1 && P[i].art == t) { | |
P[i].status=1; //process ready | |
insert(rque,i); | |
} | |
} | |
//printf("t = %d ",t); | |
// delete front process from ready queue if no process is running | |
if (pcrstat == 0) // idle processor | |
pid=delete(rque); | |
if (pid == -1) //empty ready queue | |
{ | |
t++; | |
continue; | |
} | |
else if (P[pid].status == 1) { | |
pcrstat = 1; //processor busy | |
P[pid].ei[P[pid].rc].start=t; //save start time | |
P[pid].status = 2; // set process run | |
} | |
if(P[pid].rmt > 0) { | |
t++; | |
P[pid].rmt=P[pid].rmt-1; | |
continue; | |
} | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=0; // process completed | |
pcrstat = 0; // empty processor | |
// exit timer after all process completed | |
for (i=1;i<=n;i++) { | |
//printf(" P%d = %d\n",i,P[i].status); | |
if(P[i].status != 0) { | |
break; | |
} | |
} | |
if (i == n+1) | |
break; | |
} //end timer | |
free(rque); | |
compWaiting(P,n); | |
compTurnaround(P,n); | |
compExecInt(P,n,Ei,&nit); | |
printGanttChart(Ei,nit); | |
printTable(P,n,1); | |
} | |
void sjf(Process P[], int n) | |
{ | |
int ch=0; | |
do | |
{ | |
switch(ch) | |
{ | |
case 0: | |
printf("\n0.MENU"); | |
printf("\n1.SJF non-preempted"); | |
printf("\n2.SJF preempted"); | |
printf("\n3.EXIT"); | |
break; | |
case 1: | |
printf("SJF non-preempted"); | |
sjfnp(P,n); | |
return; | |
case 2: | |
printf("SJF preempted"); | |
sjfp(P,n); | |
return; | |
case 3: | |
break; | |
} //end switch | |
printf("\nEnter your choice:"); | |
scanf("%d",&ch); | |
}while(ch<3); | |
} | |
void sjfnp(Process P[],int n) | |
{ | |
int i,j,k,temp,id,tempary[20]; | |
int t=0; // t : time counter | |
int pid; // process id | |
Priorityque *rq; // ready queue | |
rq=pqinitialize(); | |
Execinterval Ei[50]; // execution interval | |
int nit=0; //total no.of intervals | |
int pcrstat = 0; // processor status : empty processor | |
Refreshsystem(P,n); | |
printTable(P,n,0); | |
while(1) | |
{ | |
//printf("\nt = %d\n",t); | |
//pqdisplay(rq); | |
// insert the process arrivet into ready queue | |
for (i=1;i<=n;i++) { | |
if(P[i].status == -1 && P[i].art == t) { | |
P[i].status=1; //process ready | |
pqinsert(rq,i,P[i].brt); | |
} | |
} | |
// delete front process from ready queue if no process is running | |
if (pcrstat == 0) // idle processor | |
pid=pqdelete(rq); | |
if (pid == -1 ) //ready queue empty | |
{ | |
t++; | |
continue; | |
} | |
else if (P[pid].status == 1) { | |
pcrstat = 1; | |
P[pid].ei[P[pid].rc].start=t; //save start time | |
P[pid].status = 2; // process running | |
} | |
//printf("rmt = %d\n",P[pid].rmt); | |
if(P[pid].rmt > 0) { | |
t++; | |
P[pid].rmt=P[pid].rmt-1; | |
continue; | |
} | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=0; // process completed | |
pcrstat = 0; | |
// exit timer after all process completed | |
for (i=1;i<=n;i++) { | |
//printf(" P%d = %d\n",i,P[i].status); | |
if(P[i].status != 0) { | |
break; | |
} | |
} | |
if (i == n+1) | |
break; | |
} //end timer | |
free(rq); | |
compWaiting(P,n); | |
compTurnaround(P,n); | |
compExecInt(P,n,Ei,&nit); | |
printGanttChart(Ei,nit); | |
printTable(P,n,1); | |
} | |
void sjfp(Process P[],int n) | |
{ | |
int i,j,k,temp; | |
int t=0; // t : time counter | |
int pid; // process id | |
Priorityque *rq; // ready queue | |
rq=pqinitialize(); | |
Execinterval Ei[50]; // execution interval | |
int nit=0; //total no.of intervals | |
int pcrstat = 0; // empty processor | |
Refreshsystem(P,n); | |
printTable(P,n,0); | |
while(t<20) | |
{ | |
//printf("t = %d ",t); | |
//pqdisplay(rq); | |
// insert the process arrive into ready queue | |
for (i=1;i<=n;i++) { | |
if(P[i].status == -1 && P[i].art == t) { | |
P[i].status=1; //process ready | |
if (pcrstat == 1 && P[i].rmt < P[pid].rmt) { | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=1; // process completed | |
pcrstat= 0; | |
pqinsert(rq,pid,P[pid].rmt); | |
} | |
pqinsert(rq,i,P[i].rmt); | |
} | |
} | |
// delete front process from ready queue if no process is running | |
if (pcrstat == 0) // idle processor | |
pid=pqdelete(rq); | |
if (pid == -1) // empty ready queue | |
{ | |
t++; | |
continue; | |
} | |
else if (P[pid].status == 1) { | |
pcrstat = 1; | |
P[pid].ei[P[pid].rc].start=t; //save start time | |
P[pid].status = 2; // process running | |
} | |
//printf("rmt = %d\n",P[pid].rmt); | |
if(P[pid].rmt > 0) { | |
t++; | |
P[pid].rmt=P[pid].rmt-1; | |
continue; | |
} | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=0; // process completed | |
pcrstat= 0; | |
// exit timer after all process completed | |
for (i=1;i<=n;i++) { | |
//printf(" P%d = %d\n",i,P[i].status); | |
if(P[i].status != 0) { | |
break; | |
} | |
} | |
if (i == n+1) | |
break; | |
} //end timer | |
free(rq); | |
compWaiting(P,n); | |
compTurnaround(P,n); | |
compExecInt(P,n,Ei,&nit); | |
printGanttChart(Ei,nit); | |
printTable(P,n,1); | |
} | |
void priority(Process P[], int n) | |
{ | |
int ch=0; | |
do | |
{ | |
switch(ch) | |
{ | |
case 0: | |
printf("\n0.MENU"); | |
printf("\n1.Priority non-preempted"); | |
printf("\n2.Priority preempted"); | |
printf("\n3.EXIT"); | |
break; | |
case 1: | |
printf("Priority non-preempted"); | |
prioritynp(P,n); | |
return; | |
case 2: | |
printf("Priority preempted"); | |
priorityp(P,n); | |
return; | |
case 3: | |
break; | |
} //end switch | |
printf("\nEnter your choice:"); | |
scanf("%d",&ch); | |
}while(ch<3); | |
} | |
void prioritynp(Process P[],int n) | |
{ | |
int i,j,k,temp,id,tempary[20]; | |
int t=0; // t : time counter | |
int pid; // process id | |
Priorityque *rq; // ready queue | |
rq=pqinitialize(); | |
Execinterval Ei[50]; // execution interval | |
int nit=0; //total no.of intervals | |
int pcrstat = 0; // processor status : empty processor | |
Refreshsystem(P,n); | |
printTable(P,n,2); | |
while(1) | |
{ | |
//printf("\nt = %d\n",t); | |
//pqdisplay(rq); | |
// insert the process arrivet into ready queue | |
for (i=1;i<=n;i++) { | |
if(P[i].status == -1 && P[i].art == t) { | |
P[i].status=1; //process ready | |
pqinsert(rq,i,P[i].priority); | |
} | |
} | |
// delete front process from ready queue if no process is running | |
if (pcrstat == 0) // idle processor | |
pid=pqdelete(rq); | |
if (pid == -1 ) //ready queue empty | |
{ | |
t++; | |
continue; | |
} | |
else if (P[pid].status == 1) { | |
pcrstat = 1; | |
P[pid].ei[P[pid].rc].start=t; //save start time | |
P[pid].status = 2; // process running | |
} | |
//printf("rmt = %d\n",P[pid].rmt); | |
if(P[pid].rmt > 0) { | |
t++; | |
P[pid].rmt=P[pid].rmt-1; | |
continue; | |
} | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=0; // process completed | |
pcrstat = 0; | |
// exit timer after all process completed | |
for (i=1;i<=n;i++) { | |
//printf(" P%d = %d\n",i,P[i].status); | |
if(P[i].status != 0) { | |
break; | |
} | |
} | |
if (i == n+1) | |
break; | |
} //end timer | |
free(rq); | |
compWaiting(P,n); | |
compTurnaround(P,n); | |
compExecInt(P,n,Ei,&nit); | |
printGanttChart(Ei,nit); | |
printTable(P,n,3); | |
} | |
void priorityp(Process P[],int n) | |
{ | |
int i,j,k,temp; | |
int t=0; // t : time counter | |
int pid; // process id | |
Priorityque *rq; // ready queue | |
rq=pqinitialize(); | |
Execinterval Ei[50]; // execution interval | |
int nit=0; //total no.of intervals | |
int pcrstat = 0; // empty processor | |
Refreshsystem(P,n); | |
printTable(P,n,2); | |
while(t<20) | |
{ | |
//printf("t = %d ",t); | |
//pqdisplay(rq); | |
// insert the process arrive into ready queue | |
for (i=1;i<=n;i++) { | |
if(P[i].status == -1 && P[i].art == t) { | |
P[i].status=1; //process ready | |
if (pcrstat == 1 && P[i].priority < P[pid].priority) { | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=1; // process completed | |
pcrstat= 0; | |
pqinsert(rq,pid,P[pid].priority); | |
} | |
pqinsert(rq,i,P[i].priority); | |
} | |
} | |
// delete front process from ready queue if no process is running | |
if (pcrstat == 0) // idle processor | |
pid=pqdelete(rq); | |
if (pid == -1) // empty ready queue | |
{ | |
t++; | |
continue; | |
} | |
else if (P[pid].status == 1) { | |
pcrstat = 1; | |
P[pid].ei[P[pid].rc].start=t; //save start time | |
P[pid].status = 2; // process running | |
} | |
//printf("rmt = %d\n",P[pid].rmt); | |
if(P[pid].rmt > 0) { | |
t++; | |
P[pid].rmt=P[pid].rmt-1; | |
continue; | |
} | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=0; // process completed | |
pcrstat= 0; | |
// exit timer after all process completed | |
for (i=1;i<=n;i++) { | |
//printf(" P%d = %d\n",i,P[i].status); | |
if(P[i].status != 0) { | |
break; | |
} | |
} | |
if (i == n+1) | |
break; | |
} //end timer | |
free(rq); | |
compWaiting(P,n); | |
compTurnaround(P,n); | |
compExecInt(P,n,Ei,&nit); | |
printGanttChart(Ei,nit); | |
printTable(P,n,3); | |
} | |
void roundrobin(Process P[], int n,int ts) | |
{ | |
int i,j,k,temp; | |
int t=0; // t : time counter | |
int pid; // process id | |
Readyqueue *rque; // ready queue | |
rque=initialize(); | |
Execinterval Ei[50]; // execution interval | |
int nit=0; //total no.of intervals | |
int pcrstat = 0; // processor status :empty processor | |
int timer; | |
Refreshsystem(P,n); | |
printTable(P,n,0); | |
while(1) | |
{ | |
// insert the process arrivet into ready queue | |
for (i=1;i<=n;i++) { | |
if(P[i].status == -1 && P[i].art == t) { | |
P[i].status=1; //process ready | |
insert(rque,i); | |
} | |
} | |
// if timer expire | |
if (pcrstat == 1 && timer == 0) { | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=1; // process preempted | |
pcrstat= 0; | |
insert(rque,pid); | |
} | |
//printf("t = %d ",t); | |
// delete front process from ready queue if no process is running | |
if (pcrstat == 0) // idle processor | |
pid=delete(rque); | |
if (pid == -1) //empty ready queue | |
{ | |
t++; | |
continue; | |
} | |
else if (P[pid].status == 1) { | |
pcrstat = 1; //processor busy | |
P[pid].ei[P[pid].rc].start=t; //save start time | |
P[pid].status = 2; // set process run | |
timer=ts; | |
} | |
if(P[pid].rmt > 0) { | |
t++; | |
P[pid].rmt=P[pid].rmt-1; | |
timer--; | |
continue; | |
} | |
P[pid].ei[P[pid].rc].end=t; // save end time | |
P[pid].rc++; // increase run length | |
P[pid].status=0; // process completed | |
pcrstat = 0; // empty processor | |
// exit timer after all process completed | |
for (i=1;i<=n;i++) { | |
//printf(" P%d = %d\n",i,P[i].status); | |
if(P[i].status != 0) { | |
break; | |
} | |
} | |
if (i == n+1) | |
break; | |
} //end timer | |
free(rque); | |
compWaiting(P,n); | |
compTurnaround(P,n); | |
compExecInt(P,n,Ei,&nit); | |
printGanttChart(Ei,nit); | |
printTable(P,n,1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment