0% found this document useful (0 votes)
20 views

OS Lab Programs

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

OS Lab Programs

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

EXPERIMENT NO.

CPU SCHEDULINGALGORITHMS

A.FIRST COME FIRST SERVE:

AIM: To write a c program to simulate the CPU scheduling algorithm First Come First Serve (FCFS)

DESCRIPTION:

To calculate the average waiting time using the FCFS algorithm first the waiting time of the first process
is kept zero and the waiting time of the second process is the burst time of the first process and the waiting
time of the third process is the sum of the burst times of the first and the second process and so on. After
calculating all the waiting times the average waiting time is calculated as the average of all the waiting
times. FCFS mainly says first come first serve the algorithm which came first will be served first.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process name and the burst time Step 4: Set the
waiting of the first process as ‗0‘and its burst time as its turnaround time Step 5: for each process in the
Ready Q calculate
Waiting time (n) = waiting time (n-1) + Burst time (n- Turnaround time (n) = waiting time (n)+Burst
time(n)
Step 6: Calculate
Average waiting time = Total waiting Time / Number of process Average Turnaround time = Total
Turnaround Time / Number of process

Step7:stop

Program:

import java.io.*;
import java.util.Scanner;
public class DemoTranslation
{
public static void main(String[] args)
{
int[] bt = new int[20], wt = new int[20], tat = new int[20];
int n;
float wtavg, tatavg;
System.out.print("\nEnter the number of processes--");
n = STDIN_SCANNER.nextInt();
for(int i = 0; i < n; i++)
{
System.out.print("\nEnter Burst Time for Process " + i + " -- ");
bt[i] = STDIN_SCANNER.nextInt();
}
wt[0] = (int)(wtavg = 0);
tat[0] = (int)(tatavg = bt[0]);
for(int i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
tat[i] = tat[i - 1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
System.out.println("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME");
for(int i = 0; i < n; i++)
{
System.out.print("\n\t P" + i + " \t\t " + bt[i] + " \t\t " + wt[i] + " \t\t " + tat[i]);
}
System.out.printf("\nAverage Waiting Time -- %f", wtavg / n);
System.out.printf("\nAverage Turnaround Time -- %f", tatavg / n);
}

public final static Scanner STDIN_SCANNER = new Scanner(System.in);


}

INPUT:
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3

OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUND
TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000

B.SHORTEST JOB FIRST:

AIM: To write a program to stimulate the CPU scheduling algorithm Shortest job
first (Non- Preemption)

DESCRIPTION:
To calculate the average waiting time in the shortest job first algorithm the sorting of the
process based on their burst time in ascending order then calculate the waiting time of
each process as the sum of the bursting times of all the process previous or before to that
process.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to highest
burst time.
Step 5: Set the waiting time of the first process as ‗0‘ and its turnaround time as its burst time.
Step 6: Sort the processes names based on their Burt time
Step 7: For each process in the ready queue,
calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process Step 9: Stop the
process

Program:
import java.util.Scanner;
public class DemoTranslation
{
public static void main(String[] args)
{
int[] p = new int[20], bt = new int[20], wt = new int[20], tat = new int[20];
int n, temp;
float wtavg, tatavg;
System.out.print("\nEnter the number of processes -- ");
n = STDIN_SCANNER.nextInt();
for(int i = 0; i < n; i++)
{
p[i] = i;
System.out.print("Enter Burst Time for Process " + i + " -- ");
bt[i] = STDIN_SCANNER.nextInt();
}
for(int i = 0; i < n; i++)
{
for(int k = i + 1; k < n; k++)
{
if(bt[i] > bt[k])
{
temp = bt[i];
bt[i] = bt[k];
bt[k] = temp;
temp = p[i];
p[i] = p[k];
p[k] = temp;
}
}
}
wt[0] = (int)(wtavg = 0);
tat[0] = (int)(tatavg = bt[0]);
for(int i = 1; i < n; i++)
{
wt[i] = wt[i - 1] + bt[i - 1];
tat[i] = tat[i - 1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
System.out.println("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME");
for(int i = 0; i < n; i++)
{
System.out.print("\n\t P" + p[i] + " \t\t " + bt[i] + " \t\t " + wt[i] + " \t\t " + tat[i]);
}
System.out.printf("\nAverage Waiting Time -- %f", wtavg / n);
System.out.printf("\nAverage Turnaround Time -- %f", tatavg / n);
}

public final static Scanner STDIN_SCANNER = new Scanner(System.in);


}

INPUT:
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
OUTPUT:
PROCESS BURST WAITING TURNARO
TIME TIME UND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000
C.ROUND ROBIN:

AIM: To simulate the CPU scheduling algorithm round-robin.

DESCRIPTION:

To aim is to calculate the average waiting time. There will be a time slice, each process should
be executed within that time-slice and if not it will go to the waiting state so first check whether the
burst time is less than the time-slice. If it is less than it assign the waiting time to the sum of the total
times. If it is greater than the burst-time then subtract the time slot from the actual burst time and
increment it by time-slot and the loop continues until all the processes are completed.

ALGORITHM:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where No. of time slice for process (n) = burst
time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
a) Waiting time for process (n) = waiting time of process(n-1)+ burst time of process(n-1 ) + the
time difference in getting the CPU fromprocess(n-1)
b) Turnaround time for process(n) = waiting time of process(n) + burst time of process(n)+ the
time difference in getting CPU from process(n).
Step 7: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number ofprocess Step 8: Stop the
process

PROGRAM:
import java.util.Scanner;
public class DemoTranslation
{
public static void main(String[] args)
{
int n, t, max;
int[] bu = new int[10], wa = new int[10], tat = new int[10], ct = new int[10];
float awt = 0, att = 0, temp = 0;
System.out.print("Enter the no of processes -- ");
n = STDIN_SCANNER.nextInt();
for(int i = 0; i < n; i++)
{
System.out.print("\nEnter Burst Time for process " + (i + 1) + " -- ");
bu[i] = STDIN_SCANNER.nextInt();
ct[i] = bu[i];
}
System.out.print("\nEnter the size of time slice -- ");
t = STDIN_SCANNER.nextInt();
max = bu[0];
for(int i = 1; i < n; i++)
{
if(max < bu[i])
{
max = bu[i];
}
}
for(int j = 0; j < max / t + 1; j++)
{
for(int i = 0; i < n; i++)
{
if(bu[i] != 0)
{
if(bu[i] <= t)
{
tat[i] = (int)(temp + bu[i]);
temp = temp + bu[i];
bu[i] = 0;
}
else
{
bu[i] = bu[i] - t;
temp = temp + t;
}
}
}
}
for(int i = 0; i < n; i++)
{
wa[i] = tat[i] - ct[i];
att += tat[i];
awt += wa[i];
}
System.out.printf("\nThe Average Turnaround time is -- %f", att / n);
System.out.printf("\nThe Average Waiting time is -- %f ", awt / n);
System.out.println("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND
TIME");
for(int i = 0; i < n; i++)
{
System.out.println("\t" + (i + 1) + " \t " + ct[i] + " \t\t " + wa[i] + " \t\t " + tat[i] + " ");
}
}

public final static Scanner STDIN_SCANNER = new Scanner(System.in);


}

D.PRIORITY:

AIM: To write a c program to simulate the CPU scheduling priority algorithm.

DESCRIPTION:

To calculate the average waiting time in the priority algorithm, sort the burst times according to
their priorities and then calculate the average waiting time of the processes. The waiting time of each
process is obtained by summing up the burst times of all the previous processes.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‗0‘ and its burst time as its turnaround time Step 6:
Arrange the processes based on process priority
Step 7: For each process in the Ready Q calculate Step 8:
for each process in the Ready Q calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n) Step 9: Calculate
Average waiting time = Total waiting Time / Number of process
c) Average Turnaround time = Total Turnaround Time / Number of process Print
the results in an order.
Step10: Stop
Program:

import java.util.Scanner;
public class DemoTranslation
{
public static void main(String[] args)
{
int[] p = new int[20], bt = new int[20], pri = new int[20], wt = new int[20], tat = new int[20];
int n, temp;
float wtavg, tatavg;
System.out.print("Enter the number of processes --- ");
n = STDIN_SCANNER.nextInt();
for(int i = 0; i < n; i++)
{
p[i] = i;
System.out.print("Enter the Burst Time & Priority of Process " + i + " --- ");
bt[i] = STDIN_SCANNER.nextInt();
pri[i] = STDIN_SCANNER.nextInt();
}
for(int i = 0; i < n; i++)
{
for(int k = i + 1; k < n; k++)
{
if(pri[i] > pri[k])
{
temp = p[i];
p[i] = p[k];
p[k] = temp;
temp = bt[i];
bt[i] = bt[k];
bt[k] = temp;
temp = pri[i];
pri[i] = pri[k];
pri[k] = temp;
}
}
}

wtavg = (wt[0] = 0);


tatavg = (tat[0] = bt[0]);
for(int i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
tat[i] = tat[i - 1] + bt[i];

wtavg = wtavg + wt[i];


tatavg = tatavg + tat[i];
}
System.out.print("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\
tTURNAROUND TIME");
for(int i = 0; i < n; i++)
{
System.out.print("\n" + p[i] + " \t\t " + pri[i] + " \t\t " + bt[i] + " \t\t " + wt[i] + " \t\t "
+ tat[i] + " ");
}
System.out.printf("\nAverage Waiting Time is --- %f", wtavg / n);
System.out.printf("\nAverage Turnaround Time is --- %f", tatavg / n);
}

public final static Scanner STDIN_SCANNER = new Scanner(System.in);


}

INPUT:
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2
OUTPUT:
PROCESS PRIORITY BURST TIME WAITIN TURNARO
G TIME 0 UND TIME 1
1 1 1
4 2 5 1 6
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
Average Waiting Time is --- 8.200000
Average Turnaround Time is-----------------------------12.000000
VIVA QUESTIONS
1) Define the following
a) Turnaround time b) Waiting time c) Burst time d) Arrival
time
2) What is meant by process scheduling?
3) What are the various states of process?
4) What is the difference between preemptive and non-preemptive scheduling
5) What is meant by time slice?
6) What is round robin scheduling?

