OS Lab Manual
OS Lab Manual
OPERATING SYSTEM
LAB MANUAL
BCS303
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
1. Code of Conduct
4. Program Outcomes
6. Syllabus
7. Laboratory Manual
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Code of Conduct
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
1. Code of Conduct
Standards of Conduct
To,
Syeda Arbeena Kausar,
Department of Computer Science
VVIET, Mysore
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
a. Treat each other with fairness and respect, and conduct themselves with dignity and restraint in all
exchanges with colleagues.
b. Be fair and objective when presenting a professional judgment on their colleagues’ work
c. Not use threatening or abusive behavior or language, verbal harassment or intimidation of another
member of the faculty, under any circumstance while on the college campus.
d. Not knowingly misrepresent the views/positions of colleagues to further their own position or
point of view.
a. Faculty shall discuss with their Principal/HOD and submit a semester wise Lesson Plan. This will
be followed-up and monitored on a regular basis by the respective Principal/HOD.
b. The HODs/Principals shall make periodic surprise visits to classes to provide feedback on the
teaching methods and materials used. They will also check the diaries the faculty are expected to
maintain.
“I have read all the terms and conditions outlined in this document and agree to abide by them”
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Institute Vision
Institute Mission
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Department Vision
Department Mission
M3: To establish industry institute interaction, to enable students to cater the ever changing
industry demands and to nurture entrepreneurial qualities.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Programme Educational
Objectives
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Programme Outcome
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Programme Specific
Outcomes
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
2 SOFTWARE
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Syllabus
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Syllabus
OPERATING SYSTEM(BCS303)
1. Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process,
terminate process)
2. Simulate the following CPU scheduling algorithms to find turnaround time and waiting time
a) FCFS
3. b) SJF c) Round Robin d) Priority.
4. Develop a C program to simulate producer-consumer problem using semaphores.
5. Develop a C program which demonstrates interprocess communication between a reader
6. process and a writer process. Use mkfifo, open, read, write and close APIs in your program.
7. Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.
8. Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.
9. Develop a C program to simulate page replacement algorithms:
a) FIFO b) LRU
10. Simulate following File Organization Techniques
a) Single level directory b) Two level directory
11. Develop a C program to simulate the Linked file allocation strategies.
12. Develop a C program to simulate SCAN disk scheduling algorithm.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
1. Develop a c program to implement the Process system calls (fork (), exec(),
wait(), create process, terminate process)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t child_pid;
int status;
if (child_pid < 0)
{
perror("Fork failed");
exit(1);
}
if (child_pid == 0)
{
// This code will be executed by the child process
printf("Child process (PID: %d) is running\n", getpid());
// Replace the child process image with a new program using exec
char *args[] = {"./child_program", NULL};
if (WIFEXITED(status)) {
printf("Child process terminated with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child process did not terminate normally\n");
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
return 0;
}
output
Parent process (PID: 1574)
Parent process is waiting for the child to terminate...
Child process (PID: 1578) is running
Exec failed: No such file or directory
Child process terminated with status: 1
Parent process is done
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
// Calculate and print the average waiting time and turnaround time
float total_waiting_time = 0, total_turnaround_time = 0;
for (int i = 0; i < n; i++) {
total_waiting_time += waiting_time[i];
total_turnaround_time += turnaround_time[i];
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n];
int burst_time[n];
calculateFCFS(processes, n, burst_time);
return 0;
}
Output
Enter the number of processes: 5
Enter burst time for P1: 4
Enter burst time for P2: 5
Enter burst time for P3: 2
Enter burst time for P4: 6
Enter burst time for P5: 2
Process Burst Time Waiting Time Turnaround Time
P1 4 0 4
P2 5 4 9
P3 2 9 11
P4 6 11 17
P5 2 17 19
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
B) SJF
#include <stdio.h>
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int main() {
int n;
findAverageTimes(processes, n, burst_time);
return 0;
}
Output
Enter the number of processes: 5
Enter burst time for P1: 4
Enter burst time for P2: 5
Enter burst time for P3: 2
Enter burst time for P4: 6
Enter burst time for P5: 2
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
C Write a c program to Simulate CPU scheduling algorithms to find turnaround time and
waiting time using ROUNDROBIN
#include <stdio.h>
#define MAX_PROCESS 10
while (!done) {
done = 1; // Assume all processes are done
int main() {
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int n, time_quantum;
calculateTurnaroundTimeWaitingTime(processes, n, burst_time,
time_quantum);
return 0;
}
OUTPUT
Enter the number of processes: 5
Enter the time quantum: 5
Enter burst time for P1: 4
Enter burst time for P2: 5
Enter burst time for P3: 2
Enter burst time for P4: 6
Enter burst time for P5: 2
Process Burst Time Waiting Time Turnaround Time
P1 4 0 4
P2 5 4 9
P3 2 9 11
P4 6 13 19
P5 2 16 18
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
D. Write a c program to Simulate CPU scheduling algorithms to find turnaround time and
waiting time using PRIORITY.
#include <stdio.h>
#define MAX_PROCESS 10
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int main() {
int n;
return 0;
}
OUTPUT
Enter the number of processes: 5
Enter burst time for P1: 4
Enter priority for P1: 1
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int main() {
pthread_t producer_thread, consumer_thread;
// Initialize semaphores
sem_init(&mutex, 0, 1);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
// Destroy semaphores
sem_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);
return 0;
}
OUTPUT
Produced: 83
Produced: 86
Produced: 77
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
Produced: 15
Produced: 93
Consumed: 83
Consumed: 86
Consumed: 77
Consumed: 15
Consumed: 93
Produced: 35
Produced: 86
Produced: 92
Produced: 49
Produced: 21
Consumed: 35
Consumed: 86
Consumed: 92
Consumed: 49
Consumed: 21
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
4. Develop a C program which demonstrates interprocess communication between a reader process and
a writer process. Use mkfifo, open, read, write and close APIs in your program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void writer_process() {
int fd;
char message[BUFFER_SIZE];
while (1) {
// Read input from the user
fgets(message, BUFFER_SIZE, stdin);
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
void reader_process() {
int fd;
char message[BUFFER_SIZE];
while (1) {
// Read from the FIFO
read(fd, message, BUFFER_SIZE);
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int main() {
pid_t pid;
if (pid == 0) {
// Child process (reader)
reader_process();
} else {
// Parent process (writer)
writer_process();
}
return 0;
}
OUTPUT
Writer process opening FIFO...
Reader process opening FIFO...
Reader process waiting for messages...
Enter a message to send to the reader process (type 'exit' to end):
HI
Received message: HI
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int available[MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
void initialize() {
// Initialize available resources
printf("Enter the number of available resources:\n");
for (int i = 0; i < MAX_RESOURCES; i++) {
scanf("%d", &available[i]);
}
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int isSafe() {
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int work[MAX_RESOURCES];
int finish[MAX_PROCESSES] = {0};
if (j == MAX_RESOURCES) {
// Process i can finish, release its resources
finish[i] = 1;
for (int k = 0; k < MAX_RESOURCES; k++) {
work[k] += allocation[i][k];
}
i = -1; // Restart the loop
}
}
}
int main() {
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
initialize();
int process;
int request[MAX_RESOURCES];
if (requestResources(process, request)) {
printf("Request granted. System is in a safe state.\n");
} else {
printf("Request denied. System would be in an unsafe state.\n");
}
return 0;
}
OUTPUT
main.c: In function ‘requestResources’:
main.c:60:9: warning: implicit declaration of function ‘isSafe’ [-Wimplicit-
function-declaration]
60 | if (isSafe()) {
| ^~~~~~
Enter the number of available resources:
5
4
6
Enter the maximum demand of each process:
For process 0: 7
1
2
For process 1: 3
4
2
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
For process 2: 2
3
4
For process 3: 4
5
3
For process 4: 2
3
4
Enter the process number requesting resources: 2
Enter the resource request for process 2:
1
2
2
Request denied. System would be in an unsafe state.
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
6 Develop a C program to simulate the following contiguous memory allocation Techniques: a) Worst fit b)
Best fit c) First fit
#include <stdio.h>
#define MAX_BLOCKS 50
if (worstFitIdx != -1) {
allocation[i] = worstFitIdx;
blockSize[worstFitIdx] -= processSize[i];
printf("Worst Fit: Process %d allocated to block %d\n", i + 1, worstFitIdx + 1);
} else {
printf("Worst Fit: Process %d cannot be allocated\n", i + 1);
}
}
}
}
}
if (bestFitIdx != -1) {
allocation[i] = bestFitIdx;
blockSize[bestFitIdx] -= processSize[i];
printf("Best Fit: Process %d allocated to block %d\n", i + 1, bestFitIdx + 1);
} else {
printf("Best Fit: Process %d cannot be allocated\n", i + 1);
}
}
}
if (firstFitIdx != -1) {
allocation[i] = firstFitIdx;
blockSize[firstFitIdx] -= processSize[i];
printf("First Fit: Process %d allocated to block %d\n", i + 1, firstFitIdx + 1);
} else {
printf("First Fit: Process %d cannot be allocated\n", i + 1);
}
}
}
int main() {
int m, n;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
scanf("%d", &m);
int blockSize[MAX_BLOCKS];
int processSize[MAX_BLOCKS];
return 0;
}
OUTPUT
Enter the number of memory blocks: 5
Enter the number of processes: 5
Enter the sizes of memory blocks:
4
5\
Enter the sizes of processes:
Worst Fit: Process 1 allocated to block 2
Worst Fit: Process 2 allocated to block 2
Worst Fit: Process 3 allocated to block 2
Worst Fit: Process 4 allocated to block 2
Worst Fit: Process 5 allocated to block 2
Best Fit: Process 1 allocated to block 3
Best Fit: Process 2 allocated to block 3
Best Fit: Process 3 allocated to block 3
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
#define MAX_PAGES 50
if (!pageFound) {
frame[frameIndex] = page;
frameIndex = (frameIndex + 1) % capacity;
}
if (!pageFound) {
int replaceIdx = 0;
int minCounter = counter[frame[0]];
frame[replaceIdx] = page;
counter[page]++;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
}
printf("\n");
}
}
int main() {
int n, capacity;
int pages[MAX_PAGES];
fifo(pages, n, capacity);
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
lru(pages, n, capacity);
return 0;
}
OUTPUT
8. Simulate following File Organization Techniques a) Single level directory b) Two level directory
#include <stdio.h>
#include <string.h>
#define MAX_FILES 50
#define MAX_NAME_LENGTH 20
struct File {
char name[MAX_NAME_LENGTH];
int size;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
};
struct SingleLevelDirectory {
struct File files[MAX_FILES];
int fileCount;
};
struct TwoLevelDirectory {
struct SingleLevelDirectory directories[MAX_FILES];
int directoryCount;
};
switch (choice) {
case 1: {
if (rootDir->fileCount < MAX_FILES) {
printf("Enter file name: ");
scanf("%s", rootDir->files[rootDir->fileCount].name);
printf("Enter file size: ");
scanf("%d", &rootDir->files[rootDir->fileCount].size);
rootDir->fileCount++;
printf("File created successfully!\n");
} else {
printf("Maximum file limit reached!\n");
}
break;
}
case 2: {
printf("\nFiles in the Single Level Directory:\n");
for (int i = 0; i < rootDir->fileCount; i++) {
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
switch (choice) {
case 1: {
if (rootDir->directoryCount < MAX_FILES) {
printf("Enter directory name: ");
scanf("%s", rootDir->directories[rootDir-
>directoryCount].files[0].name);
rootDir->directories[rootDir->directoryCount].fileCount = 1;
rootDir->directoryCount++;
printf("Directory created successfully!\n");
} else {
printf("Maximum directory limit reached!\n");
}
break;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
}
case 2: {
char directoryName[MAX_NAME_LENGTH];
printf("Enter directory name: ");
scanf("%s", directoryName);
if (dirIndex != -1) {
if (rootDir->directories[dirIndex].fileCount < MAX_FILES) {
printf("Enter file name: ");
scanf("%s", rootDir->directories[dirIndex].files[rootDir-
>directories[dirIndex].fileCount].name);
printf("Enter file size: ");
scanf("%d", &rootDir->directories[dirIndex].files[rootDir-
>directories[dirIndex].fileCount].size);
rootDir->directories[dirIndex].fileCount++;
printf("File created successfully!\n");
} else {
printf("Maximum file limit reached in the directory!\n");
}
} else {
printf("Directory not found!\n");
}
break;
}
case 3: {
printf("\nFiles in the Two Level Directory:\n");
for (int i = 0; i < rootDir->directoryCount; i++) {
printf("Directory Name: %s\n", rootDir->directories[i].files[0].name);
for (int j = 0; j < rootDir->directories[i].fileCount; j++) {
printf("File Name: %s, Size: %d\n", rootDir-
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
>directories[i].files[j].name, rootDir->directories[i].files[j].size);
}
printf("\n");
}
break;
}
case 4: {
printf("Exiting Two Level Directory Menu...\n");
break;
}
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
}
int main() {
int choice;
struct SingleLevelDirectory singleDir;
singleDir.fileCount = 0;
do {
printf("\nFile Organization Techniques Menu:\n");
printf("1. Single Level Directory\n");
printf("2. Two Level Directory\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
singleLevelDirectory(&singleDir);
break;
}
case 2: {
twoLevelDirectory(&twoDir);
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
break;
}
case 3: {
printf("Exiting File Organization Techniques Menu...\n");
break;
}
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3);
return 0;
}
OUTPUT
File Organization Techniques Menu:
1. Single Level Directory
2. Two Level Directory
3. Exit
Enter your choice: 1
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
#define MAX_FILES 50
#define MAX_BLOCKS 100
struct Block {
int blockNumber;
struct Block* nextBlock;
};
struct File {
char name[20];
struct Block* firstBlock;
};
void createFile(struct File* files, int* fileCount, char name[20], int* blockCount) {
if (*fileCount < MAX_FILES) {
struct File* file = &files[*fileCount];
strcpy(file->name, name);
file->firstBlock = NULL;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
if (file->firstBlock == NULL) {
file->firstBlock = block;
} else {
struct Block* currentBlock = file->firstBlock;
while (currentBlock->nextBlock != NULL) {
currentBlock = currentBlock->nextBlock;
}
currentBlock->nextBlock = block;
}
(*blockCount)++;
} else {
printf("Maximum block limit reached!\n");
break;
}
}
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
printf("\n");
}
}
}
int main() {
struct File files[MAX_FILES];
int fileCount = 0;
int blockCount = 0;
int choice;
do {
printf("\nLinked File Allocation Menu:\n");
printf("1. Create File\n");
printf("2. Display Files\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
char fileName[20];
printf("Enter the name of the file: ");
scanf("%s", fileName);
createFile(files, &fileCount, fileName, &blockCount);
break;
}
case 2: {
displayFiles(files, fileCount);
break;
}
case 3: {
printf("Exiting Linked File Allocation Menu...\n");
break;
}
default:
printf("Invalid choice! Please try again.\n");
}
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
return 0;
}
OUTPUT
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
#include <stdio.h>
#include <stdlib.h>
if (direction == 'left') {
// Sort the requests in ascending order
for (int i = 0; i < n - 1; i++) {
for (int 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;
}
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
}
}
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
int main() {
int n, head, requests[MAX_REQUESTS];
char direction;
VVIET, Mysuru
VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
return 0;
}
Output
VVIET, Mysuru