0% found this document useful (0 votes)
2 views

OS docs (1)

The document outlines various programming tasks related to process management, inter-process communication, and producer-consumer problems using C. It includes code snippets for removing spaces from input, converting file contents to lowercase, simulating process interactions with fork(), and implementing scheduling algorithms. Additionally, it covers deadlock detection in resource allocation systems.

Uploaded by

23f2001886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OS docs (1)

The document outlines various programming tasks related to process management, inter-process communication, and producer-consumer problems using C. It includes code snippets for removing spaces from input, converting file contents to lowercase, simulating process interactions with fork(), and implementing scheduling algorithms. Additionally, it covers deadlock detection in resource allocation systems.

Uploaded by

23f2001886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

DONT MAKE ANY CHANGES TO THIS DOCS

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.");
}}

Neka, a junior software developer,to simulate a parent-child process interaction


using the fork () system call
#code​
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<string.h>
int main()
{
int a;
scanf("%d",&a);
pid_t pid=fork();
if(pid<0)
{
exit(EXIT_FAILURE);​
}
else if(pid==0)
{
printf("Child process:\nChild Process ID: 5678\nParent Process ID: 1234\n");
printf("Received integer: %d\n",a);
}
else
{
wait(NULL);
printf("Parent process:\nParent process ID: 1234\nChild 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;
}

Akil is developing a simulation for a producer-consumer problem using a


circular buffer with a maximum capacity.
#code​
#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 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;
}
}
}

// Problem 3: Circular Buffer Simulation with Semaphores


// Description: Akil's simulation uses semaphores to synchronize
production/consumption of M items.
// Each item takes T seconds to produce. Consumer consumes immediately.

#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;

for (int i = 0; i < n; i++) {


if (!fnish[i]) {
deadlockDetected = true;
deadlock[i] = 1;
} else {
deadlock[i] = 0;
}
}
free(work);
return deadlockDetected;
}
void printDeadlock(int n, int *deadlock) {
printf("System is in Deadlock and the Deadlock process are\n");
for (int i = 0; i < n; i++) {
if (deadlock[i]) {
printf("P%d\t", i + 1);
}
}
printf("\n");
}
int main() {
int n, r;
scanf("%d", &n);
scanf("%d", &r);
int **allocation = (int **)malloc(n * sizeof(int *));
int **max = (int **)malloc(n * sizeof(int *));
for (int i = 0; i < n; i++) {
allocation[i] = (int *)malloc(r * sizeof(int));
max[i] = (int *)malloc(r * sizeof(int));
}
int *available = (int *)malloc(r * sizeof(int));
for (int i = 0; i < n; i++) {
for (int j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}

for (int i = 0; i < n; i++) {


for (int j = 0; j < r; j++) {
scanf("%d", &allocation[i][j]);
}
}
for (int i = 0; i < r; i++) {
scanf("%d", &available[i]);
}
int *deadlock = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
deadlock[i] = 0;
}
printHeader(n, r, allocation, max, available);
if (isDeadlocked(n, r, allocation, max, available, deadlock)) {
printDeadlock(n, deadlock);
} else {
printf("No Deadlock Occur\n");
}
// Free allocated memory
for (int i = 0; i < n; i++) {
free(allocation[i]);
free(max[i]);
}
free(allocation);
free(max);
free(available);
free(deadlock);
return 0;
}

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;

// Initialize all frames to -1 (empty)


for (int i = 0; i < frames; i++) {
memory[i] = -1;
}

// Print header
printf("Incoming\t");
for (int i = 0; i < frames; i++) {
printf("Frame %d\t", i + 1);
}
printf("\n");

for (int i = 0; i < n; i++) {


int currentPage = pages[i];
bool pageFound = false;

// Check if page is already in memory


for (int j = 0; j < frames; j++) {
if (memory[j] == currentPage) {
pageFound = true;
break;
}
}

// If page not found, handle page fault


if (!pageFound) {
pageFaults++;

// If memory isn't full yet, fill empty slots first


if (!isFull) {
memory[nextToReplace] = currentPage;
nextToReplace++;
if (nextToReplace == frames) {
isFull = true;
nextToReplace = 0;
}
} else {
// Standard FIFO replacement
memory[nextToReplace] = currentPage;
nextToReplace = (nextToReplace + 1) % frames;
}
}

// Print current state


printf("%d\t\t", currentPage);
for (int j = 0; j < frames; j++) {
if (memory[j] == -1)
printf("-\t");
else
printf("%d\t", memory[j]);
}
printf("\n");
}

printf("Total Page Faults: %d\n", pageFaults);


return 0;
}

LRU Algorithm:​

#include <stdio.h>

int main() {
int no_pages;
scanf("%d", &no_pages);

int pages[100]; // constraint: 1 ≤ no_pages ≤ 100


for (int i = 0; i < no_pages; i++) {
scanf("%d", &pages[i]);
}

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;
}

int time = 0, faults = 0;

for (int i = 0; i < no_pages; i++) {


int page = pages[i];
int found = 0;

for (int j = 0; j < total_frames; j++) {


if (frames[j] == page) {
recent[j] = time++;
found = 1;
break;
}
}

if (!found) {
faults++;
int empty = -1;

for (int j = 0; j < total_frames; j++) {


if (frames[j] == -1) {
empty = j;
break;
}
}

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");
}

printf("Total Number of Page Faults: %d\n", faults);


return 0;
}

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;
}

Lina is managing a set of memory blocks and needs to allocate them to


different processes using the Best Fit algorithm​
#code ​
#include <stdio.h>
void bestFit(int blockSizes[], int m, int processSizes[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1;
}
for (int i = 0; i < n; i++) {
int bestIdx = -1;
int minDiff = 10001;
for (int j = 0; j < m; j++) {
if (blockSizes[j] >= processSizes[i] && blockSizes[j] - processSizes[i] <
minDiff) {
minDiff = blockSizes[j] - processSizes[i];
bestIdx = j;
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSizes[bestIdx] -= processSizes[i];
}
}
printf("Process No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSizes[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 blockSizes[m];
for (int i = 0; i < m; i++) {
scanf("%d", &blockSizes[i]);
}
scanf("%d", &n);
int processSizes[n];
for (int i = 0; i < n; i++) {
scanf("%d", &processSizes[i]);
}
bestFit(blockSizes, m, processSizes, n);
return 0;
}

Week 9
Aryan working as junior administor First Come First Serve (FCFS) disk scheduling
algorithm
# code​
#include <stdio.h>
#include <math.h>
void FCFS(int arr[], int size, int head) {
int seek_count = 0;
int cur_track, distance;
// Loop through the request array to calculate seek count
for (int i = 0; i < size; i++) {
cur_track = arr[i];
// Calculate absolute distance
distance = fabs(head - cur_track);
// Increase the total seek count
seek_count += distance;
// Accessed track becomes new head
head = cur_track;
}
printf("Total number of seek operations: %d\n", seek_count);
// Print the seek sequence
printf("Seek Sequence is: ");
for (int i = 0; i < size; i++) {
printf("%d", arr[i]);
if (i < size - 1) {
printf(" ");
}
}
}
int main() {
int size;
scanf("%d", &size);
int arr[size];
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int head;
scanf("%d", &head);
FCFS(arr, size, head);
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;
}

You might also like