1) Simulate the following CPU scheduling algorithms a) FCFS b) SJF c) Priority d) Round Robin a) FCFS: AIM: A program to simulate

the FCFS CPU scheduling algorithm
PROGRAM:
#include<stdio.h> #include<conio.h> void main() { `char pn[10][10]; int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n; int totwt=0,tottat=0; clrscr(); printf("Enter the number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the Process Name, Arrival Time & Burst Time:"); scanf("%s%d%d",&pn[i],&arr[i],&bur[i]); } for(i=0;i<n;i++) { if(i==0) { star[i]=arr[i]; wt[i]=star[i]-arr[i]; finish[i]=star[i]+bur[i]; tat[i]=finish[i]-arr[i]; } else { star[i]=finish[i-1]; wt[i]=star[i]-arr[i]; finish[i]=star[i]+bur[i]; tat[i]=finish[i]-arr[i]; } } printf("\nPName Arrtime Burtime Start TAT Finish");

for(i=0.finish[i]).i++) { printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d".i<n. Arrival Time & Burst Time: 2 5 6 Enter the Process Name.(float)tottat/n).pn[i]. } OUTPUT: Input: Enter the number of processes: 3 Enter the Process Name.arr[i]. tottat+=tat[i]. Arrival Time & Burst Time: 3 6 7 Output: PName Arrtime Burtime Srart TAT Finish 1 2 3 2 3 5 2 5 6 5 6 4 3 6 7 6 7 10 Average Waiting Time: 3.tat[i].000 .(float)totwt/n).bur[i]. getch(). Arrival Time & Burst Time: 1 2 3 Enter the Process Name. } printf("\nAverage Waiting time:%f". printf("\nAverage Turn Around Time:%f".333 Average Turn Around Time: 7.star[i]. totwt+=wt[i].

i<n.ata.j. clrscr(). char pn[10][10]. scanf("%d".j++) { if(et[i]<et[j]) { temp=at[i].&et[i]).i++) { printf("Enter process name.pn[j]). at[i]=at[j].h> #include<string. int totwt=0.totta=0.&n). float awt. } for(i=0. arrival time & execution time:"). flushall(). } } for(i=0. strcpy(pn[i]. at[j]=temp.i++) for(j=0.ta[10].t).i<n.n.wt[10].b) SJF: AIM: A program to simulate the SJF CPU scheduling algorithm PROGRAM: #include<stdio.ft[10].temp.h> #include<conio. strcpy(t.t[10].i<n. et[i]=et[j]. et[j]=temp.&at[i].pn[i]. temp=et[i].pn[i]).i.j<n. . scanf("%s%d%d".i++) { if(i==0) st[i]=at[i]. printf("Enter the number of process:").at[10].st[10]. for(i=0. strcpy(pn[j].h> void main() { int et[20]. else st[i]=ft[i-1].

i++) printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d". printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime"). for(i=0.at[i]. } awt=(float)totwt/n.ta[i]).i<n. printf("\nAverage waiting time is:%f".awt). ta[i]=ft[i]-at[i].3333 .wt[i]=st[i]-at[i]. totwt+=wt[i]. ata=(float)totta/n. Arrival Time & Burst Time: 2 5 15 Enter the Process Name.ata). printf("\nAverage turnaroundtime is:%f". Arrival Time & Burst Time: 1 4 6 Enter the Process Name. ft[i]=st[i]+et[i]. Arrival Time & Burst Time: 3 6 11 Output: Pname arrivaltime executiontime waitingtime tatime 1 4 6 0 6 3 6 11 4 15 2 5 15 16 31 Average Waiting Time: 6.et[i].pn[i]. } OUTPUT: Input: Enter the number of processes: 3 Enter the Process Name.wt[i]. getch(). totta+=ta[i].6667 Average Turn Around Time: 17.

