OS Exam practical

 OS Exam practical 


Q.1 : Write a program using c to copy files using system call.


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#define BUF_SIZE 4096

int main(int argc, char *argv[]) {
    int source_fd, dest_fd;
    ssize_t num_read;
    char buf[BUF_SIZE];

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    source_fd = open(argv[1], O_RDONLY);
    if (source_fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (dest_fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    while ((num_read = read(source_fd, buf, BUF_SIZE)) > 0) {
        if (write(dest_fd, buf, num_read) != num_read) {
            perror("write");
            exit(EXIT_FAILURE);
        }
    }

    if (num_read == -1) {
        perror("read");
        exit(EXIT_FAILURE);
    }

    if (close(source_fd) == -1) {
        perror("close");
        exit(EXIT_FAILURE);
    }

    if (close(dest_fd) == -1) {
        perror("close");
        exit(EXIT_FAILURE);
    }

    printf("File copied successfully!\n");
    exit(EXIT_SUCCESS);
}

Q.2: Write a program using c to implement fcfs scheduling algorithm.


#include<stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[]) {
    wt[0] = 0;
    for (int i = 1; i < n; i++) {
        wt[i] = wt[i - 1] + bt[i - 1];
    }
}

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
    for (int i = 0; i < n; i++) {
        tat[i] = bt[i] + wt[i];
    }
}

void findAverageTime(int processes[], int n, int bt[]) {
    int wt[n], tat[n];
    int total_wt = 0, total_tat = 0;

    findWaitingTime(processes, n, bt, wt);
    findTurnAroundTime(processes, n, bt, wt, tat);

    printf("Process Burst Time Waiting Time Turn-Around Time\n");
    for (int i = 0; i < n; i++) {
        total_wt += wt[i];
        total_tat += tat[i];
        printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
    }

    printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);
    printf("Average turn-around time = %.2f\n", (float)total_tat / (float)n);
}

int main() {
    int processes[] = {1, 2, 3};
    int n = sizeof processes / sizeof processes[0];
    int burst_time[] = {10, 5, 8};

    findAverageTime(processes, n, burst_time);
    return 0;
}

Q.3: Write a program using c to implement non preemptive priority based scheduling algorithm.


#include<stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[], int priority[]) {
    int service_time[n];
    service_time[0] = 0;
    wt[0] = 0;

    for (int i = 1; i < n; i++) {
        service_time[i] = service_time[i - 1] + bt[i - 1];
        wt[i] = service_time[i] - service_time[i - 1];
    }
}

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
    for (int i = 0; i < n; i++) {
        tat[i] = bt[i] + wt[i];
    }
}

void findAverageTime(int processes[], int n, int bt[], int priority[]) {
    int wt[n], tat[n];
    int total_wt = 0, total_tat = 0;

    findWaitingTime(processes, n, bt, wt, priority);
    findTurnAroundTime(processes, n, bt, wt, tat);

    printf("Process Burst Time Priority Waiting Time Turn-Around Time\n");
    for (int i = 0; i < n; i++) {
        total_wt += wt[i];
        total_tat += tat[i];
        printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], priority[i], wt[i], tat[i]);
    }

    printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);
    printf("Average turn-around time = %.2f\n", (float)total_tat / (float)n);
}

void sortProcessesByPriority(int processes[], int bt[], int priority[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (priority[j] > priority[j + 1]) {
                int temp = processes[j];
                processes[j] = processes[j + 1];
                processes[j + 1] = temp;
                temp = bt[j];
                bt[j] = bt[j + 1];
                bt[j + 1] = temp;
                temp = priority[j];
                priority[j] = priority[j + 1];
                priority[j + 1] = temp;
            }
        }
    }
}

int main() {
    int processes[] = {1, 2, 3};
    int n = sizeof processes / sizeof processes[0];
    int burst_time[] = {10, 5, 8};
    int priority[] = {3, 1, 2};

    sortProcessesByPriority(processes, burst_time, priority, n);
    findAverageTime(processes, n, burst_time, priority);
    return 0;
}

Q.4: Write a program using c to implement round Robin scheduling algorithm.


