From d46358f2dadf3578695a12f461eef52938638bf7 Mon Sep 17 00:00:00 2001 From: sherlock Date: Fri, 11 Apr 2025 15:22:22 +0530 Subject: [PATCH] Added Endsem Questions --- OS/endsem/bashq.sh | 48 ++++++++++++++++++ OS/endsem/cq.c | 121 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 OS/endsem/bashq.sh create mode 100644 OS/endsem/cq.c diff --git a/OS/endsem/bashq.sh b/OS/endsem/bashq.sh new file mode 100644 index 0000000..2d81ab7 --- /dev/null +++ b/OS/endsem/bashq.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Ask for the directory +echo "Enter directory path:" +read dir + +while true; do + echo "" + echo "Menu:" + echo "1. Convert .txt files to .py in $dir" + echo "2. List ownership properties (ls -l) of all files in $dir" + echo "3. Count .sh files and subdirectories in $dir" + echo "4. Exit" + echo -n "Choose an option: " + read option + + case $option in + 1) + for file in "$dir"/*.txt; do + if [ -f "$file" ]; then + new="${file%.txt}.py" + mv "$file" "$new" + echo "Renamed: $file -> $new" + fi + done + ;; + 2) + ls -l "$dir" + ;; + 3) + # Count .sh files in the given directory (errors hidden if none found) + sh_count=$(ls -1 "$dir"/*.sh 2>/dev/null | wc -l) + # Count subdirectories (using a trailing slash pattern) + dir_count=$(ls -d "$dir"/*/ 2>/dev/null | wc -w) + echo ".sh file count: $sh_count" + echo "Subdirectory count: $dir_count" + ;; + 4) + echo "Exiting..." + break + ;; + *) + echo "Invalid option. Please try again." + ;; + esac +done + +echo "Bye!" diff --git a/OS/endsem/cq.c b/OS/endsem/cq.c new file mode 100644 index 0000000..0aaf784 --- /dev/null +++ b/OS/endsem/cq.c @@ -0,0 +1,121 @@ +#include +#include +#include +#define MAX_PROC 10 +#define MAX_REF 100 +#define MAX_FRAMES 10 + +typedef struct { + int id; // Process ID + int frames; // Number of frames for this process + int n; // Number of page references + int ref[MAX_REF]; // Array of page references + int faults; // Page faults counter +} Process; + +Process procs[MAX_PROC]; +int proc_count = 0; + +void *simulateOptimal(void *arg) { + Process *p = (Process *)arg; + int frameArr[MAX_FRAMES]; + int i, j; + + // Initialize frames as empty (-1) + for (i = 0; i < p->frames; i++) { + frameArr[i] = -1; + } + p->faults = 0; + + // Process each page reference + for (i = 0; i < p->n; i++) { + int page = p->ref[i]; + int found = 0; + // Check if page is already in a frame + for (j = 0; j < p->frames; j++) { + if (frameArr[j] == page) { + found = 1; + break; + } + } + if (found) + continue; + // Page fault occurs + p->faults++; + + // Look for an empty frame (-1) + int empty = -1; + for (j = 0; j < p->frames; j++) { + if (frameArr[j] == -1) { + empty = j; + break; + } + } + if (empty != -1) { + frameArr[empty] = page; + continue; + } + + // No empty frame; choose a victim using Optimal algorithm: + int replace = 0, farthest = -1; + for (j = 0; j < p->frames; j++) { + int k, nextUse = INT_MAX; + for (k = i + 1; k < p->n; k++) { + if (frameArr[j] == p->ref[k]) { + nextUse = k; + break; + } + } + if (nextUse > farthest) { + farthest = nextUse; + replace = j; + } + } + frameArr[replace] = page; + } + + printf("Process %d: Faults = %d\n", p->id, p->faults); + return NULL; +} + +int main() { + int i, j; + pthread_t threads[MAX_PROC]; + + // Input the number of processes + printf("Enter number of processes (max %d): ", MAX_PROC); + scanf("%d", &proc_count); + if (proc_count > MAX_PROC) proc_count = MAX_PROC; + + // Input process details + for (i = 0; i < proc_count; i++) { + procs[i].id = i + 1; + printf("\nProcess %d:\n", procs[i].id); + printf("Enter number of frames (max %d): ", MAX_FRAMES); + scanf("%d", &procs[i].frames); + if (procs[i].frames > MAX_FRAMES) procs[i].frames = MAX_FRAMES; + printf("Enter number of page references (max %d): ", MAX_REF); + scanf("%d", &procs[i].n); + if (procs[i].n > MAX_REF) procs[i].n = MAX_REF; + printf("Enter %d page references (space separated): ", procs[i].n); + for (j = 0; j < procs[i].n; j++) { + scanf("%d", &procs[i].ref[j]); + } + } + + // Create a thread for each process simulation + for (i = 0; i < proc_count; i++) { + pthread_create(&threads[i], NULL, simulateOptimal, &procs[i]); + } + + int totalFaults = 0; + // Wait for all threads to complete + for (i = 0; i < proc_count; i++) { + pthread_join(threads[i], NULL); + totalFaults += procs[i].faults; + } + + // Calculate and display average faults + printf("\nAverage Page Faults: %.2f\n", (float)totalFaults / proc_count); + return 0; +}