OS lab manual 2025
OS lab manual 2025
– 1
Study of hardware and software requirements of different operating systems
(UNIX,LINUX,WINDOWS XP, WINDOWS7/8
UNIX-compatible operating system, such as Sun Solaris, IBM AIX, HP-UX, etc.
Compiler and development tools (optional)
X Window System for graphical user interface (optional)
Networking tools for network communication (optional)
Experiment No. – 2
Execute various UNIX system calls for
i. Process management
ii. File management
iii. Input/output Systems calls
i. Process Management
#include <stdio.h>
#include <sys/types.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process
} else {
// Parent process
printf("Hello! I am the parent process. My PID is %d, and my child's PID is %d\n",
getpid(), pid);
return 0;
#include <stdio.h>
#include <stdlib.h>
if (file == NULL) {
return;}
}
// Function to read data from a file
if (file == NULL) {
return;
char ch;
if (file == NULL) {
return;
}
int main() {
// Writing to a file
writeToFile(filename);
readFromFile(filename);
appendToFile(filename);
readFromFile(filename);
return 0;}
Experiment No. – 3
i. SJF
ii. Priority
iii. FCFS
iv. Multi-level Queue
1. First-Come-First-Serve (FCFS):
FCFS Algorithm:
c
Copy
#include <stdio.h>
typedef struct {
int pid; // Process ID
int arrivalTime;
int burstTime;
int waitingTime;
int turnaroundTime;
} Process;
int main() {
Process proc[] = {
{1, 0, 5, 0, 0},
{2, 1, 3, 0, 0},
{3, 2, 8, 0, 0},
{4, 3, 6, 0, 0}
};
int n = sizeof(proc) / sizeof(proc[0]);
FCFS(proc, n);
return 0;
}
In SJF, the process with the shortest burst time is executed first.
c
Copy
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int pid; // Process ID
int burstTime;
int waitingTime;
int turnaroundTime;
} Process;
int main() {
Process proc[] = {
{1, 6, 0, 0},
{2, 8, 0, 0},
{3, 7, 0, 0},
{4, 3, 0, 0}
};
int n = sizeof(proc) / sizeof(proc[0]);
SJF(proc, n);
return 0;
}
3. Priority Scheduling:
In Priority Scheduling, each process has a priority. The process with the highest priority
(lowest priority number) gets executed first.
c
Copy
#include <stdio.h>
typedef struct {
int pid; // Process ID
int priority;
int burstTime;
int waitingTime;
int turnaroundTime;
} Process;
int main() {
Process proc[] = {
{1, 2, 6, 0, 0},
{2, 1, 8, 0, 0},
{3, 4, 7, 0, 0},
{4, 3, 3, 0, 0}
};
int n = sizeof(proc) / sizeof(proc[0]);
PriorityScheduling(proc, n);
return 0;
}
In Multi-Level Queue Scheduling, there are multiple queues, each with a different priority.
Processes are assigned to different queues based on their priority.
MLQ Algorithm:
c
Copy
#include <stdio.h>
#define HIGH_QUEUE 0
#define LOW_QUEUE 1
typedef struct {
int pid;
int burstTime;
int priority;
} Process;
int main() {
Process proc[] = {
{1, 6, 1}, // High priority
{2, 8, 2}, // Low priority
{3, 5, 1}, // High priority
{4, 3, 2} // Low priority
};
int n = sizeof(proc) / sizeof(proc[0]);
MLQ(proc, n);
return 0;
}
Experiment No. – 4
i. Contiguous(using array)
ii. Linked –list(using linked-list)
iii. Indirect allocation (indexing)
In contiguous allocation, files are stored in contiguous blocks on the disk. We can represent this
using an array where each index represents a block on the disk.
#include <stdio.h>
#include <string.h>
typedef struct {
int blockNumber; // Block number on the disk
char data[50]; // Data stored in the block
} Block;
typedef struct {
int size; // Size of the file in blocks
int startBlock; // Starting block of the file
int blocks[MAX_BLOCKS]; // Array of block numbers allocated to the file
} File;
int main() {
File file1;
memset(disk, 0, sizeof(disk)); // Initialize all disk blocks as free
if (allocateContiguously(&file1, 5)) {
printf("Contiguous Allocation Successful!\n");
displayFileDetails(file1);
} else {
printf("Contiguous Allocation Failed!\n");
}
return 0;
}
In linked list allocation, each block contains a pointer to the next block, and the file is stored in non-
contiguous blocks. This is implemented using a linked list, where each node represents a disk block.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int size; // Size of the file (in blocks)
Block* head; // Head pointer to the first block
} File;
int main() {
File file1;
memset(disk, 0, sizeof(disk)); // Initialize all disk blocks as free
if (allocateLinkedList(&file1, 4)) {
printf("Linked List Allocation Successful!\n");
displayFileDetails(file1);
} else {
printf("Linked List Allocation Failed!\n");
}
return 0;
}
3. Indexed Allocation (using indexing)
In indexed allocation, each file has an index block that contains pointers to the data blocks. We
implement this using an array for the index block.
#include <stdio.h>
typedef struct {
int blockNumber; // Block number on the disk
char data[50]; // Data stored in the block
} Block;
typedef struct {
int size; // Size of the file in blocks
int index[MAX_BLOCKS]; // Index block that stores block numbers
} File;
int main() {
File file1;
memset(disk, 0, sizeof(disk)); // Initialize all disk blocks as free
if (allocateIndexed(&file1, 6)) {
printf("Indexed Allocation Successful!\n");
displayFileDetails(file1);
} else {
printf("Indexed Allocation Failed!\n");
}
return 0;
}
Experiment No. – 5
i. Worst-Fit
ii. Best- Fit
iii. First- Fit
(i) Best Fit Algorithm The best fit deals with allocating the smallest free partition
which meets the requirement of the requesting process. This algorithm first
searches the entire list of free partitions and considers the smallest hole that is
adequate. It then tries to find a hole which is close to actual process size needed.
Algorithm:
3. Start by picking each process and find the minimum block size that can be assigned to
current process i.e., find min(bockSize[1], blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign it to the current process.
4. If not then leave that process and keep checking the further processes.
(ii) Worst Fit Algorithm Worst Fit allocates a process to the partition which is largest
sufficient among the freely available partitions available in the main memory. If a
large process comes at a later stage, then memory will not have space to
accommodate it.
Algorithm:
3. Start by picking each process and find the maximum block size that can be assigned to
current process i.e., find max(bockSize[1], blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign it to the current process.
4. If not then leave that process and keep checking the further processes.
(iii) Next Fit algorithm Next fit is a modified version of first fit. It begins as first fit to
find a free partition. When called next time it starts searching from where it left
off, not from the beginning.
Algorithm:
1. Input the number of memory blocks and their sizes and initializes all the blocks as free.
3. Start by picking each process and check if it can be assigned to the current block, if yes,
allocate it the required memory and check for next process but from the block where we left
not from starting.
4. If the current block size is smaller then keep checking the further blocks. Assignment
Quest
Experiment No. – 6
Calculation of external and internal fragmentation
i. Free space list of blocks from system
ii. List process file from the system
External Fragmentation refers to the situation where free blocks of memory or disk space
are scattered throughout the system, making it difficult to allocate large contiguous blocks
even though the total free space might be sufficient.
Internal Fragmentation occurs when allocated memory or disk space is larger than the
required size, leading to unused space within the allocated blocks.
Below is a C program that calculates both internal and external fragmentation using a free
space list and a list of processes (files in this case). The program simulates a disk allocation
system, where free space blocks and allocated blocks are managed to calculate fragmentation.
We will define a free space list that represents blocks on the disk, where each block can be
free or allocated.
Each process will represent a file, with the size (in blocks) and the blocks allocated to the file.
#include <stdio.h>
#include <stdlib.h>
// If the file uses fewer blocks than required, internal fragmentation occurs
int unusedSpace = (lastBlock + 1) - file->size;
internalFrag = unusedSpace;
return internalFrag;
}
return externalFrag;
}
int main() {
initializeDisk(); // Initialize the disk
if (allocateFile(&file2)) {
displayFileDetails(&file2);
} else {
printf("File 2 Allocation Failed\n");
}
if (allocateFile(&file3)) {
displayFileDetails(&file3);
} else {
printf("File 3 Allocation Failed\n");
}