EXPERIMENT:NO 5

AIM: To Write a JAVA program to simulate producer-consumer problem using semaphores.

DESCRIPTION:

Producer consumer problem is a synchronization problem. There is a fixed size buffer where the
producer produces items and that is consumed by a consumer process. One solution to the producer-
consumer problem uses shared memory. To allow producer and consumer processes to run
concurrently, there must be available a buffer of items that can be filled by the producer and emptied
by the consumer. This buffer will reside in a region of memory that is shared by the producer and
consumer processes. The producer and consumer must be synchronized, so that the consumer does
not try to consume an item that has not yet been produced.

Program:
import java.util.Scanner;
public class DemoTranslation
{
public static void main(String[] args)
{
int[] buffer = new int[10];
int bufsize, in, out, produce, consume, choice = 0;
in = 0;
out = 0;
bufsize = 10;
while(choice != 3)
{
System.out.print("\n1.Produce \t 2.Consume \t3.Exit");
System.out.print("\nEnter your choice: ");
choice = STDIN_SCANNER.nextInt();
switch(choice) {
case 1:
if((in + 1) % bufsize == out)
{
System.out.print("\nBuffer is Full");
} else
{
System.out.print("\nEnter the value: ");
produce = STDIN_SCANNER.nextInt();
buffer[in] = produce;
in = (in + 1) % bufsize;
}
break;

case 2:
if(in == out)
{
System.out.print("\nBuffer is Empty");
} else
{
consume = buffer[out];
System.out.print("\nThe consumed value is " + consume);
out = (out + 1) % bufsize;
}
break;
}
}
}
public final static Scanner STDIN_SCANNER = new Scanner(System.in);
}

You might also like