OneCompiler

Lifeline

290

Explain Shortest Job First Algorithm with suitable example. Write program to implement SJF.

#include<stdio.h>

int main() {

int n;

printf("Enter the number of processes: ");

scanf("%d", &n);

int pid[n], burst_time[n];

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

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

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

    pid[i] = i + 1;

}



// Sort processes based on burst time using bubble sort

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]) {

            // Swap burst time

            int temp = burst_time[j];

            burst_time[j] = burst_time[j + 1];

            burst_time[j + 1] = temp;

            // Swap process IDs

            temp = pid[j];

            pid[j] = pid[j + 1];

            pid[j + 1] = temp;

        }

    }

}



// Execute processes

printf("Order of execution:\n");

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

    printf("Executing Process %d for %d units\n", pid[i], burst_time[i]);

}



return 0;

}

Explain FCFS Algorithm with suitable example. Write program to implement FCFS.

#include <stdio.h>
#define max 30

int main() {
int i, n, bt[max], wt[max], tat[max];
float awt = 0, atat = 0;

printf("Enter the number of processes: ");
scanf("%d", &n);

printf("Enter the burst time for each process:\n");
for (i = 0; i < n; i++) {
    printf("Process %d: ", i + 1);
    scanf("%d", &bt[i]);
}

wt[0] = 0;
tat[0] = bt[0];
for (i = 1; i < n; i++) {
    wt[i] = wt[i - 1] + bt[i - 1];
    tat[i] = wt[i] + bt[i];
}

// Calculating average waiting time and average turnaround time
for (i = 0; i < n; i++) {
    awt=awt+wt[i];
    atat=atat+tat[i];
}
awt=awt/n;
atat=atat/n;

// Displaying the results
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
    printf("%d\t\t%d\t\t\t%d\t\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f\n", awt);
printf("Average Turnaround Time: %.2f\n", atat);

return 0;

}

Round robin************************************************

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i, qt, count = 0, temp, sq = 0, bt[10], wt[10], tat[10], rem_bt[10];
float awt = 0, atat = 0;

printf("Enter number of processes: ");
scanf("%d", &n);

printf("Enter burst time of processes: ");
for (i = 0; i < n; i++) {
    scanf("%d", &bt[i]);
    rem_bt[i] = bt[i];
}

printf("Enter quantum time: ");
scanf("%d", &qt);

while (1) {
    for (i = 0, count = 0; i < n; i++) {
        temp = qt;
        if (rem_bt[i] == 0) {
            count++;
            continue;
        }
        if (rem_bt[i] > qt)
            rem_bt[i] -= qt;
        else {
            temp = rem_bt[i];
            rem_bt[i] = 0;
        }
        sq += temp;
        tat[i] = sq;
    }
    if (n == count)
        break;
}

printf("\nProcess\tBurst Time\tTurnaround Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
    wt[i] = tat[i] - bt[i];
    awt += wt[i];
    atat += tat[i];
    printf("%d\t\t%d\t\t%d\t\t%d\n", i + 1, bt[i], tat[i], wt[i]);
}

awt /= n;
atat /= n;
printf("Average Waiting Time: %f\n", awt);
printf("Average Turnaround Time: %f\n", atat);

}

Write Program to implement Producer-Consumer problem using semaphores.

#include <stdio.h>
#include <stdlib.h>

int mutex = 1;
int full = 0;
int empty = 10, x = 0;

void producer() {
if (mutex == 1 && empty != 0) {
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces item %d", x);
++mutex;
} else {
printf("Buffer is full!");
}
}

void consumer() {
if (mutex == 1 && full != 0) {
--mutex;
--full;
++empty;
printf("\nConsumer consumes item %d", x);
x--;
++mutex;
} else {
printf("Buffer is empty!");
}
}

int main() {
int n;
while (1) {
printf("\n1. Press 1 for Producer\n2. Press 2 for Consumer\n3. Press 3 for Exit\n");
printf("Enter your choice:");
scanf("%d", &n);

    switch (n) {
        case 1:
            producer();
            break;
        case 2:
            consumer();
            break;
        case 3:
            exit(0);
            break;
        default:
            printf("Invalid choice\n");
    }
}
return 0;

}

C program for Banker's Algorithm*

#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the names of Process

int n, r, i, j, k; 
n = 5; // Indicates the Number of processes 
r = 3; //Indicates the Number of resources 
int alloc[5][3] = { { 0, 0, 1 }, // P0 // This is Allocation Matrix 
					{ 3, 0, 0 }, // P1 
					{ 1, 0, 1 }, // P2 
					{ 2, 3, 2 }, // P3 
					{ 0, 0, 3 } }; // P4 

int max[5][3] = { { 7, 6, 3 }, // P0 // MAX Matrix 
				{ 3, 2, 2 }, // P1 
				{ 8, 0, 2 }, // P2 
				{ 2, 1, 2 }, // P3 
				{ 5, 2, 3 } }; // P4 

int avail[3] = { 2, 3, 2 }; // These are Available Resources 

