Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views38 pages

Lab 3rd OS

device management, process management, file management related algorithms implementation in c.

Uploaded by

useranjalee
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)
5 views38 pages

Lab 3rd OS

device management, process management, file management related algorithms implementation in c.

Uploaded by

useranjalee
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/ 38

7.

a) WAP to simulate FIFO Page Replacement Algorithm

Theory: This program simulates the FIFO (First-In, First-Out) Page Replacement Algorithm,
which replaces the oldest page in memory when a page fault occurs. It uses arrays (frames[] to
store current pages, pages[] for the reference string) and loops to iterate through page requests.
Conditionals (if statements) check for page hits or faults, and a circular index (k) tracks which
frame to replace next, implementing the FIFO behavior. Standard I/O functions like printf()
and scanf() handle input and output. The program is written in C, and can be edited in VS Code
and compiled using a C compiler (GCC or similar).

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int frames[10], pages[30], temp[10];
int n, f, i, j, k = 0, flag, faults = 0;

printf("Enter number of pages: ");


scanf("%d", &n);

printf("Enter the reference string (page numbers):\n");


for (i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

for (i = 0; i < f; i++)


frames[i] = -1;

printf("\nPage Reference String -> Frame Status\n");

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


flag = 0;

for (j = 0; j < f; j++) {


if (frames[j] == pages[i]) {
flag = 1; // Page hit
break;
}
}
if (flag == 0) {
frames[k] = pages[i];
k = (k + 1) % f; // FIFO: replace in round robin manner
faults++;
}

printf("%2d -> ", pages[i]);


for (j = 0; j < f; j++) {
if (frames[j] == -1)
printf(" - ");
else
printf("%2d ", frames[j]);
}
if (flag == 0)
printf(" (Page Fault)");
printf("\n");
}

printf("\nTotal Page Faults = %d\n", faults);


printf("\nLab No.: 7.a)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:
7.b) WAP to simulate Optimal Page Replacement Algorithm

Theory: This program simulates the Optimal Page Replacement Algorithm, which replaces
the page in memory that will not be used for the longest time in the future when a page fault
occurs. It uses arrays (frames[] for memory, pages[] for the reference string) and loops to
iterate through page requests. Conditionals (if statements) check for page hits or faults, and
nested loops determine the optimal page to replace based on future usage. The program uses
standard I/O functions (printf() and scanf()) to handle input and output. It is written in C, and
can be edited in VS Code and compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int frames[10], pages[30];
int n, f, i, j, k, flag, faults = 0, pos, farthest;

printf("Enter number of pages: ");


scanf("%d", &n);

printf("Enter the reference string (page numbers):\n");


for (i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

for (i = 0; i < f; i++)


frames[i] = -1;

printf("\nPage Reference String -> Frame Status\n");

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


flag = 0;

for (j = 0; j < f; j++) {


if (frames[j] == pages[i]) {
flag = 1; // Page hit
break;
}
}
if (flag == 0) {
// Find empty frame first
for (j = 0; j < f; j++) {
if (frames[j] == -1) {
frames[j] = pages[i];
flag = 1;
faults++;
break;
}
}
}

if (flag == 0) {
farthest = -1;
pos = -1;

for (j = 0; j < f; j++) {


int found = 0;
for (k = i + 1; k < n; k++) {
if (frames[j] == pages[k]) {
if (k > farthest) {
farthest = k;
pos = j;
}
found = 1;
break;
}
}
if (!found) {
pos = j;
break;
}
}
frames[pos] = pages[i];
faults++;
}

printf("%2d -> ", pages[i]);


for (j = 0; j < f; j++) {
if (frames[j] == -1)
printf(" - ");
else
printf("%2d ", frames[j]);
}
if (flag == 0)
printf(" (Page Fault)");
printf("\n");
}

printf("\nTotal Page Faults = %d\n", faults);


printf("\nLab No.: 7.b)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:

7.c) WAP to simulate LRU Page Replacement Algorithm


