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

Os File

The document contains multiple C programs demonstrating different CPU scheduling algorithms including FCFS, SJF, and Round Robin, as well as memory management techniques like paging, segmentation, best fit, and first fit. Each program includes user input for parameters and outputs the results of the scheduling or memory allocation. The programs showcase fundamental concepts in operating systems related to process management and memory allocation.

Uploaded by

ch sarthak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Os File

The document contains multiple C programs demonstrating different CPU scheduling algorithms including FCFS, SJF, and Round Robin, as well as memory management techniques like paging, segmentation, best fit, and first fit. Each program includes user input for parameters and outputs the results of the scheduling or memory allocation. The programs showcase fundamental concepts in operating systems related to process management and memory allocation.

Uploaded by

ch sarthak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write a C program for implementation of FCFS CPU scheduling

#include<stdio.h>
// Function to find the waiting time for all
// processes

void findWaitingTime(int processes[], int n, int bt[], int wt[])


{
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


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

// Function to calculate turn around time


void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
//Function to calculate average time
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt);

//Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

//Display processes along with all details


printf("Processes Burst time Waiting time Turn around time\n");

// Calculate total waiting time and total turn


// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
float s=(float)total_wt / (float)n;
float t=(float)total_tat / (float)n;
printf("Average waiting time = %f",s);
printf("\n");
printf("Average turn around time = %f ",t);
}

// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
//Burst time of all processes
int burst_time[] = {10, 5, 8};

findavgTime(processes, n, burst_time);
return 0;
}

Output:
Processes Burst Time Waiting TimeTurn-around Time

1 10 0 10

2 5 10 15

3 8 15 23
Write a C program for implementation of SJF CPU scheduling
#include <stdio.h>
int main()
{
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.

int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");

// User Input Burst Time and alloting Process Id.

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


printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}

// Sorting process according to their Burst Time.

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 = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;

temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;

// Calculation of Waiting Times

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\n");

// Calculation of Turn Around Time and printing the data.

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\n", 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("\nAverage Turnaround Time= %f", avg_tat);
}
Write a C program for implementation of ROUND ROBIN CPU scheduling
#include<stdio.h>

int main()
{

int cnt,j,n,t,remain,flag=0,tq;
int wt=0,tat=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d", &n);
remain=n;
for(cnt=0; cnt<n; cnt++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",cnt+1);
scanf("%d",&at[cnt]);
scanf("%d",&bt[cnt]);
rt[cnt]=bt[cnt];
}
printf("Enter Time Quantum:\t");
scanf("%d",&tq);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(t=0,cnt=0;remain!=0;)
{
if(rt[cnt]<=tq && rt[cnt]>0)
{
t+=rt[cnt];
rt[cnt]=0;
flag=1;
}
else if(rt[cnt]>0)
{
rt[cnt]-=tq;
t+=tq;
}
if(rt[cnt]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",cnt+1,t-at[cnt],t-at[cnt]-bt[cnt]);
wt+=t-at[cnt]-bt[cnt];
tat+=t-at[cnt];
flag=0;
}
if(cnt==n-1)
cnt=0;
else if(at[cnt+1]<=t)
cnt++;
else
cnt=0;
}
printf("\nAverage Waiting Time= %f\n",wt*1.0/n);
printf("Avg Turnaround Time = %f",tat*1.0/n);

return 0;
}

Output:

Enter Total Process: 4


Enter Arrival Time and Burst Time for Process Process Number 1 :0 5
Enter Arrival Time and Burst Time for Process Process Number 2 :1 4
Enter Arrival Time and Burst Time for Process Process Number 3 :2 2
Enter Arrival Time and Burst Time for Process Process Number 4 :4 1
Enter Time Quantum: 2

Process |Turnaround Time|Waiting Time

P[3] | 4 | 2
P[4] | 3 | 2
P[2] | 10 | 6
P[1] | 12 | 7

Average Waiting Time= 4.250000


Avg Turnaround Time = 7.250000
Write a program to simulate bankers algorithm for deadlock avoidance
#include <stdio.h>
#define MAX 10

