OS_Programs
OS_Programs
Operating System
LAB MANUAL
Department of IT
(A.Y. 2024-25)
Credit
Teaching Scheme Examination Scheme
Scheme
Practical : 02 Credits Term work : 25 Marks
4 Hrs. / Week Practical: 25 Marks
Prerequisites:
1. C Programming
2. Fundamentals of Data Structure
Course Objectives:
Course Outcomes:
CO3: Implement basic building blocks like processes, threads under the Linux.
CO4: Develop various system programs for the functioning of OS concepts in user space like
concurrency control, CPU Scheduling, Memory Management and Disk Scheduling in Linux.
CO5: Develop system programs for Inter Process Communication in Linux.
INDEX
1 A. Study of Basic Linux Commands: echo, ls, read, cat, touch, test, loops,
arithmetic comparison, conditional loops, grep, sed etc.
2 Process control system calls: The demonstration of FORK, EXECVE and WAIT
system calls along with zombie and orphan states.
3 Implement the C program for CPU Scheduling Algorithms: Shortest Job First
(Pre-emptive) and Round Robin with different arrival time.
6 Implement the C program for Page Replacement Algorithms: FCFS, LRU, and
Optimal for frame size as minimum three.
8 Implement the C program for Disk Scheduling Algorithms: SSTF, SCAN, C-Look
considering the initial head position moving away from the spindle.
Study Assignment: Implement a new system call in the kernel space, add this new system
call in the Linux kernel by the compilation of this kernel (any kernel source, any architecture
and any Linux kernel distribution) and demonstrate the use of this embedded system call
using C program in user space.
Assignment 1A
Problem Statement: Study of Basic Linux Commands: echo, ls, read, cat, touch, test, loops,
arithmetic comparison, conditional loops, grep, sed etc.
Program:
# 1) Print
echo "Students"
# 2) Remove spaces
echo -e "PES \bModern \bCollege \bOf \bEngineering"
# 5) Add tabs
echo -e "Operating \tSystem \tLab"
## ls
## read
## cat
## touch
# 1) Creat file
touch tempfile.txt
## test
## Loops
# 1) For loop
for i in 1 2 3 4 5
do
echo "Value of i $i"
done
# 2) While loop
num=0
while [ "$num" -lt 5 ]
do
echo "Value of num $num"
num=`expr $num + 1`
done
## Conditionals
# 1) If else block
read num
if [ `expr $num % 2` -eq 0 ]
then
echo "$num is even"
else
echo "$num is odd"
fi
## grep
# 3) Return the lines contains given string also adds line number at
begining
grep -n '20' ./myaddressbook.txt
## sed
# 1) Replace 20 with 30
sed 's/20/30/' ./myaddressbook.txt
Output:
Roll No.: Name:
Assignment 1B
Problem Statement: Write a program to implement an address book with options given
below: a) Create address book. b) View address book. c) Insert a record. d) Delete a record.
e) Modify a record. f) Exit.
Program 1:
#!/bin/bash
clear
choice=0
address_book='addressbook'
case "$choice" in
"1")
echo ""
read -p "Enter address book name: " address_book
touch D:/Ubuntu18/osl/practical1/$address_book.txt
if [ $? -eq 0 ]
then
echo -e "\n$address_book.txt created."
else
echo "Could not create $address_book.txt"
fi
;;
"2")
echo -e "\n---- Record of address book ---- \n"
cat D:/Ubuntu18/osl/practical1/$address_book.txt
echo ""
if [ $? -ne 0 ]
then
echo "Could not read $address_book.txt"
fi
;;
"3")
# Name validation
read -p "Enter name: " name
name_pattern="^[A-Za-z]"
while [[ ! "$name" =~ $name_pattern ]]
do
read -p "Please enter valid name: " name
done
# Email validation
read -p "Enter email : " email
email_pattern="^[a-z0-9._%-+]+@[a-z]+\.[a-z]{2,4}$"
while [[ ! $email =~ $email_pattern ]]
do
read -p "Please enter valid email: " email
done
# Age validation
read -p "Enter age : " age
age_pattern="[0-9]"
while [[ ! $age =~ $age_pattern ]]
do
read -p "Please enter valid age: " age
done
# Address validation
read -p "Enter address : " address
address_pattern="^[A-Za-z0-9]"
while [[ ! $address =~ $address_pattern ]]
do
read -p "Please enter valid address: " address
done
if [ $? -eq 0 ]
then
echo -e "\nData inserted to $address_book.txt."
else
echo "Could not insert data to $address_book.txt"
fi
;;
"4")
echo -e "\n---- Record of address book ---- \n"
cat D:/Ubuntu18/osl/practical1/$address_book.txt
echo ""
read -p "Enter user name to delete record: " delete_name
sed -i "/$delete_name/d"
D:/Ubuntu18/osl/practical1/$address_book.txt
if [ $? -ne 0 ]
then
echo "Could not delete data in $address_book.txt"
else
echo -e "\n---- Record of address book ---- \n"
cat D:/Ubuntu18/osl/practical1/$address_book.txt
fi
;;
"5")
echo -e "\n---- Record of address book ---- \n"
cat D:/Ubuntu18/osl/practical1/$address_book.txt
sed -i "s/$search_string/$modify_string/"
D:/Ubuntu18/osl/practical1/$address_book.txt
if [ $? -ne 0 ]
then
echo "Could not modify $address_book.txt"
else
echo -e "\n---- Record of address book ---- \n"
cat D:/Ubuntu18/osl/practical1/$address_book.txt
fi
;;
"6")
echo ""
read -p "Enter name to search: " search_string
if [ $? -ne 0 ]
then
echo "Could not search data in $address_book.txt"
fi
;;
"7")
echo "Exit."
exit
;;
esac
done
exit
Output:
Roll No.: Name:
Assignment 2A
Problem Statement: Process control system calls: The demonstration of FORK, EXECVE
and WAIT system calls along with zombie and orphan states.
A. Implement the C program in which main program accepts the integers to be sorted. Main
program uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT
system call to sort the integers using any sorting algorithm. Also demonstrate zombie and
orphan states.
Program:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#define MAX 20
int i,j,temp;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
void bubble_dsc(int arr[],int n)
int i,j,temp;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(arr[j]<arr[j+1])
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
int i;
for(i=0;i<n;i++)
printf("\t%d",arr[i]);
printf("\n\n");
int main()
int i,n,arr[MAX],f,pid;
scanf("%d",&n);
printf("ENTER ARRAY ELEMENT : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
pid=fork();
if(pid==0)
/*ORPHAN STATE */
printf("\n\t\t**********ORPHAN STATE**************\n");
sleep(5);
bubble_asc(arr,n);
print(arr,n);
/*ZOMBIE STATE */
printf("\n\t\t\t**********ZOMBIE STATE**************\n");
bubble_dsc(arr,n);
print(arr,n);
else
printf("\nERROR IN FORK!!");
return 0;
Output:
Roll No.: Name:
Assignment 2B
Problem Statement: Process control system calls: The demonstration of FORK, EXECVE
and WAIT system calls along with zombie and orphan states.
B. Implement the C program in which main program accepts an array. Main program uses the
FORK system call to create a new process called a child process. Parent process sorts an
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
which display array in reverse order.
Program B.1:
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include<stdlib.h>
int i,j,temp;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
for(i=0;i<n;i++)
printf("\t%d",arr[i]);
int i,j=2,l,k;
int n=argc-2;
int a[n];
char *args[n+1];
args[0]=argv[1];
for(i=1;i<=n;i++,j++)
args[i]=argv[j];
args[i]=(char*)0;//0 1 2 3 4 5 6
//second 5 2 7 9 4 '0'
for(l=0,i=1;i<=n;i++,l++)
a[l]=atoi(args[i]);
bubble_asc(a,n);
for(l=0,i=1;i<=n;i++,l++)
sprintf(args[i],"%d",a[l]);
pid_t pid;
pid=fork();
if(pid==0)
execve(argv[1],args,NULL);
else
k=wait(0);
return 0;
Output:
Program B.2:
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include<stdlib.h>
int i,j,temp;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
{
if(arr[j]<arr[j+1])
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
for(i=0;i<n;i++)
printf("\t%d",arr[i]);
printf("\n");
int i,j=1,l;
int n=argc-1;
int a[n];
char *args[n+1];
for(i=0;i<=n;i++,j++)
args[i]=argv[j];
args[i]=(char*)0;
for(l=0,i=0;i<n;i++,l++)
{
a[l]=atoi(args[i]);
bubble_dsc(a,n);
return 0;
Output:
Roll No.: Name:
Assignment 3
Problem Statement: Implement the C program for CPU Scheduling Algorithms: Shortest
Job First (Pre-emptive) and Round Robin with different arrival time.
Program 1:
//sjf_preemptive
#include <stdio.h>
#include <stdbool.h>
struct Process
{
int pid;
int at;
int bt;
int ct, tt, wt, rt, st;
};
int main()
{
int size = 0;
printf("Enter number of processes: ");
scanf("%d", &size);
struct Process ps[size];
printf("\
n======================================================================
==============\n\n");
printf("PID\tAT \t BT\n");
for (int i = 0; i < size; i++)
{
printf("%d \t %d \t %d \n", ps[i].pid, ps[i].at, ps[i].bt);
}
printf("\n\
n======================================================================
==============\n\n");
// Input Done
int n = size;
int completed = 0;
int currentTime = 0;
int burstTimeR[4];
bool iscompleted[4] = {false};
while (completed != n)
{
if (miniI == -1)
{
currentTime++;
}
else
{
if (burstTimeR[miniI] == ps[miniI].bt)
{
ps[miniI].st = currentTime;
}
burstTimeR[miniI] -= 1;
currentTime++;
if (burstTimeR[miniI] == 0)
{
ps[miniI].ct = currentTime;
ps[miniI].tt = ps[miniI].ct - ps[miniI].at;
ps[miniI].wt = ps[miniI].tt - ps[miniI].bt;
ps[miniI].rt = ps[miniI].st - ps[miniI].at;
avgWT += ps[miniI].wt;
avgTT += ps[miniI].tt;
avgRT += ps[miniI].rt;
completed++;
iscompleted[miniI] = true;
}
}
}
printf("PID \t AT \t BT \t CT \t TT \t WT \t RT \t\n");
for (int i = 0; i < n; i++)
{
printf("%d \t %d \t %d \t %d \t %d \t %d \t %d \t\n",
ps[i].pid, ps[i].at, ps[i].bt, ps[i].ct, ps[i].tt, ps[i].wt, ps[i].rt);
}
printf("\n\
n======================================================================
==============\n");
Output:
Program 2:
//round_robin
#include <stdio.h>
// #include <limits.h>
#include <stdbool.h> //for bool datatype
// #include <stdlib.h> //for qsort
struct process_struct
{
int pid;
int at;
int bt;
int ct, wt, tat, rt, start_time;
int bt_remaining;
} ps[100];
int main()
{
int n, index;
int cpu_utilization;
// queue<int> q;
while (completed != n)
{
index = queue[front];
// q.pop();
front++;
if (ps[index].bt_remaining == ps[index].bt)
{
ps[index].start_time = findmax(current_time, ps[index].at);
total_idle_time += (is_first_process == true) ? 0 :
ps[index].start_time - current_time;
current_time = ps[index].start_time;
is_first_process = false;
}
if (ps[index].bt_remaining - tq > 0)
{
ps[index].bt_remaining -= tq;
current_time += tq;
}
else
{
current_time += ps[index].bt_remaining;
ps[index].bt_remaining = 0;
completed++;
ps[index].ct = current_time;
ps[index].tat = ps[index].ct - ps[index].at;
ps[index].wt = ps[index].tat - ps[index].bt;
ps[index].rt = ps[index].start_time - ps[index].at;
sum_tat += ps[index].tat;
sum_wt += ps[index].wt;
sum_rt += ps[index].rt;
}
// check which new Processes needs to be pushed to Ready Queue from
Input list
for (int i = 1; i < n; i++)
{
if (ps[i].bt_remaining > 0 && ps[i].at <= current_time &&
visited[i] == false)
{
// q.push(i);
queue[++rear] = i;
visited[i] = true;
}
}
// check if Process on CPU needs to be pushed to Ready Queue
if (ps[index].bt_remaining > 0)
// q.push(index);
queue[++rear] = index;
// Output
printf("\nProcess No.\tAT\tCPU Burst Time\tStart Time\tCT\tTAT\tWT\
tRT\n");
for (int i = 0; i < n; i++)
printf("%d\t\t%d\t%d\t\t%d\t\t%d\t%d\t%d\t%d\n", i, ps[i].at,
ps[i].bt, ps[i].start_time, ps[i].ct, ps[i].tat, ps[i].wt, ps[i].rt);
printf("\n");
Output:
Roll No.: Name:
Assignment 4A
Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
/*
This program provides a possible solution for producer-consumer problem
using mutex and semaphore.
I have used 5 producers and 5 consumers to demonstrate the solution.
You can always play with these values.
*/
sem_t empty;
sem_t full;
int in = 0;
int out = 0;
int buffer[BufferSize];
pthread_mutex_t mutex;
int main()
{
pthread_t pro[5],con[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&empty,0,BufferSize);
sem_init(&full,0,0);
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
Output:
Roll No.: Name:
Assignment 4B
Problem Statement: Thread synchronization and mutual exclusion using mutex. Application
to demonstrate: Reader- Writer problem with reader priority.
Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
/*
This program provides a possible solution for first readers writers
problem using mutex and semaphore.
I have used 10 readers and 5 producers to demonstrate the solution. You
can always play with these values.
*/
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt); // If this id the first reader, then it will
block the writer
}
pthread_mutex_unlock(&mutex);
// Reading Section
printf("Reader %d: read count as %d\n",*((int *)rno),cnt);
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
Output:
Roll No.: Name:
Assignment 5
Problem Statement: Implement the C program for Deadlock Avoidance Algorithm: Bankers
Algorithm.
Program:
// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
printf("\nEnter Number of processes: ");
scanf("%d", &n);
int alloc[n][m];
int max[n][m];
if (flag == 0)
{
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
if (flag == 1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}
Output:
Roll No.: Name:
Assignment 6
Problem Statement: Implement the C program for Page Replacement Algorithms: FCFS,
LRU, and Optimal for frame size as minimum three.
Program 1:
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
struct PageTable
{
int frame_no;
bool valid;
};
int main()
{
int i,n,no_of_frames,page_fault=0,current=0;
bool flag=false;
printf("\n Enter the no. of pages:\n");
scanf("%d",&n);
//create reference string array
int reference_string[n];
printf("\n Enter the reference string(different page numbers) :\
n");
for(int i=0;i<n;i++)
scanf("%d",&reference_string[i]);
printf("\n Enter the no. of frames you want to give to the
process :");
scanf("%d",&no_of_frames);
//create frame array to store the pages at different point of times
int frame[no_of_frames];
memset(frame,-1,no_of_frames*sizeof(int));
struct PageTable PT[50] ; //asume page table can have entries for
page 0 to 49
for(int i=0;i<50;i++)
PT[i].valid=0;
Output:
Program 2:
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<limits.h>
struct PageTable
{
int frame_no;
int last_time_of_access;
bool valid;
};
//Function to check if referenced/asked page is already present in
frame[] or not
//Returns true if page is already present else returns false
bool isPagePresent(struct PageTable PT[],int page)
{
if(PT[page].valid == 1)
return true;
return false;
}
int main()
{
int i,n,no_of_frames,page_fault=0,current=0;
bool flag=false;
printf("\n Enter the no. of pages:\n");
scanf("%d",&n);
//create reference string array
int reference_string[n];
printf("\n Enter the reference string(different page numbers) :\
n");
for(int i=0;i<n;i++)
scanf("%d",&reference_string[i]);
printf("\n Enter the no. of frames you want to give to the
process :");
scanf("%d",&no_of_frames);
//create frame array to store the pages at different point of times
int frame[no_of_frames];
memset(frame,-1,no_of_frames*sizeof(int));
struct PageTable PT[50] ; //asume page table can have entries for
page 0 to 49
for(int i=0;i<50;i++)
PT[i].valid=0;
frame[current]=reference_string[i];
printFrameContents(frame,no_of_frames);
updatePageTable(PT,reference_string[i],current,1,i);
current = current + 1;
if(current == no_of_frames)
{
//current=0;
flag=true;
}
frame[LRU_page_index]=reference_string[i];
printFrameContents(frame,no_of_frames);
//Update PT
updatePageTable(PT,reference_string[i],LRU_page_index,1,i);
}
}
//Update the Page Access time for reference_string[i]
PT[reference_string[i]].last_time_of_access = i;
} //end of for loop
Output:
Roll No.: Name:
Assignment 7A
A. FIFOS: Full duplex communication between two independent processes. First process
accepts sentences and writes on one pipe to be read by second process and second process
counts number of characters, number of words and number of lines in accepted sentences,
writes this output in a text file and writes the contents of the file on second pipe to be read by
first process and displays on standard output.
Program 1:
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
int main()
{
int fd,fd1;
char * myfifo = "myfifo";
char * myfifo1 = "myfifo1";
char buf[1024];
mkfifo(myfifo,0666);
mkfifo(myfifo1,0777);
fd=open(myfifo,O_WRONLY);
write(fd,"Hello Everyone! \nWelcome to Progressive Education
Society's Modern College of Engineering! \nPune, Maharashtra,
India.",sizeof("Hello Everyone! \nWelcome to Progressive Education
Society's Modern College of Engineering! \nPune, Maharashtra,
India."));
close(fd);
unlink(myfifo);
fd1=open(myfifo1,O_RDONLY);
read(fd1,buf,sizeof(buf));
printf("%s", buf);
unlink(myfifo1);
close(fd1);
return 0;
}
Output:
Program 2:
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
int main()
{
int fd,fd1;
char * myfifo = "myfifo";
char * myfifo1 = "myfifo1";
char buf[1024], ch[400];
mkfifo(myfifo1, 0777);
fd=open(myfifo, O_RDONLY);
read(fd, buf, 1024);
printf("\nFirst message received: \n\n%s\n\n",buf);
while(buf[i]!='\0')
{
while(buf[i]==' ')
{
words++;
i++;
}
while(buf[i]=='\n')
{
line++;
i++;
}
i++;
character++;
}
fp=fopen("test.txt","w+");
fprintf(fp,"\nTotal Words := %d\n", words);
fprintf(fp,"\nTotal Lines := %d\n", line);
fprintf(fp,"\nTotal Charactes := %d\n", character);
fclose(fp);
fp=fopen("test.txt","r");
while(!feof(fp))
{
ch[j]=fgetc(fp);
j++;
}
fclose(fp);
close(fd);
unlink(myfifo);
fd1=open(myfifo1,O_WRONLY);
write(fd1,ch, strlen(ch));
close(fd1);
return 0;
}
Output:
Roll No.: Name:
Assignment 7B
Program:
Client:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 1;//5679
*shm = '*';
printf("\n\nClient Terminated!!!\n\n");
exit(0);
}
Server:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 1; // 5679
s = shm;
/*
* Waiting for other process
*/
printf("\n\nServer Terminated!!!\n\n");
exit(0);
}
Output:
Roll No.: Name:
Assignment 8
Problem Statement: Implement the C program for Disk Scheduling Algorithms: SSTF,
SCAN, C-Look considering the initial head position moving away from the spindle.
Program 1: SCAN
#include <stdio.h>
//#inlude<Maths.h>
#include <limits.h>
int no_Track, N, Initial_Head_Pos, No_head_move, direction;
right[r++] = no_Track - 1;
for (int i = 0; i < N; i++)
{
if (req_que[i] > Initial_Head_Pos)
{
right[r++] = req_que[i];
}
}
left[l++] = 0;
for (int i = 0; i < N; i++)
{
if (req_que[i] < Initial_Head_Pos)
{
left[l++] = req_que[i];
}
}
sort(right, 0, r);
sort(left, 1, l);
int completed = 2;
while (completed > 0)
{
if (direction == 0)
{
for (int i = 0; i < r; i++)
{
No_head_move += abs(Initial_Head_Pos - right[i]);
Initial_Head_Pos = right[i];
printf("\t%d", right[i]);
}
direction = 1;
}
else
{
for (int i = 0; i < l; i++)
{
No_head_move += abs(Initial_Head_Pos - left[i]);
Initial_Head_Pos = left[i];
printf("\t%d", left[i]);
}
direction = 0;
}
completed--;
}
/* printf("\nright");
for(int i=0;i<r;i++)
printf("\t%d",right[i]);
printf("\nleft");
for(int i=0;i<l;i++)
printf("\t%d",left[i]);*/
}
void main()
{
printf("Enter the Number of track on Disk:");
scanf("%d", &no_Track);
printf("Enter the Number of process requesting to access Disk:");
scanf("%d", &N);
int Request_que[N];
printf("Enter the Request Queue:");
scanf("%d", &Initial_Head_Pos);
Output:
Program 2: SSTF
#include <stdio.h>
//#inlude<Maths.h>
#include <limits.h>
int no_Track, N, Initial_Head_Pos, No_head_move;
printf("\n=========================================");
printf("\n O U T P U T ");
printf("\n SEEK Sequesnce:");
SSTF(Request_que, Visited);
printf("\n Number of Head Move:%d", No_head_move);
}
Output:
Program 3: LOOK
#include <stdio.h>
//#inlude<Maths.h>
#include <limits.h>
int no_Track, N, Initial_Head_Pos, No_head_move, direction;
// adding the request in right which are higher than initial head
possition
// adding the request in left which are lower than initial head
possition
sort(right, r);
sort(left, l);
int completed = 2;
while (completed > 0)
{
if (direction == 0)
{
for (int i = 0; i < r; i++)
{
No_head_move += abs(Initial_Head_Pos - right[i]);
Initial_Head_Pos = right[i];
printf("\t%d", right[i]);
}
direction = 1;
}
else
{
for (int i = 0; i < l; i++)
{
No_head_move += abs(Initial_Head_Pos - left[i]);
Initial_Head_Pos = left[i];
printf("\t%d", left[i]);
}
direction = 0;
}
completed--;
}
/* printf("\nright");
for(int i=0;i<r;i++)
printf("\t%d",right[i]);
printf("\nleft");
for(int i=0;i<l;i++)
printf("\t%d",left[i]);*/
}
void main()
{
printf("Enter the Number of track on Disk:");
scanf("%d", &no_Track);
printf("Enter the Number of process requesting to access Disk:");
scanf("%d", &N);
int Request_que[N];
printf("Enter the Request Queue:");
scanf("%d", &Initial_Head_Pos);