Theory: This program simulates the Least Recently Used (LRU) Page Replacement
Algorithm, which replaces the page in memory that was least recently accessed when a page
fault occurs. It uses arrays (frames[] for memory, pages[] for the reference string,
recentlyUsed[] to track last access) and loops to process page requests. Conditionals (if
statements) check for page hits or faults, and the least recently used page is determined by
comparing last access indices. Standard I/O functions (printf() and scanf()) handle input and
output. The program is written in C, and can be edited in VS Code and compiled using a C
compiler like GCC.

Code:

#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int frames[10], pages[30], n, f, i, j, k, flag, faults = 0;
int recentlyUsed[10];

printf("Enter number of pages: ");


scanf("%d", &n);
printf("Enter the reference string (page numbers):\n");
for (i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

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


frames[i] = -1;
recentlyUsed[i] = -1;
}

printf("\nPage Reference String -> Frame Status\n");

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


flag = 0;

for (j = 0; j < f; j++) {


if (frames[j] == pages[i]) {
flag = 1;
break;
}
}

if (flag == 0) {
int empty = -1;
for (j = 0; j < f; j++) {
if (frames[j] == -1) {
empty = j;
break;
}
}

if (empty != -1) {
frames[empty] = pages[i];
recentlyUsed[empty] = i;
} else {
int lruIndex = 0;
for (j = 1; j < f; j++) {
if (recentlyUsed[j] < recentlyUsed[lruIndex])
lruIndex = j;
}
frames[lruIndex] = pages[i];
recentlyUsed[lruIndex] = i;
}
faults++;
} else {
for (j = 0; j < f; j++) {
if (frames[j] == pages[i]) {
recentlyUsed[j] = i;
break;
}
}
}

printf("%2d -> ", pages[i]);


for (j = 0; j < f; j++) {
if (frames[j] == -1)
printf(" - ");
else
printf("%2d ", frames[j]);
}
if (flag == 0)
printf(" (Page Fault)");
printf("\n");
}

printf("\nTotal Page Faults = %d\n", faults);


printf("\nLab No.: 7.c)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:

7.d) WAP to simulate Second Chance Page Replacement Algorithm

Theory: This program simulates the Second Chance Page Replacement Algorithm, which
gives a page a “second chance” if it was recently used before replacing it. Memory frames are
stored in an array (frames[]), and each frame has a reference bit (refBit[]) indicating recent
use. The reference string (pages[]) represents page requests. Loops iterate through page
requests, and conditionals (if statements) check for page hits or faults. A pointer tracks the
next frame for replacement, giving pages with reference bit 1 a second chance by resetting
their bit to 0 and moving the pointer forward. Standard I/O functions (printf() and scanf())
handle input and output. The program is written in C, and can be edited in VS Code and
compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int frames[10], refBit[10], pages[30];
int n, f, i, j, pointer = 0, flag, faults = 0;

printf("Enter number of pages: ");


scanf("%d", &n);

printf("Enter the reference string (page numbers):\n");


for (i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);

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


frames[i] = -1;
refBit[i] = 0;
}

printf("\nPage Reference String -> Frame Status\n");

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


flag = 0;

for (j = 0; j < f; j++) {


if (frames[j] == pages[i]) {
refBit[j] = 1;
flag = 1;
break;
}
}

if (flag == 0) {
while (refBit[pointer] == 1) {
refBit[pointer] = 0;
pointer = (pointer + 1) % f;
}
frames[pointer] = pages[i];
refBit[pointer] = 0;
pointer = (pointer + 1) % f;
faults++;
}

printf("%2d -> ", pages[i]);


for (j = 0; j < f; j++) {
if (frames[j] == -1)
printf(" - ");
else
printf("%2d ", frames[j]);
}
if (flag == 0)
printf(" (Page Fault)");
printf("\n");
}

printf("\nTotal Page Faults = %d\n", faults);


printf("\nLab No.: 7.d)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}
Output:

7.e) WAP to simulate LFU Page Replacement Algorithm

Theory: This program simulates the Least Frequently Used (LFU) Page Replacement
Algorithm, which replaces the page in memory that has been accessed the fewest times when
a page fault occurs. Memory frames are stored in an array (frames[]), and each frame has a
frequency counter (freq[]) tracking the number of accesses. The reference string (pages[])
contains page requests. Loops iterate through the reference string, and conditionals (if
statements) check for page hits or faults. On a page hit, the frequency is incremented; on a
page fault, an empty frame is used if available, otherwise the least frequently used page is
replaced. Standard I/O functions (printf(), scanf()) handle input and output, and getcwd()
fetches the current working directory. The program is written in C, and can be edited in VS
Code and compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}

