0% found this document useful (0 votes)
5 views1 page

FCFS and SJF Scheduling Algorithms

The document contains various scheduling algorithms for process management in operating systems, including FCFS, SJF, Round Robin, and Priority Scheduling. Each algorithm is implemented in C, demonstrating how to calculate waiting time, turnaround time, and average metrics for processes. Additionally, it discusses memory allocation strategies like First Fit, Best Fit, and the Banker’s algorithm for deadlock avoidance.

Uploaded by

vashalisharma04
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)
5 views1 page

FCFS and SJF Scheduling Algorithms

The document contains various scheduling algorithms for process management in operating systems, including FCFS, SJF, Round Robin, and Priority Scheduling. Each algorithm is implemented in C, demonstrating how to calculate waiting time, turnaround time, and average metrics for processes. Additionally, it discusses memory allocation strategies like First Fit, Best Fit, and the Banker’s algorithm for deadlock avoidance.

Uploaded by

vashalisharma04
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

FCFS scheduling : . #include<stdio.h>…void findWaitingTime(int processes[], int n, int bt[], int wt[]) { … Producer cons:… #include <stdio.

clude <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h>


wt[0] = 0; … for (int i = 1; i < n; i++ ).. wt[i] = bt[i-1] + wt[i-1]; } … void findTurnAroundTime(int #include <semaphore.h> #define BUFFER_SIZE 5 int buffer[BUFFER_SIZE]; int in = 0; int out = 0;
processes[], int n, int bt[], int wt[], int tat[]) { … for (int i = 0; i < n; i++)… tat[i] = bt[i] + wt[i]; }… void sem_t empty; sem_t full; pthread_mutex_t mutex; void *producer(void *arg) { int item = 0; (void)arg;
findavgTime(int processes[], int n, int bt[]) { … int wt[n], tat[n], total_wt = 0, total_tat = 0; … while (1) { item++; sem_wait(&empty); pthread_mutex_lock(&mutex); buffer[in] = item; printf("Producer
findWaitingTime(processes, n, bt, wt); … findTurnAroundTime(processes, n, bt, wt, tat); … produced: %d (buffer[%d])", item, in); in = (in + 1) % BUFFER_SIZE; pthread_mutex_unlock(&mutex);
printf("Processes Burst time Waiting time Turn around time"); … for (int i=0; i<n; i++) { total_wt = sem_post(&full); sleep(1); } return NULL; } void *consumer(void *arg) { int item; (void)arg; while (1) {
total_wt + wt[i]; … total_tat = total_tat + tat[i]; … printf(" %d ",(i+1)); printf(" %d ", bt[i]); … printf(" sem_wait(&full); pthread_mutex_lock(&mutex); item = buffer[out]; printf("Consumer consumed: %d
%d", wt[i]); printf(" %d", tat[i]); } … float s = (float)total_wt / (float)n; … float t = (float)total_tat / (buffer[%d])", item, out); out = (out + 1) % BUFFER_SIZE; pthread_mutex_unlock(&mutex);
(float)n; … printf("Average waiting time = %f", s); … printf("Average turn around time = %f ", t); } … int sem_post(&empty); sleep(2); } return NULL; } int main(void) { pthread_t prod_thread, cons_thread; if
main() { int processes[] = { 1, 2, 3 }; int n = sizeof processes / sizeof processes[0]; int burst_time[] = {10, (sem_init(&empty, 0, BUFFER_SIZE) != 0) { perror("sem_init empty"); exit(EXIT_FAILURE); } if
5, 8}; findavgTime(processes, n, burst_time); return 0; } // This code is contributed by Shivi_Aggarwal (sem_init(&full, 0, 0) != 0) { perror("sem_init full"); exit(EXIT_FAILURE); } if
(pthread_mutex_init(&mutex, NULL) != 0) { perror("pthread_mutex_init"); exit(EXIT_FAILURE); } if
SJF NP:… #include <stdio.h> .. int main() {.. int A[100][4]; … int i, j, n, total = 0, index, temp; … float (pthread_create(&prod_thread, NULL, producer, NULL) != 0) { perror("pthread_create producer");
avg_wt, avg_tat; … printf("Enter number of process: "); … scanf("%d", &n); … printf("Enter Burst Time: exit(EXIT_FAILURE); } if (pthread_create(&cons_thread, NULL, consumer, NULL) != 0) {
"); … for (i = 0; i < n; i++) { … printf("P%d: ", i + 1); … scanf("%d", &A[i][1]); A[i][0] = i + 1; } … for (i perror("pthread_create consumer"); exit(EXIT_FAILURE); } pthread_join(prod_thread, NULL);
= 0; i < n; i++) { … index = i; … for (j = i + 1; j < n; j++) if (A[j][1] < A[index][1]) index = j; … temp = pthread_join(cons_thread, NULL); sem_destroy(&empty); sem_destroy(&full);
A[i][1]; … A[i][1] = A[index][1]; … A[index][1] = temp; … temp = A[i][0]; A[i][0] = A[index][0]; … pthread_mutex_destroy(&mutex); return 0; }
A[index][0] = temp; }… A[0][2] = 0; … for (i = 1; i < n; i++) {… A[i][2] = 0; …for (j = 0; j < i; j++)
A[i][2] += A[j][1]; … total += A[i][2]; }… avg_wt = (float)total / n; total = 0; … printf("P BT WT TAT "); Dinning_philosipher:…#include <stdio.h> #include <pthread.h> #include <semaphore.h> #include
… for (i = 0; i < n; i++) {… A[i][3] = A[i][1] + A[i][2]; … total += A[i][3]; … printf("P%d %d %d %d ", <unistd.h> #define BUFFER_SIZE 5 int buffer[BUFFER_SIZE]; int in = 0, out = 0; sem_t empty; sem_t
A[i][0], A[i][1], A[i][2], A[i][3]); }… avg_tat = (float)total / n; … printf("Average Waiting Time= %f", full; pthread_mutex_t mutex; void* producer(void* arg) { int item = 0; while (1) { item++;
avg_wt); … printf("Average Turnaround Time= %f", avg_tat); } sem_wait(&empty); pthread_mutex_lock(&mutex); buffer[in] = item; printf("Producer produced %d",
item); in = (in + 1) % BUFFER_SIZE; pthread_mutex_unlock(&mutex); sem_post(&full); sleep(1); } }
SJF P :… #include <stdio.h> … int main() { … int A[100][4]; … int i, j, n, total = 0, … index, temp; … void* consumer(void* arg) { int item; while (1) { sem_wait(&full); pthread_mutex_lock(&mutex); item =
float avg_wt, avg_tat; … printf("Enter number of process: "); … scanf("%d", &n); … printf("Enter Burst buffer[out]; printf("Consumer consumed %d", item); out = (out + 1) % BUFFER_SIZE;
Time: ");… for (i = 0; i < n; i++) { … printf("P%d: ", i + 1); … scanf("%d", &A[i][1]); A[i][0] = i + 1; }… pthread_mutex_unlock(&mutex); sem_post(&empty); sleep(2); } }…… int main() { pthread_t prod, cons;
for (i = 0; i < n; i++) { … index = i; for (j = i + 1; j < n; j++) if (A[j][1] < A[index][1]) index = j; … temp = sem_init(&empty, 0, BUFFER_SIZE); sem_init(&full, 0, 0); pthread_mutex_init(&mutex, NULL);
A[i][1]; A[i][1] = A[index][1]; … A[index][1] = temp; … temp = A[i][0]; A[i][0] = A[index][0]; … pthread_create(&prod, NULL, producer, NULL); pthread_create(&cons, NULL, consumer, NULL);
A[index][0] = temp; }… A[0][2] = 0; … for (i = 1; i < n; i++) { … A[i][2] = 0; …for (j = 0; j < i; j++) pthread_join(prod, NULL); pthread_join(cons, NULL); return 0; }
A[i][2] += A[j][1]; … total += A[i][2]; }… avg_wt = (float)total / n; total = 0; … printf("P BT WT TAT ");
… for (i = 0; i < n; i++) { … A[i][3] = A[i][1] + A[i][2]; … total += A[i][3]; … printf("P%d %d %d %d ",
A[i][0], A[i][1], A[i][2], A[i][3]); } … avg_tat = (float)total / n; … printf("Average Waiting Time= %f",
avg_wt); … printf("Average Turnaround Time= %f", avg_tat); }

