0% found this document useful (0 votes)
7 views11 pages

Program 4 To 8 Os

The document contains multiple C programs addressing various computer science concepts, including the producer-consumer problem using semaphores, memory allocation methods (first fit, worst fit, best fit), page replacement algorithms (FIFO, LRU, LFU), paging techniques for memory management, and the Banker's algorithm for deadlock avoidance. Each section includes code implementations and explanations for the respective algorithms. The programs demonstrate practical applications of synchronization, memory management, and resource allocation in operating systems.

Uploaded by

sujathaurs143
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)
7 views11 pages

Program 4 To 8 Os

The document contains multiple C programs addressing various computer science concepts, including the producer-consumer problem using semaphores, memory allocation methods (first fit, worst fit, best fit), page replacement algorithms (FIFO, LRU, LFU), paging techniques for memory management, and the Banker's algorithm for deadlock avoidance. Each section includes code implementations and explanations for the respective algorithms. The programs demonstrate practical applications of synchronization, memory management, and resource allocation in operating systems.

Uploaded by

sujathaurs143
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/ 11

4. Write a program to solve producer-consumer problem using Semaphores.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h> // Include this header for the sleep function

#define BUFFER_SIZE 5 // Define the buffer size

int buffer[BUFFER_SIZE]; // Shared buffer


int in = 0, out = 0; // Indices for producer and consumer
sem_t empty, full, mutex; // Semaphores

// Producer thread function


void* producer(void* param) {
int item;
while (1) {
item = rand() % 100; // Produce an item (random number)
sem_wait(&empty); // Wait for space in the buffer
sem_wait(&mutex); // Enter critical section
buffer[in] = item; // Place the item in the buffer
printf("Produced: %d\n", item);
in = (in + 1) % BUFFER_SIZE; // Update the 'in' index
sem_post(&mutex); // Exit critical section
sem_post(&full); // Signal that the buffer is not empty
sleep(1); // Simulate production time
}
}

// Consumer thread function


void* consumer(void* param) {
int item;
while (1) {
sem_wait(&full); // Wait for an item in the buffer
sem_wait(&mutex); // Enter critical section
item = buffer[out]; // Consume an item from the buffer
printf("Consumed: %d\n", item);
out = (out + 1) % BUFFER_SIZE; // Update the 'out' index
sem_post(&mutex); // Exit critical section
sem_post(&empty); // Signal that there is space in the buffer
sleep(1); // Simulate consumption time
}
}

int main() {
pthread_t prod, cons;

// Initialize semaphores
sem_init(&empty, 0, BUFFER_SIZE); // Initially, the buffer is empty
sem_init(&full, 0, 0); // Initially, there are no items in the buffer
sem_init(&mutex, 0, 1); // Mutex to ensure mutual exclusion for buffer access

// Create producer and consumer threads


pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);

// Wait for threads to finish (not really necessary as these threads run infinitely)
pthread_join(prod, NULL);
pthread_join(cons, NULL);

// Destroy semaphores (this code won't be reached due to infinite loops)


sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);