int frames[10], pages[30], freq[10];


int n, f, i, j, flag, faults = 0, minFreq, lfuIndex;

printf("Enter number of pages: ");


scanf("%d", &n);
if (n > 30) {
printf("Number of pages should not exceed 30.\n");
return 1;
}

printf("Enter the reference string (page numbers):\n");


for (i = 0; i < n; i++)
scanf("%d", &pages[i]);

printf("Enter number of frames: ");


scanf("%d", &f);
if (f > 10) {
printf("Number of frames should not exceed 10.\n");
return 1;
}

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


frames[i] = -1;
freq[i] = 0;
}

printf("\nPage Reference String -> Frame Status\n");

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


flag = 0;

for (j = 0; j < f; j++) {


if (frames[j] == pages[i]) {
freq[j]++;
flag = 1;
break;
}
}

if (flag == 0) {
int empty = -1;

for (j = 0; j < f; j++) {


if (frames[j] == -1) {
empty = j;
break;
}
}

if (empty != -1) {
frames[empty] = pages[i];
freq[empty] = 1;
} else {
minFreq = freq[0];
lfuIndex = 0;
for (j = 1; j < f; j++) {
if (freq[j] < minFreq) {
minFreq = freq[j];
lfuIndex = j;
}
}
frames[lfuIndex] = pages[i];
freq[lfuIndex] = 1;
}
faults++;
}

printf("%2d -> ", pages[i]);


for (j = 0; j < f; j++) {
if (frames[j] == -1)
printf(" - ");
else
printf("%2d ", frames[j]);
}
if (flag == 0)
printf(" (Page Fault)");
printf("\n");
}

printf("\nTotal Page Faults = %d\n", faults);


printf("\nLab No.: 7.e)\nName: Anjali Rai\nRoll No./Section: 02/A\n");
return 0;
}

Output:

8.a) WAP to simulate Contiguous File Allocation Technique

Theory: This program simulates the Contiguous File Allocation Technique, where each file
occupies a set of consecutive memory blocks. Memory is represented as an array (memory[])
with 0 indicating free blocks and 1 indicating allocated blocks. The program takes the
number of files and their sizes as input and searches for a contiguous sequence of free blocks
large enough for each file. If found, the blocks are marked allocated and the starting block is
recorded; otherwise, the file is not allocated. Loops iterate through memory and files, and
conditionals (if statements) check availability. Standard I/O functions (printf() and scanf())
handle input and output. The program is written in C, and can be edited in VS Code and
compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, size[20], start[20], i, totalBlocks = 100;
int memory[100] = {0}; // 0 = free, 1 = allocated

printf("Enter number of files: ");


scanf("%d", &n);

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


printf("Enter size of file %d (in blocks): ", i + 1);
scanf("%d", &size[i]);

int found = 0;
for (int j = 0; j <= totalBlocks - size[i]; j++) {
int k;
for (k = j; k < j + size[i]; k++) {
if (memory[k] == 1)
break;
}
if (k == j + size[i]) {
start[i] = j;
for (k = j; k < j + size[i]; k++)
memory[k] = 1;
found = 1;
break;
}
}

if (!found) {
printf("Not enough contiguous space for file %d.\n", i + 1);
start[i] = -1;
}
}

printf("\nFile Allocation Table:\n");


printf("File\tStart Block\tSize\n");
for (i = 0; i < n; i++) {
if (start[i] != -1)
printf("%d\t%d\t\t%d\n", i + 1, start[i], size[i]);
else
printf("%d\tNot Allocated\t%d\n", i + 1, size[i]);
}
printf("\nLab No.: 8.a)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}
Output:
8.b) WAP to simulate Linked File Allocation Technique

