M Tech-Practicals
M Tech-Practicals
-1
Write a prgram to create a new process that exec new program using system calls fork(),execlp(), and wait().
Description:-
The child process uses execlp() to run a different program (e.g., ls command).
The parent process waits for the child to complete using wait().
Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
// Error occurred
perror("fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
// If execlp fails
perror("execlp failed");
exit(EXIT_FAILURE);
} else {
// Parent process
return 0;
Experiment No.-2
Write a program to display PID and PPID using system calls getpid() & getppid()
Description:-
Program :
#include <stdio.h>
#include <unistd.h>
int main() {
return 0;
}
Experiment No.-3
Write a program using I/O system calls open(),read() & write() to copy contents of one file to another file.
Description:
Uses open() (with flags) to create or truncate the destination file in write-only mode.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
char buffer[BUFFER_SIZE];
if (argc != 3) {
exit(EXIT_FAILURE);
if (src_fd < 0) {
exit(EXIT_FAILURE);
if (dest_fd < 0) {
exit(EXIT_FAILURE);
if (bytes_written != bytes_read) {
close(src_fd);
close(dest_fd);
exit(EXIT_FAILURE);
} if (bytes_read < 0) {
// Close files
close(src_fd);
close(dest_fd);
return 0;
Experiment No.-4
Description :
Process Management :
Process Management is a key function of any operating system (OS). It involves creating, scheduling, and
terminating processes — where a process is a running instance of a program.
Contains:
Program code
Stack
Data
Heap
2. Process States
Typical states in a process lifecycle:
css
CopyEdit
[New] → [Ready] → [Running] → [Waiting] → [Terminated]
PID
Process state
CPU registers
Memory limits
Open files
4. Operations on Processes
Creation: fork() in UNIX, CreateProcess() in Windows
A multithreaded C program using pthreads that creates multiple threads, where each thread runs a function
independently. This is a basic and clear example of process management with threads.
The main thread waits for all threads to complete using pthread_join().
Program:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 5
// Thread function
pthread_exit(NULL);
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
thread_ids[i] = i + 1;
exit(EXIT_FAILURE);
pthread_join(threads[i], NULL);
return 0;
Experiment No.-5
Write a program to simulate the following CPU Scheduling algorithms a)FCFS b)SJF c)Priority d) Round Robin
Description:
Input: Process ID, Burst Time, Arrival Time, and Priority (where applicable)
Program :
#include <stdio.h>
#include <stdlib.h>
typedef struct {
} Process;
scanf("%d", n);
p[i].pid = i + 1;
scanf("%d", &p[i].arrival_time);
scanf("%d", &p[i].burst_time);
scanf("%d", &p[i].priority);
p[i].remaining_time = p[i].burst_time;
p[i].completed = 0;
printf("\nPID\tAT\tBT\tWT\tTAT\n");
avg_wt += p[i].waiting_time;
avg_tat += p[i].turnaround_time;
p[i].waiting_time, p[i].turnaround_time);
int main() {
Process p[MAX];
input_processes(p, &n);
do {
scanf("%d", &choice);
switch (choice) {
case 1:
fcfs(p, n);
break;
case 2:
sjf(p, n);
break;
case 3:
priority_scheduling(p, n);
break;
case 4:
scanf("%d", &tq);
round_robin(p, n, tq);
break;
case 5:
exit(0);
return 0;
int time = 0;
Process temp[MAX];
temp[i] = p[i];
Process t = temp[i];
temp[i] = temp[j];
temp[j] = t;
time = temp[0].arrival_time;
time += temp[i].burst_time;
print_results(temp, n);
Process temp[MAX];
temp[i] = p[i];
temp[i].completed = 0;
min_idx = -1;
min_bt = temp[i].burst_time;
min_idx = i;
if (min_idx == -1) {
time++;
} else {
time += temp[min_idx].burst_time;
temp[min_idx].completed = 1;
completed++;
}
}
print_results(temp, n);
Process temp[MAX];
temp[i] = p[i];
temp[i].completed = 0;
idx = -1;
min_pr = temp[i].priority;
idx = i;
if (idx == -1) {
time++;
} else {
time += temp[idx].burst_time;
temp[idx].completed = 1;
completed++;
print_results(temp, n);
Process temp[MAX];
int time = 0, remain = n;
temp[i] = p[i];
temp[i].remaining_time = p[i].burst_time;
temp[i].waiting_time = 0;
time += exec_time;
temp[i].remaining_time -= exec_time;
if (temp[i].remaining_time == 0) {
remain--;
print_results(temp, n);
}
Experiment No.-6
Process Synchronization- Write a program to simulate producer –consumer problem using semaphores.
Description:
Process Synchronization :
Process Synchronization is a fundamental concept in operating systems that ensures multiple processes or
threads can coordinate their execution when sharing resources (like memory, files, or hardware) without causing race
conditions, deadlocks, or inconsistent data.
Semaphores
A signaling mechanism.
Types:
Monitors
Condition Variables
Allows threads to wait for specific conditions to be true (e.g., queue not empty).
Semaphores used:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
pthread_mutex_t mutex;
int item;
while (1) {
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
while (1) {
item = buffer[out];
int main() {
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_join(prod_thread, NULL);
pthread_join(cons_thread, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
Experiment No.-7
Deadlock Write a program to simulate Bankers algorithm for the purpose of deadlock avoidance.
Description:
The Banker's Algorithm is used in operating systems to avoid deadlock by ensuring resource allocation happens only if
it leads to a safe state.
#include<stdio.h>
int main() {
printf("%d\t", need[i][j]);
printf("\n");
/* once process execute variable done will stop them for again execution */
done[i] = 0;
if (done[i] == 0) {
break;
//when need matrix is not greater then available matrix then if j==c will true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in available resources */
available[j] += alc[i][j];
count++;
terminate = 0;
} else {
terminate++;
if (terminate == (p - 1)) {
break;
if (terminate != (p - 1)) {
printf("%d\t", available[i]);
printf("p%d\t", safe[i]);
return 0;
}
Experiment No.-8
Description:
Deadlock occurs when a group is stuck in a process because everyone is hanging onto resources while waiting for other
ways to get them.
Detection of deadlocks:
The OS predicts a future deadlock with this plan of action. As a result, it runs a deadlock detection procedure
periodically and performs a recovery plan when it detects a dead draw.
Deadlocks can be created through deadlock detection. After that, assess the system's condition to see if a
stalemate has occurred and resolve it. Tracking the allocation of resources and process status, rolling back,
restarting one or more processes, and breaking discovered deadlocks are all done using algorithms. Identifying
deadlocks is simple since the operating system's resource scheduler is aware of the resources that every process
has locked or is actively seeking.
Program:
1. #include <stdio.h>
2. #define MAX_PROCESSES 10
3. #define MAX_RESOURCES 10
4.
5. int allocation[MAX_PROCESSES][MAX_RESOURCES];
6. int request[MAX_PROCESSES][MAX_RESOURCES];
7. int available[MAX_RESOURCES];
8. int resources[MAX_RESOURCES];
9. int work[MAX_RESOURCES];
10. int marked[MAX_PROCESSES];
11.
12. int main() {
13. int num_processes, num_resources;
14.
15. printf("Enter the number of processes: ");
16. scanf("%d", &num_processes);
17.
18. printf("Enter the number of resources: ");
19. scanf("%d", &num_resources);
20.
21. // Input total resources
22. for (int i = 0; i < num_resources; i++) {
23. printf("Enter the total amount of Resource R%d: ", i + 1);
24. scanf("%d", &resources[i]);
25. }
26.
27. // Input request matrix
28. printf("Enter the request matrix:\n");
29. for (int i = 0; i < num_processes; i++) {
30. for (int j = 0; j < num_resources; j++) {
31. scanf("%d", &request[i][j]);
32. }
33. }
34.
35. // User Input allocation matrix
36. printf("Enter the allocation matrix:\n");
37. for (int i = 0; i < num_processes; i++) {
38. for (int j = 0; j < num_resources; j++) {
39. scanf("%d", &allocation[i][j]);
40. }
41. }
42.
43. // Initialization of the available resources
44. for (int j = 0; j < num_resources; j++) {
45. available[j] = resources[j];
46. for (int i = 0; i < num_processes; i++) {
47. available[j] -= allocation[i][j];
48. }
49. }
50.
51. // Mark processes with zero allocation
52. for (int i = 0; i < num_processes; i++) {
53. int count = 0;
54. for (int j = 0; j < num_resources; j++) {
55. if (allocation[i][j] == 0) {
56. count++;
57. } else {
58. break;
59. }
60. }
61. if (count == num_resources) {
62. marked[i] = 1;
63. }
64. }
65.
66. // Initialize work with available
67. for (int j = 0; j < num_resources; j++) {
68. work[j] = available[j];
69. }
70.
71. // Mark processes with requests <= work
72. for (int i = 0; i < num_processes; i++) {
73. int can_be_processed = 1;
74. if (marked[i] != 1) {
75. for (int j = 0; j < num_resources; j++) {
76. if (request[i][j] > work[j]) {
77. can_be_processed = 0;
78. break;
79. }
80. }
81. if (can_be_processed) {
82. marked[i] = 1;
83. for (int j = 0; j < num_resources; j++) {
84. work[j] += allocation[i][j];
85. }
86. }
87. }
88. }
89.
90. // Check for unmarked processes (deadlock)
91. int deadlock = 0;
92. for (int i = 0; i < num_processes; i++) {
93. if (marked[i] != 1) {
94. deadlock = 1;
95. break;
96. }
97. }
98.
99. if (deadlock) {
100. printf("Deadlock detected\n");
101. } else {
102. printf("No deadlock possible\n");
103. }
104.
105. return 0;
106. }
Experiment No.-9
1. FIFO (First-In-First-Out)
2. LRU (Least Recently Used)
3. LFU (Least Frequently Used)
The user can choose which algorithm to run. The page reference string and number of frames are input
interactively.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Function prototypes
int main() {
int pages[MAX];
scanf("%d", &n);
scanf("%d", &pages[i]);
scanf("%d", &capacity);
scanf("%d", &choice);
switch (choice) {
case 1:
fifo(pages, n, capacity);
break;
case 2:
lru(pages, n, capacity);
break;
case 3:
lfu(pages, n, capacity);
break;
default:
printf("Invalid choice.\n");
return 0;
int found = 0;
if (frames[j] == pages[i]) {
found = 1;
break;
if (!found) {
frames[front] = pages[i];
faults++;
printf("Frame: ");
if (frames[k] != -1)
printf("%d ", frames[k]);
else
printf("- ");
printf("\n");
frames[i] = -1;
time[i] = 0;
int found = 0;
if (frames[j] == pages[i]) {
found = 1;
time[j] = i;
break;
if (!found) {
if (frames[j] == -1) {
pos = j;
break;
min = time[j];
pos = j;
frames[pos] = pages[i];
time[pos] = i;
faults++;
printf("Frame: ");
if (frames[k] != -1)
else
printf("- ");
printf("\n");
int faults = 0;
frames[i] = -1;
freq[i] = 0;
time[i] = 0;
int found = 0;
if (frames[j] == pages[i]) {
freq[j]++;
time[j] = i;
found = 1;
break;
if (!found) {
if (frames[j] == -1) {
pos = j;
break;
} else if (freq[j] < min_freq || (freq[j] == min_freq && time[j] < min_time)) {
min_freq = freq[j];
min_time = time[j];
pos = j;
frames[pos] = pages[i];
freq[pos] = 1;
time[pos] = i;
faults++;
printf("Frame: ");
if (frames[k] != -1)
else
printf("- ");
printf("\n");
Experiment No.-10
I/O System Write a program to simulate the following file organization techniques
Description:
#include <stdio.h>
#include <string.h>
typedef struct {
char filename[50];
} File;
typedef struct {
char username[50];
File files[MAX];
int fileCount;
} UserDirectory;
// Function prototypes
void singleLevelDirectory();
void twoLevelDirectory();
int main() {
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
singleLevelDirectory();
break;
case 2:
twoLevelDirectory();
break;
default:
printf("Invalid choice!\n");
return 0;
void singleLevelDirectory() {
File files[MAX];
int fileCount = 0;
int choice;
char name[50];
while (1) {
printf("1. Create File\n2. Delete File\n3. Search File\n4. List Files\n5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%s", name);
if (strcmp(files[i].filename, name) == 0) {
printf("File already exists!\n");
goto skip;
strcpy(files[fileCount].filename, name);
fileCount++;
break;
case 2:
scanf("%s", name);
if (strcmp(files[i].filename, name) == 0) {
fileCount--;
goto skip;
break;
case 3:
scanf("%s", name);
if (strcmp(files[i].filename, name) == 0) {
printf("File found.\n");
goto skip;
break;
case 4:
printf("Files in directory:\n");
printf("%s\n", files[i].filename);
break;
case 5:
return;
default:
printf("Invalid choice!\n");
skip:;
void twoLevelDirectory() {
UserDirectory users[MAX];
int userCount = 0;
int choice;
while (1) {
printf("1. Create User\n2. Create File\n3. Delete File\n4. Search File\n5. List Files\n6. Exit\n");
switch (choice) {
case 1:
scanf("%s", uname);
if (strcmp(users[i].username, uname) == 0) {
goto skip;
strcpy(users[userCount].username, uname);
users[userCount].fileCount = 0;
userCount++;
break;
case 2:
scanf("%s", uname);
scanf("%s", fname);
if (strcmp(users[i].username, uname) == 0) {
if (strcmp(users[i].files[j].filename, fname) == 0) {
goto skip;
}
}
strcpy(users[i].files[users[i].fileCount].filename, fname);
users[i].fileCount++;
goto skip;
break;
case 3:
scanf("%s", uname);
scanf("%s", fname);
if (strcmp(users[i].username, uname) == 0) {
if (strcmp(users[i].files[j].filename, fname) == 0) {
users[i].fileCount--;
goto skip;
goto skip;
}
break;
case 4:
scanf("%s", uname);
scanf("%s", fname);
if (strcmp(users[i].username, uname) == 0) {
if (strcmp(users[i].files[j].filename, fname) == 0) {
printf("File found.\n");
goto skip;
goto skip;
break;
case 5:
scanf("%s", uname);
if (strcmp(users[i].username, uname) == 0) {
printf("Files for user %s:\n", uname);
printf("%s\n", users[i].files[j].filename);
goto skip;
break;
case 6:
return;
default:
printf("Invalid choice!\n");
skip:;
Experiment No.-11
Write a program to simulate the following file allocation strategies. A) Sequencial b) Indexed
Description :
Program :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BLOCKS 100
typedef struct {
char name[20];
int start;
int length;
} SequentialFile;
typedef struct {
char name[20];
int indexBlock;
int length;
} IndexedFile;
void sequentialAllocation();
void indexedAllocation();
int main() {
int choice;
scanf("%d", &choice);
disk[i] = 0;
switch (choice) {
case 1:
sequentialAllocation();
break;
case 2:
indexedAllocation();
break;
default:
printf("Invalid choice.\n");
return 0;
void sequentialAllocation() {
SequentialFile files[20];
while (1) {
scanf("%d", &choice);
if (choice == 1) {
SequentialFile f;
scanf("%s", f.name);
scanf("%d", &f.start);
scanf("%d", &f.length);
int allocated = 1;
continue;
}
if (disk[i] == 1) {
allocated = 0;
break;
if (allocated) {
disk[i] = 1;
files[fileCount++] = f;
} else {
} else if (choice == 2) {
printf("Allocated Files:\n");
printf("\n");
} else if (choice == 3) {
break;
} else {
printf("Invalid choice.\n");
}
}
void indexedAllocation() {
IndexedFile files[20];
while (1) {
scanf("%d", &choice);
if (choice == 1) {
IndexedFile f;
scanf("%s", f.name);
scanf("%d", &f.length);
if (disk[i] == 0) {
indexBlock = i;
disk[i] = 1;
break;
if (indexBlock == -1) {
continue;
}
f.indexBlock = indexBlock;
int allocatedBlocks = 0;
if (disk[i] == 0) {
f.blocks[allocatedBlocks++] = i;
disk[i] = 1;
disk[indexBlock] = 0;
disk[f.blocks[i]] = 0;
} else {
files[fileCount++] = f;
} else if (choice == 2) {
printf("Allocated Files:\n");
files[i].name, files[i].indexBlock);
printf("\n");
} else if (choice == 3) {
break;
} else {
printf("Invalid choice.\n");
}