0% found this document useful (0 votes)
9 views12 pages

Os Program

The document contains several C programming examples related to process management algorithms including Fork-Exec, FCFS, SJN, Round Robin, Priority Scheduling, and the Dining Philosopher problem. Each section provides code snippets demonstrating the implementation of these algorithms along with user input for process burst times and priorities. The examples illustrate how to calculate waiting time and turnaround time for processes in different scheduling scenarios.

Uploaded by

Funny Bunny
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)
9 views12 pages

Os Program

The document contains several C programming examples related to process management algorithms including Fork-Exec, FCFS, SJN, Round Robin, Priority Scheduling, and the Dining Philosopher problem. Each section provides code snippets demonstrating the implementation of these algorithms along with user input for process burst times and priorities. The examples illustrate how to calculate waiting time and turnaround time for processes in different scheduling scenarios.

Uploaded by

Funny Bunny
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/ 12

#include <stdio.

h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <string.h>

Int main(int argc, char *argv[]) {

If (argc < 3) {

Fprintf(stderr, “Usage: %s <file_to_cat> <command>\n”, argv[0]);

Exit(1);

Int pid;

Char s[100];

Pid = fork();

If (pid < 0) {

Perror(“Fork failed”);

Exit(1);

Else if (pid > 0) { // Parent process

Wait(NULL); // Wait for child to finish

Printf(“\nParent Process:\n”);

Printf(“\n\tParent Process ID: %d\n”, getpid());

// Execute ‘cat’ command on given file


Execlp(“cat”, “cat”, argv[1], (char *)0);

// If execlp fails

Perror(“Can’t execute cat”);

Exit(1);

Else { // Child process

Printf(“\nChild Process:\n”);

Printf(“\n\tChild Process Parent ID: %d\n”, getppid());

Printf(“\n\tChild Process ID: %d\n”, getpid());

// Execute the given command

Execvp(argv[2], &argv[2]);

// If execvp fails

Perror(“Can’t execute command”);

Exit(1);

Return 0;

Fcfs algorithm

#include <stdio.h>

Int main() {

Int n, i;

Printf(“Enter the number of processes: “);

Scanf(“%d”, &n);
Int bt[n], wt[n], tat[n]; // Burst time, waiting time, turnaround time

Printf(“Enter burst times for each process:\n”);

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

Printf(“Process %d: “, i + 1);

Scanf(“%d”, &bt[i]);

// Waiting time calculation

Wt[0] = 0; // First process has no waiting time

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

Wt[i] = wt[i – 1] + bt[i – 1];

// Turnaround time calculation

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

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

// Display results

Printf(“\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n”);

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

Printf(“%d\t%d\t\t%d\t\t%d\n”, i + 1, bt[i], wt[i], tat[i]);

Return 0;

}
Sjn

#include <stdio.h>

Void sortProcesses(int n, int bt[], int p[]) {

For (int i = 0; i < n – 1; i++) {

For (int j = i + 1; j < n; j++) {

If (bt[i] > bt[j]) { // Sort by Burst Time

Int temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;

Temp = p[i]; p[i] = p[j]; p[j] = temp;

Int main() {

Int n;

Printf(“Enter the number of processes: “);

Scanf(“%d”, &n);

Int p[n], bt[n], wt[n], tat[n];

Printf(“Enter burst times for each process:\n”);

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

P[i] = i + 1; // Process Ids

Printf(“Process %d: “, p[i]);

Scanf(“%d”, &bt[i]);

}
sortProcesses(n, bt, p); // Sort processes by Burst Time

// Calculate Waiting Time

Wt[0] = 0;

For (int i = 1; i < n; i++) {

Wt[i] = wt[i – 1] + bt[i – 1];

// Calculate Turnaround Time

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

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

// Display Results

Printf(“\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n”);

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

Printf(“%d\t%d\t\t%d\t\t%d\n”, p[i], bt[i], wt[i], tat[i]);

Return 0;

Round robin

#include <stdio.h>

Void roundRobin(int n, int bt[], int tq) {

Int remaining_bt[n], wt[n], tat[n], time = 0, done = 0;


// Copy burst times to remaining burst times

For (int i = 0; i < n; i++)

Remaining_bt[i] = bt[i];

// Process execution in Round Robin manner

While (done < n) {

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

If (remaining_bt[i] > 0) {

If (remaining_bt[i] > tq) {

Time += tq;

Remaining_bt[i] -= tq;

} else {

Time += remaining_bt[i];

Wt[i] = time – bt[i]; // Waiting Time

Remaining_bt[i] = 0;

Done++; // Process is completed

// Calculate Turnaround Time (TAT = WT + BT)

For (int i = 0; i < n; i++)

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

// Display results

Printf(“\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n”);

For (int i = 0; i < n; i++)


Printf(“%d\t%d\t\t%d\t\t%d\n”, i + 1, bt[i], wt[i], tat[i]);

Int main() {

Int n, tq;

Printf(“Enter the number of processes: “);

Scanf(“%d”, &n);

Int bt[n];

Printf(“Enter burst times for each process:\n”);

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

Printf(“Process %d: “, i + 1);

Scanf(“%d”, &bt[i]);

Printf(“Enter time quantum: “);

Scanf(“%d”, &tq);

roundRobin(n, bt, tq);

return 0;

Priority scheduling

#include <stdio.h>

Void sortProcesses(int n, int bt[], int pr[], int p[]) {

For (int i = 0; i < n – 1; i++) {

For (int j = i + 1; j < n; j++) {


If (pr[i] > pr[j]) { // Sort by Priority (Lower number = Higher priority)

Int temp = pr[i]; pr[i] = pr[j]; pr[j] = temp;

Temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;

Temp = p[i]; p[i] = p[j]; p[j] = temp;

Int main() {

Int n;

Printf(“Enter the number of processes: “);

Scanf(“%d”, &n);

Int p[n], bt[n], pr[n], wt[n], tat[n];

Printf(“Enter burst times and priority for each process:\n”);

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

P[i] = i + 1; // Process Ids

Printf(“Process %d (Burst Time Priority): “, p[i]);

Scanf(“%d %d”, &bt[i], &pr[i]);

sortProcesses(n, bt, pr, p); // Sort processes by priority

// Calculate Waiting Time

Wt[0] = 0;

For (int i = 1; i < n; i++) {


Wt[i] = wt[i – 1] + bt[i – 1];

// Calculate Turnaround Time

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

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

// Display Results

Printf(“\nProcess\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n”);

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

Printf(“%d\t%d\t\t%d\t\t%d\t\t%d\n”, p[i], pr[i], bt[i], wt[i], tat[i]);

Return 0;

Dining philosopher

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

Int tph, philname[20], status[20], howhung, hu[20], cho;

Void one();

Void two();

Int main() {
Int i;

Printf(“\n\nDINING PHILOSOPHER PROBLEM”);

Printf(“\nEnter the total number of philosophers: “);

Scanf(“%d”, &tph);

For (i = 0; i < tph; i++) {

Philname[i] = (i + 1);

Status[i] = 1; // 1 means thinking

Printf(“How many are hungry: “);

Scanf(“%d”, &howhung);

If (howhung == tph) {

Printf(“\nAll are hungry..\nDeadlock stage will occur”);

Printf(“\nExiting..\n”);

Return 0;

} else {

For (i = 0; i < howhung; i++) {

Printf(“Enter philosopher %d position: “, (i + 1));

Scanf(“%d”, &hu[i]);

Status[hu[i]] = 2; // 2 means hungry

Do {

Printf(“\n1. One can eat at a time”);

Printf(“\n2. Two can eat at a time”);

Printf(“\n3. Exit”);
Printf(“\nEnter your choice: “);

Scanf(“%d”, &cho);

Switch (cho) {

Case 1: one(); break;

Case 2: two(); break;

Case 3: return 0;

Default: printf(“\nInvalid option..”);

} while (1);

Return 0;

Void one() {

Int pos = 0, x, i;

Printf(“\nAllow one philosopher to eat at any time\n”);

For (i = 0; i < howhung; i++, pos++) {

Printf(“\nP %d is granted to eat”, philname[hu[pos]]);

For (x = pos + 1; x < howhung; x++) {

Printf(“\nP %d is waiting”, philname[hu[x]]);

Void two() {

Int i, j, s = 0, t, r, x;

Printf(“\nAllow two philosophers to eat at the same time\n”);


For (i = 0; i < howhung; i++) {

For (j = i + 1; j < howhung; j++) {

If (abs(hu[i] – hu[j]) >= 1 && abs(hu[i] – hu[j]) != 4) { // Condition for valid pair

Printf(“\n\nCombination %d\n”, (s + 1));

T = hu[i];

R = hu[j];

S++;

Printf(“\nP %d and P %d are granted to eat”, philname[t], philname[r]);

For (x = 0; x < howhung; x++) {

If (hu[x] != t && hu[x] != r) {

Printf(“\nP %d is waiting”, philname[hu[x]]);

You might also like