0% found this document useful (0 votes)
25 views9 pages

Os Lab

Uploaded by

delulusolulu1111
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)
25 views9 pages

Os Lab

Uploaded by

delulusolulu1111
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/ 9

FCFS

#include <stdio.h>
void fcfs(int processes[], int n, int burst_time[]) {
int waiting_time[n], turnaround_time[n];
float total_wt = 0, total_tat = 0;

// Calculate waiting time for each process


waiting_time[0] = 0; // First process has no waiting time
for (int i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
total_wt += waiting_time[i];
}

// Calculate turnaround time for each process


for (int i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
total_tat += turnaround_time[i];
}

// Print process details


printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\n", processes[i], burst_time[i], waiting_time[i], turnaround_time[i]);
}

printf("\nAverage Waiting Time: %.2f\n", total_wt / n);


printf("Average Turnaround Time: %.2f\n", total_tat / n);
}

int main() {
int processes[] = {1, 2, 3};
int burst_time[3];

printf("Enter Burst Times for Processes 1, 2, 3:\n");


for (int i = 0; i < 3; i++) {
printf("Burst Time for Process %d: ", processes[i]);
scanf("%d", &burst_time[i]);
}

fcfs(processes, 3, burst_time);
return 0;
}

OUTPUT:--------
Enter Burst Times for Processes 1, 2, 3:
Burst Time for Process 1: 53
Burst Time for Process 2: 3
Burst Time for Process 3: 8
Process Burst Time Waiting Time Turnaround Time
P1 5 0 5
P2 3 5 8
P3 8 8 16

Average Waiting Time: 4.33


Average Turnaround Time: 9.67
SJS

#include <stdio.h>

void sjf(int processes[], int n, int burst_time[]) {


int wt[n], tat[n], total_wt = 0, total_tat = 0, temp;

// Sort processes based on burst time


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

// Calculate waiting and turnaround time


wt[0] = 0;
for (int i = 1; i < n; i++) total_wt += (wt[i] = wt[i - 1] + burst_time[i - 1]);
for (int i = 0; i < n; i++) total_tat += (tat[i] = burst_time[i] + wt[i]);

// Print process details and averages


printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++)
printf("P%d\t%d\t\t%d\t\t%d\n", processes[i], burst_time[i], wt[i], tat[i]);
printf("\nAverage Waiting Time: %.2f\nAverage Turnaround Time: %.2f\n",
(float)total_wt / n, (float)total_tat / n);
}

int main() {
int processes[] = {1, 2, 3}, burst_time[3];
printf("Enter Burst Times for Processes 1, 2, 3:\n");
for (int i = 0; i < 3; i++) scanf("%d", &burst_time[i]);

sjf(processes, 3, burst_time);
return 0;
}

OUTPUT
Enter Burst Times for Processes 1, 2, 3:
Burst Time for Process 1: 6
Burst Time for Process 2: 2
Burst Time for Process 3: 8
Process Burst Time Waiting Time Turnaround Time
P2 2 0 2
P1 6 2 8
P3 8 8 16

Average Waiting Time: 3.33


Average Turnaround Time: 8.67
ROUND ROBIN

#include <stdio.h>

void round_robin(int processes[], int n, int burst_time[], int quantum) {


int rem_time[n], wt[n], tat[n], time = 0;

for (int i = 0; i < n; i++) rem_time[i] = burst_time[i];

while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (rem_time[i] > 0) {
done = 0;
int exec_time = (rem_time[i] > quantum) ? quantum : rem_time[i];
time += exec_time;
rem_time[i] -= exec_time;
if (rem_time[i] == 0) wt[i] = time - burst_time[i];
}
}
if (done) break;
}

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


tat[i] = burst_time[i] + wt[i];
printf("P%d\t%d\t\t%d\t\t%d\n", processes[i], burst_time[i], wt[i], tat[i]);
}

float total_wt = 0, total_tat = 0;


for (int i = 0; i < n; i++) total_wt += wt[i], total_tat += tat[i];

printf("\nAvg WT: %.2f\nAvg TAT: %.2f\n", total_wt / n, total_tat / n);


}

int main() {
int processes[] = {1, 2, 3}, burst_time[3], quantum;
for (int i = 0; i < 3; i++) {
printf("Burst Time for P%d: ", processes[i]);
scanf("%d", &burst_time[i]);
}
printf("Time Quantum: ");
scanf("%d", &quantum);

round_robin(processes, 3, burst_time, quantum);


return 0;
}
OUTPUT
Burst Time for P1: 10
Burst Time for P2: 5
Burst Time for P3:
8
Time Quantum: 3
P1 10 13 23
P2 5 9 14
P3 8 14 22

Avg WT: 12.00


Avg TAT: 19.67
DEADLOCK DETECTION

#include <stdio.h>
int main() {
int p, r, i, j, count = 0;
printf("Enter number of processes and resources: ");
scanf("%d %d", &p, &r);

int alloc[p][r], max[p][r], avail[r], need[p][r], finish[p], work[r];

printf("Enter Allocation Matrix:\n");


for (i = 0; i < p; i++)
for (j = 0; j < r; j++) scanf("%d", &alloc[i][j]);

printf("Enter Max Matrix:\n");


for (i = 0; i < p; i++)
for (j = 0; j < r; j++) scanf("%d", &max[i][j]);

printf("Enter Available Resources: ");


for (i = 0; i < r; i++) scanf("%d", &avail[i]);

for (i = 0; i < p; i++) finish[i] = 0; // Initialize finish array


for (i = 0; i < p; i++)
for (j = 0; j < r; j++) need[i][j] = max[i][j] - alloc[i][j]; // Calculate Need matrix
for (i = 0; i < r; i++) work[i] = avail[i]; // Initialize work array

while (count < p) {


int found = 0;
for (i = 0; i < p; i++) {
if (!finish[i]) {
for (j = 0; j < r; j++) if (need[i][j] > work[j]) break;
if (j == r) {
for (j = 0; j < r; j++) work[j] += alloc[i][j];
finish[i] = 1; found = 1; count++;
}
}
}
if (!found) {
printf("System is in deadlock.\n");
return 0;
}
}
printf("System is safe.\n");
return 0;
}

