0% found this document useful (0 votes)
4 views20 pages

Os Program

The document contains multiple C programs that implement different CPU scheduling algorithms: Round Robin, Shortest Job First (SJF), First-Come First-Served (FCFS), and Priority Scheduling. Each program prompts the user for the number of processes and their burst times, calculates waiting and turnaround times, and displays the results along with average times. Additionally, it includes file allocation methods such as Sequential, Indexed, and Linked allocation with corresponding code implementations.
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)
4 views20 pages

Os Program

The document contains multiple C programs that implement different CPU scheduling algorithms: Round Robin, Shortest Job First (SJF), First-Come First-Served (FCFS), and Priority Scheduling. Each program prompts the user for the number of processes and their burst times, calculates waiting and turnaround times, and displays the results along with average times. Additionally, it includes file allocation methods such as Sequential, Indexed, and Linked allocation with corresponding code implementations.
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/ 20

Round robin

#include <stdio.h>

struct Process {

int pid;

int burst_time;

int remaining_time;

int waiting_time;

int turnaround_time;

};

int main() {

int n, quantum, time = 0;

printf("Enter number of processes: ");

scanf("%d", &n);

struct Process p[n];

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

p[i].pid = i + 1;

printf("Enter burst time for process %d: ", p[i].pid);

scanf("%d", &p[i].burst_time);

p[i].remaining_time = p[i].burst_time;

p[i].waiting_time = 0;
p[i].turnaround_time = 0;

printf("Enter time quantum: ");

scanf("%d", &quantum);

int done;

do {

done = 1;

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

if (p[i].remaining_time > 0) {

done = 0;

if (p[i].remaining_time > quantum) {

time += quantum;

p[i].remaining_time -= quantum;

} else {

time += p[i].remaining_time;

p[i].waiting_time = time - p[i].burst_time;

p[i].remaining_time = 0;

} while (!done);

float total_wt = 0, total_tat = 0;


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

p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;

total_wt += p[i].waiting_time;

total_tat += p[i].turnaround_time;

printf("\nProcess\tBurst\tWaiting\tTurnaround\n");

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

printf("P%d\t%d\t%d\t%d\n", p[i].pid, p[i].burst_time, p[i].waiting_time,


p[i].turnaround_time);

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

printf("\nAverage Turnaround Time = %.2f\n", total_tat / n);

return 0;

[user@localhost ~]$ vi round.c

[user@localhost ~]$ cc -std=c99 round.c -o round

[user@localhost ~]$ ./round


Enter number of processes: 3

Enter burst time for process 1: 10

Enter burst time for process 2: 5

Enter burst time for process 3: 8

Enter time quantum: 4

Process Burst Waiting Turnaround

P1 10 13 23

P2 5 9 14

P3 8 12 20

Average Waiting Time = 11.33

Average Turnaround Time = 19.00

SJF PROGRAM

#include <stdio.h>

int main() {

int n, i, j;

int bt[20], p[20], wt[20], tat[20], temp;

float avg_wt = 0, avg_tat = 0;

printf("Enter number of processes: ");


scanf("%d", &n);

// Input burst times

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

printf("Enter burst time for process %d: ", i + 1);

scanf("%d", &bt[i]);

p[i] = i + 1;

// Sort by burst time (SJF)

for (i = 0; i < n - 1; i++) {

for (j = i + 1; j < n; j++) {

if (bt[i] > bt[j]) {

// Swap burst times

temp = bt[i];

bt[i] = bt[j];

bt[j] = temp;

// Swap process numbers

temp = p[i];

p[i] = p[j];

p[j] = temp;

}
// Calculate waiting time

wt[0] = 0;

for (i = 1; i < n; i++) {

wt[i] = wt[i - 1] + bt[i - 1];

// Calculate turnaround time and averages

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

tat[i] = wt[i] + bt[i];

avg_wt += wt[i];

avg_tat += tat[i];

// Print results

printf("\nProcess\tBurst\tWaiting\tTurnaround\n");

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

printf("P%d\t%d\t%d\t%d\n", p[i], 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;

}
[user@localhost ~]$ vi sjf.c

[user@localhost ~]$ cc -std=c99 sjf.c -o sjf

[user@localhost ~]$ ./sjf

Enter number of processes: 3

Enter burst time for process 1: 5

Enter burst time for process 2: 3

Enter burst time for process 3: 8

Process Burst Waiting Turnaround

P2 3 0 3

P1 5 3 8

P3 8 8 16

Average Waiting Time = 3.67

Average Turnaround Time = 9.00

FCFS PROGRAM

#include <stdio.h>

int main() {

int n, i;
int bt[20], wt[20], tat[20];

float avg_wt = 0, avg_tat = 0;

printf("Enter number of processes: ");

scanf("%d", &n);

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

printf("Enter burst time for process %d: ", i + 1);

scanf("%d", &bt[i]);

wt[0] = 0; // First process has no 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\tBurst\tWaiting\tTurnaround\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;

[user@localhost ~]$ vi fcfs.c

[user@localhost ~]$ cc -std=c99 fcfs.c -o fcfs

[user@localhost ~]$ ./fcfs

Enter number of processes: 3

Enter burst time for process 1: 5

Enter burst time for process 2: 3

Enter burst time for process 3: 8

Process Burst Waiting Turnaround

P1 5 0 5

P2 3 5 8

P3 8 8 16
Average Waiting Time = 4.33

Average Turnaround Time = 9.67

#include <stdio.h>

int main() {

int n, i, j;

int bt[20], p[20], priority[20], wt[20], tat[20], temp;

float avg_wt = 0, avg_tat = 0;

printf("Enter number of processes: ");

scanf("%d", &n);

// Input burst time and priority

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

p[i] = i + 1;

printf("Enter burst time for process %d: ", p[i]);

scanf("%d", &bt[i]);

printf("Enter priority for process %d (lower = higher priority): ", p[i]);

scanf("%d", &priority[i]);

// Sort by priority (ascending)

for (i = 0; i < n - 1; i++) {

for (j = i + 1; j < n; j++) {


if (priority[i] > priority[j]) {

// Swap priority

temp = priority[i];

priority[i] = priority[j];

priority[j] = temp;

// Swap burst time

temp = bt[i];

bt[i] = bt[j];

bt[j] = temp;

// Swap process ID

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

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


tat[i] = wt[i] + bt[i];

avg_wt += wt[i];

avg_tat += tat[i];

printf("\nProcess\tBurst\tPriority\tWaiting\tTurnaround\n");

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

printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i], bt[i], priority[i], wt[i], tat[i]);

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

printf("\nAverage Turnaround Time = %.2f\n", avg_tat / n);

return 0;

[user@localhost ~]$ vi priority.c

[user@localhost ~]$ cc -std=c99 priority.c -o priority

[user@localhost ~]$ ./priority

Enter number of processes: 3

Enter burst time for process 1: 5

Enter priority for process 1: 2

Enter burst time for process 2: 3

Enter priority for process 2: 1

Enter burst time for process 3: 8


Enter priority for process 3: 3

Process Burst Priority Waiting Turnaround

P2 3 1 0 3

P1 5 2 3 8

P3 8 3 8 16

Average Waiting Time = 3.67

Average Turnaround Time = 9.00

SEQUENTIAL ALLOCATION:

#include <stdio.h>

#include <stdlib.h> // For exit()

int main() {

int f[50], i, st, j, len, c;

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

f[i] = 0;

X:

printf("\nEnter the starting block & length of file: ");

scanf("%d%d", &st, &len);

for (j = st; j < (st + len); j++) {


if (f[j] == 0) {

f[j] = 1;

printf("\n%d -> Allocated", j);

} else {

printf("\nBlock %d already allocated!", j);

break;

if (j == (st + len))

printf("\nThe file is successfully allocated to disk.");

printf("\nDo you want to enter more files? (1 = Yes / 0 = No): ");

scanf("%d", &c);

if (c == 1)

goto X;

else

exit(0);

OUTPUT:

[user@localhost ~]$ vi seq.c

[user@localhost ~]$ cc -std=c99 seq.c -o seq

[user@localhost ~]$ ./seq


Enter the starting block & length of file: 4 10

4-> Allocated

5-> Allocated

6-> Allocated

7-> Allocated

8 -> Allocated

9-> Allocated

10-> Allocated

11 -> Allocated

12 -> Allocated

13 -> Allocated

The file is successfully allocated to disk.


Do you want to enter more files? (1 = Yes / 0 =No): 1

Enter the starting block & length of file: 73

Block 7 already allocated!

Do you want to enter more files? (1 = Yes / 0 =No):

// File: indexed_allocation.c

#include <stdio.h>

#include <stdlib.h>

int main() {

int f[50], i, p, indexBlock, n, b[50], count = 0;

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

f[i] = 0;

printf("Enter index block: ");

scanf("%d", &indexBlock);

if (f[indexBlock] == 0) {

f[indexBlock] = 1;

printf("Enter number of blocks to be allocated: ");

scanf("%d", &n);
printf("Enter blocks to be allocated:\n");

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

scanf("%d", &b[i]);

if (f[b[i]] == 0)

count++;

if (count == n) {

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

f[b[i]] = 1;

printf("File Allocation Table:\n");

printf("Index Block: %d\n", indexBlock);

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

printf("Block %d -> Allocated\n", b[i]);

} else {

printf("Some blocks are already allocated! Try again.\n");

} else {

printf("Index block is already allocated.\n");

return 0;

}
Output:

[user@localhost ~]$ vi indexed.c

[user@localhost ~]$ cc -std=c99 indexed.c -o indexed

[user@localhost ~]$ ./indexed

Enter index block: 5

Enter number of blocks to be allocated: 3

Enter blocks to be allocated:

10

11

12

File Allocation Table:

Index Block: 5

Block 10 -> Allocated

Block 11 -> Allocated

Block 12 -> Allocated

// File: linked_allocation.c

#include <stdio.h>

int main() {

int files[50] = {0}, start, length, i, j, flag = 0;

printf("Enter starting block and length of file: ");


scanf("%d%d", &start, &length);

for (i = start; i < (start + length); i++) {

if (files[i] == 0) {

files[i] = 1;

} else {

flag = 1;

break;

if (flag == 0) {

printf("The file is allocated using linked allocation:\n");

for (i = start; i < (start + length - 1); i++) {

printf("%d -> ", i);

printf("%d\n", start + length - 1);

} else {

printf("Block %d is already allocated. File allocation failed.\n", i);

return 0;

Output:

[user@localhost ~]$ vi link.c


[user@localhost ~]$ cc -std=c99 link.c -o link

[user@localhost ~]$ ./link

Enter starting block and length of file: 10 4

The file is allocated using linked allocation:

10 -> 11 -> 12 -> 13

You might also like