0% found this document useful (0 votes)
30 views18 pages

All One File

The program simulates the round robin CPU scheduling algorithm. It takes the burst time of processes as input, allocates a time slice, and calculates waiting time and turnaround time of each process. The average waiting time is also calculated.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views18 pages

All One File

The program simulates the round robin CPU scheduling algorithm. It takes the burst time of processes as input, allocates a time slice, and calculates waiting time and turnaround time of each process. The average waiting time is also calculated.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Aim: Simulate MFT(memory fixed type partition) Description: Memory management CPU runs program instructions only when

n program is in memory. Programs do I/O sometimes IMPLY CPU wasted. Solution : Multiprogramming Multiple programs share the memory One program at a time gets CPU Simultaneous resource possession Better performance Multiple Programming with Fixed Number of Tasks (MFT): IBM in their Mainframe Operating system OS/MFT implements the MFT concept. OS/MFT uses fixed partitioning concept to load programs into main memory. Fixed Partitioning: In fixed partitioning concept, RAM is divided into set of fixed partitions of equal size. Programs having the size less than the partition size are loaded into memory Programs having size more than the size of partition size is rejected The program having the size less than the partition size will lead to internal fragmentation If all partitions are allocated and if a new program is to be loaded, the program that leads to maximum internal fragmentation can be replaced. Program : #include<stdio.h> main() { int n,i,p[4],r,bs,np; printf("enter the buffer size"); scanf("%d",&n); if(n<=0) { printf("memory is not allocated"); exit(0); } printf("\nnumber of process:");

scanf("%d",&np); printf("\nenter the process size:"); for(i=1;i<=np;i++) scanf("%d",&p[i]); printf("\n enter the block size:"); scanf("%d",&bs); n=bs; printf("\n processn\t block size \t process time \t space"); for(i=1;i<=np;i++) { r=n-p[i]; printf("\n %d\t %d \t\t%d \t\t%d",i,n,p[i],r); } getch(); } Output : enter the buffer size 100 number of process: 3 enter the process size:30 25 30 enter the block size:30 processn block size process time 1 30 30 2 30 25 3 30 20 Manual calculation: Buffer size=100 Number of processes =3 Size of process 1=30 Size of process 2=25 Size of process 3=20 Size of each block is 30 Remaining space for process 1=30-30=0 Remaining space for process 2=30-25=5 Remaining space for process 3=30-20=10

space 0 5 10

Aim: program to simulate memory variable type partitioning Description: Description :Memory management CPU runs program instructions only when program is in memory. Programs do I/O sometimes IMPLY CPU wasted. Solution : Multiprogramming Multiple programs share the memory One program at a time gets CPU Simultaneous resource possession Better performance Multi-programming with variable number of tasks (MVT): IBM in their Mainframe Operating system OS/MVT implements the MVT concept. OS/MVT uses dynamic partitioning concept to load programs into main memory. Dynamic Partitioning: Initially RAM is portioned according to the size of programs to be loaded into Memory till such time that no other program can be loaded. The left over memory is called a hole which is too small to fit any process When a new program is to be loaded into memory look for the partition, which leads to least external fragmentation and load the program. The space that is not used in a partition is called as external fragmentation Program: #include<stdio.h> main() { int mem,pro[10],n,i,total=0; printf("\nenter the memory size:"); scanf("%d",&mem); if(mem<=0) { printf("\nthe memory is not required"); exit(0); } else { printf("\nenter the number of process:"); scanf("%d",&n); printf("\nenter the process time:"); for(i=1;i<=n;i++) { scanf("%d",&pro[i]); }

for(i=1;i<=n;i++) total=total+pro[i]; printf("\nthe total memory is %d",total); if(mem==total) printf("\nthe memory is completely used"); else { if(mem>total) printf("\nthe remeaning memory is %d",mem-total); else printf("\n the required memory is %d",total-mem); } } getch(); } Output: enter the memory size:100 enter the number of process:3 enter the process time:30 25 30 the total memory is 85 the remeaning memory is 15

manual calculations: buffer size= 100 number of processes= 3 time of process 1=25 time of process 2=30 time of process 3=30 Total space is 25+30+30=85 Remaning space is 100-85=15

Aim: write a program which implements priority scheduling algorithm.


Description: In priority scheduling algorithm each process will be having a priority value along with its Burst time. Therefore ach process will be allocated the CPU time in consideration with their priority value. If the priority value is same for two processes, then the FCFS method will be implemented. Burst time: The time at which a process is completed. Every process will be having a burst time and a waiting time. Waiting time: The time spent by a process in the ready queue. For every process there will be a Turn around Time. Turn Around Time: The time interval between process submission and completion. The first process will be having the submission time 0and its completion time. The submission time for the second process will be starting from the completion time of the first process. In this way the time interval for each process, will be added and finally gives the Turn around Time. Then the average Turn around Time is calculated i.e., Turn Around time/number of processes, which is the average completion time of a process. In order to calculate the waiting time, burst time, Turn around time, we have to build a Gantt chart in which the processes are arranged according to their priority values. The main disadvantage with this method is the process with the least priority value will not be given the CPU time easily. It has to wait for along time. This phenomenon is called Starvation. To overcome this, the priority of the processes will be increasing with their waiting time in the queue, so they will be allocated the CPU time. This phenomenon is called Aging.

