Nama: Dhita Pratiwi Nim:102406030 Kelas: Kom B
Nama: Dhita Pratiwi Nim:102406030 Kelas: Kom B
Nama: Dhita Pratiwi Nim:102406030 Kelas: Kom B
: Dhita Pratiwi
Nim
:102406030
Kelas
: Kom B
PREEMPTIVE
#include<stdio.h>
#define true 1
#define false 0
int n,tq,totwt=0,tottrnd=0;
struct pr
{
int srvst,wt;
// int wt;
int trndt;
int flag;
int temp;
}prc[10];
void printpr()
{
printf("\nProcess_id\tServicetime\tWaitingtime\tTurnarndtime\n");int
i;
for(i=0;i<n;i++)
{
printf("\n%d \t\t%d \t\t%d \t\t
%d\t\n",i,prc[i].srvst,prc[i].wt,prc[i].trndt);
}
printf("Average waiting time=%f\nAverage turnaroundtime=
%f\n\n",(float)totwt/n,(float)tottrnd/n);
} void rschedule() {
int trnd=0,i=0,t1;
while(completed()==false) {
if(prc[i].flag==false)
{
if(prc[i].temp==0||prc[i].temp<=tq) {
prc[i].flag=true;
trnd+=prc[i].temp;
tottrnd+=prc[i].trndt=trnd;
prc[i].temp=0;
} else {
trnd+=tq;
prc[i].temp-=tq;
}}
i=(i+1)%n;
}}
int completed()
{
int sum=0,i;
for(i=0;i<n;i++)
{
if(prc[i].flag==true)
sum++;
}
if(sum==n)
return true;
return false;
}
main()
{
int i;
printf("\n\t\t<<<ROUND ROBIN SCHEDULING>>>\nEnter the
timequantum: ");
scanf("%d",&tq);
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
//printf("\nEnter the details for process %d:\nService time:",i);
printf("\n Enter process %d Service time:",i);
scanf("%d",&prc[i].srvst);
prc[i].flag=false;
prc[i].temp=prc[i].srvst;
}
prc[0].wt=0;int wtprmtr=0;
for(i=0;i<n-1;i++)
{
if(prc[i].srvst<tq)
wtprmtr+=prc[i].srvst;
else
wtprmtr+=tq;
prc[i+1].wt=wtprmtr;
totwt+=prc[i+1].wt;
}
rschedule();
printpr();
system("pause");
return(0);
}
<stdio.h>
<stdlib.h>
<time.h>
<math.h>
plist = plist->next;
free(ptmp);
};
return(0);
};
/* Process list entry initialization routine */
struct process *init_process (int pid, int burst, int wait) {
struct process *proc;
proc = malloc(sizeof(struct process));
if (proc == NULL) {
printf("Fatal error: memory allocation
failure.\nTerminating.\n");
exit(1);
};
proc->pid = pid;
proc->burst = burst;
proc->wait = wait;
proc->next = NULL;
return(proc);
};
/* Process listing */
void listprocs (struct process *proc) {
struct process *tmp = proc;
printf("\t\t Process Listing");
printf("\n\t\t ~~~~~~~~~~~~~~~\n");
//printf(" -----------------------------------\n");
//
printf(" | |");
printf(" \n \tPID \t Burst \t Wait \n");
printf(" \t---\t -----\t ----- \n");
while (tmp != NULL) {
total_wait = tot;
tot += tmp->wait;
avg_wait = (float) total_wait/4;
printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround:
%d\n", tmp->pid, time, start, end);
beforeshortest->next = tmp->next;
free(tmp);
};
};
printf("\nTotal wait : %d\n\n", total_wait);
printf("\nAverage wait : %.2f\n\n", avg_wait);
system ("pause");
return(0);
//printf("\tShortest Job First scheduling simulation\n\n");
};