0% found this document useful (0 votes)
24 views18 pages

OS Solved

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)
24 views18 pages

OS Solved

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/ 18

Assignment 1-Bankers algorithm:1,3,4,5,7,9,11,13,19,23,24,26

#include<stdio.h>
int allocation[10][10], max[10][10], available[10], work[10],
need[10][10], finish[5], seq[10], scnt=0, no_of_processes,
no_of_resources,
check_process,request[10];
void accept()
{
int i,j;
printf("\nHow many number of processes:");
scanf("%d",&no_of_processes);
printf("\nHow many number of resources:");
scanf("%d",&no_of_resources);
printf("\nACCEPTING ALLOCATION MATRIX:\n");
for(i=0;i<no_of_processes;i++)
{
for(j=0;j<no_of_resources;j++)
{
printf("\nFor P%d enter resources for %c=",i,65+j);
scanf("%d",&allocation[i][j]);
}
}
printf("\nACCEPTING MAXIMUM MATRIX:");
for(i=0;i<no_of_processes;i++)
{
for(j=0;j<no_of_resources;j++)
{
printf("\nFor P%d enter resources for %c=",i,65+j);
scanf("%d",&max[i][j]);
}
}
printf("\nACCEPTING AVAILABILITY:\n");
for(j=0;j<no_of_resources;j++)
{
printf("Available resource for %c=",65+j);
scanf("%d",&available[j]);
work[j]=available[j];
}
}
void calc_need()
{
int i,j;
for(i=0;i<no_of_processes;i++)
{
for(j=0;j<no_of_resources;j++)
{
need[i][j]=max[i][j]-allocation[i][j];
}
}
}
int check_need_with_available(int i)
{
int j;
for(j=0;j<no_of_resources;j++)
{
if(need[i][j]>available[j])
return 0;
}
return 1;
}
int check_request(int i)
{
int j,flag=0;
for(j=0;j<no_of_resources;j++)
{
if(need[i][j]>=request[j] && available[j]>=request[j])
flag=1;
break;
}
if(flag==1)
printf("request will grant immediatly");
else
printf("request will not grant ");
}
void banker()
{
int cnt=1,i=0,j;
while(cnt<=2)
{
if(finish[i]==0)
{
if(check_need_with_available(i))
{
finish[i]=1;
seq[scnt]=i;
scnt++;
for(j=0;j<no_of_resources;j++)
{
available[j]=allocation[i][j]+available[j];
}
}
}
i=(i+1)%no_of_processes;
if(i==0)
cnt++;
}
}
void display()
{
int i,j;
printf("\nALLOCATION MATRIX\n");
for(j=0;j<no_of_resources;j++)
{
printf("\t%c",65+j);
}
for(i=0;i<no_of_processes;i++)
{
printf("\nP%d\t",i);
for(j=0;j<no_of_resources;j++)
{
printf("%d\t",allocation[i][j]);
}
printf("\n");
}
printf("MAXIMUM MATRIX\n");
for(j=0;j<no_of_resources;j++)
{
printf("\t%c",65+j);
}
for(i=0;i<no_of_processes;i++)
{
printf("\nP%d\t",i);
for(j=0;j<no_of_resources;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("NEED MATRIX\n");
for(j=0;j<no_of_resources;j++)
{
printf("\t%c",65+j);
}
for(i=0;i<no_of_processes;i++)
{
printf("\nP%d\t",i);
for(j=0;j<no_of_resources;j++)
{
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("AVAILABLE MATRIX\n");
for(j=0;j<no_of_resources;j++)
{
printf("%c\t",65+j);
}
printf("\n");
for(j=0;j<no_of_resources;j++)
{
printf("%d\t",available[j]);
}
printf("\n");
}
void main()
{
int i,j;
accept();
calc_need();
display();
banker();
if(scnt==no_of_processes)
{
printf("\n System is in Safe State....");
printf("\nSafe Sequence=");
printf("<");
for(i=0;i<scnt;i++)
{
printf("P%d\t",seq[i]);
}
printf(">");
}
else
{
printf("\nGiven System is not in Safe State....");
}
printf("\nWhich process you want to check that it will be granted or
not?");
scanf("%d",&check_process);
if(check_process>no_of_processes)
{
printf("\nInvalid Process..Please Enter valid process\n");
//return;
}
for(j=0;j<no_of_resources;j++)
{
printf("Enter For P%d enter %c=",check_process,65+j);
scanf("%d",&request[j]);
}
check_request(check_process);
}
Assignment2-linked allocation :2,6,15,25

#include <stdio.h>
#include <stdlib.h>
void recursivePart(int pages[]){
int st, len, k, c, j;
printf("Enter the index of the starting block and its length: ");
scanf("%d%d", &st, &len);
k = len;
if (pages[st] == 0){
for (j = st; j < (st + k); j++){
if (pages[j] == 0){
pages[j] = 1;
printf("%d------>%d\n", j, pages[j]);
}
else {
printf("The block %d is already allocated \n", j);
k++;}}}
else
printf("The block %d is already allocated \n", st);
printf("Do you want to enter more files? \n");
printf("Enter 1 for Yes, Enter 0 for No: ");
scanf("%d", &c);
if (c==1)
recursivePart(pages);
else
exit(0);
return;
}
int main(){
int pages[50], p, a;
for (int i = 0; i < 50; i++)
pages[i] = 0;
printf("Enter the number of blocks already allocated: ");
scanf("%d", &p);
printf("Enter the blocks already allocated: ");
for (int i = 0; i < p; i++){
scanf("%d", &a);
pages[a] = 1;
}
recursivePart(pages);
return 0;
}

Assignment3:SSTF:8,14,23

#include<stdio.h>
#include<stdlib.h>
#define max 100

int a[max];
void sstf(int n,int head)
{
int total=0,count=0,min,dist,indx=0;
while(count<n)
{
min=9554;
for(int i=0;i<n;i++)
{
if(a[i]!=-1)
{
dist=abs(head-a[i]);
if(dist<=min)
{
min=dist;
indx=i;
}
}
}
printf("\n %d",a[indx]);
total=total+min;
head=a[indx];
a[indx]=-1;
count++;
}
printf("\nTotal head movements are : %d\n",total);
}

int main()
{
int n;
printf("Enter the size:\n");
scanf("%d",&n);
for(int i=0;i<n;i++)

{
printf("Enter the %d element\n",i+1);
scanf("%d",&a[i]);
}
int head;
printf("Enter the head:\n");
scanf("%d",&head);
int dir;
sstf(n,head);

Assignment4:MPI:

Slot1:3,10,12,2,28,21,29,22,24

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define ARRAY_SIZE 1000
int main(int argc, char *argv[])
{
int rank, size;
int i, sum = 0, total_sum = 0;
int local_array[ARRAY_SIZE], global_sum_array[ARRAY_SIZE];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Generate random numbers
srand(rank); // Use rank as seed for simplicity
for (i = 0; i < ARRAY_SIZE; i++)
{
local_array[i] = rand() % 100; // Generate numbers between 0
and 99
}
// Calculate local sum
for (i = 0; i < ARRAY_SIZE; i++)
{
sum += local_array[i];
}
// Gather all local sums to root process (rank 0)
MPI_Gather(&sum, 1, MPI_INT, global_sum_array, 1, MPI_INT, 0,
MPI_COMM_WORLD);
// Root process calculates the global sum
if (rank == 0)
{
for (i = 0; i < size; i++)
{
total_sum += global_sum_array[i];
}
printf("Global sum: %d\n", total_sum);
}
MPI_Finalize();
return 0;
}

Slot2:5,11,16,20,27,30

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define ARRAY_SIZE 1000
int main(int argc, char *argv[]) {
int rank, size;
int data[ARRAY_SIZE];
int local_min, local_max, global_min, global_max;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Generate random numbers
srand(rank); // Seed based on rank for different random numbers
for (int i = 0; i < ARRAY_SIZE; i++) {
data[i] = rand();
}
// Calculate local min and max
local_min = data[0];
local_max = data[0];
for (int i = 1; i < ARRAY_SIZE; i++) {
if (data[i] < local_min) {
local_min = data[i];
}
if (data[i] > local_max) {
local_max = data[i];
}
}
// Reduce local min and max to find global min and max
MPI_Reduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, 0,
MPI_COMM_WORLD);
MPI_Reduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, 0,
MPI_COMM_WORLD);
// Output result
if (rank == 0)
{
printf("Global minimum: %d\n", global_min);
printf("Global maximum: %d\n", global_max);
}
MPI_Finalize();
return 0;
}
Assignment2:Index allocation:17,18

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200
typedef struct dir
{
char fname[20];
int start;
struct dir *next;
}NODE;
NODE *first,*last;
int n,fb,bit[MAX];
void init()
{
int i;
printf("Enter total no.of disk blocks:");
scanf("%d",&n);
fb = n;
for(i=0;i<10;i++)
{
int k = rand()%n;
if(bit[k]!=-2)
{
bit[k]=-2;
fb--;
}
}
}
void show_bitvector()
{
int i;
for(i=0;i<n;i++)
printf("%d ",bit[i]);
printf("\n");
}
void show_dir()
{
NODE *p;
int i;
printf("File\tChain\n");
p = first;
while(p!=NULL)
{
printf("%s\t",p->fname);
i = p->start;
while(i!=-1)
{
printf("%d->",i);
i=bit[i];
}
printf("NULL\n");
p=p->next;
}
}
void create()
{
NODE *p;
char fname[20];
int i,j,nob;
printf("Enter file name:");
scanf("%s",fname);
printf("Enter no.of blocks:");
scanf("%d",&nob);
if(nob>fb)
{
printf("Failed to create file %s\n",fname);
return;
}
for(i=0;i<n;i++)
{
if(bit[i]==0) break;
}
p = (NODE*)malloc(sizeof(NODE));
strcpy(p->fname,fname);
p->start=i;
p->next=NULL;
if(first==NULL)
first=p;
else
last->next=p;
last=p;
fb-=nob;
j=i+1;
nob--;
while(nob>0)
{
if(bit[j]==0)
{
bit[i]=j;
i=j;
nob--;
}
j++;
}
bit[i]=-1;
printf("File %s created successully.\n",fname);
}
void indexalloc()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on
the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
}
void delete()
{
char fname[20];
NODE *p,*q;
int nob=0,i,j;
printf("Enter file name to be deleted:");
scanf("%s",fname);
p = q = first;
while(p!=NULL)
{
if(strcmp(p->fname,fname)==0)
break;
q=p;
p=p->next;
}
if(p==NULL)
{
printf("File %s not found.\n",fname);
return;
}
i = p->start;
while(i!=-1)
{
nob++;
j = i;
i = bit[i];
bit[j] = 0;
}
fb+=nob;
if(p==first)
first=first->next;
else if(p==last)
{
last=q;
last->next=NULL;
}
else
q->next = p->next;
free(p);
printf("File %s deleted successfully.\n",fname);
}
int main()
{
int ch;
init();
while(1)
{
printf("1.Show bit vector\n");
printf("2.Create new file\n");
printf("3.Show directory\n");
printf("4.Delete file\n");
printf("5.Index allocation\n");
printf("6.Exit\n");
printf("Enter your choice (1-6):");
scanf("%d",&ch);
switch(ch)
{
case 1:
show_bitvector();
break;
case 2:
create();
break;
case 3:
show_dir();
break;
case 4:
delete();
break;
case 5:
indexalloc();
break;
case 6:
exit(0);
}
}
return 0;
}

OR

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_BLOCKS 100
int disk[MAX_BLOCKS] = {0}; // 0 represents free block, 1 represents
allocated block
int indexBlock[MAX_BLOCKS] = {0}; // Index block to store addresses of
allocated blocks
void showBitVector(int n) {
printf("\nBit Vector (Disk Allocation):\n");
for (int i = 0; i < n; i++) {
printf("%d ", disk[i]);
}
printf("\n");
}
void showDirectory(int n) {
printf("\nDirectory Listing:\n");
for (int i = 0; i < n; i++) {
if (indexBlock[i] != -1) {
printf("File starting from block %d\n", i);
}
}
}
void deleteFile(int n) {
int start;
printf("\nEnter the starting block of the file to delete: ");
scanf("%d", &start);
if (start < 0 || start >= n || indexBlock[start] == -1) {
printf("\nInvalid starting block or the block is not allocated.\n");
return;
}
// Deallocate the blocks for the file
int current = indexBlock[start];
while (current != -1) {
int next = disk[current];
disk[current] = 0;
current = next;
}
indexBlock[start] = -1;
printf("\nFile deleted successfully.\n");
}
int main() {
int n;
printf("Enter the number of blocks on the disk: ");
scanf("%d", &n);
srand(time(NULL)); // Seed for random block allocation
// Randomly mark some blocks as allocated
for (int i = 0; i < n; i++) {
if (rand() % 2 == 1) {
disk[i] = 1;
indexBlock[i] = i; // Initialize index block with the block itself
} else {
indexBlock[i] = -1; // Initialize index block with -1 for free blocks
}
}
int choice;
do {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Show Directory\n");
printf("3. Delete File\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
showDirectory(n);
break;
case 3:
deleteFile(n);
break;
case 4:
printf("\nExiting the program.\n");
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}

You might also like