Round Robin: … #include <stdio.h> … int main() { int n, quantum; printf("Enter number of processes: ");
scanf("%d", &n); int bt[n], rt[n]; printf("Enter burst times of each process:\n"); for (int i = 0; i < n; i++) {
printf("P%d: ", i + 1); scanf("%d", &bt[i]); rt[i] = bt[i]; } printf("Enter time quantum: "); scanf("%d",
&quantum); int time = 0; int completed = 0; int wt[n], tat[n]; for (int i = 0; i < n; i++) { wt[i] = 0; } while
(completed < n) { for (int i = 0; i < n; i++) { if (rt[i] == 0) continue; if (rt[i] > quantum) { time += quantum;
rt[i] -= quantum; } else { time += rt[i]; tat[i] = time; wt[i] = tat[i] - bt[i]; rt[i] = 0; completed++; } } }
printf("\nProcess\tBT\tWT\tTAT\n"); for (int i = 0; i < n; i++) { printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i],
wt[i], tat[i]); } float avgWT = 0, avgTAT = 0; for (int i = 0; i < n; i++) { avgWT += wt[i]; avgTAT += tat[i];
} avgWT /= n; avgTAT /= n; printf("\nAverage Waiting Time = %.2f", avgWT); printf("\nAverage
Turnaround Time = %.2f\n", avgTAT); return 0; }

Priority scheduling:…#include <stdio.h> struct process { int pid; int bt; int rt; int wt; int tat; }; int main() {
int n, quantum; printf("Enter number of processes: "); scanf("%d", &n); struct process p[n]; printf("Enter
burst times: "); for (int i = 0; i < n; i++) { p[i].pid = i + 1; printf("P%d: ", p[i].pid); scanf("%d", &p[i].bt);
p[i].rt = p[i].bt; p[i].wt = 0; p[i].tat = 0; } printf("Enter time quantum: "); scanf("%d", &quantum); int time
= 0, completed = 0; while (completed < n) { for (int i = 0; i < n; i++) { if (p[i].rt == 0) continue; if (p[i].rt >
quantum) { time += quantum; p[i].rt -= quantum; } else { time += p[i].rt; p[i].rt = 0; p[i].tat = time; p[i].wt
= p[i].tat - p[i].bt; completed++; } } } printf("Process BT WT TAT "); for (int i = 0; i < n; i++) {
printf("P%d %d %d %d ", p[i].pid, p[i].bt, p[i].wt, p[i].tat); } float avgWT = 0, avgTAT = 0; for (int i = 0; i
< n; i++) { avgWT += p[i].wt; avgTAT += p[i].tat; } avgWT /= n; avgTAT /= n; printf("Average Waiting
Time = %.2f", avgWT); printf("Average Turnaround Time = %.2f", avgTAT); return 0; }

First , worst , Best:… #include <stdio.h> void resetBlocks(int block[], int original[], int b) { for (int i = 0; i
< b; i++) block[i] = original[i]; } int main() { int b, p; int block[20], original[20], process[20]; printf("Enter
number of blocks: "); scanf("%d", &b); printf("Enter block sizes: "); for (int i = 0; i < b; i++) { scanf("%d",
&block[i]); original[i] = block[i]; } printf("Enter number of processes: "); scanf("%d", &p); printf("Enter
process sizes: "); for (int i = 0; i < p; i++) scanf("%d", &process[i]); int alloc[20]; // FIRST FIT
…resetBlocks(block, original, b); for (int i = 0; i < p; i++) alloc[i] = -1; for (int i = 0; i < p; i++) { for (int j
= 0; j < b; j++) { if (block[j] >= process[i]) { alloc[i] = j; block[j] -= process[i]; break; } } } printf("FIRST
FIT"); printf("Process Size Block "); for (int i = 0; i < p; i++) { printf("P%d %d ", i, process[i]); if (alloc[i]
!= -1) printf("%d ", alloc[i]); else printf("Not Allocated "); } // BEST FIT resetBlocks(block, original, b);
for (int i = 0; i < p; i++) alloc[i] = -1; for (int i = 0; i < p; i++) { int best = -1; for (int j = 0; j < b; j++) { if
(block[j] >= process[i]) { if (best == -1 || block[j] < block[best]) best = j; } } if (best != -1) { alloc[i] = best;
block[best] -= process[i]; } } printf(" BEST FIT "); printf("Process Size Block "); for (int i = 0; i < p; i++) {
printf("P%d %d ", i, process[i]); if (alloc[i] != -1) printf("%d ", alloc[i]); else printf("Not Allocated "); }
return 0; }