return 0;
}
5. Implement the following memory allocation methods for fixed partition
a)First fit b) Worst fit c)Best fit
A) FIRST FIT:
#include <stdio.h>
#define MAX_PARTITIONS 10
#define MAX_PROCESSES 10
void firstFit(int partitions[], int m, int processes[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // No allocation yet
}
// Allocate memory to processes
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (partitions[j] >= processes[i]) {
allocation[i] = j;
partitions[j] -= processes[i]; // Reduce partition size
break;
}
}
}
// Display allocation
for (int i = 0; i < n; i++) {
if (allocation[i] != -1)
printf("Process %d allocated to Partition %d\n", i + 1, allocation[i] + 1);
else
printf("Process %d not allocated\n", i + 1);
}
}
int main() {
int partitions[MAX_PARTITIONS] = {100, 500, 200, 300, 600};
int processes[MAX_PROCESSES] = {212, 417, 112, 426};
int m = 5, n = 4;
firstFit(partitions, m, processes, n);
return 0;
}
B)WORST FIT:
#include <stdio.h>
#define MAX_PARTITIONS 10
#define MAX_PROCESSES 10
void worstFit(int partitions[], int m, int processes[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // No allocation yet
}
// Allocate memory to processes
for (int i = 0; i < n; i++) {
int maxIndex = -1;
for (int j = 0; j < m; j++) {
if (partitions[j] >= processes[i]) {
if (maxIndex == -1 || partitions[j] > partitions[maxIndex])
maxIndex = j;
}
}
if (maxIndex != -1) {
allocation[i] = maxIndex;
partitions[maxIndex] -= processes[i]; // Reduce partition size
}
}
// Display allocation
for (int i = 0; i < n; i++) {
if (allocation[i] != -1)
printf("Process %d allocated to Partition %d\n", i + 1, allocation[i] + 1);
else
printf("Process %d not allocated\n", i + 1);
}
}
int main() {
int partitions[MAX_PARTITIONS] = {100, 500, 200, 300, 600};
int processes[MAX_PROCESSES] = {212, 417, 112, 426};
int m = 5, n = 4;
worstFit(partitions, m, processes, n);
return 0;
}
C) BEST FIT:
#include <stdio.h>
#define MAX_PARTITIONS 10
#define MAX_PROCESSES 10
void bestFit(int partitions[], int m, int processes[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) {
allocation[i] = -1; // No allocation yet
}
// Allocate memory to processes
for (int i = 0; i < n; i++) {
int minIndex = -1;
for (int j = 0; j < m; j++) {
if (partitions[j] >= processes[i]) {
if (minIndex == -1 || partitions[j] < partitions[minIndex])
minIndex = j;
}
}
if (minIndex != -1) {
allocation[i] = minIndex;
partitions[minIndex] -= processes[i]; // Reduce partition size
}
}
// Display allocation
for (int i = 0; i < n; i++) {
if (allocation[i] != -1)
printf("Process %d allocated to Partition %d\n", i + 1, allocation[i] + 1);
else
printf("Process %d not allocated\n", i + 1);
}
}
int main() {
int partitions[MAX_PARTITIONS] = {100, 500, 200, 300, 600};
int processes[MAX_PROCESSES] = {212, 417, 112, 426};
int m = 5, n = 4;
bestFit(partitions, m, processes, n);
return 0;
}
6. Simulate the following page replacement algorithms
a) FIFO b) LRU c) LFU
a) FIFO
#include<stdio.h>
void main() {
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
printf("Enter the length of reference string: ");
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("Enter no. of frames: ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("The Page Replacement Process is: \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("The number of Page Faults using FIFO are %d\n",pf);
}

b) LRU
#include<stdio.h>
void main() {
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
printf("Enter the length of reference string: ");
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames: ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("The Page Replacement process is: \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
{
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
}
printf("\nThe number of page faults using LRU are %d",pf);
}
c) LFU
#include<stdio.h>
void main() {
int i, j, k, f, pf=0, min, rs[25], m[10], entr[20], n;
printf("Enter the length of reference string: ");
scanf("%d",&n);
printf("Enter the reference string: ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("Enter no. of frames: ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
entr[i]=0
m[i]=-1;
}
printf("The Page Replacement Process is: \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
{
entr[k]++;
break;
}
}
if(k==f)
{
min=0;
for(j=1;j<f;j++)
if(entr[j]<entr[min])
min=j;
m[min]=rs[i];
pf++;
}
printf("\n");
for(j=0;j<f;j++) printf("\t%d",m[j]);
if(j==f)
printf("\tPF No. %d",pf);
}
printf("The number of Page Faults using LFU are %d\n",pf);
}
7. Simulate Paging Technique of memory management
#include<stdio.h>
void main() {
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
printf("Enter the memory size: ");
scanf("%d",&ms);
printf("Enter the page size: ");
scanf("%d",&ps);
nop = ms/ps;
printf("The no. of pages available in memory are: %d \n",nop);
printf("Enter number of processes: ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
printf("Enter no. of pages required for p[%d]: ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("Memory is Full\n");
break;
}
rempages = rempages - s[i];
printf("Enter pagetable for p[%d]: ",i);
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("Enter Logical Address to find Physical Address\n");
printf("Enter process no. and pagenumber and offset: ");
scanf("%d %d %d",&x,&y, &offset);
if(x>np || y>=s[i] || offset>=ps)
printf("Invalid Process or Page Number or offset\n");
else
{
pa=fno[x][y]*ps+offset;
printf("The Physical Address is: %d\n",pa);
}
}
8. Implement Bankers Algorithm for Dead Lock Avoidance
#include<stdio.h>
int alc[50][50],max[50][50],avl[100],ned[50][50],work[100],fin[100],ss[100],maximum=0;
int i,j,k,l,m,n,z,t,y,u;
int check(int k,int sz)
{
z=0;
for(i=1;i<=sz;i++)
{
if((ned[k][i]<=work[i])&&(fin[k]==0))
{
++z;
}
}
return z;
}
void main()
{
int i,j,k,h=1;
printf("Enter no of process:");
scanf("%d",&n); printf("Enter no of resources:");
scanf("%d",&m); printf("Enter resource allocation matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&alc[i][j]);
}
fin[i]=0;
}
printf("Enter maximum resources matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter available resources:");
for(i=1;i<=m;i++)
{
scanf("%d",&avl[i]);
work[i]=avl[i];
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
ned[i][j]=max[i][j]-alc[i][j];
}
}
printf("Needed resources are:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("%d ",ned[i][j]);
}
printf("\n");
}
for(i=1;(h!=n+1)&&(maximum<=m*n*n);i++)
{
++maximum;
u=check(i,m);
if(u==m)
{
fin[i]=1;
ss[h]=i;
h++;
for(j=1;j<=m;j++)
{
work[j]=work[j]+alc[i][j];
}
}
if(i==n)
{
i=0;
}
}
if(h>n) {
printf("The safe sequence is:");
for(i=1;i<=n;i++)
{
printf(" p%d ",ss[i]);
}
printf("\n");
}
else
{
printf("Dead lock occured\n");
}
}

You might also like