Theory: This program simulates the Linked File Allocation Technique, where each file
occupies non-contiguous blocks in memory, with each block pointing to the next. Memory is
represented as an array (memory[]) where 0 indicates free blocks and 1 indicates allocated
blocks. Each file is defined using a struct with startBlock, size, and an array blocks[] to store
allocated block numbers. The program takes the number of files and their sizes as input and
allocates free blocks anywhere in memory, linking them sequentially in the blocks[] array.
Loops iterate through memory and files, and conditionals (if statements) check allocation
feasibility. If enough free blocks are unavailable, the file is not allocated. Standard I/O
functions (printf() and scanf()) handle input and output. The program is written in C, and can
be edited in VS Code and compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

struct File {
int startBlock;
int size;
int blocks[50];
};

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, totalBlocks = 100, memory[100] = {0};
struct File files[20];
int i, j, k;

printf("Enter number of files: ");


scanf("%d", &n);

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


printf("Enter size of file %d (in blocks): ", i + 1);
scanf("%d", &files[i].size);

int allocated = 0;
for (j = 0, k = 0; j < totalBlocks && k < files[i].size; j++) {
if (memory[j] == 0) {
memory[j] = 1;
files[i].blocks[k++] = j;
allocated = 1;
}
}
if (k != files[i].size) {
printf("Not enough free blocks for file %d.\n", i + 1);
for (j = 0; j < k; j++)
memory[files[i].blocks[j]] = 0;
files[i].size = 0;
} else {
files[i].startBlock = files[i].blocks[0];
}
}

printf("\nFile Allocation Table (Linked Allocation):\n");


printf("File\tStart Block\tBlocks\n");
for (i = 0; i < n; i++) {
if (files[i].size > 0) {
printf("%d\t%d\t\t", i + 1, files[i].startBlock);
for (j = 0; j < files[i].size; j++)
printf("%d ", files[i].blocks[j]);
printf("\n");
} else {
printf("%d\tNot Allocated\n", i + 1);
}
}
printf("\nLab No.: 8.b)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:
8.c) WAP to simulate File Allocation using File Allocation Table

Theory: This program simulates File Allocation using a File Allocation Table (FAT), where
each file is stored in non-contiguous memory blocks, and each block contains a pointer to the
next block. Memory is represented by an array (memory[]) with 0 indicating free blocks and 1
indicating allocated blocks. The FAT is implemented using another array (fat[]), which stores
the next block number for each allocated block or -1 if it is the last block. The program takes
the number of files and their sizes as input, allocates free blocks sequentially, and links them
in the FAT array. Loops iterate through files and memory blocks, and conditionals (if
statements) handle allocation and check for free blocks. Standard I/O functions (printf() and
scanf()) handle input and output. The program is written in C, editable in VS Code, and
compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, totalBlocks = 100, memory[100], fat[100];
int i, j, k;

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


memory[i] = 0; // 0 = free, 1 = allocated
fat[i] = -1; // FAT pointer, -1 = end of file
}

printf("Enter number of files: ");


scanf("%d", &n);

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


int size;
printf("Enter size of file %d (in blocks): ", i + 1);
scanf("%d", &size);

int allocated = 0, prev = -1;


for (j = 0, k = 0; j < totalBlocks && k < size; j++) {
if (memory[j] == 0) {
memory[j] = 1;
if (prev != -1)
fat[prev] = j;
else
fat[i] = j; // start block stored in FAT index
prev = j;
k++;
}
}

if (k != size) {
printf("Not enough free blocks for file %d.\n", i + 1);
for (j = 0; j < totalBlocks; j++)
if (fat[j] != -1 || memory[j] == 1) memory[j] = 0;
} else {
fat[prev] = -1;
}
}

printf("\nFile Allocation Table (FAT Simulation):\n");


printf("Block\tNext\n");
for (i = 0; i < totalBlocks; i++) {
if (memory[i] == 1)
printf("%d\t%d\n", i, fat[i]);
}
printf("\nLab No.: 8.c)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:

8.d) WAP to implement File Allocation using Inode

Theory: This program simulates File Allocation using Inode, where each file is represented by
an inode structure containing the startBlock, size, and a list of all allocated blocks (blocks[]).
Memory is represented by an array (memory[]) where 0 indicates free blocks and 1 indicates
allocated blocks. The program takes the number of files and their sizes as input, allocates free
blocks anywhere in memory, and stores their block numbers in the inode. Loops iterate through
memory and files, and conditionals (if statements) check availability and handle allocation
failures. Standard I/O functions (printf() and scanf()) are used for input and output. The
program is written in C, editable in VS Code, and compiled using a C compiler like GCC.
Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

