OS Lab Manual
OS Lab Manual
Website:www.theoxford.edu/http://theoxfordengg.org/Email :[email protected]
APPROVED BY AICTE, ACCREDITED BY NAAC, AFFILIATED TO VTU
OPERATING SYSTEMS
BCS303
1
Vision of the Institute:
“To be a Respected and Most Sought after Engineering Educational Institution Engaged in
Equipping Individuals capable of Building Learning Organizations in the New
Millennium”.
2
PROGRAM OUTCOMES
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and
an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
3
INDEX
1 Develop a c program to implement the Process system calls (fork (), exec(), 5
wait(), create process, terminate process)
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)) {
printf("Child process terminated with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child process terminated abnormally\n");
5
}
}
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.
PROGRAM a)
#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");
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:
7
Average Waiting Time: 8.33
Average Turnaround Time: 16.0
if (check == 0) {
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];
8
}
}
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};
findAvgTime(processes, n, burst_time);
return 0;
}
OUTPUT:
SJF Scheduling:
Average Waiting Time: 2.50
Average Turnaround Time: 8.5
c)Round Robin
#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];
}
}
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:
Round Robin Scheduling (Quantum Time = 2):
Average Waiting Time: 19.25
Average Turnaround Time: 28.00
d) Priority
#include <stdio.h>
10
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;
}
}
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);
}
11
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
return 0;
}
OUTPUT:
Priority Scheduling:
Average Waiting Time: 10.75
Average Turnaround Time: 16.75
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
12
#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;
// 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
Produced item: 5
Produced item: 6
Consumed item: 4
Produced item: 7
Produced item: 8
Consumed item: 5
Produced item: 9
Consumed item: 6
Produced item: 10
Produced item: 11
Consumed item: 7
Produced item: 12
Consumed item: 8
Produced item: 13
Consumed item: 9
Consumed item: 10
Produced item: 14
Consumed item: 11
Consumed item: 12
Consumed item: 13
Consumed item: 14
Produced item: 15
Consumed item: 15
Produced item: 16
Consumed item: 16
Produced item: 17
Consumed item: 17
Produced item: 18
Consumed item: 18
RESULT:
14
EX.NO.4: IMPLEMENTATION OF INTERPROCESS COMMUNICATION
BETWEEN A READER PROCESS AND A WRITER PROCESS
AIM: 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.
PROGRAM:
int main() {
char *fifoFile = "myfifo";
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);
15
exit(EXIT_FAILURE);
}
char message[100];
read(fd, message, sizeof(message));
printf("Message received from the writer: %s\n", message);
close(fd);
return 0;
}
OUTPUT :
RESULT:
DESRIPTION:
PROGRAM:
16
#include <stdio.h>
int main() {
int processes, resources, i, j, k;
return 0;
}
OUTPUT :
Enter number of processes: 3
Enter number of resources: 4
18
Enter maximum resources for each process:
Process 0: 2
3
4
5
Process 1:
1
2
3
4
Process 2: 2
4
4
5
Enter resources currently allocated to each process:
Process 0: 3
2
4
5
Process 1: 2
1
3
4
Process 2: 4
4
5
2
Enter available resources: 2
3
4
1
Safe Sequence: 0 1 2
RESULT:
AIM: Develop a C program to simulate the following contiguous memory allocation Techniques:
a) Worst fit b) Best fit c) First fit.
PROGRAM:
19
i) Contiguous Memory Allocation - Worst Fit
#include <stdio.h>
if (wstIdx != -1) {
allocation[i] = wstIdx;
blockSize[wstIdx] -= 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 :
Worst Fit Allocation:
Process 0 allocated to Block 4
Process 1 allocated to Block 1
Process 2 allocated to Block 4
Process 3 cannot be allocated
20
ii) Contiguous Memory Allocation – Best Fit
#include <stdio.h>
#include <limits.h>
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 :
Best Fit Allocation:
Process 0 allocated to Block 3
21
Process 1 allocated to Block 1
Process 2 allocated to Block 2
Process 3 allocated to Block 4
#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 :
First Fit Allocation:
Process 0 allocated to Block 1
Process 1 allocated to Block 4
Process 2 allocated to Block 1
Process 3 cannot be allocated
22
RESULT:
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;
}
OUTPUT:
Page 7 -> Frame: 7 - -
Page 0 -> Frame: 7 0 -
Page 1 -> Frame: 7 0 1
Page 2 -> Frame: 2 0 1
Page 0 -> Frame: 2 0 1
Page 3 -> Frame: 2 3 1
Page 0 -> Frame: 2 3 0
Page 4 -> Frame: 4 3 0
Page 2 -> Frame: 4 2 0
Page 3 -> Frame: 4 2 3
Page 0 -> Frame: 0 2 3
24
Page 3 -> Frame: 0 2 3
Page 2 -> Frame: 0 2 3
Total Page Faults: 10
#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;
}
}
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 :
Page 7 -> Frame: 7 0 0
Page 0 -> Frame: 7 0 0
Page 1 -> Frame: 1 0 0
Page 2 -> Frame: 1 2 0
Page 0 -> Frame: 1 2 0
Page 3 -> Frame: 3 2 0
Page 0 -> Frame: 3 2 0
Page 4 -> Frame: 3 4 0
Page 2 -> Frame: 2 4 0
Page 3 -> Frame: 2 4 3
Page 0 -> Frame: 2 0 3
Page 3 -> Frame: 2 0 3
Page 2 -> Frame: 2 0 3
Total Page Faults: 8
RESULT:
PROGRAM:
26
#include <stdio.h>
#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 :
Single Level Directory:
- file1.txt
- document.docx
- image.jpg
- data.csv
struct File {
char name[50];
};
struct UserDirectory {
27
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 :
Two Level Directory:
User1:
- file1.txt
- document1.docx
User2:
- image.jpg
- data.csv
28
RESULT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
29
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;
}
}
int main() {
struct Block* start = NULL;
return 0;
30
}
OUTPUT :
Linked File Allocation Simulation:
Linked List Blocks:
1 -> 2 -> 3 -> 4 -> NULL
RESULT:
#include <stdio.h>
#include <stdlib.h>
int main() {
31
int queue[20], seekSequence[20], head, size, totalSeekSequence = 0;
int left = 0, right = 199, distance, temp, i, j;
32
printf("NULL\n");
printf("Total seek sequence length: %d\n", totalSeekSequence);
return 0;
}
OUTPUT :
Enter the size of the disk queue: 3
Enter the disk queue (requests): 5
3
4
Enter the initial head position: 3
3 -> 4 -> 5 -> NULL
Total seek sequence length: 2
RESULT:
33