int main() {
int n, m; // n = processes, m = resources
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resources: ");
scanf("%d", &m);
int alloc[MAX][MAX], max[MAX][MAX], need[MAX][MAX];
int avail[MAX];
printf("\nEnter Allocation Matrix:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("\nEnter Maximum Matrix:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
scanf("%d", &max[i][j]);
}
}
printf("\nEnter Available Resources:\n");
for(int i = 0; i < m; i++) {
scanf("%d", &avail[i]);
}
// Calculate Need matrix
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
int finish[MAX] = {0};
int safeSeq[MAX];
int work[MAX];
// Initialize work = available
for(int i = 0; i < m; i++) {
work[i] = avail[i];
}
int count = 0;
while(count < n) {
int found = 0;
for(int i = 0; i < n; i++) {
if(finish[i] == 0) {
int j;
for(j = 0; j < m; j++) {
if(need[i][j] > work[j])
break;
}
if(j == m) {
// Process can execute
for(int k = 0; k < m; k++) {
work[k] += alloc[i][k];
}
safeSeq[count++] = i;
finish[i] = 1;
found = 1;
}
}
}
if(found == 0) {
printf("\nSystem is NOT in a safe state (Deadlock possible)\n");
return 0;
}
}
printf("\nSystem is in a SAFE state.\nSafe sequence is: ");
for(int i = 0; i < n; i++) {
printf("P%d ", safeSeq[i]);
}
printf("\n");
return 0;
}

Input
Processes: 5
Resources: 3
Allocation:
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Max:
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Available:
3 3 2

Output
System is in a SAFE state.
Safe sequence is: P1 P3 P4 P0 P2
Write a program to simulate paging memory management technique
#include <stdio.h>
int main() {
int psize, lsize, fsize;
int npages, nframes;
printf("Enter page size: ");
scanf("%d", &psize);
printf("Enter logical memory size: ");
scanf("%d", &lsize);
printf("Enter physical memory size: ");
scanf("%d", &fsize);
// Calculate number of pages and frames
npages = lsize / psize;
nframes = fsize / psize;
int page_table[npages];
printf("\nEnter page table (frame number for each page):\n");
for(int i = 0; i < npages; i++) {
printf("Page %d: ", i);
scanf("%d", &page_table[i]);
}
int logical_address;
printf("\nEnter logical address: ");
scanf("%d", &logical_address);
// Calculate page number and offset
int page_no = logical_address / psize;
int offset = logical_address % psize;
if(page_no >= npages) {
printf("\nInvalid logical address!\n");
return 0;
}
int frame_no = page_table[page_no];
if(frame_no >= nframes) {
printf("\nInvalid frame number in page table!\n");
return 0;
}
int physical_address = frame_no * psize + offset;
printf("\nPage Number: %d", page_no);
printf("\nOffset: %d", offset);
printf("\nFrame Number: %d", frame_no);
printf("\nPhysical Address: %d\n", physical_address);
return 0;
}
Input
Page size: 100
Logical memory size: 1000
Physical memory size: 500
Page Table:
Page 0: 2
Page 1: 1
Page 2: 3
Page 3: 0
Page 4: 4……
Logical Address: 250

Output:
Page Number: 2
Offset: 50
Frame Number: 3
Physical Address: 350
Write a program to simulate segmentation memory management technique
#include <stdio.h>
#define MAX 10
int main() {
int n; // number of segments
printf("Enter number of segments: ");
scanf("%d", &n);
int base[MAX], limit[MAX];
printf("\nEnter base and limit for each segment:\n");
for(int i = 0; i < n; i++) {
printf("Segment %d - Base: ", i);
scanf("%d", &base[i]);
printf("Segment %d - Limit: ", i);
scanf("%d", &limit[i]);
}
int seg_no, offset;
printf("\nEnter logical address (segment number and offset):\n");
printf("Segment number: ");
scanf("%d", &seg_no);
printf("Offset: ");
scanf("%d", &offset);
// Check validity
if(seg_no >= n) {
printf("\nInvalid segment number!\n");
return 0;
}
if(offset >= limit[seg_no]) {
printf("\nSegmentation Fault! Offset exceeds limit.\n");
return 0;
}
// Calculate physical address
int physical_address = base[seg_no] + offset;
printf("\nSegment Number: %d", seg_no);
printf("\nOffset: %d", offset);
printf("\nBase Address: %d", base[seg_no]);
printf("\nPhysical Address: %d\n", physical_address);
return 0;
}

Input 1:
Number of segments: 3
Segment 0 → Base: 100, Limit: 300
Segment 1 → Base: 400, Limit: 200
Segment 2 → Base: 700, Limit: 100
Logical Address:
Segment = 1
Offset = 50
Output:
Segment Number: 1
Offset: 50
Base Address: 400
Physical Address: 450

