OS Practical Programs PDF
OS Practical Programs PDF
#include <stdio.h>
// Function to find the waiting time for all processes
int waitingtime(int proc[], int n,
int burst_time[], int wait_time[]) {
// waiting time for first process is 0
wait_time[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}
// Function to calculate turn around time
int turnaroundtime( int proc[], int n,
int burst_time[], int wait_time[], int tat[]) {
// calculating turnaround time by adding
// burst_time[i] + wait_time[i]
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}
//Function to calculate average time
int avgtime( int proc[], int n, int burst_time[]) {
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
//Function to find waiting time of all processes
waitingtime(proc, n, burst_time, wait_time);
//Function to find turn around time for all processes
turnaroundtime(proc, n, burst_time, wait_time, tat);
//Display processes along with all details
printf("Processes Burst Waiting Turn around \n");
// Calculate total waiting time and total turn
// around time
for ( i=0; i<n; i++) {
total_wt = total_wt + wait_time[i];
total_tat = total_tat + tat[i];
printf(" %d\t %d\t\t %d \t%d\n", i+1, burst_time[i], wait_time[i], tat[i]);
}
printf("Average waiting time = %f\n", (float)total_wt / (float)n);
printf("Average turn around time = %f\n", (float)total_tat / (float)n);
return 0;
}
// main function
int main() {
//process id's
int proc[] = { 1, 2, 3};
int n = sizeof proc / sizeof proc[0];
//Burst time of all processes
int burst_time[] = {5, 8, 12};
avgtime(proc, n, burst_time);
return 0;
}
Output
Processes Burst Waiting Turn around
1 5 0 5
2 8 5 13
3 12 13 25
Average Waiting time = 6.000000
Average turn around time = 14.333333
Link: https://www.youtube.com/watch?v=MZdVAVMgNpA
2. Write a C program to implement SJF process scheduling algorithm.
int main()
scanf("%d", &n);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
index = i;
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
A[0][2] = 0;
A[i][2] = 0;
A[i][2] += A[j][1];
total += A[i][2];
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// data.
total += A[i][3];
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
Link: https://www.youtube.com/watch?v=pYO-FAg-TpQ
Output:
Link: https://www.youtube.com/watch?v=TxjIlNYRZ5M
4. Write a C program to implement Priority process scheduling
algorithm
1 #include<stdio.h>
3 int main()
4 {
5 int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
7 scanf("%d",&n);
10 for(i=0;i<n;i++)
11 {
12 printf("\nP[%d]\n",i+1);
13 printf("Burst Time:");
14 scanf("%d",&bt[i]);
15 printf("Priority:");
16 scanf("%d",&pr[i]);
18 }
19
20 //sorting burst time, priority and process number in ascending order using selection sort
21 for(i=0;i<n;i++)
22 {
23 pos=i;
24 for(j=i+1;j<n;j++)
25 {
26 if(pr[j]<pr[pos])
27 pos=j;
28 }
29
30 temp=pr[i];
31 pr[i]=pr[pos];
32 pr[pos]=temp;
33
34 temp=bt[i];
35 bt[i]=bt[pos];
36 bt[pos]=temp;
37
38 temp=p[i];
39 p[i]=p[pos];
40 p[pos]=temp;
41 }
42
44
46 for(i=1;i<n;i++)
47 {
48 wt[i]=0;
49 for(j=0;j<i;j++)
50 wt[i]+=bt[j];
51
52 total+=wt[i];
53 }
54
56 total=0;
57
58 printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
59 for(i=0;i<n;i++)
60 {
62 total+=tat[i];
64 }
65
69
70return 0;
71}
Link: https://www.youtube.com/watch?v=rsDGfFxSgiY
5. Write C Program to simulate Multi Level Feedback Queue CPU
Scheduling algorithm.
1 #include<stdio.h>
3 #define N 10
5 typedef struct
6 {
8 int q, ready;
9 }process_structure;
10
12 {
13 if(t1 == 0 || t1 == 1 || t1 == 2 || t1 == 3)
14 {
15 return 1;
16 }
17 else
18 {
19 return 2;
20 }
21 }
22
23 int main()
24 {
25 int limit, count, temp_process, time, j, y;
26 process_structure temp;
28 scanf("%d", &limit);
29 process_structure process[limit];
31 {
32 printf("\nProcess ID:\t");
33 scanf("%d", &process[count].process_id);
34 printf("Arrival Time:\t");
35 scanf("%d", &process[count].arrival_time);
36 printf("Burst Time:\t");
37 scanf("%d", &process[count].burst_time);
38 printf("Process Priority:\t");
39 scanf("%d", &process[count].priority);
40 temp_process = process[count].priority;
41 process[count].q = Queue(temp_process);
42 process[count].ready = 0;
43 }
44 time = process[0].burst_time;
46 {
48 {
50 {
51 process[count].ready = 1;
52 }
53 }
54 for(count = y; count < limit - 1; count++)
55 {
57 {
59 {
61 {
62 temp = process[count];
63 process[count] = process[j];
64 process[j] = temp;
65 }
66 }
67 }
68 }
70 {
72 {
74 {
76 {
78 {
79 temp = process[count];
80 process[count] = process[j];
81 process[j] = temp;
82 }
83 else
84 {
85 break;
86 }
87 }
88 }
89 }
90 }
Link: https://www.youtube.com/watch?v=-94WGbrWveI
6. Write C Program to simulate Multi Level Queue CPU Scheduling
algorithm.
#include<stdio.h>
int main()
scanf("%d",&n);
for(i=0;i<n;i++)
p[i] = i;
scanf("%d",&bt[i]);
scanf("%d", &su[i]);
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=su[i];
su[i]=su[k];
su[k]=temp;
wtavg = wt[0] = 0;
for(i=1;i<n;i++)
for(i=0;i<n;i++)
return 0;
Output
Link: https://www.youtube.com/watch?v=hBPYP0ZEvS8
7. Write a C Program to simulate Multi Process Scheduling algorithm.
1 contributor
RawBlame
def __repr__(self):
class MultiProcessScheduler:
def __init__(self):
self.connWRITE = conn2
self._closestTask = None
self.queue = SimpleQueue()
# task is added
self.service = Process()
def __getClosestTask(self):
'''
'''
if self.connREAD.poll():
ret = None
while self.connREAD.poll():
ret = self.connREAD.recv()
self._closestTask = ret
return self._closestTask
raise TypeError
self.queue.put(task)
if not self.service.is_alive():
self.service = Process(
target=MultiProcessScheduler.__run,
daemon=False
self.service.start()
else:
closestTask = self.__getClosestTask()
self.cond.acquire()
self.cond.notify()
self.cond.release()
@staticmethod
tasksQueue = []
while True:
task = queue.get()
heappush(tasksQueue,task)
if tasksQueue:
now = time()
# closest one
p = Process(target=fn, args=args,
daemon=False)
p.start()
else:
conn.send(task)
cond.acquire()
cond.wait(timeout=delay)
break
print("[run] done")
if __name__ == "__main__":
s = MultiProcessScheduler()
s.add('foo')
except TypeError:
pass
else:
sleep(3)
sleep(1)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void main()
{
int n,i,j,k,temp=65,flag=0;
char process[20];
int brust[20],priority[20],pos;
int time=0,quantom=1,tbt=0;
int z=0,lottery[20],ticket[20][20],q=0;
printf("Enter Number Of Process: ");
scanf("%d",&n);
if(n<=0)
{
printf("\n\n:::: Invalid Value Of Number Of
Process ::::");
exit(0);
}
for(i=0;i<n;i++)
{
process[i] = temp;
temp+=1;
}
for(i=0;i<n;i++)
{
printf("\nEnter The Brust Time For Process %c:
",process[i]);
scanf("%d",&brust[i]);
printf("Enter The Priority For Process %c(b/w 1 to
%d): ",process[i],n);
scanf("%d",&priority[i]);
}
temp=process[i];
process[i]=process[pos];
process[pos]=temp;
temp=brust[i];
brust[i]=brust[pos];
brust[pos]=temp;
temp=priority[i];
priority[i]=priority[pos];
priority[pos]=temp;
if(brust[i]<0)
{
flag = 1;
}
}
if(flag==1)
{
printf("\n\n::: Invalid Time Entered ::: \n");
exit(0);
}
for(i=0;i<n;i++)
{
printf("\n | %d \t | %c \t| %d
|",priority[i],process[i],brust[i]);
tbt = tbt + brust[i];
}
printf("\n\n-------------------------------------");
printf("<<<< Winner: %d >>>>",winner);
printf("-------------------------------------\n");
if ((brust[q]>0))
{
brust[q]-=quantom;
if (brust[q]>0)
{
time+=quantom;
}
else
{
time+=(brust[q]+quantom);
}
if(brust[q]<0)
{
brust[q]=0;
}
printf("\n\t\t\t\t Process That Are Running Is:
%c",process[q]);
printf("\n\t (Total Time << Remaining Brust Time Of
This Process << process ): ( %d << %d << %c
)\n",time,brust[q],process[q]);
}
else
{
printf("\n\t\t >>>>>>Related Process With This Ticket
Has Been Completed<<<<<<\n");
}
}
9. write a C program to implement Memory Fixed Partitioning
Technique (MFT) algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define inp(x) scanf("%d",&x)
#define loop(i,n) for(i=0;i<n;++i)
#define N 1000
int mm_size, free_space, n, partition_count, process_count, pid,
partid;
int i,j,k;
int parts_allocated=0;
void println(){
printf("======================================================\n");
}
int alloc(int pid, Partition part[], Process pro[]){
loop(j,partition_count){
if(part[j].allocated==false && pro[i].req<=part[j].size){
//pro i in jth partition
part[j].allocated=true;
pro[i].int_frag= part[j].size - pro[i].req;
part[j].pid = pro[i].pid;
pro[i].partid = j;
free_space -= part[j].size;
printf("%d\t\t%d\t\tInternal = %d\n",i, j,
pro[i].int_frag);
++parts_allocated;
return 0;
}
}
//Could not allocate
pro[i].ext_frag = free_space;
if(free_space >= pro[i].req)
printf("%d\t\t-\t\tExternal = %d\n",i, pro[i].ext_frag);
else
printf("%d\t\t-\t\tInsufficient Memory\n",i);
}
void first_fit(Partition part[], Process pro[]){
println();
loop(i,process_count){
alloc(i, part, pro);
}
}
void dealloc(int pid, Process pro[], Partition part[]){
parts_allocated--;
partid = pro[pid].partid;
printf("Freeing Partition %d\n",partid);
pro[pid].allocated = false;
if(partid>=0 && partid<partition_count)
part[partid].allocated = false;
printf("P%d deallocated.\n",pid);
}
void initialize_pro(int i, Process pro[]){
printf("Process %d : ", i);
inp(pro[i].req);
pro[i].pid = i;
pro[i].partid = -1;
pro[i].allocated = false;
pro[i].int_frag = -1;
pro[i].ext_frag = -1;
}
int main(){
system("clear");
printf("MFT (First Fit)\n");
println();
printf("Enter Main memory size\n");
inp(mm_size);
loop(i,partition_count){
printf("Part %d : ",i);
inp(part[i].size);
part[i].allocated = false;
part[i].partid = i;
part[i].pid = -1;
free_space += part[i].size;
}
println();
if(free_space != mm_size){
printf("Sum of partitions does not match MM size\n");
return -1;
}
printf("Enter no. of processes\n");
inp(process_count);
printf("Enter memory reqd for \n");
Process pro[N];
loop(i,process_count){
initialize_pro(i, pro);
}
println();
printf("\nPID\t\tPartID\t\tIssues/Fragments\n");
first_fit(part, pro);
again:
printf("\nPress 1 to add more : ");
inp(i);
println();
if(i!=1)
return 0;
if(parts_allocated == partition_count){
printf("Partitions Full\nEnter PID to remove from memory :
");
inp(pid);
dealloc(pid, pro, part);
}
i = process_count++;
printf("Enter size\n");
initialize_pro(i,pro);
alloc(i, part, pro);
goto again;
return 0;
}
[vishnu@os memory]$ cc mft.c
[vishnu@os memory]$ ./a.out
MFT (First Fit)
======================================================
Enter Main memory size
10
Enter no. of partitions
5
Enter size of each partitions
Part 0 : 1
Part 1 : 1
Part 2 : 2
Part 3 : 2
Part 4 : 4
======================================================
Enter no. of processes
4
Enter memory reqd for
Process 0 : 1
Process 1 : 1
Process 2 : 1
Process 3 : 1
======================================================
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define inp(x) scanf("%d",&x)
#define loop(i,n) for(i=0;i<n;++i)
#define N 1000
int mm_size, slot_count = 1, pid = -1, pos=0, free_mem, pid_del;
int i, j, k;
int parts_allocated=0;
void println(){
printf("======================================================\n");
}
void mem_table(){
println();
printf("Memory Contents\n");
println();
printf("Size\t\tContents\t\tRange\n");
loop(i,slot_count){
printf("%d\t\t",slots[i].size);
(slots[i].pid==-1) ? printf("-\t\t\t%d-
%d\n",slots[i].start,slots[i].end) :
printf("P%d\t\t\t%d-
%d\n",slots[i].pid,slots[i].start,slots[i].end);
}
println();
}
int find_slot(){
for(i=0; i<slot_count; i++)
if(slots[i].pid==-1 && slots[i].size >=
pro[pid].req)
return i;
return -1;
}
void alloc(int pos)
{
//Move everything to right to create a slot at pos
for(i=slot_count; i>pos; i--)
slots[i] = slots[i-1];
slots[pos].pid = pid;
slots[pos].size = pro[pid].req;
pro[pid].allocated = true;
slots[pos].empty == false;
slots[pos].start == (pos==0)?0:slots[pos+1].end;
slots[pos].end= slots[pos].start+ slots[pos].size;
free_mem -= pro[pid].req;
//Decrease req from the chosen chunk
slots[pos+1].size -= pro[pid].req;
slots[pos+1].start += pro[pid].req;
slot_count++;
}
loop(i, slot_count)
if(slots[i].pid == pid_del){
dealloc_merge(i);
break;
}
}
else
return 0;
mem_table();
goto again;
return 0;
}
[vishnu@os memory]$ cc mvt.c
[vishnu@os memory]$ ./a.out
MVT
======================================================
Enter Main memory size
32
======================================================
Memory Contents
======================================================
Size Contents Range
32 - 0-32
======================================================
1. Allocate 2. Deallocate
1
Mem for P0 : 10
======================================================
Memory Contents
======================================================
Size Contents Range
10 P0 0-10
22 - 10-32
======================================================
1. Allocate 2. Deallocate
1
Mem for P1 : 20
======================================================
Memory Contents
======================================================
Size Contents Range
10 P0 0-10
20 P1 10-30
2 - 30-32
======================================================
1. Allocate 2. Deallocate
1
Mem for P2 : 3
Insufficient Memory
======================================================
Memory Contents
======================================================
Size Contents Range
10 P0 0-10
20 P1 10-30
2 - 30-32
======================================================
1. Allocate 2. Deallocate
2
Enter PID to remove : 1
======================================================
Memory Contents
======================================================
Size Contents Range
10 P0 0-10
22 - 10-30
======================================================
1. Allocate 2. Deallocate
Link:
https://www.youtube.com/watch?v=JdPmsrYqRDY&t=571s
11 write a program to simulate Dynamic Memory Allocation
in C using malloc().
12 write a program to simulate Dynamic Memory Allocation
in C using calloc().
13 write a program to simulate Dynamic Memory Allocation
in C using free().
14 write a program to simulate Dynamic Memory Allocation
in C using realloc().
Link: https://github.com/RAGUL1902/Dynamic-Memory-
Allocation-in-C#freec
#include<stdio.h>
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of processes:");
scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}
// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof(blockSize)/sizeof(blockSize[0]);
int n = sizeof(processSize)/sizeof(processSize[0]);
// Driver code
int main()
{
int m; //number of blocks in the memory
int n; //number of processes in the input queue
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
m = sizeof(blockSize) / sizeof(blockSize[0]);
n = sizeof(processSize) / sizeof(processSize[0]);
return 0 ;
}
Link: https://www.youtube.com/watch?v=N3rG_1CEQkQ
#include<stdio.h>
#define MAX 50
int main()
{
int page[MAX],i,n,f,ps,off,pno;
int choice=0;
printf("\nEnter the no of peges in memory: ");
scanf("%d",&n);
printf("\nEnter page size: ");
scanf("%d",&ps);
printf("\nEnter no of frames: ");
scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any frame)\n\n");
printf("\npageno\tframeno\n-------\t-------");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i);
scanf("%d",&page[i]);
}
do
{
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
return 1;
}
Output:
Link: https://www.youtube.com/watch?v=6c-mOFZwP_8
20. write a C program for Static Memory Allocation.
// C program to implement
// static memory allocation
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main()
{
int size;
printf("Enter limit of the text: \n");
scanf("%d", &size);
char str[size];
printf("Enter some text: \n");
scanf(" ");
gets(str);
printf("Inputted text is: %s\n", str);
return 0;
}
Input:
Output:
21. write a C program for Dynamic Memory Allocation.
• C
// Driver Code
int main()
{
int size, resize;
char* str = NULL;
printf("Enter limit of the "
"text: \n");
scanf("%d", &size);
return 0;
}
Input:
Output:
22. write a C program to implement FIFO page replacement
algorithm.
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
Link: https://www.youtube.com/watch?v=8rcUs5RutX0
23. write a C program to implement LRU page replacement
algorithm.
#include<stdio.h>
#include<limits.h>
return 0;
}
int main()
{
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printFrame(queue, occupied);
}
else{
if(queue[j] == incomingStream[k])
break;
}
printf("\n");
}
return 0;
}
Output –
#include<stdio.h>
int main()
{
int m, n, position, k, l;
int a = 0, b = 0, page_fault = 0;
int total_frames = 3;
int frames[total_frames];
int temp[total_frames];
int pages[] = {1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3, 6, 1, 2, 4, 3};
int total_pages = sizeof(pages)/sizeof(pages[0]);
return 0;
}
Output –
1: 1 -1 -1
2: 1 2 -1
3: 1 2 3
2: 1 2 3
1: 1 2 3
5: 1 2 5
2: 1 2 5
1: 1 2 5
6: 1 2 6
2: 1 2 6
5: 5 2 6
6: 5 2 6
3: 5 3 6
1: 1 3 6
3: 1 3 6
6: 1 3 6
1: 1 3 6
2: 1 2 6
4: 1 2 4
3: 3 2 4
Link: https://www.youtube.com/watch?v=dYIoWkCvd6A
24. Write a C program to simulate Optimal page replacement
algorithms.
class GFG {
// driver function
public static void main(String[] args)
{
int pg[]
= { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 };
int pn = pg.length;
int fn = 4;
optimalPage(pg, pn, fn);
}
}
Link: https://www.youtube.com/watch?v=AyEf36TlQjU
if(j==f)
{ min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i]; cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
}
printf(“\tPF No. %d”,pf);}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}
INPUT
Enter number of page references -- 10
Enter the reference string -- 1 2 3 4 5 2 5 2 5 1 4 3
Enter the available no. of frames -- 3
OUTPUT
The Page Replacement Process is –
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 2 3
5 2 3
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
// Driver code
public static void main(String[] args)
{
// request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
FCFS(arr, head);
}
}
Output:
Total number of seek operations = 510
Seek Sequence is
176
79
34
60
92
11
41
114
Link: https://www.youtube.com/watch?v=yP89YlEGCqA
27. Write a C program to implement SCAN Disk scheduling
algorithm.
#include<stdio.h>
int absoluteValue(int); // Declaring function absoluteValue
void main()
{
int queue[25],n,headposition,i,j,k,seek=0, maxrange,
difference,temp,queue1[20],queue2[20],temp1=0,temp2=0;
float averageSeekTime;
// END OF CODE
//Code written and commented by Nived Kannada
Output
Link: https://www.youtube.com/watch?v=xouo556RGiE
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
Link: https://www.youtube.com/watch?v=vLqZ6ZMBkX8
class GFG{
// Driver code
public static void main(String []args)
{
// Request array
int arr[] = { 176, 79, 34, 60,
92, 11, 41, 114 };
int head = 50;
CLOOK(arr, head);
}
}
Output:
Initial position of head: 50
Total number of seek operations = 321
Seek Sequence is
60
79
92
114
176
11
34
41
Link: https://www.youtube.com/watch?v=gwCgG5ORXW8
Code:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
Output:-
Link: https://www.youtube.com/watch?v=P_dA8VGJjA8
31. Write an algorithm to implement SCAN Disk scheduling
algorithm.
Link: https://www.youtube.com/watch?v=xouo556RGiE
Program Output:
Enter the directory name:sss
Enter the number of files:3
Enter file name to be created:aaa
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:bbb
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:ccc
Do you want to enter another file(yes - 1 or no - 0):0
Directory name is:sss
Files names are:
aaa
bbb
ccc
Link: https://www.youtube.com/watch?v=DKzSGo9b28Q
#include
#include
struct st
{
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
{
int i,j,k,n;
clrscr();
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)< span="" style="box-sizing: border-box;">
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)< span="" style="box-sizing: border-box;">
{
printf("enter file name:");
scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)< span="" style="box-sizing: border-box;">
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)< span="" style="box-sizing: border-box;">
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)< span="" style="box-sizing: border-box;">
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");
}
getch();
}
</dir[i].sds[j];k++)<></dir[i].ds;j++)<></n;i++)<></dir[i].sds[j];k++)<></dir[
i].ds;j++)<></n;i++)<>
Link: https://www.youtube.com/watch?v=dNwdZs0iWWU
35. Write program to Implementation indexed file allocation.
Program Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the
index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the
index 6 on the disk :
2
78
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
Link:
https://www.youtube.com/watch?v=S6lLRz7SQU
w
36. Write program to Implementation linked file allocation.
Program to implement
linked file allocation
technique.
BY · PUBLISHED OCTOBER 21, 2019 · UPDATED OCTOBER 21, 2019
Concept: In the chained method file allocation table contains a field which
points to starting block of memory. From it for each bloc a pointer is kept to
next successive block. Hence, there is no external fragmentation
ALGORTHIM:
Step 1: Start
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q=
random(100);
a) Check whether the selected location is free .
b) If the location is free allocate and set flag=1 to the allocated locations.
While allocating next location address to attach it to previous location
for(i=0;i0)
{
}
}
p=r[i][j-1];
b[p].next=q;
}
Step 5: Print the results file no, length ,Blocks allocated.
Step 6: Stop
#include
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)< span="" style="box-sizing: border-box;">
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & length");
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}
</p;i++)<>
OUTPUT:
Enter how many blocks that are already allocated 3
Enter the blocks no.s that are already allocated 4 7 9
Enter the starting index block & length 3
7
3->1
4->1 file is already allocated
5->1
6->1
7->1 file is already allocated
8->1
9->1file is already allocated
10->1
11->1
12->1
Link: https://www.youtube.com/watch?v=irGdM3iIS54
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int start[10],num=0,count=0,length[10],j,f=1,i=0;
char name[20][10];
char del[2];
int ch=0;
while(1){
printf("Contiguous file allocation\n");
printf("1.File Creation\n");
printf("2.File Deletion\n");
printf("3.Display File Allocation Table\n");
printf("4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the name of the file\n");
scanf("%s",&name[i][0]);
printf("Enter the start block of the file\n");
scanf("%d",&start[i]);
printf("Enter the length of the file\n");
scanf("%d",&length[i]);
num++;
i++;
if(f==1){
f++;
}
for(j=0;j<num;j++){
if(start[j+1]<=start[j] || start[j+1]>=length[j]){
}
else{
count++;
}
}
if(count==1){
printf("%s cannot be allocated disk space\n",name[i-1
]);
}
else{
printf("file %s allocated disk space\n",name[i-1]);
}
break;
case 2:
printf("Enter the name of the file to be deleted\n");
scanf("%s",&del[0]);
printf("file %s deleted\n",&del[0]);
f--;
f--;
break;
case 3:
printf("File Allocation Table\n");
printf("File Name Start Block Length\n");
if(f==2){
printf("%s %d %d\n",name[0],start[0],length[0]);
}
for(int k=0,n=1;k<num && n<num ;k++,n++){
if(start[k+1]<=start[k] || start[k+1]>=length[k]){
printf("%s %d %d\n",name[n],start[n],length[n]);
}
}
break;
case 4:
exit(1);
default:
printf("invalid");
}
getchar();
}
return 0;
}
Link: https://www.youtube.com/watch?v=XHx-ms5Ldi4