Program: #include<stdio.h> #include<conio.h> main() { int p[10], w[10], b[10],i,j,n,temp,add,tat,sum; float avg; clrscr(); printf(enter the number of jobs:); scanf(%d,&n); printf(\n enter the burst time:); for(i=0; i<n; i++) scanf(%d,&b[i]); printf(enter the priority order :); for(i=0; i<n; i++) scanf(%d,&p[i]);

for(i=0; i<n; i++) { for(j=j+1; j<n; j++) { if(p[i]>p[j]) { temp=b[i]; b[i]=b[j]; b[j]=temp; } } } printf(\n priority order is:); for(i=0; i<n; i++) { printf(\t %d,b[i]); } w[0]=0; for(i=0; i<n; i++) { w[i+1]=w[i]+b[i]; printf(waiting time %d,w[i]); } sum=0; for(i=0; i<n; i++) { sum=sum+b[i]; } printf(\n sum of burst time %d,sum); add=0; for(i=0; i<n; i++) { add=add+w[i]; } printf(\n total turn around time); tat=add+sum; printf(%d,tat); printf(\n avg tat is %d,tat/n); getch(); } output : enter number of jobs 3 enter the burst time: 5 6 2

enter the priority order: 1 2 4 priority order: 5 6 2 1 2 3 waiting time: 0 waiting time: 5 waiting time: 11

sum of burst times : 13 total turn around time: 29 avg tat: 9 Manual calculation : no of jobs = 3 process p1 p2 p3 burst time 5 6 2 priority order 1 2 4

Gantt chart: p1(5) 0 5 sum of burst times=0+5+11+13 = 29 total turn around time=(0+5)+(5+6)+(11+2) =5+11+13 =29 avg of turn around time=29/3 =9.6 =~9 p2(6) 11 P3(2) 13

Aim : To write a program to simulate round robin cpu scheduling algorithm Description: Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating system. As the term is generally used, time slices are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive).Round-robin scheduling is simple, easy to implement, and starvation-free. Round-robin scheduling can also be applied to other scheduling problems, such as data packet scheduling in computer networks. In order to schedule processes fairly, a round-robin scheduler generally employs time-sharing, giving each job a time slot or quantum[1] (its allowance of CPU time), and interrupting the job if it is not completed by then. The job is resumed next time a time slot is assigned to that process. In the absence of time-sharing, or if the quanta were large relative to the sizes of the jobs, a process that produced large jobs would be favoured over other processes. Program: #include<stdio.h> #include<conio.h> int z[10],b[10],n,m[50],r,q,e=0,avg=0,i,j; float f; main() { printf("\n\tJOB SCHEDULING ALGORITHM[RR]"); printf("\n\t*******************************************************\n"); printf("\nEnter how many jobs:"); scanf("%d",&n); printf("\nEnter burst time for corresponding job...\n");

for(i=1;i<=n;i++) { printf("\nProcess %d: ",i); scanf("%d",&b[i]); z[i]=b[i]; } printf("\nENTER THE TIME SLICE VALUE:"); scanf("%d",&q); rr(); average(); getch(); return 0; } rr() { int max=0; max=b[1]; for(j=1;j<=n;j++) if(max<=b[j]) max=b[j]; if((max%q)==0) r=(max/q); else r=(max/q)+1; for(i=1;i<=r;i++) { printf("\nround %d",i); for(j=1;j<=n;j++) { if(b[j]>0) { b[j]=b[j]-q; if(b[j]<=0) { b[j]=0; printf("\nprocess %d is completed",j);

} else printf("\nprocess %d remaining time is %d",j,b[j]); } } } return 0; } average() { for(i=1;i<=n;i++) { e=0; for(j=1;j<=r;j++) { if(z[i]!=0) { if(z[i]>=q) { m[i+e]=q; z[i]-=q; } else { m[i+e]=z[i]; z[i]=0; } } else m[i+e]=0; e=e+n; } } for(i=2;i<=n;i++) for(j=1;j<=i-1;j++) avg=avg+m[j]; for(i=n+1;i<=r*n;i++)

{ if(m[i]!=0) { for(j=i-(n-1);j<=i-1;j++) avg=m[j]+avg; } } f=avg/n; printf("\nTOTAL WATING:%d",avg); printf("\n\nAVERAGE WAITING TIME:%f\n",f); for(i=1;i<=r*n;i++) { if(m[i]!=0) if(i%n==0){ printf("P%d",(i%n)+(n)); } else printf("P%d",(i%n)); for(j=1;j<=m[i];j++) printf("%c",22); } printf("\n"); getch(); return 0; }

