The document contains multiple C programming tasks related to system resource management and scheduling algorithms. It includes implementations for a menu-driven Banker's algorithm, disk scheduling using FCFS and SCAN algorithms, and a simulation of linked file allocation methods. Each task provides code snippets and outlines the required functionalities such as accepting input, displaying matrices, and calculating head movements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
8 views
Operating System 2 Practical Slips With Sol
The document contains multiple C programming tasks related to system resource management and scheduling algorithms. It includes implementations for a menu-driven Banker's algorithm, disk scheduling using FCFS and SCAN algorithms, and a simulation of linked file allocation methods. Each task provides code snippets and outlines the required functionalities such as accepting input, displaying matrices, and calculating head movements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 107
Slip:1
Q.1_) Write a C Menu driven Program to implement following functionality
a) Accept Available
b) Display Allocation, Max
c) Display the contents of need matrix
d) Display Available
Process! Allocation Max Available
A/|B{[cl[a[B[c[a[Bl{c
PO 2 3. |2|9|7] 5 3 | 3 | 2
PL 4 o|;o|)| 5) 2) 2
p |5|o0{/4]/1il/o0|4
p | 4]3 [3/4/44
pa [2/2t[4lels[s
Answer:
#include
int main()
{
int n,i,p[10],m,all[10][10],j,max{10][10],avail[10],need[10][10];
printf("\n Enter total number of processes:");
scanf("%d",8n);
printf("\n Enter total number of resource instances:");
scanf("%d",&m);
printf("\n Enter Allocation matrix:");
for(i=0;i
#include
int main()
t
int RQ[25],i,n, TotalHeadMov=0, initial;
printf("ENter the number of request:\n");
scanf("%d",&n);
print{("Enter the sequence of request:\n");
for(i=O;i
int mainQ)
{int
1n,i,p[10],maalloc{10][10},j,max{10][10],availf 10],need[ 10]{10],[10],ans{ 10],ind=0
printf("\n Enter total number of processes:");
scanf("%d",&n);
printf("\n Enter total number of resource instances:");
scanf("%d",&m);
printf("\n Enter Allocation matrix:");
for(i=O;iavail[j])
1
flag=1;
break;
}
}
}
}
if(flag—0)
1
ans[ind++]=i;
for(y-0;y",ans{il]);
print{("p%d" ans[n-1]):
}
printf("\n");
}
Q.2 Write an MPI program to calculate sum and average of randomly generated 1000
Answer:
#inckude
#include
#include
#include
#define ARRAY SIZE 1000
int main(int arge, char** argv)
t
int rank,size;
int array[ARRAY_SIZE];
int sum=
int local_sum=0;
MPI _Init(NULL,NULL);
MPI_Comm_rank(MPICOMM_WORLD,&rank);
MPI_Comm_size(MPl_COMM_WORLD,é&size);
stand(time(NULL)+rank);
for (int i=0;i
int main()
{
int n,i,p[10],m,all[10][10],j,max[10][10],avail[10],need[10][10];
printf("\n Enter total number of processes:");
scanf("%d",&n);
printf("\n Enter total number of resource instances:");
scanf("%d",&m);
printf("\n Enter Allocation matrix:");
for(i=O;i
#include
int main()
t
int RQ[100],i,j,n,TotalHeadMoment=0, initial,sizeymove;
printf("Enter the number of requests: ");
scanf("%d" gen);
printf("Enter the request sequence: ");
for(i-O;i=0:i-)
t
printf("Serving Request: %d\n",RQ{i):
TotalHeadMoment=TotalHeadMoment+ abs(RQ[iJ-initial);
initial-RQ[i;
}
TotalHeadMoment=TotalHeadMoment+abs(RQli+ 1-0);
for (i=index;i
#include
#include#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()
t
inti;
printf("Enter total no of disk blocks:");
scani"%d",&n);
fb=n;
int k=rand()%n;
iffbit[k]!=-2)
{
bit[k]=2;
fo-;
}
}
}
void show_bitvector()
{
int i;
for(i=O;ifname);
i=p->start;
while(i!=-1)
t
printf("%d->",i);
i-bitfi];
}
printf("NULL\n");
p=p>next;
}
}
void create()
t
NODE *p;
char fname[20];
int i,j,nob;
printf("Enter filename:
scanf('"%s",fname);
printf("Enter no of blocks:");
scanf("%d",&nob);
if(nob>n)
t
printf("'failed to create file %s\n" fname);
return;
}
for(i=0;i fname, fname);
p->stal
p->nexielse
while(nob>0)
1
if(bit{j]--0)
bit[iJ=-1;
printf("File %s created successfully\n",fname);
}
void delete()
{
char fname[20};
NODE *p,*q
int nob=0,i,j;
printf("Enter name to be deleted:");
scanf(""%s",fname);
p=q=first;
while(p!=NULL)
t
if(stremp(p->fname,fname)=0)
break;
}
if(»—=NULL)
f
{printf("File %s not found\n" fname);
return;
Fa
bit{j]-0;
}
else
{
q->next=p->next;
}
free(p);
printi("File %s deleted successfully\n" fname);
}
int main()
t
int ch;
init();
while(1)
t
printi(" | show bit vector\n");
printi("2.create new file\n");
printf{("3.show directory\n");
printf("4.delete file\n");
printf("5.exit\n");
printf("Enter your choice(1-5):\n");scanf("%d",&ch);
switch(ch)
t
case 1:show_bitvector();
break;
case 2:create();
break;
case 3:show_dir();
break;
case 4:delete();
break;
case S:exit(0);
}
}
return 0;
}
Q.2 Write a simulation program for disk scheduling using C-SCAN algorithm.
Accept total number of disk blocks, disk request string, and current head position
from the user. Display
the list of request in the order in which it is served. Also display the total number of
head moments.
80, 150, 60,135, 40, 35, 170
Starting Head Position: 70
Direction: Right
Answer:
#include
#include
int main()
t
int RQ[100],i,j,n, TotalHeadMoment=0 initial, size,move:
printf("Enter the Number of Requests : ");
scani("%d",&n);
printf("Enter the request sequence : ");for (i=O;i
int main)
4
int
2,i,p[10],malloc{10][10],j,max[ 10][10] availf 10],need{ 10][10),f{10],ans[ 10},ind=0
printf("\n Enter total number of processes:");
scanf("%d" &n);
printf("\n Enter total number of resource instances:");
scanf("%d" &m);
printf("\n Enter Allocation matrix:");
for(i=0;iavaillj))
{
flag=1;
break;
}
}
if(flag==0)
1
ans[ind++]=i;
for(y=O;y",ans[i]);
printf("p%d",ans[n-1]);
}
printf(’\n");
Q.2 Write a simulation program for disk scheduling using SCAN algorithm. Accept
total number of disk blocks, disk request string, and current head position from the
user. Display the list of request in the order in which it is served. Also display the
total number of head moments.
82, 170, 43, 140, 24, 16, 190
Starting Head Position: 50
Direction: Right
Answer:
#include
#include
int main()
t
int RQ[100},i,j.n, TotalHeadMoment=0, initial,size,move;
printf("Enter the number of requests: ");
scanf("%d",&n);
printf("Enter the request sequence: ");
for(i=0;i0;i-)
t
printf("Serving Request: %d\n",RQLi));
TotalHeadMoment=TotalHeadMoment+abs(RQ[il-initial);
initial-RQ[il;
}
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
for (i=index;i
#include
#include
#define MAX 200
typedef struct dir
t
char fname[20];
int start,length;
struct dir*next;
}NODE;
NODE *first,*last;
int n,fb,bit{ MAX];
void init()
t
int i;
print{("Enter total no of disk blocks:");
scanf("%d",&n);
fo=n;
for(i=0;i<10;i++)if(bit{i]!=1)
t
bit{i]=1;
fo
}
void show_bitvector()
{
int i;
for(i=O;ifname, p->start,p->length);
p=p>next;}
void create()
t
NODE *p;
char fname[20];
int i=0,j-0,nob, start;
printf("Enter filename:");
scanf("%s", fname);
printf("Enter no of blocks:");
seanf("%d",&nob);
if{nob>n)
1
printf("failed to create file %s\n" fname);
return;
}
for(i-0;ifname, fname);
p->start=i;
p->length=nob;
p->next=NULL;
if(first =NULL)
first=p;else
Jast->next=p;
last=p;
jit];
nob--;
while(nob>0)
t
if(bit{j]--0)
t
bitfi=j;
bit[i]=-1;
printf("File %s created successfullly\n" fname);
}
void delete()
t
char fhame[20];
NODE *p,*q;
int nob=0,i,j;
printf{("Enter name to be deleted:");
scanf(("%s" fname);
p=q=first;
while(p!=NULL)1
if(stremp(p>fname, fname)—0)
break;
oP:
p=p->next;
}
if(p—=NULL)
t
printf("File %s not found\n" fname);
return;
}
i=p->start;
while(i!
t
nob++;
1)
iti);
bit[j]=0:
}
fb+=nob;
if_p=first)
first=first->next;
else if(p==last)
t
last=q;
last->next=NULL;
}
elseq->next=p->next;
}
free(p);
printf("File %s deleted successfully\n",fname);
}
int main()
t
int ch;
init();
while(1)
t
printf("1.show bit vector\n");
printf("2.create new file\n");
printi("3. show directory\n");
printf("4.delete file\n");
printi("s.exit\n");
printf("Enter your choice(1-5):\n");
scanf("%d" ch):
switch(ch)
t
case 1:show_bitvector();
break;
case 2:create();
break;
case 3:show_dit();
break;case 4:delete();
break;
case S:exit(0);
return 0;
Q.2 Write a simulation program for disk scheduling using SSTF algorithm. Accept
total number of disk blocks, disk request string, and current head position from the
user, Display the list of request in the order in which it is served. Also display the
total number of head moments.
186, 89, 44, 70, 102, 22, $1, 124
Start Head Position: 70
Answer:
#include
#include
int main()
{
int RQ[25],in, TotalHeadMov=0, initial,count=0;
printf("ENter the number of request:\n");
scanf("%d",&n);
printf("Enter the sequence of request:\n");
for(i=0;i=diff)
TotalHeadMov=TotalHeadMov+min;
initial=RQfindex];
printi("%d\n",RQfindex));
RQfindex]=1000;
count++;
}
printi("Total Head Moment=%d", TotalHeadMov);
}
Slip 9:
Q.1. Consider the following snapshot of system, A, B,C, D is the resource type.
Proces| Allocation Max Available
s
A B Cc D |A |B jc [D A |B |{|C |D
PO 0 0 1 2 0 0 1 2 1 5 2 |0
Pl 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 i} 6 5 2
P4 0 0 1 4 i} 6 5 6
Using Resource Request algorithm to Check whether the current system is in safe
state
or not
Answer:
#includeint main()
{
int
n,i,p[10],m,alloc[10][10],j,max[10][10],availf 10],need[10][10], [1 0],ans{10],ind=0
printf("\n Enter total number of processes:");
scanf("%d",&n);
printf("\n Enter total number of resource instances:");
scanf("%d",&m);
printf("\n Enter Allocation matrix:");
for(i=0;iavaillj))
{
flag=1;
break;
}
}
if(flag—0)
ans[ind++]=i:
for(y=0;y",ans[i));
printf("p%d" ans[n-1]);
}
printf('\n");
}
Q.2 Write a simulation program for disk scheduling using LOOK algorithm. Accept
total number of disk blocks, disk request string, and current head position from the
user. Display the list of request in the order in which it is served. Also display the
total number of head
moments. Us]
176, 79, 34, 60, 92, 11, 41, 114
Starting Head Position: 65
Direction: Left
Answer:
#include
#include
int main()
{
int RQ[100],i,j,n,thm=0, initial,sizezmove;
printf{("Enter number of request\n");
scanf("%d",8n);
printf("Enter the request sequence\n");
for(i-0;i—0;i--)
f
{
thm=thm+abs(RQfif-initial);
initial=RQ[i}:
else
for(i=index-1;i>=tthm=thm+abs(RQ[i}-initial);
initial-RQ[i;
for(i=index;i
#include
#include
#include
#define ARRAY SIZE 1000
int main(int arge, char** argv)int rank,size;
int array[ARRAY_SIZE];
int sum=0;
int local_sum=0;
MPI Init(NULL.NULL);
MPI_Comm_rank(MPIL_COMM_WORLD,&rank);
MPI_Comm_size(MPL_COMM_WORLD, &size);
srand(time(NULL)+rank);
for (int i=0;i
#include
int mainQ)
{
int RQ[100],i,j,n, TotalHeadMoment=0, initial,size,move;
printf("Enter the Number of Requests : ");
scanf("%d",&n);
printf(""Enter the request sequence : ");
for (i=O;i
t
printf('Servicing request : d\n" RQ[i));
TotalHeadMoment=TotalHeadMoment+abs(RQ{it1]-0);
TotalHeadMoment=abs(size-1-0);
initial=size-1;for (i=n-1;i>=0;i--)
1
printf("Servicing request : %d\n",RQ[i));
TotalHeadMoment=TotalHeadMoment+abs(RQ[i}-initial);
initial-RQ[i};
printf("\nTotal head movement is %d\n",TotalHeadMoment);
return 0;
Slip 13:
Q.1 Write a C program to simulate Banker’s algorithm for the purpose of deadlock
avoidance. The following snapshot of system, A.B, Cand D are the resource type.
Proces| Allocation Max Available
8
A |B c JA |B [Cc A [|B {Cc
PO 0 1 Oo |0 jo Jo 0 |0 0
Pl 2 0 oOo |2 |o {2
P2 3 0 3. j|0 |0 |0
P3 2 1 1 1 0 |0
P4 0 0 2 |0 |o {2
a) Calculate and display the content of need matrix?
b)Is the system in safe state? If display the safe sequence.Answer:
#include
int
n,i,p[10],malloc{10][10]j,max[10][10] availl 10],need{ 10][10],[10],ans[10],ind
=0,k;
void accept()
t
printf("\n Enter total number of processes:");
scanf("%d" gn);
printf("\n Enter total number of resource instances:");
scanf("%d" gm);
print{("\n Enter Allocation matrix:");
for(i=0;iavaillj])
t
flag=1;
break;
}
}
}
if(flag—=0)
f
{ans[ind++]=i;
for(
avail[y]+=alloc{i][y];
flilFls
}
}
}
}
int flag=l;
for(i=0;i" ans[i));
print{("p%d",ans[n-1]);
printf("\n");
}
int main()
{
accept();
print();
bankers();
int pno,req[ 10];
printf("Enter requesting process no:\n");
scanf("%d",&pno);
printf("Enter resource request of process p%d:\n",pno-1);
for(j=0;jneed[pno][j])
break;
}
ifm)
4
for(j=0;javail{j])
break;
for(j=0;j
#include
int main()
1
int RQ[100],i,j,n, TotalHeadMoment=0 initial, size,move;
printf("Enter the number of requests: ");
scanf("%d",&n);
printf("Enter the request sequence: ");
for(i=O;i=0:i--)
t
printf("'Serving Request: %d\n",RQ[i]);}
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial-RQ[iJ;
}
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial=
for (i=index;i
#include
4include
#define MAX 200typedef struct dir
{
char fname[20];
int start,length;
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);
fo=n;
for(i=0;i<10:i++)
{
if(bit(i]!=1)
{
bit{i]=1;
void show_bitvector()
t
int i;
for(i=0;ifname,p->start,p->length);
p=p>next;
}
void create()
t
NODE *p;
char fname[20];
int i=0,j=0,nob,start;
printi("Enter filename:");
scanf(("%s" fname);
printi("Enter no of blocks:");
scanf("%d" ,&nob);
if(nob>n)
{printf("'failed to create file %s\n",fname);
return;
}
for(i=O;ifname,fname);
p->start=i;
p-length=nob;
p->next=NULL;
if(firs—=NULL)
first=p;
else
last->next=p;
last=p;
fb=nob;
iets
nob--;
while(nob>0)os
}
bit{iJ=1;
printf("File %s created successfully\n",fname);
}
void delete()
{
char fname[20];
NODE *p,*q;
int nob=0,i,j;
printf("Enter name to be deleted:");
scanf(""%s",fname);
p=q=first;
while(p!=NULL)
{
if(stremp(p->fname,fname)—=0)
break;
oP:
p=p->next;
}
if(p==NULL)
{
printf("File %s not found\n",fname);
return;
}
3
i=p->start;
while(i!=-1)1
nob++;
Fis
iebitfi);
bitLj]=0;
}
fo+=nob;
if(p=
first=first->next;
first)
else if(p==last)
1
last=q;
last->next=-NULL;
}
else
t
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("S.exit\n");
printf("Enter your choice(1-5):\n"
scanf("%d",&ch);
switch(ch)
1
case 1:show_bitvector();
break;
case 2:create();
break;
case 3:show_dir();
break;
case 4:delete();
break;
case S:exit(0);
return 0;
Q.2 Write a simulation program for disk scheduling using SSTF algorithm. Accept
total number of disk blocks, disk request string, and current head position from the
user. Display the list of request in the order in which it is served. Also display the
total number of head moments.
55, 58, 39, 18, 90, 160, 150, 38, 184
Start Head Position: 50Answer:
#include
#include
int main()
{
int RQ(25],i,n, TotalHeadMov=0, initial,count=0;
printf("ENter the number of request:\n");
scanf("%d",&n);
printf("Enter the sequence of request:\n");
for(i=O;i
a[sfelefslola
=lelslelelsta
ale[slel-fato
aly[elolsfale
alalololslala
wlefel—lelala
sfo/s/elesto
PS
a) Calculate and display the content of need matrix?
b)Is the system in safe state? If display the safe sequence.
Answer:
#include
int
n,i,p[10],m,alloc{10][10],j,max[10][10],availf 10],need[10][10],4[10],ans[ 10],ind=
0,k;
void accept()
{
printf("\n Enter total number of processes:");
scani("%d",&n);
printf("\n Enter total number of resource instances:
scanf("%d",8m);
printf("\n Enter Allocation matrix:");
for(i=0;iavailfj))
{
flag=1;
break;
}
}
if(flag—0)
ans[ind++]=i;
for(y—O;y",ans[i);
printf("p%d"ans[n-1]);
}
}
printf("\n");
}
}
int main()
{
accept();
print();
bankers();
int pno,req[ 10);
printf{("Enter requesting process no:\n");
scanf("%d",g-pno);
printf("Enter resource request of process p%d:\n",pno-1);
for(=0;jneed[pno][j])
break;
}
itm)
f
{
for(j=0;javaillj])
break;
}
ifj—m)
[
{
for(j=0;j
#include
int main()
t
int RQ[100],i,j,n, TotalHeadMoment=0 initial, size,move:
printi("Enter the Number of Requests : ");
scani("%d",&n);
printf("Enter the request sequence : ");
for (i=0:i=0;i--)
{
printf(""Servicing request : %d\n" ,RQ[i]):
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
TotalHeadMoment+=abs(size-1-0);
initial=size-1;
}
for (i=n-1;i>=0;i--)
‘
printf("'Servicing request : %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial-RQ[iJ;
}
}
printf("\nTotal head movement is %d\n",TotalHeadMoment);
return 0;
}
Slip 21:
Q.1 Writea simulation program for disk scheduling using FCFS algorithm. Accept
total number of disk blocks, disk request string, and current head position from the
user. Display the list of request in the order in which it is served. Also display the
total number of head moments.
55, 58, 39, 18, 90, 160, 150, 38, 184
Start Head Position: 50Answer:
#include
#include
int main()
t
int RQ(25],i,n, TotalHeadMov=0, initial;
printf("ENter the number of request:\n");
scanf("%d",&n);
printf("Enter the sequence of request:\n");
for(i=O;i
#include
#include
#includedefine ARRAY_SIZE 1000
int main(int arge, char** argv)
{
int rank,size;
int array[ARRAY_SIZE];
int sum=0;
int local_sum=0;
MPI Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&:rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
stand(time(NULL)+rank);
for (int i-0;i
int main)
{ .
int
1n,i,p[10],malloc{10][10],j,max{10][10],avail{ 10] need 10][10],f[10],ans{10],ind
=O:
printf("\n Enter total number of processes:");
scanf("%d",&n);
printi("\n Enter total number of resource instances:");
scanf("%d",&m);
printf("\n Enter Allocation matri:
for(i=O;iavaillj))
{
flag=1;
break;
}
}
if(flag—=0)
{
ans[ind++]=i:
for(y=0;y" ans[i]);
printi("p%d" ans[a-1]);
}
printf("\n");
}
Q2. Write a simulation program for disk scheduling using SSTF algorithm.
Accept total number of disk blocks, disk request string, and current head position
from the user. Display the list of request in the order in which it is served. Also
display the total number of head moments.
24, 90, 133, 43, 188, 70, 37,
55 Start Head Position: 58
Answer:
#include
#include
int main()
tint
RQ[25],i,n,TotalHeadMov
=0, initial,count=0;
printf("ENter the number
of request:\n");
scanf("%d",&n);
printf("Enter the sequence
of request:\n");
for(i=O;i=diff)TotalHeadMov=TotalHead
Mov+min;
initial=RQ[index];
printf("%6d\n" RQlindex]);
RQ[index}=1000;
count++;
}
printf("Total Head
Moment=%d",TotalHeadM
ov);
}
Slip 25:
Q.1 Write a simulation program for disk scheduling using LOOK algorithm.
Accept total number of disk blocks, disk request string, and current head position
from the user. Display the list of request in the order in which it is served. Also
display the total number of head moments.
86, 147, 91, 170, 95, 130, 102, 70Starting Head position= 125
Direction: User Defined
Answer:
#include
#include
int main()
{
int RQ[100},i,.n,thm=90,initial,size, move;
printf("Enter number of request\n");
seanfi"%d" én);
printf{"Enter the request sequence\n");
for(i=O;i=0;i-)
{
thm=thm+abs(RQ[i}-initial);
initial-RQ[i);
for(i-index:icnsi++)
t
thm=thm+abs(RQ[i)-initial):
initial-RQ[i);
,
printf("Total head moment=%d" thm);
Q.2. Write a program to simulate Linked file allocation method. Assume disk
withn number of blocks. Give value of n as input. Randomly mark some block as
allocated and accordingly maintain the list of free blocks Write menu driver
program with menu options as mentioned below and implement each option.
* Show Bit Vector
* Create New File+ Show Directory
Exit
Answer:
#include
#include
#include
#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++)
f
{
int k=rand(%n;if(bit{k]!~2)
t
bit[k]=-2;
fo--;
}
}
}
void show_bitvector()
t
int i;
for(i=O;ifname);
i=p->start;
while(i!=-1)1
printf("%d->".i);
i-bitfi];
}
printf("NULL\n");
p=p->next;
}
void create()
{
NODE *p;
char fname[20};
int i,j,nob;
printf("Enter filename:");
scanf('"%s",fname);
printf("Enter no of
blocks:");
scanf("%d" nob);
if(nob>n)
{
printf("failed to create
file %s\n", fname);
return;
}
for(i=0;ifname,fname);
p->stai
‘AS
p->next=NULL;
if(firs-—NULL)
first=p;
else
last->next=p;
while(nob>0)
{
iffbit[jJ==0)
{
bithili;
5
nob--;mS
}
bit[iJ=-1;
printf("File %s_ created
successfully\n" fname);
}
void delete()
t
char fname[20};
NODE *p,*q;
int nob=0,i,
printf("Enter name to be
deleted:");
scanf('"%s",fname);
Pp
while(p!=NULL)
f
{
=first;
if(stremp(p-
>fname,fname)—=0)
break;
ce
p=p->next;
}
if(~—=NULL)
{printf("File %s not
found\n",fname);
return;
}
iep->start;
i-bit[i;
bitfj]=0;
}
fb+=nob;
if{p—first)
first=first->next;
else if(p==last)
{
last=q;
last->next=NULL:
}
else
t
q->next=p->next;
}
free(p):printf("File %s deleted
successfully\n", fname);
}
int main()
t
int ch;
init;
while(1)
t
print{("1.show bit
vector\n");
printf("2.create new
file\n");
printf("3.show
directory\n");
printf("4.delete file\n");
printi("s.exit\n");
printf("Enter your
choice(1-5):\n");
scanf("%d" ch);
switch(ch)
t
case
1:show_bitvector();
break;case 2:create();
break;
case 3:show_dir();
break;
case 4:delete();
break;
case S:exit(0);
}
return 0;
Slip 26:
Q.1 Write a C program to simulate Banker’s algorithm for the purpose
of deadlock avoidance. Consider the following snapshot of system, A,
B, C and D is the resource type.
Proces| Allocation Max Available
8
A |B |c [D [A {|B |c [D_|A |B >
PO 0 0 1 2 |0 |0 1 2 1 5 |2 |0
Pl 1 0 0 0 1 7 (5 [0
P2 1 3 5 4 |2 3 5 6
P3 0 6 3 2 |0 |6 [5 [2
P4 i} 0 1 4 |0 |6 [5 6
a)Calculate and display the content of need matrix?
b)Is the system in safe state? If display the safe sequence.
Answer:#include
int main()
{
int
n,i,p[10],m,alloc[10][10],j,max[10][10],avail[ 10],need[ 10][10],f[10],ans{ 10],ind=0
ok;
printf("\n Enter total number of processes:");
scanf("%d",&n);
printf("\n Enter total number of resource instances:");
scanf("%d",&m);
printf("\n Enter Allocation matrix:
for(i=0;iavail[j))
{
flag=1;
break;
}
}
if(flag—0)
t
ans[ind++]=i;
for(y=0:y" ans{i]):
printi("p%d" ans[n-1]);
}
printf("\n");
}
Q.2 Writea simulation program for disk scheduling using FCFS algorithm. Accept
total number of disk blocks, disk request string, and current head position from the
user. Display the list of request in the order in which it is served. Also display the
total number of head moments.
56, 59, 40, 19, 91, 161, 151, 39, 185
Start Head Position: 48
Answer:
#include
#include
int main()
t
int
RQ[25],i.n,TotalHeadMov=
0, initial;
printf("ENter the number of
request:\n");
scanf("%d",&n);printf("Enter the sequence
of request:\n");
for(i=O;i
#include
int main()
t
int
queue[20],n,head,i,j,k,seek=0,max.diff,temp, queue 1[20],queue2[20},temp 1=0, temp
2-0;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter size of queue request\n");
scanf("%d",8n);
printf("Enter queue of disk positions to be read\n");
for(i=1;i<=n;i++)
t
scanf("%d",&temp);if(temp>=head)
t
queuel [temp] ]=temp;
templ++;
}
else
t
queue2[temp2}=temp;
temp2++;
}
for(i-0;iqueuel [j])
t
temp=queuel [i];
queuel [i]-queuel [j];
queuel [j]temp:
i-queue2[j))
{
temp=queue?{i];
queue2[i]=queue2{j];
queue2([j]=temp;
}
for(i=1,j=0;j
#include
#inchude
#include
#define ARRAY_SIZE 1000
int main(int argc, char**
argv)
{
int rank,size;
intarray[ARRAY_SIZE];
int sum=0;
int local_sum=0;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_CO
MM_WORLD,&rank);MPI_Comm size(MPLCO
MM_WORLD, &size);
srand(time(NULL)+rank);
for (int
i=0;i
#include
#include
#include
#define ARRAY _SIZE 1000
int main(int arge, char** argv)
{
int rank,size;
int array[ARRAY_SIZE];
int sum=0;
int local_sum=0;
MPI Init(NULL,NULL);
MPI_Comm_rank(MPL COMM WORLD, &rank);MPI_Comm size(MPI_COMM_WORLD,&size);
srand(time(NULL)+rank);
for (int i=0;is ARRAY _SIZE;i++)
t
array[iJ=rand()%100;
}
3
for (int i=rank;i#include
int main()
t
int RQ(100},i,j.n,thm=0, initial curr,move,m:
printf(""Enter number of request\n");
scanf("%d",&n);
printf("Enter the request sequence\n");
forti=0:i<
t
scanf("%d",&RQ[i);
}
printf("Enter initial head position\n");
sit+)
scanf("%d", &initial);
//printf{("Enter total disk size\n");
/iscani("%d" &size);
printf("Enter the head moment direction for high 1 and for low 0\n");
scanf("%d",Smove);
for(i=O;iinitial)
{
m=i;
break;
}
curr=initial;
if(move==1)
t
for(j=ij=0;j--)
t
thm=thm+abs(RQIj}-curr);
curr=-RQ[j]:
}
curr=RQ[n-1];
for(j=n-1:j>=m;j--)
t
thm=thmtabs(RQ[j]-curr);
curr=RQ{[j]:
}
printf("Total head moment=%d",thm);