Input 2:
Segment = 2
Offset = 150
Output:
Segmentation Fault! Offset exceeds limit.
Write a program to simulate best fit contiguous memory allocation technique
#include <stdio.h>
#define MAX 20
int main() {
int nb, np; // number of blocks and processes
int blockSize[MAX], processSize[MAX];
int allocation[MAX];
printf("Enter number of memory blocks: ");
scanf("%d", &nb);
printf("Enter size of each block:\n");
for(int i = 0; i < nb; i++) {
scanf("%d", &blockSize[i]);
}
printf("\nEnter number of processes: ");
scanf("%d", &np);
printf("Enter size of each process:\n");
for(int i = 0; i < np; i++) {
scanf("%d", &processSize[i]);
allocation[i] = -1; // initially not allocated
}
// Best Fit Allocation
for(int i = 0; i < np; i++) {
int bestIdx = -1;
for(int j = 0; j < nb; j++) {
if(blockSize[j] >= processSize[i]) {
if(bestIdx == -1 || blockSize[j] < blockSize[bestIdx]) {
bestIdx = j;
}
}
}

if(bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
// Output
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for(int i = 0; i < np; i++) {
printf("%d\t\t%d\t\t", i, processSize[i]);
if(allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}
return 0;
}

Input
Blocks: 5
Block sizes: 100 500 200 300 600
Processes: 4
Process sizes: 212 417 112 426

Output
Process No. Process Size Block No.
0 212 3
1 417 1
2 112 2
3 426 4

Write a program to simulate first fit contiguous memory allocation technique


#include <stdio.h>
#define MAX 20
int main() {
int nb, np; // number of blocks and processes
int blockSize[MAX], processSize[MAX];
int allocation[MAX];
printf("Enter number of memory blocks: ");
scanf("%d", &nb);
printf("Enter sizes of memory blocks:\n");
for(int i = 0; i < nb; i++) {
scanf("%d", &blockSize[i]);
}
printf("\nEnter number of processes: ");
scanf("%d", &np);
printf("Enter sizes of processes:\n");
for(int i = 0; i < np; i++) {
scanf("%d", &processSize[i]);
allocation[i] = -1; // initially not allocated
}
// First Fit Allocation
for(int i = 0; i < np; i++) {
for(int j = 0; j < nb; j++) {
if(blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break; // allocate first suitable block
}
}
}
// Output
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for(int i = 0; i < np; i++) {
printf("%d\t\t%d\t\t", i, processSize[i]);
if(allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}

return 0;
}

Input
Blocks: 5
Block sizes: 100 500 200 300 600

Processes: 4
Process sizes: 212 417 112 426

Output
Process No. Process Size Block No.
0 212 1
1 417 4
2 112 1
3 426 Not Allocated

Write a program to simulate concept of dinning-philosopher problem


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define N 5 // number of philosophers
sem_t forks[N]; // semaphore for each fork
sem_t room; // limits philosophers entering room
void* philosopher(void* num) {
int id = *(int*)num;
printf("Philosopher %d is thinking\n", id);
sleep(1);
// Try to enter room (at most N-1 philosophers)
sem_wait(&room);
// Pick left and right forks
sem_wait(&forks[id]);
sem_wait(&forks[(id + 1) % N]);
printf("Philosopher %d is eating\n", id);
sleep(2);
// Put down forks
sem_post(&forks[id]);
sem_post(&forks[(id + 1) % N]);
printf("Philosopher %d has finished eating\n", id);
// Leave room
sem_post(&room);
return NULL;
}
int main() {
pthread_t ph[N];
int ids[N];
// Initialize semaphores
sem_init(&room, 0, N - 1); // allow max N-1 philosophers
for(int i = 0; i < N; i++) {
sem_init(&forks[i], 0, 1); // each fork is available
}
// Create philosopher threads
for(int i = 0; i < N; i++) {
ids[i] = i;
pthread_create(&ph[i], NULL, philosopher, &ids[i]);
}
// Join threads
for(int i = 0; i < N; i++) {
pthread_join(ph[i], NULL);
}
return 0;
}

Output
Philosopher 0 is thinking
Philosopher 1 is thinking
...
Philosopher 2 is eating
Philosopher 2 has finished eating
...

You might also like