Output: JOB SCHEDULING ALGORITHM[RR] ******************************************************* Enter how many jobs:3 Enter burst time for corresponding job... Process 1: 10 Process 2: 5 Process 3: 5

ENTER THE TIME SLICE VALUE:5 round 1 process 1 remaining time is 5 process 2 is completed process 3 is completed round 2 process 1 is completed TOTAL WATING:25 AVERAGE WAITING TIME:8.000000 P1P2P3P1 Manual calculations: Process p1=10 Process p2=5 Process p3=5

P1 0 5

p2 10

p3 15

p1 20

Waiting time of p1=0+10=10 Waiting time of p2=5 Waiting time of p3=10 Total waiting time=10+5+10=25 Average waiting time =25/3=8.333

Aim: Implement Shortest Job First (SJF) scheduling algorithm.


Description: The Shortest Job First (SJF) CPU scheduling algorithm is which it allocates the CPU time for the process which is having the least Burst time. Burst time: The time at which a process is completed. Every process will be having a burst time and a waiting time. Waiting time: The time spent by a process in the ready queue. For every process there will be a Turn Around Time. Turn Around Time: The time interval between process submission and completion. The first process will be having the submission time 0and its completion time. The submission time for the second process will be starting from the completion time of the first process. In this way the time interval for each process, will be added and finally gives the Turn around Time. Then the average Turn around Time is calculated i.e., Turn Around time/number of processes, which is the average completion time of a process. In order to calculate the waiting time, burst time, Turn around time, we have to build a Gantt chart in which the processes are arranged according to the least burst time.

Program: #include<stdio.h> void main() { int a[10],b[10],w[10],i,j,n; int t,sum=0; float avg; printf("enter the no.of jobs"); scanf("%d",&n); printf("enter the burst time"); for(i=0;i<n;i++); { scanf("%d",&b[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(b[i]>b[j]) { t=b[i];

b[i]=b[j]; b[j]=t; } } }} w[0]=0; printf("waiting time"); for(i=0;i<n;i++) { w[i+1]=w[i]+b[i]; sum=sum+w[i]; printf("%d",w[i+1]); } printf("burst time is %d",w[i]); printf("total waiting time is %d",sum); printf("total turn around time is %d",w[i]+sum); avg=(sum+w[i])/n; printf("avg time is %d",avg); getch(); } output: enter number of jobs 4 enter the burst time: 38 6 6 5 38 6 6 5 after sorting 5 6 6 38 waiting time: 5111755 total burst time: 55 total waiting time: 33 turn around time: 88 average turn around time: 22.000000

Manual calculation: process p1 p2 p3 p4 sorted order: p4, p2, p3, p1. burst time 38 6 6 5

Gantt chart: p4(5) 0 5 p2(6) 11 p3(6) 17 p1(38) 55

total waiting time=0+5+11+17 =33 average=33/4 =8.2 turn around time =(0+5)+(5+6)+(11+6)+(17+38) =88 average=88/4 =22

Aim: simulate First Come First Served(FCFS) CPU scheduling algorithm.


Description: The First Come First Served (FCFS) CPU scheduling algorithm is which it allocates the CPU time for the process which arrives first i.e., the process with the highest burst time will be allocated the CPU time. Burst time: The time at which a process is completed. Every process will be having a burst time and a waiting time. Waiting time: The time spent by a process in the ready queue. For every process there will be a Turn Around Time. Turn Around Time: The time interval between process submission and completion. The first process will be having the submission time 0and its completion time. The submission time for the second process will be starting from the completion time of the first process. In this way the time interval for each process, will be added and finally gives the Turn Around Time. Then the average Turn around Time is calculated i.e., Turn Around time/number of processes, which is the average completion time of a process. In order to calculate the Waiting time, Burst time, Turn around Time, we have to build a Gantt chart, in which the processes are arranged according to their burst times.

program: #include<stdio.h> main() { int wt,p1,p2,p3,p4,w1=0,w2,w3,w4,n,t1,avg; printf("enter the n value"); scanf("%d",&n); printf("enter the values of p1,p2,p3,p4:"); scanf("%d%d%d%d",&p1,&p2,&p3,&p4); w2=w1+p1; w3=w2+p2;

w4=w3+p3; wt=w4; t1=(w1+p1)+(w2+p2)+(w3+p3)+(w4+p4); avg=t1/n; printf("turn around time is:%d \n",t1); printf("average is %d",avg); getch(); }

output: enter the n value:4 enter the values of p1,p2,p3,p4: 38 6 6 5 turn around time is: 187 average is: 46.

Manual Calculation: process p1 p2 p3 p4 Gantt chart: p1(38) 0 55 38 p2(6) 44 p3(6) 50 p4(5) burst time 38 6 6 5

Total waiting time = 0+38+44+50 = 38+94 average = 132/4 = 33 turn around time = (0+38)+(38+6)+(44+6)+(50+5) = 187 average = 187/4 =46.75

You might also like