struct Inode {
int startBlock;
int size;
int blocks[50];
};

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, totalBlocks = 100, memory[100] = {0};
struct Inode files[20];
int i, j, k;

printf("Enter number of files: ");


scanf("%d", &n);

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


printf("Enter size of file %d (in blocks): ", i + 1);
scanf("%d", &files[i].size);

int allocated = 0;
for (j = 0, k = 0; j < totalBlocks && k < files[i].size; j++) {
if (memory[j] == 0) {
memory[j] = 1;
files[i].blocks[k++] = j;
allocated = 1;
}
}

if (k != files[i].size) {
printf("Not enough free blocks for file %d.\n", i + 1);
for (j = 0; j < k; j++)
memory[files[i].blocks[j]] = 0;
files[i].size = 0;
} else {
files[i].startBlock = files[i].blocks[0];
}
}

printf("\nFile Allocation Table (Inode Simulation):\n");


printf("File\tStart Block\tBlocks\n");
for (i = 0; i < n; i++) {
if (files[i].size > 0) {
printf("%d\t%d\t\t", i + 1, files[i].startBlock);
for (j = 0; j < files[i].size; j++)
printf("%d ", files[i].blocks[j]);
printf("\n");
} else {
printf("%d\tNot Allocated\n", i + 1);
}
}
printf("\nLab No.: 8.d)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:

9.a) WAP to simulate Free Space Management using Bitmaps

Theory: This program simulates Free Space Management using Bitmaps, where each memory
block is represented in a bitmap array (bitmap[]) with 0 indicating a free block and 1 indicating
an allocated block. The program takes the number of block allocations and the specific block
numbers as input. Conditionals (if statements) check for valid block numbers and whether a
block is already allocated. On allocation, the bitmap is updated, and messages are displayed.
Finally, a bitmap table is printed showing the status of all blocks. The program uses loops to
iterate through memory blocks and allocations, standard I/O functions (printf() and scanf()) for
input/output, and is written in C, editable in VS Code, and compiled with a C compiler like
GCC.

Code:
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int totalBlocks = 100;
int bitmap[100] = {0}; // 0 = free, 1 = allocated
int n, i, block;

printf("Enter number of allocations: ");


scanf("%d", &n);

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


printf("Enter block number to allocate (0-%d): ", totalBlocks - 1);
scanf("%d", &block);

if (block < 0 || block >= totalBlocks) {


printf("Invalid block number.\n");
i--;
continue;
}

if (bitmap[block] == 0) {
bitmap[block] = 1;
printf("Block %d allocated successfully.\n", block);
} else {
printf("Block %d is already allocated.\n", block);
}
}

printf("\nBitmap Table:\nBlock\tStatus\n");
for (i = 0; i < totalBlocks; i++) {
printf("%d\t%s\n", i, bitmap[i] ? "Allocated" : "Free");
}
printf("\nLab No.: 9.a)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:
9. b) WAP to simulate Free Space Management using Linked List

Theory: This program simulates Free Space Management using a Linked List, where each free
memory block is represented as a node (struct Block) in a linked list. The program initializes
the free list with all available blocks, and users input block numbers to allocate. Loops traverse
the linked list to find and remove allocated blocks. Conditionals check whether a block is valid
and free. Allocated blocks are removed from the list using dynamic memory allocation (malloc)
and deallocation (free) for efficient memory management. After all allocations, the program
prints the remaining free blocks. The program uses standard I/O functions (printf() and scanf()),
is written in C, editable in VS Code, and compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

struct Block {
int blockNumber;
struct Block* next;
};

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int totalBlocks = 100, n, i, block;
struct Block* freeList = NULL;
struct Block* temp;

// Initialize free list


for (i = totalBlocks - 1; i >= 0; i--) {
temp = (struct Block*)malloc(sizeof(struct Block));
temp->blockNumber = i;
temp->next = freeList;
freeList = temp;
}

printf("Enter number of allocations: ");


scanf("%d", &n);

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