#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum) {
    int remaining_bt[n];
    for (int i = 0; i < n; i++) {
        remaining_bt[i] = bt[i];
    }

    int t = 0; // Current time
    while (1) {
        int done = 1; // Flag to indicate if all processes are done
        for (int i = 0; i < n; i++) {
            if (remaining_bt[i] > 0) {
                done = 0; // At least one process is not done
                if (remaining_bt[i] > quantum) {
                    t += quantum;
                    remaining_bt[i] -= quantum;
                } else {
                    t += remaining_bt[i];
                    wt[i] = t - bt[i]; // Waiting time = current time - burst time
                    remaining_bt[i] = 0;
                }
            }
        }
        if (done == 1) // All processes are done
            break;
    }
}

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
    for (int i = 0; i < n; i++) {
        tat[i] = bt[i] + wt[i];
    }
}

void findAverageTime(int processes[], int n, int bt[], int quantum) {
    int wt[n], tat[n];
    int total_wt = 0, total_tat = 0;

    findWaitingTime(processes, n, bt, wt, quantum);
    findTurnAroundTime(processes, n, bt, wt, tat);

    printf("Process Burst Time Waiting Time Turn-Around Time\n");
    for (int i = 0; i < n; i++) {
        total_wt += wt[i];
        total_tat += tat[i];
        printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
    }

    printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);
    printf("Average turn-around time = %.2f\n", (float)total_tat / (float)n);
}

int main() {
    int processes[] = {1, 2, 3, 4};
    int n = sizeof processes / sizeof processes[0];
    int burst_time[] = {10, 5, 8, 12};
    int quantum = 2;

    findAverageTime(processes, n, burst_time, quantum);
    return 0;
}

Q.5: Write a program using c to implement sktf scheduling algorithm.


#include<stdio.h>
#include<limits.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[]) {
    int rt[n]; // Remaining time

    // Initialize remaining time as burst time
    for (int i = 0; i < n; i++) {
        rt[i] = bt[i];
    }

    int complete = 0, t = 0, min = INT_MAX;
    int shortest = 0, finish_time;
    int check = 0; // Check if the loop breaks

    // Process until all processes are done
    while (complete != n) {
        // Find the process with minimum remaining burst time
        for (int j = 0; j < n; j++) {
            if ((rt[j] <= t) && (rt[j] < min) && (rt[j] > 0)) {
                min = rt[j];
                shortest = j;
                check = 1;
            }
        }

        if (check == 0) {
            t++;
            continue;
        }

        // Reduce remaining burst time
        rt[shortest]--;
        min = rt[shortest];
        if (min == 0)
            min = INT_MAX;

        // If a process is finished
        if (rt[shortest] == 0) {
            complete++;
            check = 0;
            finish_time = t + 1;
            wt[shortest] = finish_time - bt[shortest];
            if (wt[shortest] < 0)
                wt[shortest] = 0;
        }

        // Move to the next time slot
        t++;
    }
}

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
    // Turnaround time = Burst time + Waiting time
    for (int i = 0; i < n; i++) {
        tat[i] = bt[i] + wt[i];
    }
}

void findAverageTime(int processes[], int n, int bt[]) {
    int wt[n], tat[n];
    int total_wt = 0, total_tat = 0;

    findWaitingTime(processes, n, bt, wt);
    findTurnAroundTime(processes, n, bt, wt, tat);

    printf("Process Burst Time Waiting Time Turn-Around Time\n");
    for (int i = 0; i < n; i++) {
        total_wt += wt[i];
        total_tat += tat[i];
        printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
    }

    printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);
    printf("Average turn-around time = %.2f\n", (float)total_tat / (float)n);
}

int main() {
    int processes[] = {1, 2, 3};
    int n = sizeof processes / sizeof processes[0];
    int burst_time[] = {6, 8, 7};

    findAverageTime(processes, n, burst_time);
    return 0;
}

Q.6: What is Unix operating system? Explain different types of commands?


Unix is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, developed in the 1970s. It's a powerful, stable, and versatile operating system primarily used in servers and workstations. Unix systems are known for their robustness, scalability, and support for multitasking and multiuser functionality.


























ଭାଇମାନେ କାଇଁ Copy କରୁଛ?

                                   ମୋଠୁ କିଛି ଶିଖ l

                                                                    ଧନ୍ୟବାଦ🙏


    

                    




Post a Comment

Previous Post Next Post