int f[n], ans[n], ind = 0; 
for (k = 0; k < n; k++) { 
	f[k] = 0; 
} 
int need[n][r]; 
for (i = 0; i < n; i++) { 
	for (j = 0; j < r; j++) 
		need[i][j] = max[i][j] - alloc[i][j]; 
} 
int y = 0; 
for (k = 0; k < 5; k++) { 
	for (i = 0; i < n; i++) { 
		if (f[i] == 0) { 

			int flag = 0; 
			for (j = 0; j < r; j++) { 
				if (need[i][j] > avail[j]){ 
					flag = 1; 
					break; 
				} 
			} 

			if (flag == 0) { 
				ans[ind++] = i; 
				for (y = 0; y < r; y++) 
					avail[y] += alloc[i][y]; 
				f[i] = 1; 
			} 
		} 
	} 
} 

printf("Th SAFE Sequence is as follows\n"); 
for (i = 0; i < n - 1; i++) 
	printf(" P%d ->", ans[i]); 
printf(" P%d", ans[n - 1]); 

return (0); 

}

Write Program to implement different memory management techniques.

#include <stdio.h>
#include <stdlib.h>

// Define process structure
typedef struct {
int id;
int size;
int allocated;
} Process;

// Contiguous memory allocation
void contiguousMemoryAllocation(Process processes[], int n) {
int memorySize = 1000; // Total memory size
int allocatedMemory = 0;

for (int i = 0; i < n; i++) {
    if (allocatedMemory + processes[i].size <= memorySize) {
        processes[i].allocated = 1;
        allocatedMemory += processes[i].size;
        printf("Process %d allocated: %d KB\n", processes[i].id, processes[i].size);
    } else {
        printf("Process %d cannot be allocated due to insufficient memory\n", processes[i].id);
    }
}

}

// Paging
void paging(Process processes[], int n) {
int frameSize = 100; // Size of each frame
int totalFrames = 10; // Total number of frames
int allocatedFrames = 0;

for (int i = 0; i < n; i++) {
    int requiredFrames = (processes[i].size + frameSize - 1) / frameSize;
    if (allocatedFrames + requiredFrames <= totalFrames) {
        processes[i].allocated = 1;
        allocatedFrames += requiredFrames;
        printf("Process %d allocated: %d KB\n", processes[i].id, processes[i].size);
    } else {
        printf("Process %d cannot be allocated due to insufficient memory\n", processes[i].id);
    }
}

}

// Segmentation
void segmentation(Process processes[], int n) {
int maxSegments = 5; // Maximum number of segments per process
int segmentSizes[] = {100, 200, 300, 400, 500}; // Sizes of segments
int totalSegments = 0;

for (int i = 0; i < n; i++) {
    int requiredSegments = processes[i].size / 100;
    if (requiredSegments <= maxSegments && totalSegments + requiredSegments <= maxSegments) {
        processes[i].allocated = 1;
        totalSegments += requiredSegments;
        printf("Process %d allocated: %d KB\n", processes[i].id, processes[i].size);
    } else {
        printf("Process %d cannot be allocated due to insufficient memory\n", processes[i].id);
    }
}

}

int main() {
int n = 3; // Number of processes
Process processes[3] = {{1, 600, 0}, {2, 300, 0}, {3, 400, 0}};

printf("Contiguous Memory Allocation:\n");
contiguousMemoryAllocation(processes, n);

printf("\n\nPaging:\n");
paging(processes, n);

printf("\n\nSegmentation:\n");
segmentation(processes, n);

return 0;

}

Write Program to implement sequential and indexed file allocation methods.

#include <stdio.h>

int main() {
int f[50], i, st, len, j, c, k, count = 0;

for (i = 0; i < 50; i++)
    f[i] = 0;

printf("Files Allocated are : \n");

do {
    count = 0;
    printf("Enter starting block and length of files: ");
    scanf("%d %d", &st, &len);
    
    if (st < 0 || st >= 50 || len <= 0 || len > 50 || st + len > 50) {
        printf("Invalid input. Please enter valid starting block and length.\n");
        continue;
    }
    
    for (k = st; k < (st + len); k++) {
        if (f[k] == 0)
            count++;
    }
    
    if (len == count) {
        for (j = st; j < (st + len); j++) {
            if (f[j] == 0) {
                f[j] = 1;
                printf("%d\t%d\n", j, f[j]);
            }
        }
        printf("The file is allocated to disk\n");
    } else {
        printf("The file is not allocated \n");
    }
    
    printf("Do you want to enter more file (Yes - 1 / No - 0): ");
    scanf("%d", &c);
} while (c == 1);

return 0;

}

Write a shell program to find even or odd number.

echo "Enter a number: "
read num
if [ ((numecho"((num % 2)) -eq 0 ]; then echo "num is even"
else
echo "$num is odd"
fi

Write a shell program to display list of even numbers from 1 to 100.
for ((i=2; i<=100; i+=2))
do
echo $i
done