0% found this document useful (0 votes)
11 views

Saved Programs

Uploaded by

pradeepshettar50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Saved Programs

Uploaded by

pradeepshettar50
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Fork System Call

#include <stdio.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

pid_t pid = fork();

if (pid == 0) {

// Child process

printf("Child process ID: %d, Parent process ID: %d\n", getpid(), getppid());

} else if (pid > 0) {

// Parent process

printf("Parent process ID: %d\n", getpid());

wait(NULL);

printf("Parent process: Child process terminated\n");

} else {

// Error

perror("fork");

return 0;

}
FCFS Scheduling
#include <stdio.h>

int main() {

int n, i, bt[10], wt[10], tat[10], avwt = 0, avtat = 0;

printf("Enter n: ");

scanf("%d", &n);

printf("Enter burst time:\n");

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

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

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] = bt[i] + wt[i];

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

avwt += wt[i];

avtat += tat[i];

avwt /= n;

avtat /= n;
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

printf(" %d\t\t %d\t\t %d\t\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);

printf("\nAverage Waiting Time: %d\n", avwt);

printf("Average Turnaround Time: %d\n", avtat);

return 0;

}
Bubble Sort with Average TAT and WT
#include <stdio.h>

int main() {

int n, i, j, temp, bt[10], wt[10], tat[10], avwt = 0, avtat = 0;

scanf("%d", &n);

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

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

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

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

if (bt[j] > bt[j + 1]) {

temp = bt[j];

bt[j] = bt[j + 1];

bt[j + 1] = 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] = bt[i] + wt[i];


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

avwt += wt[i];

avtat += tat[i];

avwt /= n;

avtat /= n;

printf("\nAvg WT: %d, Avg TAT: %d\n", avwt, avtat);

return 0;

}
Priority Scheduling
#include <stdio.h>

int main() {

int n, i, j, temp, bt[10], wt[10], tat[10], pr[10], avwt = 0, avtat = 0;

scanf("%d", &n);

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

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

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

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

if (pr[j] > pr[j + 1]) {

temp = pr[j];

pr[j] = pr[j + 1];

pr[j + 1] = temp;

temp = bt[j];

bt[j] = bt[j + 1];

bt[j + 1] = 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] = bt[i] + wt[i];

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

avwt += wt[i];

avtat += tat[i];

avwt /= n;

avtat /= n;

printf("\nAvg WT: %d, Avg TAT: %d\n", avwt, avtat);

return 0;

}
Best Fit Memory Allocation
#include <stdio.h>

struct Process {

int pid;

int size;

};

struct Partition {

int pid;

int size;

int isFree;

};

void bestFit(struct Process p[], struct Partition f[], int n, int m) {

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

int best = -1;

for (int j = 0; j < m; ++j) {

if (f[j].isFree && f[j].size >= p[i].size && (best == -1 || f[j].size <

f[best].size))

best = j;

if (best != -1) {

f[best].isFree = 0;

f[best].pid = p[i].pid;

printf("P%d -> F%d\n", p[i].pid, best+1);

} else {
printf("P%d not allocated\n", p[i].pid);

int main() {

struct Process p[] = {{1, 10}, {2, 20}, {3, 15}, {4, 5}, {5, 18}};

struct Partition f[] = {{1, 25, 1}, {2, 15, 1}, {3, 10, 1}, {4, 20, 1}, {5, 18, 1}};

int n = sizeof(p) / sizeof(p[0]);

int m = sizeof(f) / sizeof(f[0]);

bestFit(p, f, n, m);

return 0;

}
First Fit Memory Allocation
#include <stdio.h>

int main() {

int p[5] = {212, 417, 112, 426, 211};

int b[4] = {100, 500, 200, 300};

int a[5] = {-1};

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

for (int j = 0; j < 4; j++) {

if (b[j] >= p[i]) {

a[i] = j;

b[j] -= p[i];

break;

printf("Process No.\tProcess Size\tPartition No.\n");

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

printf("%d\t\t\t%d\t\t\t", i + 1, p[i]);

if (a[i] != -1) printf("%d\n", a[i] + 1);

else printf("Not Allocated\n");

return 0;

}
FIFO Page Replacement
#include <stdio.h>

int main() {

int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};

int frames = 3;

int page_faults = 0;

int m[frames];

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

m[i] = -1;

int ptr = 0;

for (int i = 0; i < sizeof(pages) / sizeof(pages[0]); i++) {

int page_found = 0;

for (int j = 0; j < frames; j++) {

if (m[j] == pages[i]) {

page_found = 1;

break;

if (page_found == 0) {

m[ptr] = pages[i];

ptr = (ptr + 1) % frames;

page_faults++;
}

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

return 0;

You might also like