printf("Enter block number to allocate (0-%d): ", totalBlocks - 1);
scanf("%d", &block);

struct Block* prev = NULL;


temp = freeList;
int found = 0;

while (temp != NULL) {


if (temp->blockNumber == block) {
found = 1;
if (prev == NULL)
freeList = temp->next;
else
prev->next = temp->next;
free(temp);
printf("Block %d allocated successfully.\n", block);
break;
}
prev = temp;
temp = temp->next;
}

if (!found)
printf("Block %d is already allocated or invalid.\n", block);
}

printf("\nFree Blocks Remaining:\n");


temp = freeList;
while (temp != NULL) {
printf("%d ", temp->blockNumber);
temp = temp->next;
}
printf("\n");
printf("\nLab No.: 9.b)\nName: Anjali Rai\nRoll No./Section: 02/A\n");
return 0;
}

Output:

10. a) WAP to simulate FCFS Disk Scheduling Algorithm

Theory: This program simulates the First-Come, First-Served (FCFS) Disk Scheduling
Algorithm, where disk requests are processed in the order they arrive. The program takes the
number of requests, the request sequence, and the initial head position as input. Loops iterate
through the requests, and absolute difference (abs()) is used to calculate the seek distance for
each movement. The total seek time is accumulated and displayed. The program uses standard
I/O functions (printf() and scanf()), arrays to store request sequences, and basic arithmetic
operations for computation. It is written in C, editable in VS Code, and compiled using a C
compiler like GCC.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, head, i, seek = 0;
printf("Enter number of disk requests: ");
scanf("%d", &n);

int requests[n];
printf("Enter disk request sequence:\n");
for (i = 0; i < n; i++)
scanf("%d", &requests[i]);

printf("Enter initial head position: ");


scanf("%d", &head);

printf("\nHead movements:\n");
for (i = 0; i < n; i++) {
int movement = abs(requests[i] - head);
printf("Move from %d to %d with seek %d\n", head, requests[i], movement);
seek += movement;
head = requests[i];
}

printf("\nTotal seek time = %d\n", seek);


printf("\nLab No.: 10.a)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:
10. b) WAP to simulate SSTF Disk Scheduling Algorithm

Theory: This program simulates the Shortest Seek Time First (SSTF) Disk Scheduling
Algorithm, where the disk head moves to the request closest to its current position at each step.
The program takes the number of requests, the request sequence, and the initial head position
as input. An array stores the request sequence, and a visited array keeps track of served
requests. Nested loops find the closest unvisited request using the absolute difference (abs())
to calculate the seek distance. Total seek time is accumulated and displayed. The program uses
standard I/O functions (printf() and scanf()), arrays for storage, and basic arithmetic operations.
It is written in C, editable in VS Code, and compiled using a C compiler like GCC.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, head, i, j, seek = 0;

printf("Enter number of disk requests: ");


scanf("%d", &n);

int requests[n], visited[n];


printf("Enter disk request sequence:\n");
for (i = 0; i < n; i++) {
scanf("%d", &requests[i]);
visited[i] = 0;
}

printf("Enter initial head position: ");


scanf("%d", &head);

printf("\nHead movements:\n");

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


int minDist = INT_MAX;
int index = -1;
for (j = 0; j < n; j++) {
if (!visited[j] && abs(requests[j] - head) < minDist) {
minDist = abs(requests[j] - head);
index = j;
}
}
printf("Move from %d to %d with seek %d\n", head, requests[index], minDist);
seek += minDist;
head = requests[index];
visited[index] = 1;
}

printf("\nTotal seek time = %d\n", seek);


printf("\nLab No.: 10.b)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:

10. c) WAP to simulate SCAN Disk Scheduling Algorithm

Theory: This program simulates the SCAN (Elevator) Disk Scheduling Algorithm, where the
disk head moves in a specified direction, servicing requests in order, then reverses at the end
of the disk. The program takes the number of requests, the request sequence, initial head
position, and scanning direction as input. Arrays store the request sequence, and a nested loop
is used to sort the requests in ascending order. Conditional statements determine which requests
are serviced in the current direction and calculate seek time using the absolute difference (abs())
between the current head and request. Total seek time is accumulated and displayed. The
program uses standard I/O functions (printf() and scanf()), loops, arrays, and basic arithmetic
operations, is written in C, editable in VS Code, and compiled with a C compiler like GCC.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, head, i, j, seek = 0, direction;
printf("Enter number of disk requests: ");
scanf("%d", &n);