&n).i<n.i<n.pn[i]. p[i]=p[j]. temp=et[i]. strcpy(pn[j].j<n.n.i++) { . strcpy(t. strcpy(pn[i]. scanf("%s%d%d%d".t[10].execution time & priority:").wt[10].i. temp=at[i].p[10].totta=0. float awt. clrscr().c) Priority: AIM: A program to simulate the priority CPU scheduling algorithm PROGRAM: #include<stdio.i++) for(j=0. int totwt=0.temp. for(i=0.h> #include<string.t).ft[10]. et[j]=temp. } } for(i=0.pn[j]).i++) { printf("Enter process name.arrivaltime. p[j]=temp. et[i]=et[j]. } for(i=0.st[10]. at[i]=at[j].&et[i].ta[10].h> #include<conio.at[10].j. scanf("%d". flushall(). at[j]=temp.h> void main() { int et[20].&at[i]. char pn[10][10].i<n.pn[i]).j++) { if(p[i]<p[j]) { temp=p[i].&p[i]).ata. printf("Enter the number of process:").

execution time & priority: 1 2 3 1 Enter the Process Name.awt). ta[i]=ft[i]-at[i]. } totwt+=wt[i].i<n. printf("\nAverage waiting time is:%f".p[i]. execution time & priority: 2 4 5 2 Enter the Process Name. ta[i]=ft[i]-at[i]. printf("\nAverage turnaroundtime is:%f".6667 .et[i]. Arrival Time.ata). ata=(float)totta/n. execution time & priority: 3 5 6 3 Output: Pname arrivaltime executiontime priority waitingtime 1 2 3 1 0 2 4 5 2 1 3 5 6 3 5 tatime 3 6 11 Average Waiting Time: 2.if(i==0) { st[i]=at[i].at[i]. } else { st[i]=ft[i-1]. Arrival Time. wt[i]=st[i]-at[i]. } awt=(float)totwt/n. totta+=ta[i]. getch(). Arrival Time.wt[i]. ft[i]=st[i]+et[i]. ft[i]=st[i]+et[i].i++) printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d". } OUTPUT: Input: Enter the number of processes: 3 Enter the Process Name.pn[i].0000 Average Turn Around Time: 6. for(i=0. printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime"). wt[i]=st[i]-at[i].ta[i]).

pn[i].i++) tot=tot+et[i].et[i]). et[i]=0.pn[i]. printf("Enter the no of processes:").x=0. for(i=0. } else if((et[i]<=ts)&&et[i]!=0) { x=x+et[i]. for(i=0. et[i]=et[i]-ts.i++) printf("process %d: %s\n". scanf("%s %d". clrscr(). while(x!=tot) { for(i=0.i<n.&n).h> #include<conio.i. for(i=0.i++) { if(et[i]>ts) { x=x+ts.&et[i]).&ts).i<n.pn[i]).i<n.n.h> void main() { int et[30].i<n. } printf("The processes are:").tot=0. printf("\n %s -> %d". scanf("%d".} } .i+1.ts.ts).d) Round Robin: AIM: A program to simulate the Round Robin CPU scheduling algorithm PROGRAM: #include<stdio.pn[i]. char pn[10][10]. printf("\n %s -> %d". printf("Enter the time quantum:"). scanf("%d".i++) { printf("enter process name & estimated time:").

pid_t pid.} printf("\n Total Estimated Time:%d".n. } OUTPUT: Input: Enter the no of processes: 2 Enter the time quantum: 3 Enter the process name & estimated time: p1 12 Enter the process name & estimated time: p2 15 Output: p1 -> 3 p2 -> 3 p1 -> 3 p2 -> 3 p1 -> 3 p2 -> 3 p1 -> 3 p2 -> 3 p2 -> 3 Total Estimated Time: 27 Write a C program that illustrates the creation of child process using fork system call. One process finds sum of even series and other process finds sum of odd series .sum=0.h> int main() { int i.&n) pid=fork(). Source Code :: ========== #include <stdio.h> #include <fcntl.h> #include <sys/types. getch().h> #include <unistd. system(“clear”).x). scanf(“%d”. if(pid==0) . printf(“Enter n value:”).

sum+=i.i+=2) { printf(“%d\”.i<n. } printf(“Even sum:%d\n”. } else { printf(“From process\n”). } } .sum).i). for(i=1.{ printf(“From child process\n”).i+=2) { printf(“%d\”.i).sum). sum+=i. } printf(“Odd sum:%d\n”. for(i=0.i<n.

Sign up to vote on this title
UsefulNot useful