OPERATING SYSYTEM LAB Manual
OPERATING SYSYTEM LAB Manual
OPERATING SYSYTEM LAB Manual
LAB MANUAL
INSTITUTION
Vision
To be a Respected and Most Sought after Engineering Educational Institution Engaged in
Equipping Individuals Capable of Building Learning Organizations in the New Millennium.
Mission
To Develop Competent Students with Good Value Systems to Face Challenges of the
Continuously Changing World.
DEPARTMENT
Vision
To establish the department as a renowned center of excellence in the area of scientific
education, research with industrial guidance, and exploration of the latest advances in the rapidly
changing field of computer science.
Mission
To produce technocrats with creative technical knowledge and intellectual skills to sustain and
excel in the highly demanding world with confidence.
1
Program Specific Outcomes (PSO)
1. To design efficient algorithms and develop effective code for real-time computations.
2. To apply software engineering principles in developing optimal software solutions.
2
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
THE OXFORD COLLEGE OF ENGINEERING
Hosur Road, Bommanahalli, Bengaluru-560 068
Website:www.theoxford.edu Email : [email protected]
Academic Year: 2023 -2024 (III sem)
Subject: Operating Systems Sub Code: BCS303
P
O PO1 PO PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO1 PO1 PO
2 0 1 12
CO
CO1 2 - 2 - - - - - - - - 2
CO2 1 - 2 - - - - - - - - 3
CO3 1 - 2 - - - - - - - - 2
CO4 1 - 2 - - - - - - - - 2
CO5 1 - 2 - - - - - - - - 2
3
Sl.N Experiments
O
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
b) SJF c) Round Robin d) Priority.
3 Develop a C program to simulate producer-consumer problem using semaphores.
4
EX.NO.1: IMPLEMENTATION OF FORK, EXEC, CREATE,EXIT, WAIT
SYSTEM CALLS.
AIM: To Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process,terminate process)
DESCRIPTION:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t childPid;
int status;
childPid = fork();
if (childPid < 0)
{
perror("Fork failed");
exit(EXIT_FAILURE);
}
else if (childPid == 0)
{
printf("Child process: PID = %d\n", getpid());
char *args[] = {"echo", "Hello from the child process!", NULL};
execvp(args[0], args);
perror("Exec failed");
exit(EXIT_FAILURE);
}
else {
printf("Parent process: PID = %d, Child PID = %d\n", getpid(), childPid);
wait(&status);
if (WIFEXITED(status)) {
5
printf("Child process terminated with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child process terminated abnormally\n");
}
}
return 0;
}
OUTPUT:
[root@localhost ~]# ./a.out tst date
Child process:
Child process id : 3137
Sat Apr 10 02:45:32 IST 2010
Parent Process:
Parent Process id:3136 sd dsaASD
[root@localhost ~]# cat tst sd dsaASD
RESULT: Thus the program for process management was written and successfully executed
6
EX.NO.2: IMPLEMENTATION OF CPU SCHEDULING ALGORITHMS TO FIND
TURNAROUND TIME AND WAITING TIME.
AIM: To Simulate the following CPU scheduling algorithms to find turnaround time and waiting
time a)FCFS b) SJF c) Round Robin d) Priority.
DESCRIPTION:
Scheduling Explanation
Algorithm
FCFS
SJF
Round
Robin
Priority.
PROGRAM
#include <stdio.h>
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
printf("FCFS Scheduling:\n");
7
printf("Average Waiting Time: %.2f\n", total_wt / n);
printf("Average Turnaround Time: %.2f\n", total_tat / n);
}
int main()
{
int processes[] = {1, 2, 3};
int n = sizeof (processes) / sizeof(processes[0]);
int burst_time[] = {10, 5, 8};
findAvgTime(processes, n, burst_time);
return 0;
}
OUTPUT:
FCFS Scheduling:
Average Waiting Time: 8.33
Average Turnaround Time: 16.00
if (check == 0) {
8
t++;
continue;
}
// Update minimum
minm = rt[shortest];
if (minm == 0) {
minm = 9999;
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
printf("SJF Scheduling:\n");
printf("Average Waiting Time: %.2f\n", total_wt / n);
printf("Average Turnaround Time: %.2f\n", total_tat / n);
}
int main() {
int processes[] = {1, 2, 3, 4};
int n = sizeof(processes) / sizeof(processes[0]);
int burst_time[] = {6, 8, 7, 3};
9
findAvgTime(processes, n, burst_time);
return 0;
}
OUTPUT:
SJF Scheduling:
Average Waiting Time: 417.00
Average Turnaround Time: 423.00
#include <stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int quantum, int wt[]) {
int remaining_time[n];
for (int i = 0; i < n; i++) {
remaining_time[i] = bt[i];
wt[i] = 0;
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
10
}
}
int main() {
int processes[] = {1, 2, 3, 4};
int n = sizeof(processes) / sizeof(processes[0]);
int burst_time[] = {10, 5, 8, 12};
int quantum = 2; // Quantum time (time slice)
return 0;
}
OUTPUT:
iii) Priority
#include <stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int priority[], int wt[]) {
int pos, temp;
for (int i = 0; i < n; i++) {
pos = i;
for (int j = i + 1; j < n; j++) {
if (priority[j] < priority[pos]) {
pos = j;
}
}
11
// Swap priority values
temp = priority[i];
priority[i] = priority[pos];
priority[pos] = temp;
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
}
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
printf("Priority Scheduling:\n");
printf("Average Waiting Time: %.2f\n", total_wt / n);
printf("Average Turnaround Time: %.2f\n", total_tat / n);
}
int main() {
int processes[] = {1, 2, 3, 4};
int n = sizeof(processes) / sizeof(processes[0]);
int burst_time[] = {6, 8, 7, 3};
int priority[] = {3, 2, 1, 4}; // Lower values indicate higher priority
12
findAvgTime(processes, n, burst_time, priority);
return 0;
}
OUTPUT:
Priority Scheduling:
Average Waiting Time: 10.75
Average Turnaround Time: 16.75
DESCRIPTION:
PROGRAM
13
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5
sem_post(&mutex);
sem_post(&fullSlots);
sleep(rand() % 2); // Producer sleeps for random time before producing the next item
}
}
sem_post(&mutex);
sem_post(&emptySlots);
sleep(rand() % 2); // Consumer sleeps for random time before consuming the next item
}
}
int main()
{
pthread_t producerThread, consumerThread;
14
// Initialize semaphores
sem_init(&mutex, 0, 1);
sem_init(&fullSlots, 0, 0);
sem_init(&emptySlots, 0, BUFFER_SIZE);
// Exit
sem_destroy(&mutex);
sem_destroy(&fullSlots);
sem_destroy(&emptySlots);
return 0;
}
OUTPUT :
Produced item: 1
Consumed item: 1
Produced item: 2
Consumed item: 2
Produced item: 3
Consumed item: 3
Produced item: 4
Consumed item: 4
Produced item: 5
Consumed item: 5
Produced item: 6
Consumed item: 6
Produced item: 7
Produced item: 8
Consumed item: 7
Produced item: 9
Produced item: 10
Produced item: 11
Produced item: 12
Consumed item: 8
Produced item: 13
Consumed item: 9
Consumed item: 10
Produced item: 14
Consumed item: 11
15
Consumed item: 12
Consumed item: 13
Consumed item: 14
Produced item: 15
Consumed item: 15
Produced item: 16
Consumed item: 16
RESULT:
DESRIPTION:
PROGRAM:
int main() {
char *fifoFile = "myfifo";
16
mkfifo(fifoFile, 0666);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
char *fifoFile = "myfifo";
mkfifo(fifoFile, 0666);
char message[100];
read(fd, message, sizeof(message));
printf("Message received from the writer: %s\n", message);
close(fd);
return 0;
}
OUTPUT :
17
Writer:
Message sent to the reader: Hello, Reader!
Reader:
Message received from the writer: Hello, Reader!
RESULT:
DESRIPTION:
PROGRAM:
18
#include <stdio.h>
int main() {
int processes, resources, i, j, k;
19
// Initialize work to available resources
for (i = 0; i < resources; i++)
{
work[i] = available[i];
}
20
printf("\n");
return 0;
}
OUTPUT :
RESULT:
21
EX.NO.6: IMPLEMENTATION OF CONTIGUOUS MEMORY ALLOCATION
TECHNIQUES
AIM: Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.
DESRIPTION:
PROGRAM:
if (wstIdx != -1) {
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
22
printf("Worst Fit Allocation:\n");
for (int i = 0; i < n; i++) {
if (allocation[i] != -1) {
printf("Process %d allocated to Block %d\n", i, allocation[i]);
} else {
printf("Process %d cannot be allocated\n", i);
}
}
}
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]);
return 0;
}
OUTPUT :
#include <stdio.h>
#include <limits.h>
23
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx]) {
bestIdx = j;
}
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
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]);
return 0;
}
OUTPUT :
24
#include <stdio.h>
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]);
OUTPUT :
25
RESULT:
DESRIPTION:
26
PROGRAM:
#include <stdio.h>
if (!pageFound) {
frame[frameIndex] = pages[i];
frameIndex = (frameIndex + 1) % capacity;
pageFaults++;
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3; // Number of frames
FIFO(pages, n, capacity);
return 0;
}
27
Output:
#include <stdio.h>
#include <limits.h>
if (!pageFound) {
int lruIndex = INT_MAX;
int replaceIndex = -1;
for (int j = 0; j < capacity; j++) {
if (pageLastUsed[frame[j]] < lruIndex) {
lruIndex = pageLastUsed[frame[j]];
replaceIndex = j;
}
28
}
frame[replaceIndex] = pages[i];
pageLastUsed[pages[i]] = i;
pageFaults++;
}
int main() {
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 3; // Number of frames
LRU (pages, n, capacity);
return 0;
}
OUTPUT :
29
RESULT:
DESRIPTION:
PROGRAM:
30
#include <string.h>
struct File {
char name[50];
};
void displaySingleLevelDirectory() {
printf("Single Level Directory:\n");
for (int i = 0; i < singleLevelFileCount; ++i) {
printf("- %s\n", singleLevelDirectory[i].name);
}
}
int main() {
createFileSingleLevel("file1.txt");
createFileSingleLevel("document.docx");
createFileSingleLevel("image.jpg");
createFileSingleLevel("data.csv");
displaySingleLevelDirectory();
return 0;
}
OUTPUT :
struct File {
31
char name[50];
};
struct UserDirectory {
char username[50];
struct File files[100];
int fileCount;
};
void displayTwoLevelDirectory() {
printf("Two Level Directory:\n");
for (int i = 0; i < userCount; ++i) {
printf("%s:\n", twoLevelDirectory[i].username);
for (int j = 0; j < twoLevelDirectory[i].fileCount; ++j) {
printf("- %s\n", twoLevelDirectory[i].files[j].name);
}
}
}
int main() {
createFileTwoLevel("User1", "file1.txt");
createFileTwoLevel("User1", "document1.docx");
createFileTwoLevel("User2", "image.jpg");
createFileTwoLevel("User2", "data.csv");
displayTwoLevelDirectory();
return 0;
}
OUTPUT :
32
Two Level Directory:
User1:
- file1.txt
- document1.docx
User2:
- image.jpg
- data.csv
RESULT:
33
EX.NO.9: IMPLEMENTATION OF LINKED FILE ALLOCATION STRATEGIES.
DESRIPTION:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Block {
int data;
struct Block* next;
};
if (*start == NULL) {
*start = newBlock;
} else {
struct Block* temp = *start;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newBlock;
34
}
}
int main() {
struct Block* start = NULL;
return 0;
}
OUTPUT :
RESULT:
35
EX.NO.10: IMPLEMENTATION OF SCAN DISK SCHEDULING
ALGORITHM
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main() {
int queue[20], seekSequence[20], head, size, totalSeekSequence = 0;
int left = 0, right = 199, distance, temp, i, j;
36
for (i = 0; i < size - 1; i++) {
for (j = i + 1; j < size; j++) {
if (queue[i] > queue[j]) {
temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
}
}
printf("NULL\n");
printf("Total seek sequence length: %d\n", totalSeekSequence);
return 0;
}
OUTPUT :
37
3
5
3
Enter the initial head position: 1
2 -> 3 -> 3 -> 5 -> NULL
Total seek sequence length: 4
RESULT:
38