OUTPUT

Enter number of processes and resources: 3 3


Enter Allocation Matrix:
010
200
302
Enter Max Matrix:
753
322
902
Enter Available Resources: 3 3 2
System is safe.
FIFO PAGE REPLACEMEMT

#include <stdio.h>

int main() {
int pages[30], frames[10] = {-1}, n, f, faults = 0, index = 0;

printf("Enter number of pages and frames: ");


scanf("%d %d", &n, &f);

printf("Enter page reference string: ");


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

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


int found = 0;
for (int j = 0; j < f; j++)
if (frames[j] == pages[i]) found = 1;

if (!found) {
frames[index] = pages[i];
index = (index + 1) % f;
faults++;
}

printf("Frames: ");
for (int j = 0; j < f; j++) if (frames[j] != -1) printf("%d ", frames[j]);
printf("\n");
}

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


return 0;
}
OUTPUT
Enter number of pages and frames: 7 3
Enter page reference string: 1 3 0 3 5 6 3
Frames: 1
Frames: 1 3
Frames: 1 3 0
Frames: 1 3 0
Frames: 5 3 0
Frames: 5 6 0
Frames: 5 6 3

Total Page Faults: 5


SINGLE LEVEL DIRECTORY

#include<stdio.h>
#include<string.h>

int main() {
int nf = 0, i = 0, ch;
char mdname[10], fname[10][10], name[10];

printf("Enter directory name: ");


scanf("%s", mdname);

do {
printf("Enter file name: ");
scanf("%s", name);

for (i = 0; i < nf; i++)


if (strcmp(name, fname[i]) == 0) break;

if (i == nf) strcpy(fname[nf++], name);


else printf("File %s already exists.\n", name);

printf("Add another file? (1-Yes / 0-No): ");


scanf("%d", &ch);
} while (ch == 1);

printf("\nDirectory: %s\nFiles:\n", mdname);


for (i = 0; i < nf; i++) printf("%s\n", fname[i]);

return 0;
}

OUTPUT

Enter directory name: MyDir


Enter file name: file1
Add another file? (1-Yes / 0-No): 1
Enter file name: file2
Add another file? (1-Yes / 0-No): 0

Directory: MyDir
Files:
file1
file2
IMPLEMENT SHARED MEMORY AND IPC

#include <stdio.h>
#include <sys/msg.h>
#include <string.h>

struct msg_buffer {
long msg_type;
char msg_text[100];
} message;

int main() {
int msgid = msgget(7, IPC_CREAT | 0666); // Create message queue
if (msgid < 0) return perror("msgget failed"), 1;

printf("Enter message: ");


scanf("%s", message.msg_text); // Input message
message.msg_type = 1; // Set message type

if (msgsnd(msgid, &message, sizeof(message.msg_text), 0) == -1)


perror("msgsnd failed");
else
printf("Message sent: %s\n", message.msg_text);

return 0;
}

OUTPUT

Enter message: HELLO


Message sent: HELLO
SEQUENTIAL FILE ALLOCATION

#include <stdio.h>

int main() {
int n, b[20], sb[20], c[20][20];

printf("Enter no. of files: ");


scanf("%d", &n);

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


printf("Blocks & start for file %d: ", i + 1);
scanf("%d %d", &b[i], &sb[i]);
for (int j = 0; j < b[i]; j++) c[i][j] = sb[i]++;
}

printf("Filename\tStart Block\tLength\n");
for (int i = 0; i < n; i++) printf("%d\t\t%d\t\t%d\n", i + 1, sb[i] - b[i], b[i]);

int x;
printf("Enter file number: ");
scanf("%d", &x);
printf("File: %d\nLength: %d\nBlocks: ", x, b[x - 1]);
for (int i = 0; i < b[x - 1]; i++) printf("%4d", c[x - 1][i]);

return 0;
}

OUTPUT
Enter no. of files: 2
Blocks & start for file 1: 3 5
Blocks & start for file 2: 2 10
Filename Start Block Length
1 5 3
2 10 2
Enter file number: 1
File: 1
Length: 3
Blocks: 5 6 7
indexed file allocation

#include <stdio.h>

int main() {
int n, m[20], sb[20], x;
printf("Enter no. of files: ");
scanf("%d", &n);

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


printf("\nFile %d - Start Block, Size: ", i + 1);
scanf("%d%d", &sb[i], &m[i]);
}

printf("\nFile\tIndexed\n");
for (int i = 0; i < n; i++)
printf("%d\t%d\n", i + 1, sb[i]);

printf("\nEnter File number: ");


scanf("%d", &x);
printf("File %d, Indexed: %d\n", x, sb[x - 1]);

return 0;
}

Output

Enter no. of files: 3


File 1 - Start Block, Size: 0 5
File 2 - Start Block, Size: 5 10
File 3 - Start Block, Size: 15 7

File Indexed
1 0
2 5
3 15

Enter File number: 2


File 2, Indexed: 5

Shell programming
Output
calculate_factorial() {
echo "Enter a number to find its factorial:" $ ./factorial.sh
read num Enter a number to find its factorial:
fact=1 5
while [ $num -gt 1 ]; do Factorial of the given number is 120
fact=$((fact * num))
num=$((num - 1))
done

echo "Factorial of the given number is $fact"


}

# Call the function


calculate_factorial

You might also like