Banker:… #include <stdio.h> … #define MAX 10 int banker(int n, int m, int allocation[MAX][MAX], int
maxneed[MAX][MAX], int available[MAX]) {… int need[MAX][MAX]; …int finish[MAX]; …int
safeSequence[MAX]; … int count = 0; … for (int i = 0; i < n; i++) { … for (int j = 0; j < m; j++) {
need[i][j] = maxneed[i][j] - allocation[i][j]; } } … for (int i = 0; i < n; i++) … finish[i] = 0; … while (count
< n) { … int allocated_any = 0; … for (int i = 0; i < n; i++) { … if (!finish[i]) { … int canAllocate = 1; …
for (int j = 0; j < m; j++) { … if (need[i][j] > available[j]) { canAllocate = 0; break; } } … if (canAllocate)
{ for (int j = 0; j < m; j++) { … available[j] += allocation[i][j]; } … safeSequence[count++] = i; … finish[i]
= 1; allocated_any = 1; } } } … if (!allocated_any) { … printf("System is NOT in a safe state."); … return
0; } } … printf("System is in a safe state. Safe sequence: "); … for (int i = 0; i < n; i++) { printf("P%d",
safeSequence[i]); if (i != n - 1) … printf(" -> "); } printf(""); return 1; } …… int main() { int n, m; int
allocation[MAX][MAX], maxneed[MAX][MAX], available[MAX]; … printf("Enter number of processes
(<= %d): ", MAX); if (scanf("%d", &n) != 1 || n <= 0 || n > MAX) return 0; printf("Enter number of
resource types (<= %d): ", MAX); if (scanf("%d", &m) != 1 || m <= 0 || m > MAX) return 0; for (int i = 0; i
< n; i++) for (int j = 0; j < m; j++) scanf("%d", &allocation[i][j]); for (int i = 0; i < n; i++) for (int j = 0; j <
m; j++) scanf("%d", &maxneed[i][j]); for (int j = 0; j < m; j++) scanf("%d", &available[j]); banker(n, m,
allocation, maxneed, available); return 0; }

You might also like