Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- int q[100],front=-1,rear=-1;
- struct process
- {
- char name[20];
- int at,tt,bt,wt,ct,pr,status,left;
- }p[20],temp;
- struct done
- {
- char name[20];
- int st,ct;
- }d[20];
- int n,num,idle=0;
- float average_wt=0,average_tt=0;
- void enqueue(int j)
- {
- if(front==-1 && rear==-1)
- {
- front++;
- }
- rear++;
- q[rear] = j;
- }
- int dequeue()
- {
- int item;
- item = q[front];
- if(front == rear)
- {
- front = -1;
- rear = -1;
- }
- else
- {
- front++;
- }
- return item;
- }
- void read(){
- printf("ENTER THE NUMBER OF PROCESSES : ");
- scanf("%d",&n);
- for(int i=0;i<n;i++)
- {
- printf("\nENTER DETAILS OF PROCESS %d",i+1);
- printf("\nPROCESS NAME : ");
- scanf(" %s",p[i].name);
- printf("ARRIVAL TIME : ");
- scanf("%d",&p[i].at);
- printf("BURST TIME : ");
- scanf("%d",&p[i].bt);
- printf("PRIORITY : ");
- scanf("%d",&p[i].pr);
- p[i].left = p[i].bt;
- p[i].status = 0;
- }
- }
- void sort(){
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n-i-1;j++)
- {
- if(p[j].at > p[j+1].at)
- {
- temp = p[j];
- p[j] = p[j+1];
- p[j+1] = temp;
- }
- }
- }
- }
- void FCFS(){
- int i,j;
- for(i=0,j=0,num=0;j<n;)
- {
- if(p[j].at<=i && p[j].status==0)
- {
- if(idle==1)
- {
- d[num].ct=i;
- num++;
- idle = 0;
- }
- strcpy(d[num].name,p[j].name);
- d[num].st = i;
- d[num].ct = i + p[j].bt;
- p[j].wt = d[num].st - p[j].at;
- p[j].tt = p[j].wt + p[j].bt;
- p[j].ct = d[num].ct;
- i = d[num].ct;
- p[j].status = 1;
- j++;
- num++;
- }
- else if(idle == 0)
- {
- strcpy(d[num].name,"Idle");
- d[num].st = i;
- i++;
- idle = 1;
- }
- else
- {
- i++;
- }
- }
- }
- void NP_SJF(){
- int i,j,ls,fnd,min;
- for(i=0,ls=0,num=0,idle=0;ls<n;){
- for(j=0,fnd=0;j<n;j++)
- {
- if(i>=p[j].at && p[j].status==0)
- {
- if(fnd==0){
- min = j;
- fnd = 1;
- }
- else if(p[min].bt>p[j].bt){
- min = j;
- }
- }
- }
- if(idle==0 && fnd==0){
- strcpy(d[num].name,"Idle");
- d[num].st = i;
- i++;
- idle = 1;
- }
- else if(fnd==1){
- if(idle==1){
- d[num].ct = i;
- num++;
- idle = 0;
- }
- strcpy(d[num].name,p[min].name);
- p[min].status =1;
- d[num].st = i;
- d[num].ct = i + p[min].bt;
- i = d[num].ct;
- p[min].ct = d[num].ct;
- p[min].tt = p[min].ct - p[min].at;
- p[min].wt = p[min].tt - p[min].bt;
- num++;
- ls++;
- }
- else{
- i++;
- }
- }
- }
- void NP_Priority(){
- int i,j,ls,fnd,min;
- for(i=0,ls=0,num=0,idle=0;ls<n;)
- {
- for(j=0,fnd=0;j<n;j++)
- {
- if(i>=p[j].at && p[j].status==0)
- {
- if(fnd==0)
- {
- min = j;
- fnd = 1;
- }
- else if((p[min].pr>p[j].pr)||(p[min].pr==p[j].pr && p[min].at>p[j].at))
- {
- min = j;
- }
- }
- }
- if(idle==0 && fnd==0)
- {
- strcpy(d[num].name,"Idle");
- d[num].st = i;
- i++;
- idle = 1;
- }
- else if(fnd==1)
- {
- if(idle==1)
- {
- d[num].ct = i;
- num++;
- idle = 0;
- }
- strcpy(d[num].name,p[min].name);
- p[min].status =1;
- d[num].st = i;
- d[num].ct = i + p[min].bt;
- i = d[num].ct;
- p[min].ct = d[num].ct;
- p[min].tt = p[min].ct - p[min].at;
- p[min].wt = p[min].tt - p[min].bt;
- num++;
- ls++;
- }
- else
- {
- i++;
- }
- }
- }
- void round_robin(){
- int i,j,k,ls,t;
- printf("\nENTER THE TIME QUANTUM : ");
- scanf("%d",&t);
- for(i=0,num=0,ls=0;ls<n;)
- {
- for(j=0;j<n;j++)
- {
- if(p[j].status==0 && p[j].at<=i)
- {
- enqueue(j);
- p[j].status = 1;
- }
- }
- if(idle==0 && front == -1)
- {
- strcpy(d[num].name,"Idle");
- d[num].st = i;
- idle = 1;
- i++;
- }
- else if(front!=-1)
- {
- if(idle==1)
- {
- d[num].ct = i;
- idle = 0;
- num++;
- }
- k = dequeue();
- d[num].st = i;
- strcpy(d[num].name,p[k].name);
- if(p[k].left<=t)
- {
- d[num].ct = i+p[k].left;
- p[k].ct = d[num].ct;
- i = d[num].ct;
- p[k].tt = i - p[k].at;
- p[k].wt = p[k].tt - p[k].bt;
- p[k].status = 2;
- ls++;
- num++;
- }
- else if(p[k].left>t)
- {
- d[num].ct = i+t;
- i = d[num].ct;
- p[k].left = p[k].left-t;
- num++;
- for(j=0;j<n;j++)
- {
- if(p[j].status==0 && p[j].at<=i)
- {
- enqueue(j);
- p[j].status = 1;
- }
- }
- enqueue(k);
- }
- }
- else
- {
- i++;
- }
- }
- }
- void print(){
- printf("\n==============================================================================================================");
- printf("\n| NAME\t\t| ARRIVAL\t| BURST\t\t| Priority\t| COMPLETION\t| WAITING\t| TURNAROUND |\n");
- printf("--------------------------------------------------------------------------------------------------------------\n");
- for(int i=0;i<n;i++)
- {
- printf("| %-4s\t\t| %-7d\t| %-5d\t\t| %-8d\t| %-10d\t| %-7d\t| %-10d |\n",p[i].name,p[i].at,p[i].bt,p[i].pr,p[i].ct,p[i].wt,p[i].tt);
- average_wt+=p[i].wt;
- average_tt+=p[i].tt;
- }
- printf("==============================================================================================================\n");
- printf("\n\nGANTT CHART ");
- printf("\n\t--------------------------------------------------------------------------\n\t");
- for(int i=0;i<num;i++)
- {
- printf("|");
- printf("%s\t",d[i].name);
- }
- printf(" |");
- printf("\n\t--------------------------------------------------------------------------\n\t");
- for(int i=0;i<num;i++)
- {
- printf("%d\t",d[i].st);
- }
- printf("%d\t",d[num-1].ct);
- printf("\n\nAVERAGE WAITING TIME : %f",(average_wt/n));
- printf("\nAVERAGE TURNAROUND TIME : %f\n",(average_tt/n));
- }
- int main()
- {
- int arg1,arg2;
- while(1){
- printf("Enter your choice:\n");
- printf("1. Non-Preemptive\n2. Preemptive\n0.Exit\n");
- scanf("%d",&arg1);
- switch(arg1){
- case 1:
- {
- printf("Enter your choice:\n");
- printf("1. FCFS\n2. SJF\n3. Priority\n");
- scanf("%d",&arg2);
- switch(arg2){
- case 1://FCFS
- {
- read();
- sort();
- FCFS();
- print();
- break;
- }
- case 2://SJF
- {
- read();
- NP_SJF();
- print();
- break;
- }
- case 3://Priority
- {
- read();
- NP_Priority();
- print();
- break;
- }
- }
- break;
- }
- case 2: {
- printf("Enter your choice:\n");
- printf("1. Round-Robin\n");
- scanf("%d", &arg2);
- switch (arg2) {
- case 1: //Round-Robin
- {
- read();
- round_robin();
- print();
- break;
- }
- }
- break;
- }
- case 0:
- exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement