# computer hardware and netwoking question papers

Topics: Pointer, Void type, Return statement Pages: 8 (735 words) Published: January 31, 2014
﻿
Operating Systems

Q.1.Consider the following Snapshot of a System.

AllocationMaxAvailable
A B C DA B C DA B C D
P00 0 1 20 0 1 21 5 2 0
P11 0 0 01 7 5 0
P21 3 5 42 3 5 6
P30 6 3 20 6 5 2
P40 0 1 40 6 5 6

Use Banker’s algorithm.
a) What is the content of matrix need ?
b) Is the system in a safe state ?

Implement Banker’s algorithm using a C program.

Solution :
/*-------------Banker's algorithm---------------------------------*/ #include
#include
#include

#define MAX_PROC 10
#define MAX_RESO 10

#define TRUE 1
#define FALSE 0
void main()
{
int n; //Let n be the number of processes in the system int m;//Let m be the number of resource types
int available[MAX_RESO];//Number of available resources
//of each type
int max[MAX_PROC][MAX_RESO];//Maximum demand of each process int allocation[MAX_PROC][MAX_RESO]; //No. of resources of each type
//currently allocated to each process
int need[MAX_PROC][MAX_RESO]; //Remaining resource need of each process

void assign_available(int available[], int m);
void assign_max(int max[][MAX_RESO], int n, int m);
void assign_alloc(int allocation[][MAX_RESO], int n, int m); void cal_need(int need[][MAX_RESO], int max[][MAX_RESO], int alloc[][MAX_RESO], int n, int m); int safety(int available[], int max[][MAX_RESO], int alloc[][MAX_RESO], int need[][MAX_RESO], int n, int m);

clrscr();
printf("\nEnter no. of processes : ");
scanf("%d", &n);
if(n > MAX_PROC)
{
printf("\nNumber of processes cannot be greater than %d",MAX_PROC); exit(0);
}

printf("Enter no. of resource types: ");
scanf("%d", &m);
if(m > MAX_RESO)
{
printf("\nNumber of resources cannot be greater than %d",MAX_RESO); exit(0);
}

printf("\n-------------------------------------------------------------------"); assign_available(available,m);
printf("\n-------------------------------------------------------------------"); assign_max(max,n,m);
printf("\n-------------------------------------------------------------------"); assign_alloc(allocation,n,m);
printf("\n-------------------------------------------------------------------"); cal_need(need,max,allocation,n,m);
printf("\n-------------------------------------------------------------------");

if(safety(available,max,allocation,need,n,m)==TRUE)
printf("\nSystem is in a safe state.............");
else
printf("\nSystem is not in a safe state.............");
printf("\n-------------------------------------------------------------------");

}

/*------A vector of length m indicating number of available----------*/ /*------resources of each type---------------------------------------*/ void assign_available(int avail[], int m)
{
int i;
for(i=0;i 0)
{
printf("%d\t%d\n",n->PID,n->time);
}
else
free(n);
//printf("%d\t%d\n",n->PID,n->time);
}
}

Q 13. Assume there are n jobs arriving at the same time with different CPU Burst times. Write a program to execute these jobs in a first come first served manner.

#include
#include
#include

typedef struct node
{
int PID;
int time;
struct node *next;
}node;

typedef struct
{
node *front;
node *rear;
}Queue;

node* makeNode(int p, int t)
{
node *n;
n = (node*)malloc(sizeof(node));
n->PID = p;
n->time = t;
n->next = NULL;
return n;
}

Queue* makeQueue()
{
Queue *Q ;
Q = (Queue*)malloc(sizeof(Queue));
Q->front = Q->rear = NULL;
return Q;
}

{
if (Q->front == NULL)
Q->front = Q->rear = n;
else
{ Q->rear->next = n;
Q->rear=n;
}
}

node* delQueue(Queue *Q)
{
node *n, *t;
if (Q->front == NULL)
{
printf("Queue Empty");
return NULL;
}...