Sunteți pe pagina 1din 8

Nama

: 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);
}

Program NON PREEMPTIVE


#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<time.h>
<math.h>

/* Process Data Structure */


struct process {
int pid; /* Process ID */
int burst; /* CPU Burst Time */
int wait; /* Wait Time*/
struct process *next;
};
/* Function Prototype Declarations */
struct process *init_process (int pid, int burst, int wait);
void listprocs (struct process *proc);
void sjf (struct process *proc);
/* Main Program Segment */
int main (void) {
/* Initialize process list */
struct process *plist, *ptmp;
plist = init_process(1, 4, 1);
plist->next = init_process(2, 3, 0); ptmp = plist->next;
ptmp->next = init_process(3, 6, 3); ptmp = ptmp->next;
ptmp->next = init_process(4, 7, 2);
ptmp->next = init_process(5, 2, 6);
/* Perform simulations */
listprocs(plist);
sjf(plist);
/* Terminate cleanly */
while (plist != NULL) {
ptmp = plist;

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) {

printf(" \t%d\t %d \t %d \n", tmp->pid, tmp->burst, tmp>wait);


tmp = tmp->next;
};
//printf(" -----------------------------------\n");
// printf("END:\tProcess Listing\n\n");
};
/* Shortest Job First scheduling simulation */
void sjf (struct process *proc) {
int time, start, end, shortest;
int total_wait, tot;
float avg_wait;
struct process *copy, *tmpsrc, *tmp, *beforeshortest;
printf("\n\n\tShortest Job First scheduling simulation\n\n");
/* Duplicate process list */
tmpsrc = proc;
copy = tmp = NULL;
while (tmpsrc != NULL) {
if (copy == NULL) {
copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc>wait);
tmp = copy;
} else {
tmp->next = init_process(tmpsrc->pid, tmpsrc->burst,
tmpsrc->wait);
tmp = tmp->next;
};
tmpsrc = tmpsrc->next;
};
/* Main routine */
time = 0;
while (copy != NULL) {

/* Find the next job */


beforeshortest = NULL;
shortest = copy->burst;
tmp = copy->next;
tmpsrc = copy;
while (tmp != NULL) {
if (tmp->burst < shortest) {
shortest = tmp->burst;
beforeshortest = tmpsrc;
};
tmpsrc = tmp;
tmp = tmp->next;
};
/* Process job and remove from copy of process list */
if (beforeshortest == NULL) {
/* Handle first job is shortest case */
start = time;
time += copy->burst;
end = time;
total_wait = tot;
tot += copy->wait;
avg_wait = (float) total_wait/4;
printf("Process: %d\tEnd Time: %d\tWaiting: %d\tTurnaround:
%d\n", copy->pid, time, start, end);
tmpsrc = copy;
copy = copy->next;
free(tmpsrc);
} else {
/* Handle first job is not shortest case */
tmp = beforeshortest->next;
start = time;
time += tmp->burst;
end = time;

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");
};

S-ar putea să vă placă și