0% found this document useful (0 votes)
51 views10 pages

Swe3001 Da6

The document contains code snippets for different disk scheduling algorithms: - SSTF disk scheduling algorithm is implemented to calculate the total head movement. - SCAN disk scheduling algorithm sorts the requests and services them in one direction. - FCFS disk scheduling algorithm calculates the total seek operations by servicing requests in the order of arrival. - SEQUENTIAL, INDEXED and LINKED disk allocation algorithms are implemented to allocate files on disk using contiguous, indexed and linked allocation respectively.

Uploaded by

shanmathy shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views10 pages

Swe3001 Da6

The document contains code snippets for different disk scheduling algorithms: - SSTF disk scheduling algorithm is implemented to calculate the total head movement. - SCAN disk scheduling algorithm sorts the requests and services them in one direction. - FCFS disk scheduling algorithm calculates the total seek operations by servicing requests in the order of arrival. - SEQUENTIAL, INDEXED and LINKED disk allocation algorithms are implemented to allocate files on disk using contiguous, indexed and linked allocation respectively.

Uploaded by

shanmathy shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SHANMATHY SHREE AE 21MIS0498

SWE3001 – OPERATING SYSTEM


(LAB)
DIGITAL ASSESSMENT – 6

NAME: SHANMATHY SHREE AE


REG NO: 21MIS0498
FACULTY: KUMAR PJ
SLOT: L11 + L12
SHANMATHY SHREE AE 21MIS0498

SSTF:

CODE:

#include<stdio.h>
#include<stdlib.h>
int main()
{
Printf(Pishanthi.P 21mis0306\n");
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);
// logic for sstf disk scheduling
/* loop will execute until all process is completed*/
while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
SHANMATHY SHREE AE 21MIS0498

}
printf("Total head movement is %d",TotalHeadMoment);
return 0;
}

OUTPUT:

SCAN:
CODE:

#include <stdio.h>
#include <math.h>
int main()
{
Printf(“pishanthi.p 21mis0306”);
int queue[20], n, head, i, j, k, seek = 0, max, diff, temp,
queue1[20],
queue2[20], temp1 = 0, temp2 = 0;
float avg;
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;
SHANMATHY SHREE AE 21MIS0498

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])
{
emp = 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);
SHANMATHY SHREE AE 21MIS0498

return 0;
}

OUTPUT:

CSCAN:

CODE:

#include <stdio.h>
#include <stdlib.h>
int main() {
printf(“Pishanthi.P 21MIS0306”);
int n, head, prev, i, j, seek_time = 0;
printf("Enter the size of disk queue: ");
scanf("%d", &n);
int queue[n];
printf("Enter the queue of disk positions to be read: ");
for(i = 0; i < n; i++)
scanf("%d", &queue[i]);
printf("Enter the initial head position: ");
scanf("%d", &head);
// sort the queue in ascending order
for(i = 0; i < n-1; i++) {
for(j = i+1; j < n; j++) {
if(queue[i] > queue[j]) {
int temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
}
}
// find the index of current head position
SHANMATHY SHREE AE 21MIS0498

for(i = 0; i < n; i++) {


if(head < queue[i]) {
prev = i-1;
break;
}
}
if(i == n)
prev = n-1;
// service requests in one direction
for(i = prev; i >= 0; i--) {
seek_time += abs(head - queue[i]);
head = queue[i];
}
// move to the other end of the disk and service requests in the
same direction
seek_time += head; // add the seek time to the other end of the
disk
head = 0;
for(i = n-1; i > prev; i--) {
seek_time += abs(head - queue[i]);
head = queue[i];
}
printf("Total seek time: %d\n", seek_time);
return 0;
}

FCFS:

CODE:
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
int size = 8;
void FCFS(int arr[],int head)
{
SHANMATHY SHREE AE 21MIS0498

int seek_count = 0;
int cur_track, distance;
for(int i=0;i<size;i++)
{
cur_track = arr[i];
// calculate absolute distance
distance = fabs(head - cur_track);
// increase the total count
seek_count += distance;
// accessed track is now new head
head = cur_track;
}
printf("Total number of seek operations: %d\n",seek_count);
// Seek sequence would be the same
// as request array sequence
printf("Seek Sequence is\n");
for (int i = 0; i < size; i++) {
printf("%d\n",arr[i]);
}
}
int main()
{
printf("pishanthi.p 21mis0306\n");
// request array
int arr[8] = { 176, 79, 34, 60, 92, 11, 41, 114 };
int head = 50;
FCFS(arr,head);
return 0;
}

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

SEQUENTIAL:

CODE:

#include <stdio.h>
#define BLOCK_SIZE 512
#define DISK_SIZE 2048
int main() {
printf("\nBharani Kumar \n");
int disk[DISK_SIZE] = {0}; // initialize all blocks to 0
int file_size = 1024; // 1 MB file size
int num_blocks = file_size / BLOCK_SIZE;
int allocated_blocks = 0;
int current_block = 0;
while (allocated_blocks < num_blocks && current_block < DISK_SIZE)
{
if (disk[current_block] == 0) {
disk[current_block] = 1; // mark block as allocated
allocated_blocks++;
} else {
// move to next block if current block is already allocated
current_block++;
}
}
if (allocated_blocks == num_blocks) {
printf("File has been successfully allocated on the disk.\n");
} else {
printf("There are not enough contiguous free blocks on the disk to
allocate the file.\n");
}
return 0;
SHANMATHY SHREE AE 21MIS0498

OUTPUT:

INDEXED:

CODE:
#include <stdio.h>
#include <stdlib.h>
#define BLOCK_SIZE 512
#define DISK_SIZE 2048
struct index_block {
int pointers[DISK_SIZE / BLOCK_SIZE];
};
int main() {
printf("pishanthi\n");
int disk[DISK_SIZE] = {0}; // initialize all blocks to 0
int file_size = 1024; // 1 MB file size
int num_blocks = file_size / BLOCK_SIZE;
struct index_block index;
for (int i = 0; i < num_blocks; i++) {
int allocated_block = rand() % DISK_SIZE;
while (disk[allocated_block] != 0) {
allocated_block = rand() % DISK_SIZE;
}
disk[allocated_block] = 1; // mark block as allocated
index.pointers[i] = allocated_block;
}
printf("File has been successfully allocated on the disk.\n");
return 0;}

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

LINKED:

CODE:

#include <stdio.h>
#define BLOCK_SIZE 512
#define DISK_SIZE 2048
struct data_block {
int data[BLOCK_SIZE];
int next_block;
};
int main() {
printf("pishanthi 21mis0306\n");
int disk[DISK_SIZE] = {0}; // initialize all blocks to 0
int file_size = 1024; // 1 MB file size
int num_blocks = file_size / BLOCK_SIZE;
struct data_block blocks[num_blocks];
for (int i = 0; i < num_blocks; i++) {
int allocated_block = 0;
while (disk[allocated_block] != 0) {
allocated_block++;
}
disk[allocated_block] = 1; // mark block as allocated
blocks[i].next_block = allocated_block + 1;
}
blocks[num_blocks - 1].next_block = -1; // set end-of-file marker
printf("File has been successfully allocated on the disk.\n");
return 0;
}

OUTPUT:

You might also like