1.1 Lab Mannul
1.1 Lab Mannul
i. To study the difference between Bourne Shell and BASH shell. Summarize the ideas of
Device drivers of Unix and Windows.
ii. Create a directory named “Drive1”. In this directory create few numbers of files named
File1, File2, File3, etc., Use a shell command to display all the files you have created
inside this directory.
Solution:
$ ls
file1 file2 file3 file4
iii. A user created a file named “Large” which more numbers of lines. He founds difficult
to access the head and tail of the contents of that particular file. So, help him out to find
the head and tail of the file using any shell commands.
Solution:
$ head gaint
Hari
Hiranyagarbha
Hrishikesh
Jagadguru
Jagadisha
Jagannath
Janardhana
Jayantah
Jyotiraaditya
Kamalnath
Kamalnayan
Kamsantak
Kanjalochana
$ tail gaint
Jagadguru
Jagadisha
Jagannath
Janardhana
Jayantah
Jyotiraaditya
Kamalnath
Kamalnayan
Kamsantak
Kanjalochana
iv. Display the following details of the system uid, gid, groups, number of users,
authenticated users, local accounts using any shell commands.
Solution:
$ who am i
v. Display the list of shell command to display numbers of words, lines, and characters
counts of file content.
Page 1 of 47
Solution:
$ wc -c text
36 text
$ wc -l text
2 text
$ wc -w text
2text
vi. Create a file, then sort it in the ascending order. Now number each line in the
ascending order such way all content of the files should start with a number. Finally,
display the file content.
Solution:
$ sort gaint
Apple
Ball
Cat
Dog
Elephant
vii. Create two directories named “dir1”and “newdir” separately. Change the path of
directory from “dir1” to “newdir”. Concatenate a file called “Wishes”. This file should
display your best friend’s birthday dates.
Solution:
$ pwd
/home/Sumathy
Sumathy@DESKTOP-0T3UVR2 ~
$ mkdir file1
Sumathy@DESKTOP-0T3UVR2 ~
$ mkdir file2
Sumathy@DESKTOP-0T3UVR2 ~
$ mkdir file3
Sumathy@DESKTOP-0T3UVR2 ~
$ mkdir file4
Sumathy@DESKTOP-0T3UVR2 ~
$ ls
file1 file2 file3 file4
viii. An anonymous user who tries to copy the file content from the Unix shell. He tries to
copies directly from the source file content. Is it possible for him to copy the file
content and delete the source file content?
Solution:
$ cat > cool
$ mv best cool
$ cat cool
12453365
abcsdefghijklmnopqrstuvwxz
$ cat best
cat: best: No such file or directory
ix. To display the content of file mentioned we use CAT command without „>‟ operator.
Page 2 of 47
Solution:
$cat <filename.
Options –s = to neglect the warning /error message.
x. To copy the content of one file with another. If file doesnot exist, a new file is created
and if the file exists with some data then it is overwritten.
Solution:
$ cat <filename source> >> <destination filename>
$ cat <source filename> >> <destination filename> it is avoid overwriting
xi. To copy the contents from source to destination file . so that both contents are same.
Solution:
$cp <source filename> <destination filename>
$cp <source filename path > <destination filename path>
xii. To completely move the contents from source file to destination file and to remove the
source file.
Solution:
$ mv <source filename> <destination filename>
xiii. To permanently remove the file we use this command .
Solution:
$rm <filename>
xiv. Used to provide manual help on every UNIX command.
Solution:
$man unix command
$man cat
xv. Used to search a particular word or pattern related to that word from the file.
Solution:
$grep search word filename
$grep anu student
xvi. Concatenate a file named “OS”. Use “grep” command to search and display the
specific text from the “OS” file. Finally, show us the numbers of texts counts.
Solution:
$ wc -c text
36 text
$ wc -l text
2 text
$ wc -w text
2text
xvii. This command is used to display the current data and time.
Solution:
$date
$date +%ch
xviii. This command is used to display the calendar of the year or the particular month of
Page 3 of 47
calendar year.
Solution:
a.$cal <year>
b.$cal <month> <year>
Page 4 of 47
echo 2.Subraction
echo 3.Multiplication
echo 4.Division
echo enter your choice
read a
echo enter the value of b
read b
echo enter the value of c
read c
echo b is $b c is $c
case $a in
1)d=`expr $b + $c`
echo the sum is $d
;;
2)d=`expr $b - $c`
echo the difference is $d
;;
3)d=`expr $b \* $c`
echo the product is $d
;;
4)d=`expr $b / $c`
echo the quotient is $d
;;
esac
O/P
1.Addition 2. Subtraction3. Multiplication 4. Division
Enter your choice:1
Enter the value of b:3
Enter the value of c:4
b is 3 c is 4 the sum is 7
Page 5 of 47
v. Write a Shell program to check and display 10 leap years
Solution:
for((i = 2020 ; i<= 2040 ; i++))
do if [ `expr $i % 400` = 0 ]
then echo "$i is a leap year"
elif [ `expr $i % 4` = 0 -a `expr $i % 100` != 0 ]
then echo "$i is a leap year"
fi
done
O/P
2020 is a leap year
2024 is a leap year
2028 is a leap year
2032 is a leap year
2036 is a leap year
2040 is a leap year
vi. Write a Shell program to check the given string is palindrome or not
Solution:
echo "Enter the string:"
read s
l=`expr length $s`
c=1
p=""
while [ $c -le $l ]
do
e=`expr substr $s $c 1`
p=$e$p
c=`expr $c + 1`
done
if
[ $p = $s ]
then
echo "The given string $s is a palindrome"
else
echo "The given string $s is not a palindrome"
fi
O/P
Enter the string:
Madam
The given string $s is not a palindrome
Page 6 of 47
error condition and display error message then display the parent and child process
along with its id.
Solution:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sumOdd = 0, sumEven = 0, n, i;
n = fork();
// Checking if n is not 0
if (n > 0) {
for (i = 0; i< 10; i++) {
if (a[i] % 2 == 0)
sumEven = sumEven + a[i];
}
printf("Parent process \n");
printf("Sum of even no. is %d \n",sumEven);
}
// If n is 0 i.e. we are in child process
else {
for (i = 0; i< 10; i++) {
if (a[i] % 2 != 0)
sumOdd = sumOdd + a[i];
}
printf( "Child process \n");
printf( "\nSum of odd no. is %d\n",sumOdd);
}
return 0;
}
O/P
Parent process
Child process
Sum of even no. is 30
Sum of odd no. is 25
ii. To write the program to implement the system calls getpid() and getppid().
Solution:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main( )
{
int pid;
pid=fork( ); if(pid==-1)
Page 7 of 47
{
perror(“fork failed”); exit(0);
}
if(pid==0)
{
printf(“\n Child process is under execution”);
printf(“\n Process id of the child process is %d”, getpid()); printf(“\nProcess id of the
parent process is %d”, getppid());
}
else
{
printf(“\n Parent process is under execution”);
printf(“\n Process id of the parent process is %d”, getpid());
printf(“\n Process id of the child process in parent is %d”, pid());
printf(“\n Process id of the parent of parent is %d”, getppid());
}
return(0);
}
O/P
Child process is under execution
Process id of the child process is 9314
Process id of the parent process is 9313 Parent
process is under execution
Process id of the parent process is 9313
Process id of the child process in parent is 9314 Process
id of the parent of parent is 2825
iii. Write a C program create a new child process and if it is equal to zero print the
command for using exec() system call else wait() can be used to make the parent wait
for the child to terminate.
Solution:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#define MAX 1024
main()
{
int fd1,fd2,n;
char buf[MAX];
char src[10], des[10];
printf("\nEnter the source file name");
scanf("%s",src);
printf("\nEnter the destination file name");
scanf("%s",des);
Page 8 of 47
fd1=open(src,0);
if(fd1==-1)
{
printf("Cant open a source file");
exit(1);
}
fd2=creat(des,0644);
if(fd2==-1)
{
printf("Cant open a destination file");
}
while((n=read (fd1,buf,MAX))>0)
{
write (fd2,buf,n);
printf("File copied");
}
close(fd2);
close(fd1);
}
O/P
Enter the source file namekee.c
Enter the destination file namegrancy.c
File copied
iv. To write the program to implement the system calls open( ),read( ) and write( ).
Solution:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
main( )
{
int fd[2];
char buf1[25]= ”just a test\n”; char
buf2[50];
fd[0]=open(“file1”, O_RDWR);
fd[1]=open(“file2”, O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf(“\n Enter the text now….”);
gets(buf1);
write(fd[0], buf1, strlen(buf1));
lseek(fd[0], SEEK_SET, 0);
read(fd[0], buf2, sizeof(buf1));
write(fd[1], buf2, sizeof(buf2));
close(fd[0]);
close(fd[1]);
printf(“\n”);
Page 9 of 47
return0;
}
O/P
Enter the text now….progress
Cat file1 Just a
test progress
Cat file2 Just a test progress
Task 4: Process Management CO3 S3
i. Implement the C program in which main program accepts an integer array. Main
program uses the fork system call to create a new process called a child process. Parent
process sorts an integer array and passes the sorted array to child process through the
command line arguments of execve system call. The child process uses execve system
call to load new program that uses this sorted array for performing the binary search to
search the particular item in thearray.
Solution:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
void quickSort(int [],int ,int );
int partition(int [],int ,int );
void mergeSort(int [],int ,int );
void merge(int [],int ,int ,int ,int );
int main()
{
int i,j,n;
int *status=NULL;
int arr[30];
printf("\nEnter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
pid_tpid;
pid=fork();
if(pid==0)
{
printf("\n\t This is child process. ");
printf("\n\t My process id is : %d", getpid());
printf("\n\t My Parent process id is : %d", getppid());
quickSort(arr,0,n-1);
printf("\nQuicksort");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
printf("\n\n");
Page 10 of 47
}
else
{
printf("\n\n\t Parent process resumed after the execution of child process with PID
%d", pid);
printf("\n\t My process id is : %d", getpid());
printf("\n\t My Parent process id is : %d", getppid());
mergeSort(arr,0,n-1);
printf("\nMergesort:");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
printf("\n\n");
pid=wait(status);
}}
void quickSort(int arr[],int low,int high)
{
int j;
if(low<high)
{
j=partition(arr,low,high);
quickSort(arr,low,j-1);
quickSort(arr,j+1,high);
}}
int partition(int arr[],int low,int high)
{
int i,j,temp,pivot;
pivot=arr[low];
i=low;
j=high+1;
do
{
do
i++;
while(arr[i]<pivot &&i<=high);
do
j--;
while(arr[j]>pivot);
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
while(i<j);
arr[low]=arr[j];
Page 11 of 47
arr[j]=pivot;
return(j);
}
void mergeSort(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergeSort(arr,low,mid);
mergeSort(arr,mid+1,high);
merge(arr,low,mid,mid+1,high);
}}
void merge(int arr[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(arr[i]<arr[j])
temp[k++]=arr[i++];
else
temp[k++]=arr[j++];
}
while(i<=j1)
temp[k++]=arr[i++];
while(j<=j2)
temp[k++]=arr[j++];
for(i=i1,j=0;i<=j2;i++,j++)
arr[i]=temp[j];
}
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
int main(int argc,char *argv[])
{
int i,j,c,ele;
int arr[argc];
for(i=0;i<argc-1;i++)
{
int n=atoi(argv[i]);
Page 12 of 47
arr[i]=n;
}
ele=atoi(argv[i]);
i=0;
j=argc-1;
c=(i+j)/2;
while(ele!=arr[c] &&i<=j)
{
if(arr[c]<ele)
i=c+1;
else
j=c-1;
c=(i+j)/2;
}
if(i<=j)
printf("Elements found");
else
printf("Not found");
}
O/P
Enter number of Elements:6
2
4
1
2
6
7
Sorted Array :122467
Enter the element to be serached:4
Task 5: Inter Process Management (using shared memory,
CO3 S3
pipes or message queues)
i. Create a shared memory for parent process using shmget() system call and allow the
parent process to write in shared memory and attach the same shared memory to the
child process. The data in the shared memory is read by the child process using shmdt
pointer then detach and reuse the shared memory.
Solution:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
Struct country
{
Char name[30];
Char capital_city [30];
Char currency[30];
Int population;
Page 13 of 47
};
Int main(int argc,char*argv[])
{
Int shm_id;
Char*shm_addr;
Int*countries_num;
Struct country*countries;
Struct shmid_dsshm_desc;
Shm_id=shmget(100,2048,IPC_CREAT|IPC_EXCL\0600);
If(shm_id==-1){
Perror(“main:shmget:”);
Exit(1);
}
Shm_addr=shmat(shm_id,NULL,0);
If(!shm_addr){
Perror(“main:shmat:”);
Exit(1);
}
Countries_num=(int*)shm_addr;
*countries_num=0;
Countries=(struct country*)((void*)shm_addrsizeof(int));
Strcpy(countries[0],name,”U.S.A”);
Strcpy(countries[0],capital_city,”WASHINGTON”);
Strcpy(countries[0],currency,”U.S.DOLLAR”);
Countries[0].population=250000000;
( countries_num) ;
Strcpy(countries[1].name,”israel”);
Strcpy(countries[1].capital_city,”jerushalem”);
Strcpy(countries[1].currency,”NEW ISRAEL SHEKED”);
Countries[1].population=6000000;
(*countries_num) ;
Strcpy(countries[2].name,”France”);
Strcpy(countries[2].capital_city,”paris”);
Strcpy(countries[2].currency,”Frank”);
Countries[2].population=60000000;
(*countries_num) ;
For(i=0;i<(*countries_num);i )
{
Printf(“country%d:\n”,i 1);
Printf(“name:%d:\n”,i 1);
Printf(“currency:%s:\n”,countries[i].currency);
Printf(“population:%d:\n”,countries[i].population);
}
If(shmdt(shm_addr)==-1){
Perror(“main:shmdt:”);
}
Page 14 of 47
If(shmctl(shm_id,IPC_RMID,&SHM_DESC)==-1)
{
Perror(“main:shmctl:”);
}
return 0;
}
O/P
Shared memory ID=65537 child pointer 3086680064
Child value =1
Shared memory ID=65537 child pointer 3086680064
Parent value=1
Parent value=42
Child value=42
Task 6: Semaphores CO3 S3
i. Parking lot security makes the slot available for the car owners. Owners will occupy
the vacant slot. Parking lot can hold minimum of 5 cars at the same time. When the
slot is full, car owner will wait for the slot to become vacant, when all the slots are
vacant (no cars in parking lot) security waits for the car owners.
Solution:
#include<stdio.h>
void read();
void write();
void readclose();
void writeclose();
void status();
int swrite=0,sread=0,r=0;
void main()
{
int ch;
printf("\t\tReaders Writers Problem\t\n");
do{
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch){
case 1:read(); break;
case 2:write(); break;
case 3:readclose(); break;
case 4:writeclose(); break;
case 5: status(); break;
default: break;
}
}
while(ch<6);
}
void read()
{
Page 15 of 47
if(swrite==0)
{
sread=1;
r++;
printf("\n%d Process reading the file",r);
}
else
printf("\n Some process is writing into the file\n");
}
void write()
{
if(swrite==0 &&sread==0)
{
swrite=1;
printf("\nOne process is writing into the file\n");
}
else
printf("Some process is reading so can’t write into the file");
}
void readclose()
{
if(r>0)
{
r--;
printf("\nOne process finished reading the file");
if(r==0)
{
sread=0;
printf("\nCurrently the file is not read by any process\n");
}
else
printf("\n The number of process reading the file is %d",r);
}
else if(swrite!=0)
printf("\nSome process is writing into the file\n");
else
printf("\nNo process is reading or writing the file\n");
}
void writeclose()
{
if(swrite!=0)
{
swrite=0;
printf("\n Writing process is closed,any process can write");
}
else
Page 16 of 47
printf("\n No process is writing");
}
void status()
{
if(sread==0 &&swrite==0)
printf("\n No process is reading or writing\n");
else if(sread!=0)
{
if(r>0)
printf("\nNumber of process writing the file is %d",r);
}
else if(swrite!=0)
printf("\nOne process is writing");
}
O/P
$ cc RW.c
$ ./a.out
Readers Writers Problem
1.Read the file
2.Write the file
3.Stop reading process
4.Stop writing
5.Status of the file
Enter the choice:1
1 Process reading the file
1.Read the file
2.Write the file
3.Stop reading process
4.Stop writing
5.Status of the file
Enter the choice:2
Some process is reading so can’t write into the file
1.Read the file
2.Write the file
3.Stop reading process
4.Stop writing
5.Status of the file
Enter the choice:3
One process finished reading the file
Currently the file is not read by any process
1.Read the file
2.Write the file
3.Stop reading process
4.Stop writing
5.Status of the file
Enter the choice:4
Page 17 of 47
No process is writing
1.Read the file
2.Write the file
3.Stop reading process
4.Stop writing
5.Status of the file
Enter the choice:5
No process is reading or writing
1.Read the file
2.Write the file
ii. To write a C program to implement the Producer & consumer Problem (Semaphore)
Solution:
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n 1.producer\n2.consumer\n3.exit\n");
while(1)
{
printf(" \nenter ur choice");
scanf("%d",&n);
switch(n)
{
case 1:if((mutex==1)&&(empty!=0))
producer();
else
printf("buffer is full\n");
break;
case 2:if((mutex==1)&&(full!=0))
consumer();
else
printf("buffer is empty");
break;
case 3:exit(0);
www.studentsfocus.com
41
break;
}
}
}
int wait(int s)
Page 18 of 47
{
return(--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\n producer produces the items %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}
O/P
[cse6@localhost Pgm]$ cc ex11.c -o ex11.out
[cse6@localhost Pgm]$ ./ex11.out
Produced element a
Consumed element a
Produced element b
Consumed element b
Produced element c
Consumed element c
Produced element d
Consumed element d
Produced element e
Consumed element e
Produced element f
Consumed element f
Produced element g
Consumed element g
Produced element h
Consumed element h
Task 7: Synchronization CO3 S3
i. In a barber shop which has one barber, one barber chair, and n chairs for waiting for
Page 19 of 47
customers if there are any to sit on the chair.
Cases:
If there is no customer, then the barber sleeps in his own chair.
When a customer arrives, he has to wake up the barber.
If there are many customers and the barber is cutting a customer’s hair, then the
remaining customers either wait if there are empty chairs in the waiting room or
they leave if no chairs are empty.
Solution:
// shared data
semaphore waiting_room_mutex = 1;
semaphore barber_room_mutex = 1;
semaphore barber_chair_free = k;
semaphore sleepy_barbers = 0;
semaphore barber_chairs[k] = {0, 0, 0, …};
int barber_chair_states[k] = {0, 0, 0, …};
int num_waiting_chairs_free = N;
booleancustomer_entry( ) {
// try to make it into waiting room
wait(waiting_room_mutex);
if (num_waiting_chairs_free == 0) {
signal(waiting_room_mutex);
return false;
}
num_waiting_chairs_free--; // grabbed a chair
signal(waiting_room_mutex);
// now, wait until there is a barber chair free
wait(barber_chair_free);
// a barber chair is free, so release waiting room chair
wait(waiting_room_mutex);
wait(barber_room_mutex);
num_waiting_chairs_free++;
signal(waiting_room_mutex);
// now grab a barber chair
int mychair;
for (int I=0; I<k; I++) {
if (barber_chair_states[I] == 0) { // 0 = empty chair
mychair = I;
break;
}
}
barber_chair_states[mychair] = 1; // 1 = haircut needed
signal(barber_room_mutex);
// now wake up barber, and sleep until haircut done
signal(sleepy_barbers);
wait(barber_chairs[mychair]);
// great! haircut is done, let’s leave. barber
Page 20 of 47
// has taken care of the barber_chair_states array.
signal(barber_chair_free);
return true;
}
void barber_enters() {
while(1) {
// wait for a customer
wait(sleepy_barbers);
// find the customer
wait(barber_room_mutex);
int mychair;
for (int I=0; I<k; I++) {
if (barber_chair_states[I] == 1) {
mychair = I;
break;
}
}
barber_chair_states[mychair] = 2; // 2 = cutting hair
signal(barber_room_mutex);
// CUT HAIR HERE
cut_hair(mychair);
// now wake up customer
wait(barber_room_mutex);
barber_chair_states[mychair] = 0; // 0 = empty chair
signal(barber_chair[mychair]);
signal(barber_room_mutex);
// all done, we’ll loop and sleep again
}
}
Task 8: Scheduling Algorithm
CO2 S3
a) FCFS
Read the number of processes, CPU burst time and arrival time from the user. Print the burst
time, turnaround time and waiting time for each process. Display Gantt chart for the above
scheduling. Print average waiting time and average turnaround time for FCFS.
Solution:
#include <stdio.h>
struct process
{
int pid; int btime; int wtime; int ttime;
} p[10];
main()
{
int i,j,k,n,ttur,twat; float awat,atur;
printf("Enter no. of process : ");
scanf("%d", &n);
for(i=0; i<n; i++)
Page 21 of 47
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n FCFS Scheduling\n\n");
for(i=0; i<28; i++) printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-"); for(i=0; i<n; i++)
printf("\n P%d\t%4d\t%3d\t%2d", p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");
for(i=0; i<28; i++)
printf("-");
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++) printf("-");
printf("\n");
printf("|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++) printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n");
printf("-");
or(i=0; i<(p[n-1].ttime + 2*n); i++) printf("-");
printf("\n");
Page 22 of 47
printf("0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++) printf(" ");
printf("%2d",p[i].ttime);
}
printf("\n\nAverage waiting time : %5.2fms", awat);
printf("\nAverageturn around time : %5.2fms\n", atur);
}
O/P
Enter no. of process : 3
Burst time for process P1 (in ms) : 20
Burst time for process P2 (in ms) : 15
Burst time for process P3 (in ms) : 10
FCFS Scheduling
----------------------------
Process B-Time T-Time W-Time
----------------------------
P1 20 20 0
P2 15 35 20
P3 10 45 35
----------------------------
GANTT Chart
----------------------------------------------------
| P1 | P2 | P3 |
----------------------------------------------------
0 20 35 45
Average waiting time : 18.33ms
Average turnaroundtime: 33.33ms
Task 8: Scheduling Algorithm
CO2 S3
b) SJF
Read the length of ready queue. Obtain burst time for each process. Sort the processes
according to their burst time in ascending order. Process the shortest job first. Print the burst
time, waiting time and turnaround time for each process. Display Gantt chart for the above
scheduling. Compute average waiting time and average turnaround time.
Solution:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
Page 23 of 47
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
O/P
Enter number of process:3
Enter Burst Time:
p1:13
p2:50
Page 24 of 47
p3:96
Process Burst Time Waiting Time Turnaround Time
p1 13 0 13
p2 50 13 63
p3 96 63 159
Average Waiting Time=25.333334
Average Turnaround Time=78.333336
Task 8: Scheduling Algorithm
CO2 S3
c) Round Robin and Priority Scheduling
To implement round robin scheduling and priority based round robin scheduling. For both the
algorithms use time quantum = 1000 iterations and 2000 iterations.
First you should execute sched.c in a terminal. It will take the scheduling algorithm type as a
command line argument.
1. 'RR' : Regular round robin
2. 'PR' : Priority based round robin.
The scheduler should create the message queue and continuously senses the message queue to
check if the first process has been created. Then you should run gen.c in another terminal
which will create 4 processes described above. One xterm should be created for each process
and the process that is scheduled should print “PID:, loop-counter” when it runs its iterations.
The other processes should wait at that time and should not print anything in their respective
xterms.
Upon finishing, a file (result.txt) should be created which will have the
1. Response time.
2. Total waiting time.
3. Turn-around time. for each process with their process id. And finally, the file should contain
the average of this metrics for all 4 processes.
The scheduler should display the following status in its terminal:
P1<pid> is running; P1 <pid> requests I/O; P3 <pid> is running; P4 <pid> completes I/O
Solution:
/*consumer.c*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <semaphore.h>
int main()
{
const char * name = "shared_memory";
const char * sema1= "fill";
const char * sema2= "avail";
const char * sema3="mutex";
int shm_fd; //file descriptor of
int * shelf;
int loop=6;
Page 25 of 47
sem_t * fill, * avail, * mutex;
/* open the shared memory segment */
shm_fd = shm_open(name, O_RDWR, 0666);
/* now map the shared memory segment in the address space of the process*/
shelf = mmap(0,sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED,
shm_fd, 0);
/* open semaphores*/
fill = sem_open(sema1, O_CREAT,0666,0);
avail = sem_open(sema2, O_CREAT, 0666, 3);
mutex = sem_open(sema3,O_CREAT,0666,1);
while(loop--){
sem_wait(fill);
sleep(rand()%2+1);
sem_wait(mutex);
(* shelf)--;
sem_post(mutex);
printf("Waiter: I pick up a pizza\n");
sem_post(avail);
}
/* remove semaphores*/
sem_close(fill);
sem_close(avail);
sem_close(mutex);
sem_unlink(sema1);
sem_unlink(sema2);
sem_unlink(sema3);
/* remove shared memory segment*/
munmap(shelf, sizeof(int));
close(shm_fd);
shm_unlink(name);
return 0;
}
/*producer.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <semaphore.h>
int main()
{
const char * name = "shared_memory";
const char * sema1= "fill";
const char * sema2= "avail";
Page 26 of 47
const char * sema3= "mutex";
int shm_fd; //shared memory file discriptor
int * shelf;
int loop=6;
sem_t * fill, * avail, * mutex;
/* make * shelf shared between processes*/
//create the shared memory segment
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
//configure the size of the shared memory segment
ftruncate(shm_fd,sizeof(int));
//map the shared memory segment in process address space
shelf = mmap(0,sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED,
shm_fd, 0);
/* creat/open semaphores*/
//cook post semaphore fill after cooking a pizza
fill = sem_open(sema1, O_CREAT,0666,0);
//waiter post semaphore avail after picking up a pizza, at the beginning avail=3
avail = sem_open(sema2, O_CREAT, 0666, 3);
//mutex for mutual exclusion of shelf
mutex = sem_open(sema3,O_CREAT,0666,1);
printf("\nCook: I have started cooking pizza.\n");
while(loop--){
sem_wait(avail);
sleep(rand()%2+1);
sem_wait(mutex);
(* shelf)++;
sem_post(mutex);
printf("Cook: cook a pizza, there are %d pizza now\n", * shelf);
sem_post(fill);
}
printf("Cook: Time is up. I cooked 6 pizza. %d are left.\n", * shelf);
/* close and unlink semaphores*/
sem_close(fill);
sem_close(avail);
sem_close(mutex);
sem_unlink(sema1);
sem_unlink(sema2);
sem_unlink(sema3);
/* close and unlink shared memory*/
munmap(shelf, sizeof(int));
close(shm_fd);
shm_unlink(name);
return 0;
}
O/P
$ ./cook & ./waiter &
Page 27 of 47
[1] 233
[2] 234
Cook: I have started cooking pizza.
Cook: cook a pizza, there are 1 pizza now
Cook: cook a pizza, there are 2 pizza now
Waiter: I pick up a pizza
Cook: cook a pizza, there are 2 pizza now
Waiter: I pick up a pizza
Waiter: I pick up a pizza
Cook: cook a pizza, there are 1 pizza now
Cook: cook a pizza, there are 2 pizza now
Waiter: I pick up a pizza
Cook: cook a pizza, there are 2 pizza now
Cook: Time is up. I cooked 6 pizza. 2 are left.
Waiter: I pick up a pizza
Waiter: I pick up a pizza
Task 9: Deadlocks CO4 S3
i. Deadlock Avoidance using Banker’s Algorithm: Get the number of processes,
resources in a system, no. of instances of resource, allocation matrix, maximum matrix
and the available value. Find the need matrix. Find if the system is in safe state? If it is,
find the safe sequence.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10],
completed[10],safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i< p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i< p; i++)
{
Page 28 of 47
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i< r; i++)
scanf("%d", &avail[i]);
for(i = 0; i< p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i< p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i< p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
Page 29 of 47
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i< p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
O/P
Enter the no of processes : 5
Enter the no of resources : 3
Enter the Max Matrix for each process :
For process 1 : 7
5
3
For process 2 : 3
2
2
For process 3 : 7
0
2
For process 4 : 2
2
2
For process 5 : 4
3
3
Enter the allocation for each process :
For process 1 : 0
1
1
For process 2 : 2
0
0
Page 30 of 47
For process 3 : 3
0
2
For process 4 : 2
1
1
For process 5 : 0
0
0
Enter the Available Resources : 3
3
2
Max matrix: Allocation matrix:
753 011
322 200
702 302
222 211
433 000
Process 2 runs to completion!
Max matrix: Allocation matrix:
753 011
000 000
702 302
222 211
433 000
Process 3 runs to completion!
Max matrix: Allocation matrix:
753 011
000 000
000 000
222 211
433 000
Process 4 runs to completion!
Max matrix: Allocation matrix:
753 011
000 000
000 000
000 000
433 000
Process 1 runs to completion!
Max matrix: Allocation matrix:
000 000
000 000
000 000
000 000
433 000
Page 31 of 47
Process 5 runs to completion!
The system is in a safe state!!
Safe Sequence: < 2 3 4 1 5 >
Page 32 of 47
{
ptable[i].fno = frameno;
ptable[i].pbit = 1;
}
}
printf("\n\nPAGE TABLE\n\n");
printf("PageAddressFrameNo. PresenceBit\n\n");
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddressPageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
printf("\n\n\n\tProcess to create the Physical Address\n\n");
printf("\nEnter the Base Address: ");
scanf("%d",&baddr);
printf("\nEntertheLogical Address: ");
scanf("%d",&laddr);
paddr = laddr / psize;
disp = laddr % psize;
if(ptable[paddr].pbit == 1 )
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
info();
assign();
cphyaddr();
}
O/P
MEMORY MANAGEMENT USING PAGING
Enter the Size of Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 no.of frames
The Logical memory is divided into 4 no.of pages
Enter the Frame number where page 0 must be placed: 5
Enter the Frame number where page 1 must be placed: 6
Enter the Frame number where page 2 must be placed: 7
Enter the Frame number where page 3 must be placed: 2
PAGE TABLE
Page 33 of 47
PageAddressFrameNo. PresenceBit
0 5 1
1 6 1
2 7 1
3 2 1
FRAME TABLE
FrameAddressPageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
The Physical Address where the instruction present: 1013
Page 34 of 47
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}}}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
void best_fit()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
printf("\nEnter the number of partitions:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the partitions:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
Page 35 of 47
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
ff[i]=j;
lowest=temp;
}}}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile_no \tFile_size \tBlock_no \tBlock_size \tFragment");
for(i=1;i<=nf&& ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
void worst_fit()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\nEnter the number of partitions:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the partitions:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
Page 36 of 47
}}}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no \tFile_size \tBlock_no \tBlock_size \tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
}
int main()
{
int choice;
printf("MEMORY ALLOCATION\n");
printf("-------------------\n");
printf("1.First-fit memory allocation\n2.Best-fit memory allocation\n3.Worst-fit
memory allocation\n");
printf("Enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("First_Fit Algorithm\n");
printf("--------------------\n");
first_fit();
break;
case 2:
printf("Best_Fit Algorithm\n");
printf("--------------------\n");
best_fit();
break;
case 3:
printf("Worst_Fit Algorithm\n");
printf("--------------------\n");
worst_fit();
break;
default:
printf("Error! choice is not correct");
}
return 0;
}
O/P
Page 37 of 47
MEMORY ALLOCATION
-------------------
1.First-fit memory allocation
2.Best-fit memory allocation
3.Worst-fit memory allocation
Enter the choice:1
First_Fit Algorithm
--------------------
Enter the number of partitions:5
Enter the number of files:4
Enter the size of the partitions:-
Block 1:100
Block 2:500
Block 3:200
Block 4:300
Block 5:600
Enter the size of the files:-
File 1:212
File 2:417
File 3:112
File 4:426
File_no: File_size :Block_no: Block_size: Fragment
1 212 2 500 288
2 417 5 600 183
3 112 3 200 88
4 426 0 -2145129072 -126
Enter the choice:2
Best_Fit Algorithm
--------------------
Enter the number of partitions:5
Enter the number of files:4
Enter the size of the partitions:-
Block 1:100
Block 2:500
Block 3:200
Block 4:300
Block 5:600
Enter the size of the files:-
File 1:212
File 2:417
File 3:112
File 4:426
File_noFile_sizeBlock_noBlock_size Fragment
1 212 4 300 88
2 417 2 500 83
3 112 3 200 88
Page 38 of 47
4 426 5 600 174
Enter the choice:3
Worst_Fit Algorithm
--------------------
Enter the number of partitions:5
Enter the number of files:4
Enter the size of the partitions:-
Block 1:100
Block 2:500
Block 3:200
Block 4:300
Block 5:600
Enter the size of the files:-
File 1:212
File 2:417
File 3:112
File 4:426
File_noFile_sizeBlock_noBlock_size Fragment
1 212 5 600 388
2 417 2 500 83
3 112 4 300 188
4 426 0 0 0
Task 12: Disk Scheduling CO6 S3
i. Consider a disk with 200 tracks and the queue has random requests from different
processes in the order: 55, 58, 39, 18, 90, 160, 150, 38, 184. Initially arm is positioned
at location 100. Find the Average Seek length using FCFS, SSTF, SCAN and C-
SCAN.
Solution:
#include<stdio.h>
#include <stdlib.h>
void FCFS()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
Page 39 of 47
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
}
void SSTF()
{
int queue[100], queue2[100], q_size, head, seek=0, temp;
float avg;
printf("%s\n", "-----SSTF Disk Scheduling Algorithm-----");
printf("%s\n", "Enter the size of the queue");
scanf("%d", &q_size);
printf("%s\n", "Enter queue elements");
for(int i=0; i<q_size; i++)
{
scanf("%d",&queue[i]);
}
printf("%s\n","Enter initial head position");
scanf("%d", &head);
//get distance from head of elems in queue
for(int i=0; i<q_size; i++){
queue2[i] = abs(head-queue[i]);
}
//swap elems based on their distance from each other
for(int i=0; i<q_size; i++)
{
for(int j=i+1; j<q_size;j++)
{
if(queue2[i]>queue2[j])
{
temp = queue2[i];
queue2[i]=queue[j];
queue2[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}}}
for(int i=1; i<q_size; i++)
{
seek = seek+abs(head-queue[i]);
head = queue[i];
}
printf("\nTotal seek time is %d\t",seek);
Page 40 of 47
avg = seek/(float)q_size;
printf("\nAverage seek time is %f\t", avg);
}
void C_SCAN()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("%s\n", "-----C-SCAN Disk Scheduling Algorithm-----");
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
Page 41 of 47
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
}
void SCAN()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
float avg;
printf("%s\n", "-----SCAN Disk Scheduling Algorithm-----");
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
Page 42 of 47
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
Page 43 of 47
}
int main()
{
int choice;
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],
temp1=0,temp2=0;
printf("DISK SCHEDULING ALGORITHMS\n");
printf("-------------------\n");
printf("1.FSCS algorithm \n2.SSTF algorithm\n3.SCAN algorithm\n4.C-SCAN
algorithm\n");
printf("Enter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
FCFS();
break;
case 2:
SSTF();
break;
case 3:
SCAN();
break;
case 4:
C_SCAN();
break;
default:
printf("Error! choice is not correct");
}
return 0;
}
O/P
DISK SCHEDULING ALGORITHMS
-------------------
1.FSCS algorithm
2.SSTF algorithm
3.SCAN algorithm
4.C-SCAN algorithm
Enter the choice:1
Enter the max range of disk
200
Enter the size of queue request
9
Enter the queue of disk positions to be read
Page 44 of 47
55
58
39
18
90
160
150
38
184
Enter the initial head position
100
Disk head moves from 100 to 55 with seek 45
Disk head moves from 55 to 58 with seek 3
Disk head moves from 58 to 39 with seek 19
Disk head moves from 39 to 18 with seek 21
Disk head moves from 18 to 90 with seek 72
Disk head moves from 90 to 160 with seek 70
Disk head moves from 160 to 150 with seek 10
Disk head moves from 150 to 38 with seek 112
Disk head moves from 38 to 184 with seek 146
Total seek time is 498
Average seek time is 55.333332
Enter the choice:2
-----SSTF Disk Scheduling Algorithm-----
Enter the size of the queue
9
Enter queue elements
55
58
39
18
90
160
150
38
184
Enter initial head position
100
Total seek time is 418
Average seek time is 46.444443
Enter the choice:3
-----SCAN Disk Scheduling Algorithm-----
Enter the max range of disk
200
Enter the initial head position
100
Page 45 of 47
Enter the size of queue request
9
Enter the queue of disk positions to be read
55
58
39
18
90
160
150
38
184
Disk head moves from 100 to 150 with seek 50
Disk head moves from 150 to 160 with seek 10
Disk head moves from 160 to 184 with seek 24
Disk head moves from 184 to 200 with seek 16
Disk head moves from 200 to 90 with seek 110
Disk head moves from 90 to 58 with seek 32
Disk head moves from 58 to 55 with seek 3
Disk head moves from 55 to 39 with seek 16
Disk head moves from 39 to 38 with seek 1
Disk head moves from 38 to 18 with seek 20
Disk head moves from 18 to 0 with seek 18
Total seek time is 300
Average seek time is 33.333332
Enter the choice:4
-----C-SCAN Disk Scheduling Algorithm-----
Enter the max range of disk
200
Enter the initial head position
100
Enter the size of queue request
9
Enter the queue of disk positions to be read
55
58
39
18
90
160
150
38
184
Disk head moves from 100 to 150 with seek 50
Disk head moves from 150 to 160 with seek 10
Disk head moves from 160 to 184 with seek 24
Page 46 of 47
Disk head moves from 184 to 200 with seek 16
Disk head moves from 200 to 0 with seek 200
Disk head moves from 0 to 18 with seek 18
Disk head moves from 18 to 38 with seek 20
Disk head moves from 38 to 39 with seek 1
Disk head moves from 39 to 55 with seek 16
Disk head moves from 55 to 58 with seek 3
Disk head moves from 58 to 90 with seek 32
Total seek time is 390
Average seek time is 43.333332
Page 47 of 47