Question: Convert C code to Java code : THE PROGRAMS HAVE TO BE WRITTEN in JAVA, the C code is only to give you an idea

Convert C code to Java code:

THE PROGRAMS HAVE TO BE WRITTEN in JAVA, the C code is only to give you an "idea" of how to do. So the 3 codes have to be wrtitten in Java. Thank you.

NAME OF THE EXPERIMENT: Simulate the following CPU Scheduling Algorithms

a) FCFS b) SJF c) Round Robin --> Task for each: Write the code using java (a, b, c).

AIM: Using CPU scheduling algorithms find the min & max waiting time.

SOFTWARE REQUIREMENTS:

Java

THEORY:

CPU SCHEDULING

Maximum CPU utilization obtained with multiprogramming

CPUI/O Burst Cycle Process execution consists of a cycle of

CPU execution and I/O wait

CPU burst distribution

Experiment: 2 a)

First-Come, First-Served (FCFS) Scheduling

Process Burst Time

P1 24

P2 3

P3 3

Suppose that the processes arrive in the order: P1 , P2 , P3

The Gantt Chart for the schedule is:

Convert C code to Java code: THE PROGRAMS HAVE TO BE WRITTEN

ALGORITHM 1. Start 2. Declare the array size 3. Read the number of processes to be inserted 4. Read the Burst times of processes 5. calculate the waiting time of each process wt[i+1]=bt[i]+wt[i] 6. calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1] 7. Calculate the average waiting time and average turnaround time. 8. Display the values 9. Stop

PROGRAM: #include #include void main() { int i,j,bt[10],n,wt[10],tt[10],w1=0,t1=0; float aw,at; clrscr(); printf("enter no. of processes: "); scanf("%d",&n); printf("enter the burst time of processes:"); for(i=0;i scanf("%d",&bt[i]); for(i=0;i { wt[0]=0; tt[0]=bt[0]; wt[i+1]=bt[i]+wt[i]; tt[i+1]=tt[i]+bt[i+1]; w1=w1+wt[i]; t1=t1+tt[i]; } aw=w1; at=t1; printf(" bt\t wt\t tt "); for(i=0;i printf("%d\t %d\t %d ",bt[i],wt[i],tt[i]); printf("aw=%f ,at=%f ",aw,at); getch(); }

INPUT Enter no of processes 3 enter bursttime 12 8 20

EXPECTED OUTPUT bt wt tt 12 0 12 8 12 20 20 20 40 aw=10.666670 at=24.00000

EXPERIMENT : 2 b) NAME OF THE EXPERIMENT: Simulate the following CPU Scheduling Algorithms

b) SJF AIM: Using CPU scheduling algorithms find the min & max waiting time.

SOFTWARE REQUIREMENTS: Java

THEORY: Example of Non Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 3.0 4

in JAVA, the C code is only to give you an "idea"

Example of Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 3.0 4

of how to do. So the 3 codes have to be wrtitten

ALGORITHM 1. Start 2. Declare the array size 3. Read the number of processes to be inserted 4. Read the Burst times of processes 5. sort the Burst times in ascending order and process with shortest burst time is first executed. 6. calculate the waiting time of each process wt[i+1]=bt[i]+wt[i] 7. calculate the turnaround time of each process tt[i+1]=tt[i]+bt[i+1] 8. Calculate the average waiting time and average turnaround time. 9. Display the values 10. Stop

PROGRAM: #include #include void main() { int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0; float aw,at; clrscr(); printf("enter no. of processes: "); scanf("%d",&n); printf("enter the burst time of processes:"); for(i=0;i scanf("%d",&bt[i]); for(i=0;i { for(j=i;j if(bt[i]>bt[j]) { t=bt[i]; bt[i]=bt[j]; bt[j]=t; } } for(i=0;i printf("%d",bt[i]); for(i=0;i { wt[0]=0; tt[0]=bt[0]; wt[i+1]=bt[i]+wt[i]; tt[i+1]=tt[i]+bt[i+1]; w1=w1+wt[i]; t1=t1+tt[i]; } aw=w1; at=t1; printf(" bt\t wt\t tt "); for(i=0;i printf("%d\t %d\t %d ",bt[i],wt[i],tt[i]); printf("aw=%f ,at=%f ",aw,at); getch(); }

INPUT: enter no of processes 3 enter burst time 12 8 20

OUTPUT: bt wt tt 12 8 20 8 0 8 20 20 40 aw=9.33 at=22.64

EXPERIMENT : 2c)

NAME OF THE EXPERIMENT: Simulate the following CPU Scheduling Algorithms

c) Round Robin

AIM: Using CPU scheduling algorithms find the min & max waiting time.

SOFTWARE REQUIREMENTS:

Java

THEORY:

Round Robin:

Example of RR with time quantum=3

Process Burst time
aaa 4
Bbb 3
Ccc 2
Ddd 5
Eee 1

ALGORITHM 1. Start 2. Declare the array size 3. Read the number of processes to be inserted 4. Read the burst times of the processes 5. Read the Time Quantum 6. if the burst time of a process is greater than time Quantum then subtract time quantum form the burst time Else Assign the burst time to time quantum. 7.calculate the average waiting time and turn around time of the processes. 8. Display the values 9. Stop PROGRAM: #include #include void main() { int st[10],bt[10],wt[10],tat[10],n,tq; int i,count=0,swt=0,stat=0,temp,sq=0; float awt=0.0,atat=0.0; clrscr(); printf("Enter number of processes:"); scanf("%d",&n); printf("Enter burst time for sequences:"); for(i=0;i { scanf("%d",&bt[i]); st[i]=bt[i]; } printf("Enter time quantum:"); scanf("%d",&tq); while(1) { for(i=0,count=0;i { temp=tq; if(st[i]==0) { count++; continue; } if(st[i]>tq) st[i]=st[i]-tq; else if(st[i]>=0) { temp=st[i]; st[i]=0; } sq=sq+temp; tat[i]=sq; } if(n==count) break; } for(i=0;i { wt[i]=tat[i]-bt[i]; swt=swt+wt[i]; stat=stat+tat[i]; } awt=(float)swt; atat=(float)stat; printf("Process_no Burst time Wait time Turn around time"); for(i=0;i printf(" %d\t %d\t %d\t %d",i+1,bt[i],wt[i],tat[i]); printf(" Avg wait time is %f Avg turn around time is %f",awt,atat); getch(); }

Input: Enter no of jobs 4 Enter burst time 5 12 8 20

Output: Bt wt tt 5 0 5 12 5 13 8 13 25 20 25 45 aw=10.75000 at=22.000000

ID Waiting time for P1 -0: P2-24; P3-27 Average waiting time: (0+ 24 + 27)/3 17 24 27 30 ID Waiting time for P1 -0: P2-24; P3-27 Average waiting time: (0+ 24 + 27)/3 17 24 27 30

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!