










































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











































#!/bin/bash echo "Enter two numbers:" read a b sum=$((a + b)) echo "Sum = $sum" if [ $a -gt $b ] then echo "$a is greater" else echo "$b is greater" fi
// Child process if (pid == 0) { printf("Child process (PID: %d) is running.\n", getpid()); // Execute a new program (e.g., 'ls -l') in the child process execl("/bin/ls", "ls", "-l", NULL); // If execl fails, the code below this line will be executed perror("execl failed"); exit(EXIT_FAILURE); // Terminate child with error if exec fails } // Parent process else { printf("Parent process (PID: %d) created child with PID: %d.\n", getpid(), pid); // Wait for the child process to terminate if (wait(&status) == -1) { perror("wait failed"); exit(EXIT_FAILURE); } // Check how the child process terminated if (WIFEXITED(status)) { printf("Child process exited with status: %d.\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("Child process terminated by signal: %d.\n", WTERMSIG(status)); } } // Both parent and child (if exec failed) will eventually reach here and exit printf("Process (PID: %d) exiting.\n", getpid());
exit(EXIT_SUCCESS); // Terminate successfully } fork() system call #include #include #include int main() { int x = 5; printf(" initial x = %d\n", x); pid_t pid = fork(); if (pid < 0) { printf("Fork failed\n"); return 1; } else if (pid == 0) { // This code runs in the child process printf("Child Process %d\n", getpid()); x = x + 1; printf("Child process: x = %d\n", x); } else { // This code runs in the parent process printf("Parent Process %d\n", getpid()); x = x - 1; printf("Parent process: x = %d\n", x); } return 0; }
#include #include #include int main(void) { pid_t pid = fork(); if (pid < 0) { perror("fork failed"); exit(EXIT_FAILURE); } if (pid == 0) { // Child: exit immediately printf("Child: exiting, PID = %d\n", getpid()); exit(0); } else { // Parent: sleep without wait(), child becomes zombie printf("Parent: PID = %d, child PID = %d\n", getpid(), pid); printf("Parent: sleeping, check 'ps -l' to see zombie.\n"); sleep(40); printf("Parent: exiting now.\n"); } return 0; }
#include #include void* func(void* arg) { printf("Thread running\n");
return NULL; } int main() { pthread_t t1; pthread_create(&t1, NULL, func, NULL); pthread_join(t1, NULL); return 0; }
#include #include #include // for getpid() void* kidfunc(void* arg) { printf("Child Thread:\n"); printf(" Process ID (PID) = %d\n", getpid()); printf(" Thread ID (TID) = %lu\n", pthread_self()); return NULL; } int main() { pthread_t kid; printf("Parent Thread (Main):\n"); printf(" Process ID (PID) = %d\n", getpid()); printf(" Thread ID (TID) = %lu\n", pthread_self()); pthread_create(&kid, NULL, kidfunc, NULL); pthread_join(kid, NULL); printf("Child thread has finished. Program ending.\n"); return 0;
#include int main() { int n, i; printf("Enter number of processes: "); scanf("%d", &n); int bt[n], wt[n], tat[n]; float avg_wt = 0, avg_tat = 0; printf("Enter burst times:\n"); for (i = 0; i < n; i++) { printf("P%d: ", i+1); scanf("%d", &bt[i]); } wt[0] = 0; // First process has 0 waiting time for (i = 1; i < n; i++) { wt[i] = wt[i-1] + bt[i-1]; } for (i = 0; i < n; i++) { tat[i] = wt[i] + bt[i]; avg_wt += wt[i]; avg_tat += tat[i]; } printf("\nProcess\tBT\tWT\tTAT\n"); for (i = 0; i < n; i++) { printf("P%d\t%d\t%d\t%d\n", i+1, bt[i], wt[i], tat[i]); } printf("\nAverage Waiting Time = %.2f", avg_wt/n); printf("\nAverage Turnaround Time = %.2f\n", avg_tat/n); return 0;
#include int main() { int n, i, j; printf("Enter number of processes: "); scanf("%d", &n); int bt[n], wt[n], tat[n], p[n]; float avg_wt = 0, avg_tat = 0; printf("Enter burst times:\n"); for (i = 0; i < n; i++) { printf("P%d: ", i+1); scanf("%d", &bt[i]); p[i] = i+1; } // Sort by burst time for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { if (bt[i] > bt[j]) { int temp = bt[i]; bt[i] = bt[j]; bt[j] = temp; temp = p[i]; p[i] = p[j]; p[j] = temp; } } } wt[0] = 0; for (i = 1; i < n; i++) { wt[i] = wt[i-1] + bt[i-1];
int done; do { done = 1; for(int i=0;i 0) { done = 0; if(rem[i] > tq) { wt += tq; rem[i] -= tq; } else { wt += rem[i]; tat += wt; rem[i]=0; } } } } while(!done); printf("Total WT=%d Total TAT=%d\n", wt, tat); return 0; }
#include int main() { int n; printf("Enter number of processes: "); scanf("%d", &n); int bt[n], at[n], rt[n], wt[n], tat[n];
float avg_wt = 0, avg_tat = 0; for (int i = 0; i < n; i++) { printf("Enter arrival time and burst time for P%d: ", i+1); scanf("%d %d", &at[i], &bt[i]); rt[i] = bt[i]; // remaining time } int complete = 0, t = 0, minm = 1e9, shortest = -1, finish_time; int check = 0; while (complete != n) { // Find process with minimum remaining time at time t for (int j = 0; j < n; j++) { if ((at[j] <= t) && (rt[j] < minm) && rt[j] > 0) { minm = rt[j]; shortest = j; check = 1; } } if (check == 0) { t++; continue; } rt[shortest]--; minm = rt[shortest]; if (minm == 0) minm = 1e9; if (rt[shortest] == 0) { complete++; check = 0; finish_time = t + 1; wt[shortest] = finish_time - bt[shortest] - at[shortest];
// Input values for (i = 0; i < n; i++) { printf("Enter Arrival Time, Burst Time, Priority for P%d: ", i + 1); scanf("%d %d %d", &arrival[i], &burst[i], &priority[i]); remaining[i] = burst[i]; } while (completed < n) { int highest = -1; int max_priority = INT_MIN; // Find highest priority process for (i = 0; i < n; i++) { if (arrival[i] <= time && remaining[i] > 0) { if (priority[i] > max_priority) { max_priority = priority[i]; highest = i; } } } // CPU Idle condition if (highest == -1) { time++; continue; } // Execute process for 1 unit remaining[highest]--; time++; // If process completed if (remaining[highest] == 0) {
completed++; completion[highest] = time; tat[highest] = completion[highest] - arrival[highest]; waiting[highest] = tat[highest] - burst[highest]; avg_wt += waiting[highest]; avg_tat += tat[highest]; } } // Output printf("\nProcess\tAT\tBT\tPR\tWT\tTAT\tCT\n"); for (i = 0; i < n; i++) { printf("P%d\t\t%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, arrival[i], burst[i], priority[i], waiting[i], tat[i], completion[i]); } printf("\nAverage Waiting Time = %.2f", avg_wt / n); printf("\nAverage Turnaround Time = %.2f\n", avg_tat / n); return 0; }
#include #include #define MAX 10 int n, m; int alloc[MAX][MAX], max[MAX][MAX], need[MAX][MAX], avail[MAX]; void calcNeed() { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) need[i][j] = max[i][j] - alloc[i][j];
for (int i = 0; i < m; i++) { avail[i] -= req[i]; alloc[p][i] += req[i]; need[p][i] -= req[i]; } if (isSafe()) return true; for (int i = 0; i < m; i++) { // rollback avail[i] += req[i]; alloc[p][i] -= req[i]; need[p][i] += req[i]; } return false; } int main() { int p, req[MAX]; printf("Enter processes and resources: "); scanf("%d %d", &n, &m); printf("Enter Allocation matrix:\n"); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &alloc[i][j]); printf("Enter Max matrix:\n"); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &max[i][j]); printf("Enter Available resources:\n"); for (int i = 0; i < m; i++) scanf("%d", &avail[i]); calcNeed();
if (isSafe()) printf("System is in SAFE state\n"); else { printf("System is NOT safe\n"); return 0; } printf("Enter process number: "); scanf("%d", &p); printf("Enter request vector:\n"); for (int i = 0; i < m; i++) scanf("%d", &req[i]); if (request(p, req)) printf("Request GRANTED\n"); else printf("Request DENIED\n"); return 0; }
#include #include #include #include #include #include #define SHM_KEY 0x #define SHM_SIZE 1024 /* Run both writer and reader in the same program using fork */