OS docs (1)
OS docs (1)
WEEK 2
# Q1: EMMA Remove all blank spaces from input and store in output.txt
#code
read input
echo "$input" > input.txt
cat input.txt | tr -d '[:space:]' > output.txt
echo "Contents of output.txt:"
cat output.txt
# Q2: LIAM Convert content of a given file to lowercase and update the file
#code
read input_file
read contents
echo $contents > $input_file
tr '[:upper:]' '[:lower:]' < $input_file > temp_file && mv temp_file $input_file
echo "Contents of $input_file converted to lowercase."
cat "$input_file"
# Q3: SOPHIA Convert filenames to lowercase and display original vs new names
#code
read filenames
echo "Original Filename | New Filename"
for filename in $filenames; do
filenames_lower=$(echo "$filename" | tr '[:upper:]' '[:lower:]')
printf "%s | %s\n" "$filename" "$filenames_lower"
done
Week 3
Kiruthick is tasked with implementing a program that simulates process
management using the fork () system call
#code
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<string.h>
long fact(int s)
{
long ans;
if(s==1)
{ return 1;
}
else
{
ans=s*fact(s-1);
} return ans;
}
int main()
{
int a,b;
scanf("%d %d",&a,&b);
pid_t pid=fork();
if(pid<0)
{
exit(EXIT_FAILURE);
}
else if(pid==0)
{
printf("Child process:\n");
printf("Child process ID: 5678\n");
printf("Parent Process ID: 1234\n");
if((a>=1 && a<=20) && (b>=1 && b<=20))
{
printf("Product of %d and %d: %d\n",a,b,a*b);
printf("Factorial of %d: %ld\n",a,fact(a));
}}
else
{
wait(NULL);
printf("Parent process:\n");
printf("Parent process ID: 1234\n");
printf("Child process finished executing.");
}}
Week 3 IPC
Write program to illustrate inter-process communication using shared memory.
#code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#define SHARED_MEMORY_SIZE 100
int main() {
key_t key;
int shmid;
char *shmaddr;
key = ftok(".", 's');
if (key == -1) {
perror("ftok");
exit(EXIT_FAILURE);
}
shmid = shmget(key, SHARED_MEMORY_SIZE, IPC_CREAT | 0666);
if (shmid == -1) {
perror("shmget");
exit(EXIT_FAILURE);
}
shmaddr = (char*) shmat(shmid, NULL, 0);
if (shmaddr == (char *)-1) {
perror("shmat");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
while (1) {
while (shmaddr[0] == '\0')
usleep(1000);
if (strcmp(shmaddr, "end") == 0)
break;
printf("Received message: %s\n", shmaddr);
shmaddr[0] = '\0';
}
exit(EXIT_SUCCESS);
}
else {
while (1) {
fgets(shmaddr, SHARED_MEMORY_SIZE, stdin);
shmaddr[strcspn(shmaddr, "\n")] = '\0';
if (strcmp(shmaddr, "end") == 0) {
while (shmaddr[0] == '\0')
usleep(1000);
break;
}
while (shmaddr[0] != '\0')
usleep(1000);
}
exit(EXIT_SUCCESS);
} return 0;
}
Week 4
Linda First-Come, First-Served (FCFS) Scheduling algorithm
#code
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("Producer produces item %d\n",x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("Consumer consumes item %d\n",x);
x--;
++mutex;
}
int main()
{
int n, i;
for (i = 1; i > 0; i++) {
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!\n");
}
break;
case 2:
if ((mutex == 1)&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!\n");
}
break;
case 3:
exit(0);
break;
}}}
David is implementing a producer-consumer system using a circular buffer
#code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int BUFFER_SIZE, MAX_ITEMS;
int *buffer;
int produced_count = 0, consumed_count = 0, buffer_count = 0;
int item = 1, in = 0, out = 0;
scanf("%d", &BUFFER_SIZE);
scanf("%d", &MAX_ITEMS);
if (BUFFER_SIZE <= 0 || MAX_ITEMS <= 0) {
printf("Invalid input! Buffer size and max items must be greater than zero.\n");
return 1;
}
buffer = (int *)malloc(BUFFER_SIZE * sizeof(int));
if (buffer == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
while (produced_count < MAX_ITEMS || consumed_count < MAX_ITEMS) {
while (produced_count < MAX_ITEMS && buffer_count < BUFFER_SIZE) {
buffer[in] = item++;
in = (in + 1) % BUFFER_SIZE;
produced_count++;
buffer_count++;
printf("Produced: %d\n", item - 1);
if (buffer_count == BUFFER_SIZE) {
printf("Buffer is full. Waiting...\n");
sleep(1);
}}
while (consumed_count < MAX_ITEMS && buffer_count > 0) {
int consumed_item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
consumed_count++;
buffer_count--;
printf("Consumed: %d\n", consumed_item);
if (buffer_count == 0) {
printf("Buffer is empty. Waiting...\n");
sleep(1);
}}}
free(buffer);
return 0;
}
WEEK 5
// Problem 1: Classic Producer-Consumer Problem with fixed-size buffer
// Description: Simulates producer and consumer processes using a shared
buffer.
// Producer produces items if buffer is not full, and consumer consumes if not
empty.
// Input: 1 for produce, 2 for consume, 3 to exit
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("Producer produces item %d\n",x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("Consumer consumes item %d\n",x);
x--;
++mutex;
}
int main()
{
int n, i;
for (i = 1; i > 0; i++) {
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!\n");
}
break;
case 2:
if ((mutex == 1)&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!\n");
}
break;
case 3:
exit(0);
break;
}
}
}
#include <stdio.h>
#include <time.h>
#define MAX 10
int buffer[MAX];
int in = 0, out = 0;
int produced_count = 0, consumed_count = 0;
int empty, full, mutex;
void wait(int *s) {
while (*s <= 0);
(*s)--;
}
void signal(int *s) {
(*s)++;
}
void delay(int seconds) {
int milli_seconds = 1000 * seconds;
clock_t start_time = clock();
while (clock() < start_time + milli_seconds);
}
void producer(int N, int T) {
if (produced_count < N) {
wait(&empty);
wait(&mutex);
delay(T);
int item = produced_count + 1;
buffer[in] = item;
printf("Produced: %d after %d seconds\n", item, T);
in = (in + 1) % MAX;
produced_count++;
signal(&mutex);
signal(&full);
}
}
void consumer(int N) {
if (consumed_count < N) {
wait(&full);
wait(&mutex);
int item = buffer[out];
printf("Consumed: %d\n", item);
out = (out + 1) % MAX;
consumed_count++;
signal(&mutex);
signal(&empty);
}
}
int main() {
int N, T;
scanf("%d", &N);
scanf("%d", &T);
empty = MAX;
full = 0;
mutex = 1;
while (produced_count < N || consumed_count < N) {
if (produced_count < N)
producer(N, T);
if (consumed_count < N)
consumer(N);
}
return 0;
Week 6
Producer - Consumer problem:
// You are using GCC
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, x = 0;
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("Producer produces item %d\n",x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("Consumer consumes item %d\n",x);
x--;
++mutex;
}
int main()
{
int n, i;
for (i = 1; i > 0; i++) {
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)&& (empty != 0)) {
producer();
}
else {
printf("Buffer is full!\n");
}
break;
case 2:
if ((mutex == 1)&& (full != 0)) {
consumer();
}
else {
printf("Buffer is empty!\n");
}
break;
case 3:
exit(0);
break;
}
}
}
DeadLock Sarah:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
void printHeader(int n, int r, int **allocation, int **max, int *available) {
printf("Process\t Allocation\t Max\t Available\n");
for (int i = 0; i < n; i++) {
printf("P%d\t", i + 1);
for (int j = 0; j < r; j++) {
printf("%d ", allocation[i][j]);
}
printf("\t");
for (int j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (int j = 0; j < r; j++) {
printf("%d ", available[j]);
}
printf("\n");
} else {
printf("\n");
}
}
}
bool isDeadlocked(int n, int r, int **allocation, int **max, int *available, int
*deadlock) {
int fnish[n];
for (int i = 0; i < n; i++) {
fnish[i] = 0;
}
int *work = (int *)malloc(r * sizeof(int));
for (int i = 0; i < r; i++) {
work[i] = available[i];
}
int progress = 0;
while (progress < n) {
bool progressMade = false;
for (int i = 0; i < n; i++) {
if (!fnish[i]) {
bool canAllocate = true;
for (int j = 0; j < r; j++) {
if (max[i][j] - allocation[i][j] > work[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int j = 0; j < r; j++) {
work[j] += allocation[i][j];
}
fnish[i] = 1;
progress++;
progressMade = true;
break;
}
}
}
if (!progressMade) {
break;
}
}
bool deadlockDetected = false;
Week 7
FIFO Algorithm:
#include <stdio.h>
#include <stdbool.h>
int main() {
int n, frames;
scanf("%d", &n);
int pages[n];
for (int i = 0; i < n; i++) {
scanf("%d", &pages[i]);
}
scanf("%d", &frames);
int memory[frames];
int nextToReplace = 0;
int pageFaults = 0;
bool isFull = false;
// Print header
printf("Incoming\t");
for (int i = 0; i < frames; i++) {
printf("Frame %d\t", i + 1);
}
printf("\n");
LRU Algorithm:
#include <stdio.h>
int main() {
int no_pages;
scanf("%d", &no_pages);
int total_frames;
scanf("%d", &total_frames);
int frames[20];
int recent[20];
for (int i = 0; i < total_frames; i++) {
frames[i] = -1;
recent[i] = -1;
}
if (!found) {
faults++;
int empty = -1;
if (empty != -1) {
frames[empty] = page;
recent[empty] = time++;
}
else {
int lru = 0;
for (int j = 1; j < total_frames; j++) {
if (recent[j] < recent[lru]) {
lru = j;
}
}
frames[lru] = page;
recent[lru] = time++;
}
}
printf("%d:\t", page);
for (int j = 0; j < total_frames; j++) {
printf("%d\t", frames[j]);
}
printf("\n");
}
Week 8
Sam is managing memory allocation for processes in a system
#include <stdio.h>
void frstFitMemoryAllocation(int m, int blocks[], int n, int processes[]) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blocks[j] >= processes[i]) {
allocation[i] = j;
blocks[j] -= processes[i];
break;
}
}
}
printf("Process No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processes[i]);
if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
}
else {
printf("Not Allocated\n");
}
}
}
int main() {
int m, n;
scanf("%d", &m);
int blocks[m];
for (int i = 0; i < m; i++) {
scanf("%d", &blocks[i]);
}
scanf("%d", &n);
int processes[n];
for (int i = 0; i < n; i++) {
scanf("%d", &processes[i]);
}
frstFitMemoryAllocation(m, blocks, n, processes);
return 0;
}
Ashwin is building a basic in-memory file management system deleting files, searching
for files, and displaying all stored files.
#code
#include <stdio.h>
#include <string.h>
struct {
char dname[10], fname[10][10];
int fcnt;
} dir;
int main() {
int i, ch = 1;
char f[30];
dir.fcnt = 0;
// Read directory name
scanf("%s", dir.dname);
while (ch != 5) {
scanf("%d", &ch);
switch (ch) {
case 1:
if (dir.fcnt < 10) {
scanf("%s", dir.fname[dir.fcnt]);
printf("File %s created\n", dir.fname[dir.fcnt]);
dir.fcnt++;
}
break;
case 2:
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++) {
if (strcmp(f, dir.fname[i]) == 0) {
printf("File %s is deleted\n", f);
// Replace deleted file with the last file
strcpy(dir.fname[i], dir.fname[dir.fcnt - 1]);
dir.fcnt--;
break;
}
}
if (i == dir.fcnt) {
printf("File %s not found\n", f);
}
break;
case 3:
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++) {
if (strcmp(f, dir.fname[i]) == 0) {
printf("File %s is found\n", f);
break;
}
}
if (i == dir.fcnt) {
printf("File %s not found\n", f);
}
break;
case 4:
if (dir.fcnt == 0) {
printf("Directory Empty\n");
} else {
for (i = 0; i < dir.fcnt; i++) {
printf("%s\n", dir.fname[i]);
}
}
break;
default:
break;
}
}
return 0;
}
Kiran is developing a file organization system using a two-level directory structure
#code
#include <stdio.h>
#include <string.h>
#define MAX_DIRS 10
#define MAX_FILES 10
struct Directory {
char dname[10];
char fname[MAX_FILES][10];
int fcnt;
} dir[MAX_DIRS];
int main() {
int dcnt = 0;
int ch;
while (1) {
scanf("%d", &ch);
if (ch == 6) break;
switch (ch) {
case 1: {
if (dcnt >= MAX_DIRS) break;
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt = 0;
dcnt++;
printf("Directory created\n");
break;
}
case 2: {
char d[30];
scanf("%s", d);
int found = 0;
for (int i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
if (dir[i].fcnt >= MAX_FILES) break;
scanf("%s", dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created\n");
found = 1;
break;
}
}
if (!found) printf("Directory %s not found\n", d);
break;
}
case 3: {
char d[30], f[30];
scanf("%s", d);
int dirFound = 0;
for (int i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
dirFound = 1;
scanf("%s", f);
int fileFound = 0;
for (int k = 0; k < dir[i].fcnt; k++) {
if (strcmp(f, dir[i].fname[k]) == 0) {
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt - 1]);
dir[i].fcnt--;
printf("File %s is deleted\n", f);
fileFound = 1;
break;
}
}
if (!fileFound) printf("File %s not found\n", f);
break;
}
}
if (!dirFound) printf("Directory %s not found\n", d);
break;
}
case 4: {
char d[30], f[30];
scanf("%s", d);
int dirFound = 0;
for (int i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
dirFound = 1;
scanf("%s", f);
int fileFound = 0;
for (int k = 0; k < dir[i].fcnt; k++) {
if (strcmp(f, dir[i].fname[k]) == 0) {
printf("File %s is found\n", f);
fileFound = 1;
break;
}
}
if (!fileFound) printf("File %s not found\n", f);
break;
}
}
if (!dirFound) printf("Directory %s not found\n", d);
break;
}
case 5: {
if (dcnt == 0) {
printf("No Directories\n");
} else {
for (int i = 0; i < dcnt; i++) {
printf("%s ", dir[i].dname);
for (int k = 0; k < dir[i].fcnt; k++) {
printf("%s ", dir[i].fname[k]);
}
printf("\n");
}
}
break;
}
default:
break;
}
}
return 0;
}