int requests[n];
printf("Enter disk request sequence:\n");
for (i = 0; i < n; i++)
scanf("%d", &requests[i]);

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter direction (0 = towards 0, 1 = towards max track): ");


scanf("%d", &direction);

int temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-1-i; j++) {
if (requests[j] > requests[j+1]) {
temp = requests[j];
requests[j] = requests[j+1];
requests[j+1] = temp;
}
}
}

printf("\nHead movements:\n");
if (direction == 1) {
for (i = 0; i < n; i++) {
if (requests[i] >= head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
for (i = n-1; i >= 0; i--) {
if (requests[i] < head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
} else {
for (i = n-1; i >= 0; i--) {
if (requests[i] <= head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
for (i = 0; i < n; i++) {
if (requests[i] > head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
}

printf("\nTotal seek time = %d\n", seek);


printf("\nLab No.: 10.c)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:

10. d) WAP to simulate C-SCAN Disk Scheduling Algorithm

Theory: This program simulates the C-SCAN (Circular SCAN) Disk Scheduling Algorithm,
where the disk head moves in one direction, services all requests ahead of the current head,
continues to the last track, then jumps back to the beginning (0) and services the remaining
requests. The program takes the number of requests, the request sequence, the initial head
position, and disk size as input. Requests are stored in an array and sorted in ascending order
using a loop, while seek time is calculated as the absolute difference (abs()) between the head
and the request being serviced. Total seek time is accumulated and displayed at the end. The
program is written in C using standard input/output functions (scanf(), printf()), arrays, loops,
and arithmetic operations, and can be compiled with a C compiler like GCC.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, head, i, j, seek = 0, diskSize;
printf("Enter number of disk requests: ");
scanf("%d", &n);

int requests[n];
printf("Enter disk request sequence:\n");
for (i = 0; i < n; i++)
scanf("%d", &requests[i]);

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter disk size (max track number): ");


scanf("%d", &diskSize);

// Sort requests
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}

printf("\nHead movements:\n");

// Move right from head


for (i = 0; i < n; i++) {
if (requests[i] >= head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}

// Go to end and jump to start


if (head != diskSize - 1) {
seek += abs((diskSize - 1) - head);
printf("Move from %d to %d with seek %d\n", head, diskSize - 1, abs((diskSize - 1) -
head));
head = 0;
seek += (diskSize - 1);
printf("Jump to start (0)\n");
}

// Service the remaining requests from start


for (i = 0; i < n; i++) {
if (requests[i] < head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}

printf("\nTotal seek time = %d\n", seek);


printf("\nLab No.: 10.d)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:
10. e) WAP to simulate LOOK Disk Scheduling Algorithm

Theory: This program simulates the LOOK Disk Scheduling Algorithm, where the disk head
moves in a chosen direction (towards 0 or towards the maximum track), servicing all the
requests along the way until no further requests exist in that direction, then it reverses without
going to the extreme end of the disk. The program uses arrays to store disk requests, loops and
conditional statements to determine servicing order, and the abs() function to calculate seek
distances. Bubble sort is applied to arrange the request sequence in ascending order so that
requests can be serviced systematically. The total seek time is accumulated by adding the
absolute differences between the current head position and the next request. The program
employs standard I/O functions (printf() and scanf()), loops, arrays, conditional logic, and
arithmetic operations, is written in C, and can be edited in VS Code and compiled with a C
compiler like GCC.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, head, i, j, seek = 0, direction;

printf("Enter number of disk requests: ");


scanf("%d", &n);

int requests[n];
printf("Enter disk request sequence:\n");
for (i = 0; i < n; i++)
scanf("%d", &requests[i]);

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter direction (0 = towards 0, 1 = towards max track): ");


scanf("%d", &direction);

// Sort requests
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}

printf("\nHead movements:\n");

if (direction == 1) { // Moving towards higher values


for (i = 0; i < n; i++) {
if (requests[i] >= head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
for (i = n - 1; i >= 0; i--) {
if (requests[i] < head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
} else { // Moving towards lower values
for (i = n - 1; i >= 0; i--) {
if (requests[i] <= head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
for (i = 0; i < n; i++) {
if (requests[i] > head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
}

printf("\nTotal seek time = %d\n", seek);


printf("\nLab No.: 10.e)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}
Output:

10. f) WAP to simulate C-LOOK Disk Scheduling Algorithm

Theory: This program simulates the C-LOOK Disk Scheduling Algorithm, where the disk head
services requests in the given direction until the last request in that direction, then jumps
directly to the farthest request on the opposite end without servicing intermediate tracks,
continuing from there. It uses arrays to store the request sequence, bubble sort to arrange
requests in ascending order, and loops with conditional statements to decide the servicing order
based on the scanning direction. The absolute difference function (abs()) is used to calculate
the seek time between the current head position and each request, and the total seek time is
accumulated. The program is written in C, uses standard I/O functions (printf and scanf) for
user interaction, and demonstrates concepts of loops, arrays, conditional branching, and basic
arithmetic operations, making it easy to compile in a C compiler like GCC or run in an IDE
such as VS Code.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

int main() {
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) {
printf("Running from: %s\n\n", path);
} else {
perror("getcwd() error");
}
int n, head, i, j, seek = 0, direction;

printf("Enter number of disk requests: ");


scanf("%d", &n);

int requests[n];
printf("Enter disk request sequence:\n");
for (i = 0; i < n; i++)
scanf("%d", &requests[i]);

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter direction (0 = towards 0, 1 = towards max track): ");


scanf("%d", &direction);

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


for (j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}

printf("\nHead movements:\n");

if (direction == 1) { // Moving towards higher values


for (i = 0; i < n; i++) {
if (requests[i] >= head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
for (i = 0; i < n; i++) {
if (requests[i] < head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
} else {
for (i = n - 1; i >= 0; i--) {
if (requests[i] <= head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}

for (i = n - 1; i >= 0; i--) {


if (requests[i] > head) {
seek += abs(head - requests[i]);
printf("Move from %d to %d with seek %d\n", head, requests[i], abs(head -
requests[i]));
head = requests[i];
}
}
}

printf("\nTotal seek time = %d\n", seek);


printf("\nLab No.: 10.f)\nName: Anjali Rai\nRoll No./Section: 02/A\n");

return 0;
}

Output:
Padmakanya Multiple Campus
Bagbazar, Kathmandu
(Tribhuvan University)

LAB REPORT ON

Operating System (CSC264)

SUBMITTED BY: SUBMITTED TO:


Name: Anjali Rai Bipin Timalsina
Roll no.: 02
Faculty/Semester: Fourth Semester
Section: ‘A’
Contents
Lab Title/Question Submission Date Remarks
No.
7 Simulation of Page Replacement
Algorithms
a. WAP to simulate FIFO Page Replacement
Algorithm
b. WAP to simulate Optimal Page
Replacement Algorithm
c. WAP to simulate LRU Page Replacement
Algorithm
d. WAP to simulate Second Chance Page
Replacement Algorithm
e. WAP to simulate LFU Page Replacement
Algorithm

8 Simulation of File Allocation Techniques


a. WAP to simulate Contiguous File
Allocation Technique
b. WAP to simulate Linked File Allocation
Technique
c. WAP to simulate File Allocation using File
Allocation Table 2082-05-13
d. WAP to implement File Allocation using
Inode

9 Simulation of Free Space Management


Techniques
a. WAP to simulate Free Space Management
using Bitmaps
b. WAP to simulate Free Space Management
using Linked List

10 Simulation of disk scheduling algorithms


a. WAP to simulate FCFS Disk Scheduling
Algorithm
b. WAP to simulate SSTF Disk Scheduling
Algorithm
c. WAP to simulate SCAN Disk Scheduling
Algorithm
d. WAP to simulate C-SCAN Disk Scheduling
Algorithm
e. WAP to simulate LOOK Disk Scheduling
Algorithm
f. WAP to simulate C-LOOK Disk Scheduling
Algorithm

You might also like