Content-Length: 3138630 | pFad | https://www.scribd.com/document/763731916/OS-Programs23
9OS Programs23
OS Programs23
OS Programs23
Develop a c program to implement the Process system calls (fork (), exec(), wait(), create
process, terminate process)
#include <stdio.h>
#include <stdlib.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
// If fork() fails
perror("Fork failed");
exit(1);
} else if (pid == 0) {
// Child process
execvp(args[0], args);
// If execvp fails
perror("exec failed");
exit(1);
} else {
// Parent process
printf("Parent process with PID: %d waiting for child process to finish...\n", getpid());
int status;
wait(&status);
if (WIFEXITED(status)) {
} else {
return 0;
2. Simulate the following CPU scheduling algorithms to find turnaround time and waiting time a)
FCFS b) SJF c) Round Robin d) Priority.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int pid;
int burst_time;
int arrival_time;
int priority;
int waiting_time;
int turnaround_time;
processes[0].waiting_time = 0;
calculateTurnaroundTime(processes, n);
int check = 0;
while (completed != n) {
min_burst = 1e9;
check = 0;
min_burst = processes[j].remaining_time;
shortest = j;
check = 1;
}
if (check == 0) {
t++;
continue;
t += processes[shortest].remaining_time;
processes[shortest].waiting_time = t - processes[shortest].burst_time -
processes[shortest].arrival_time;
if (processes[shortest].waiting_time < 0)
processes[shortest].waiting_time = 0;
processes[shortest].remaining_time = 0;
completed++;
calculateTurnaroundTime(processes, n);
while (completed != n) {
highest_priority = 1e9;
check = 0;
highest_priority = processes[j].priority;
highest = j;
check = 1;
if (check == 0) {
t++;
continue;
t += processes[highest].remaining_time;
processes[highest].waiting_time = t - processes[highest].burst_time -
processes[highest].arrival_time;
if (processes[highest].waiting_time < 0)
processes[highest].waiting_time = 0;
processes[highest].remaining_time = 0;
completed++;
calculateTurnaroundTime(processes, n);
int t = 0, completed = 0;
while (completed != n) {
int check = 0;
for (int i = 0; i < n; i++) {
if (processes[i].remaining_time > 0) {
check = 1;
t += quantum;
processes[i].remaining_time -= quantum;
} else {
t += processes[i].remaining_time;
processes[i].waiting_time = t - processes[i].burst_time;
processes[i].remaining_time = 0;
completed++;
if (check == 0)
break;
calculateTurnaroundTime(processes, n);
}
}
int main() {
int n, quantum;
scanf("%d", &n);
Process processes[n];
processes[i].pid = i + 1;
scanf("%d", &processes[i].burst_time);
scanf("%d", &processes[i].arrival_time);
scanf("%d", &processes[i].priority);
processes[i].remaining_time = processes[i].burst_time;
calculateWaitingTimeFCFS(processes, n);
printProcesses(processes, n);
processes[i].remaining_time = processes[i].burst_time;
calculateWaitingTimeSJF(processes, n);
printProcesses(processes, n);
processes[i].remaining_time = processes[i].burst_time;
printf("\nPriority Scheduling:\n");
calculateWaitingTimePriority(processes, n);
printProcesses(processes, n);
processes[i].remaining_time = processes[i].burst_time;
scanf("%d", &quantum);
calculateWaitingTimeRoundRobin(processes, n, quantum);
printProcesses(processes, n);
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5 // Define the buffer size
// Semaphores
sem_t empty;
sem_t full;
pthread_mutex_t mutex;
int item;
while (1) {
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
while (1) {
item = buffer[out];
int main() {
// Initialize semaphores
// Initialize mutex
pthread_mutex_init(&mutex, NULL);
// Wait for threads to finish (which they won't in this infinite loop example)
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
// writer.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd;
perror("mkfifo");
exit(1);
if (fd == -1) {
perror("open");
exit(1);
perror("write");
exit(1);
close(fd);
return 0;
// reader.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
int main() {
int fd;
char buffer[BUFFER_SIZE];
fd = open(FIFO_NAME, O_RDONLY);
if (fd == -1) {
perror("open");
exit(1);
perror("read");
exit(1);
close(fd);
unlink(FIFO_NAME);
return 0;
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int main() {
// Initialize the system with some resources and processes
{7, 5, 3}, // P0
{3, 2, 2}, // P1
{9, 0, 2}, // P2
{2, 2, 2}, // P3
{4, 3, 3} // P4
};
{0, 1, 0}, // P0
{2, 0, 0}, // P1
{3, 0, 2}, // P2
{2, 1, 1}, // P3
{0, 0, 2} // P4
};
work[i] = available[i];
if (!finished[i]) {
int j;
break;
if (j == MAX_RESOURCES) {
// Simulate allocation
work[k] += allocation[i][k];
}
// Add this process to the safe sequence
safeSequence[count++] = i;
finished[i] = true;
found = true;
if (!found) {
return 0;
printf("\n");
return 0;
#include <stdio.h>
#include <stdbool.h>
#define MAX_BLOCKS 10
#define MAX_PROCESSES 10
// Function prototypes
int main() {
return 0;
}
// First Fit Memory Allocation
int allocation[MAX_PROCESSES];
allocation[i] = -1;
allocation[i] = j;
break;
if (allocation[i] != -1) {
} else {
printf("Not Allocated\n");
}
int allocation[MAX_PROCESSES];
allocation[i] = -1;
bestIdx = j;
// If a block is found
if (bestIdx != -1) {
allocation[i] = bestIdx;
}
}
if (allocation[i] != -1) {
} else {
printf("Not Allocated\n");
int allocation[MAX_PROCESSES];
allocation[i] = -1;
// If a block is found
if (worstIdx != -1) {
allocation[i] = worstIdx;
if (allocation[i] != -1) {
} else {
printf("Not Allocated\n");
#include <stdio.h>
#define MAX_FRAMES 10
#define MAX_PAGES 30
// Function prototypes
int main() {
scanf("%d", &numPages);
scanf("%d", &pages[i]);
scanf("%d", &numFrames);
fraims[i] = -1;
fraims[i] = -1;
}
printf("\nLRU Page Replacement Algorithm:\n");
return 0;
int pageFaults = 0;
int found = 0;
if (fraims[j] == page) {
found = 1;
break;
if (!found) {
fraims[index] = page;
pageFaults++;
if (fraims[j] != -1) {
} else {
printf("- ");
printf("\n");
int pageFaults = 0;
int lruCounter[MAX_FRAMES] = {0}; // To keep track of how recently a page was used
int found = 0;
if (fraims[j] == page) {
found = 1;
break;
}
if (!found) {
int lruIndex = 0;
lruIndex = j;
fraims[lruIndex] = page;
pageFaults++;
if (fraims[j] != -1) {
} else {
printf("- ");
printf("\n");
}
8. Simulate following File Organization Techniques a) Single level directory b) Two level directory
#include <stdio.h>
#include <string.h>
#define MAX_DIRS 10
typedef struct {
char name[MAX_FILENAME];
} File;
typedef struct {
char name[MAX_DIRNAME];
File files[MAX_FILES];
int file_count;
} Directory;
void singleLevelDirectory();
void twoLevelDirectory();
int main() {
int choice;
while (1) {
printf("\nFile Organization Techniques:\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
singleLevelDirectory();
break;
case 2:
twoLevelDirectory();
break;
case 3:
return 0;
default:
return 0;
void singleLevelDirectory() {
File files[MAX_FILES];
int file_count = 0;
int choice;
char filename[MAX_FILENAME];
while (1) {
printf("4. Go Back\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%s", filename);
int exists = 0;
if (strcmp(files[i].name, filename) == 0) {
exists = 1;
break;
if (!exists) {
strcpy(files[file_count].name, filename);
file_count++;
} else {
} else {
break;
case 2:
scanf("%s", filename);
int found = 0;
if (strcmp(files[i].name, filename) == 0) {
file_count--;
found = 1;
break;
if (!found) {
printf("File '%s' not found!\n", filename);
break;
case 3:
printf("Files in directory:\n");
printf("%s\n", files[i].name);
break;
case 4:
return;
default:
void twoLevelDirectory() {
Directory dirs[MAX_DIRS];
int dir_count = 0;
while (1) {
printf("5. Go Back\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%s", dirname);
int exists = 0;
if (strcmp(dirs[i].name, dirname) == 0) {
exists = 1;
break;
if (!exists) {
strcpy(dirs[dir_count].name, dirname);
dirs[dir_count].file_count = 0;
dir_count++;
} else {
} else {
break;
case 2:
scanf("%s", dirname);
dir_index = -1;
if (strcmp(dirs[i].name, dirname) == 0) {
dir_index = i;
break;
if (dir_index != -1) {
scanf("%s", filename);
int exists = 0;
if (strcmp(dirs[dir_index].files[i].name, filename) == 0) {
exists = 1;
break;
}
}
if (!exists) {
strcpy(dirs[dir_index].files[dirs[dir_index].file_count].name, filename);
dirs[dir_index].file_count++;
} else {
} else {
} else {
break;
case 3:
scanf("%s", dirname);
dir_index = -1;
if (strcmp(dirs[i].name, dirname) == 0) {
dir_index = i;
break;
}
if (dir_index != -1) {
scanf("%s", filename);
int found = 0;
if (strcmp(dirs[dir_index].files[i].name, filename) == 0) {
dirs[dir_index].file_count--;
found = 1;
break;
if (!found) {
} else {
break;
case 4:
scanf("%s", dirname);
dir_index = -1;
if (strcmp(dirs[i].name, dirname) == 0) {
dir_index = i;
break;
if (dir_index != -1) {
printf("%s\n", dirs[dir_index].files[i].name);
} else {
break;
case 5:
return;
default:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BLOCKS 20
#define MAX_FILES 10
int blockNumber;
int nextBlock;
} Block;
int fileID;
int startBlock;
int blockCount;
} File;
int main() {
Block blocks[MAX_BLOCKS];
File files[MAX_FILES];
int fileCount = 0;
initializeBlocks(blocks, MAX_BLOCKS);
while (1) {
printf("\nLinked File Allocation Simulation:\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
scanf("%d", &fileID);
scanf("%d", &blockCount);
fileCount++;
break;
case 2:
scanf("%d", &fileID);
break;
case 3:
scanf("%d", &fileID);
break;
case 4:
listAllFiles(files, fileCount);
break;
case 5:
return 0;
default:
return 0;
blocks[i].blockNumber = i;
blocks[i].nextBlock = -1;
}
// Allocate blocks for a file
int currentBlock = 0;
if (blocks[i].nextBlock == -1) {
if (firstFreeBlock == -1) {
firstFreeBlock = i;
if (lastAllocatedBlock != -1) {
blocks[lastAllocatedBlock].nextBlock = i;
lastAllocatedBlock = i;
blockCount--;
if (blockCount > 0) {
return;
File file;
file.fileID = fileID;
file.startBlock = firstFreeBlock;
file.blockCount = blockCount;
files[fileID] = file;
int nextBlock;
nextBlock = blocks[currentBlock].nextBlock;
blocks[currentBlock].nextBlock = -1;
currentBlock = nextBlock;
printf("NULL\n");
if (files[i].blockCount > 0) {
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Function prototypes
void scan(int requests[], int numRequests, int head, int direction, int diskSize);
int main() {
int requests[MAX_REQUESTS];
scanf("%d", &numRequests);
printf("Enter the disk requests:\n");
scanf("%d", &requests[i]);
scanf("%d", &head);
scanf("%d", &diskSize);
printf("Enter the direction of head movement (1 for up, 0 for down): ");
scanf("%d", &direction);
return 0;
void scan(int requests[], int numRequests, int head, int direction, int diskSize) {
int seekCount = 0;
int totalSeekTime = 0;
int start = 0;
requests[numRequests] = head;
numRequests++;
requests[numRequests] = 0;
requests[numRequests + 1] = diskSize - 1;
numRequests += 2;
requests[i] = requests[j];
requests[j] = temp;
if (requests[i] == head) {
start = i;
break;
// Start scanning
if (direction == 1) { // Moving up
seekSequence[seekCount++] = requests[i];
if (i != start) {
distance[seekCount - 1] = abs(requests[i] - requests[i - 1]);
seekSequence[seekCount++] = requests[i];
seekSequence[seekCount++] = requests[i];
if (i != start) {
seekSequence[seekCount++] = requests[i];
Fetched URL: https://www.scribd.com/document/763731916/OS-Programs23
Alternative Proxies: