0% found this document useful (0 votes)
568 views107 pages

Abc

The document contains two questions related to disk scheduling algorithms. For the first question, the document provides sample input of disk request blocks and asks to simulate the SCAN disk scheduling algorithm. It asks to display the served requests order and total head movements. The second question asks to implement a menu driven Banker's algorithm to accept allocation and max values from the user. It provides a sample process allocation snapshot and asks to find and display the need matrix and available resources.

Uploaded by

Rishi Gai
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)
568 views107 pages

Abc

The document contains two questions related to disk scheduling algorithms. For the first question, the document provides sample input of disk request blocks and asks to simulate the SCAN disk scheduling algorithm. It asks to display the served requests order and total head movements. The second question asks to implement a menu driven Banker's algorithm to accept allocation and max values from the user. It provides a sample process allocation snapshot and asks to find and display the need matrix and available resources.

Uploaded by

Rishi Gai
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/ 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 C A B C A B C
P0 2 3 2 9 7 5 3 3 2
P1 4 0 0 5 2 2
P2 5 0 4 1 0 4
P3 4 3 3 4 4 4
P4 2 2 4 6 5 5

Answer:
#include<stdio.h>
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=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&all[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",all[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-all[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
}

Q.2 Write a 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

Answer:
#include<stdio.h>
#include<stdlib.h>
int main()
{
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=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head position:\n");
scanf("%d",&initial);
printf("\n\nSequence of request:\n");
for(i=0;i<n;i++)
{
TotalHeadMov=TotalHeadMov+abs(RQ[i]-initial);

printf("%d\n",RQ[i]);
initial=RQ[i];
}
printf("Total Head Moment=%d",TotalHeadMov);
}

Slip: 3

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.
Process Allocation Max Available
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 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 0 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<stdio.h>
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
,k;
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;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
for(k=0;k<n;k++)
{
f[k]=0;
}
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
int y=0;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(f[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0)
{
ans[ind++]=i;
for(y=0;y<m;y++)
avail[y]+=alloc[i][y];
f[i]=1;
}
}
}
}
int flag=1;
for(i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("\n The following system is not safe");
break;
}
}
if(flag==1)
{
printf("\n Following system is safe\n\n Safe sequence are:");
for(i=0;i<n-1;i++)
printf("p%d->",ans[i]);
printf("p%d",ans[n-1]);
}
printf("\n");
}

Q.2 Write an MPI program to calculate sum and average of randomly generated 1000

Answer:
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ARRAY_SIZE 1000

int main(int argc, 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);

srand(time(NULL)+rank);
for (int i=0;i<ARRAY_SIZE;i++)
{
array[i]=rand()%100;
}
for (int i=rank;i<ARRAY_SIZE;i+=size)
{
local_sum+=array[i];
}

MPI_REDUCE(&local_sum,&sum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WOR
LD);

if (rank==0)
{
printf("Sum of %d random numbers : %d\n",ARRAY_SIZE,sum);
}
MPI_Finalize();
return 0;
}

Slip 4:

Q.1 Implement the Menu driven Banker's algorithm for accepting Allocation,
Max from user.
a) Accept Available
b) Display Allocation, Max
c) Find Need and display It,
d) Display Available
Consider the system with 3 resources types A,B, and C with 7,2,6 instances
respectively. Consider the following snapshot:
Process Allocation Request
A B C A B C
P0 0 1 0 0 0 0
P1 4 0 0 5 2 2
P2 5 0 4 1 0 4
P3 4 3 3 4 4 4
P4 2 2 4 6 5 5

Answer:
#include<stdio.h>
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=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&all[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",all[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-all[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
}
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. 86, 147, 91, 170, 95, 130, 102, 70
Starting Head position= 125
Direction: Left

Answer:

#include<stdio.h>
#include<stdlib.h>

int main()
{
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;i<n;i++)
scanf("%d",&RQ[i]);

printf("Enter initial head position: ");


scanf("%d",&initial);

printf("Enter total disk size: ");


scanf("%d",&size);

printf("Enter the head movement direction for high 1 and for low 0: ");
scanf("%d",&move);

for (i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}

int index;
for (i=0;i<n;i++)
{
if (initial<RQ[i])
{
index=i;
break;
}
}

if (move==1)
{
for (i=index;i<n;i++)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial=size-1;
for (i=index-1;i>=0;i--)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for (i=index-1;i>=0;i--)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial=0;
for (i=index;i<n;i++)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}

printf("Total head movement is %d",TotalHeadMoment);


return 0;
}

Slip6:

Q.1 Write a program to simulate Linked file allocation method. Assume disk with
n 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<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 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;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 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(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.exit\n");
printf("Enter your choice(1-5):\n");
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: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<stdio.h>
#include<stdlib.h>

int main()
{
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;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position : ");
scanf("%d",&initial);
printf("Enter total disk size : ");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0 : ");
scanf("%d",&move);
for (i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}

int index;
for (i=0;i<n;i++)
{
if (initial<RQ[i])
{
index=i;
break;
}
}

if (move==1)
{
for (i=index;i<n;i++)
{
printf("Servicing request : %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Servicing request : %d\n",size);
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for (i=0;i<index;i++)
{
printf("Servicing request : %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for (i=index-1;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[i];
}
}

printf("\nTotal head movement is %d\n",TotalHeadMoment);


return 0;
}

Slip 7:
Q.1 Consider the following snapshot of the system.
Proces Allocation Max Available
s
A B C D A B C D A B C D
P0 2 0 0 1 4 2 1 2 3 3 2 1
P1 3 1 2 1 5 2 5 2
P2 2 1 0 3 2 3 1 6
P3 1 3 1 2 1 4 2 4
P4 1 4 3 2 3 6 6 5
Using Resource –Request algorithm to Check whether the current system is in safe
state
or not
Answer:
#include<stdio.h>
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
,k;
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;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
for(k=0;k<n;k++)
{
f[k]=0;
}
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
int y=0;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(f[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0)
{
ans[ind++]=i;
for(y=0;y<m;y++)
avail[y]+=alloc[i][y];
f[i]=1;
}
}
}
}
int flag=1;
for(i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("\n The following system is not safe");
break;
}
}
if(flag==1)
{
printf("\n Following system is safe\n\n Safe sequence are:");
for(i=0;i<n-1;i++)
printf("p%d->",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<stdio.h>
#include<stdlib.h>

int main()
{
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;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position: ");
scanf("%d",&initial);

printf("Enter total disk size: ");


scanf("%d",&size);

printf("Enter the head movement direction for high 1 and for low 0: ");
scanf("%d",&move);

for (i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}

int index;
for (i=0;i<n;i++)
{
if (initial<RQ[i])
{
index=i;
break;
}
}

if (move==1)
{
for (i=index;i<n;i++)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial=size-1;
for (i=index-1;i>=0;i--)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}

else
{
for (i=index-1;i>=0;i--)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial=0;
for (i=index;i<n;i++)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}

printf("Total head movement is %d",TotalHeadMoment);


return 0;
}
Slip8:

Q.1 Write a program to simulate Contiguous file allocation method. Assume disk
with n 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 above and implement each option.
• Show Bit Vector
• Create New File
• Show Directory
• Exit
Answer:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200

typedef 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);
fb=n;
for(i=0;i<10;i++)
{

if(bit[i]!=1)
{
bit[i]=1;
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\tstart\tlength\n");
p=first;
while(p!=NULL)
{
printf("%s\t%d\t%d\n",p->fname,p->start,p->length);

p=p->next;
}
}
void create()
{
NODE *p;
char fname[20];
int i=0,j=0,nob,start;
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;i<n;i++)
{
if(bit[i]==0)
break;
}
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->fname,fname);
p->start=i;
p->length=nob;
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 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(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.exit\n");
printf("Enter your choice(1-5):\n");
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: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, 51, 124
Start Head Position: 70

Answer:
#include<stdio.h>
#include<stdlib.h>
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=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head position:\n");
scanf("%d",&initial);
printf("\n\nSequence of request:\n");
while(count!=n)
{
int min=1000,diff,index;
for(i=0;i<n;i++)
{
diff=abs(RQ[i]-initial);
if(min>=diff)
{
min=diff;
index=i;
}
}

TotalHeadMov=TotalHeadMov+min;
initial=RQ[index];
printf("%d\n",RQ[index]);
RQ[index]=1000;
count++;
}
printf("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 C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 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 0 0 1 4 0 6 5 6
Using Resource –Request algorithm to Check whether the current system is in safe
state
or not .

Answer:
#include<stdio.h>
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
,k;
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;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
for(k=0;k<n;k++)
{
f[k]=0;
}
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
int y=0;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(f[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0)
{
ans[ind++]=i;
for(y=0;y<m;y++)
avail[y]+=alloc[i][y];
f[i]=1;
}
}
}
}
int flag=1;
for(i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("\n The following system is not safe");
break;
}
}
if(flag==1)
{
printf("\n Following system is safe\n\n Safe sequence are:");
for(i=0;i<n-1;i++)
printf("p%d->",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. [15]
176, 79, 34, 60, 92, 11, 41, 114
Starting Head Position: 65
Direction: Left
Answer:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,thm=0,initial,size,move;
printf("Enter number of request\n");
scanf("%d",&n);
printf("Enter the request sequence\n");
for(i=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head moment direction for high 1 and for low 0\n");
scanf("%d",&move);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
if(move==1)
{
for(i=index;i<n;i++)
{
thm=thm+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=index-1;i>=0;i--)
{
thm=thm+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for(i=index-1;i>=0;i--)
{
thm=thm+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=index;i<n;i++)
{
thm=thm+abs(RQ[i]-initial);
initial=RQ[i];
}
}
printf("Total head moment=%d",thm);

Slip 10:
Q.1 Write an MPI program to calculate sum and average of randomly generated 1000
numbers (stored in array) on a cluster
Answer:
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ARRAY_SIZE 1000

int main(int argc, 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);

srand(time(NULL)+rank);
for (int i=0;i<ARRAY_SIZE;i++)
{
array[i]=rand()%100;
}

for (int i=rank;i<ARRAY_SIZE;i+=size)


{
local_sum+=array[i];
}

MPI_REDUCE(&local_sum,&sum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WO
RLD);

if (rank==0)
{
printf("Sum of %d random numbers : %d\n",ARRAY_SIZE,sum);
}
MPI_Finalize();
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.
33, 99, 142, 52, 197, 79, 46, 65
Start Head Position: 72
Direction: Left
Answer:
#include<stdio.h>
#include<stdlib.h>

int main()
{
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;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position : ");
scanf("%d",&initial);
printf("Enter total disk size : ");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0 : ");
scanf("%d",&move);
for (i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}

int index;
for (i=0;i<n;i++)
{
if (initial<RQ[i])
{
index=i;
break;
}
}

if (move==1)
{
for (i=index;i<n;i++)
{
printf("Servicing request : %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Servicing request : %d\n",size);
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for (i=0;i<index;i++)
{
printf("Servicing request : %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for (i=index-1;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[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, C and D are the resource type.
Proces Allocation Max Available
s
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
a) Calculate and display the content of need matrix?
b) Is the system in safe state? If display the safe sequence.
Answer:
#include<stdio.h>
int
n,i,p[10],m,alloc[10][10],j,max[10][10],avail[10],need[10][10],f[10],ans[10],ind
=0,k;
void accept()
{
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;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
for(k=0;k<n;k++)
{
f[k]=0;
}
int need[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
}
void print()
{
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
}
void bankers()
{
int y=0;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(f[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0)
{
ans[ind++]=i;
for(y=0;y<m;y++)
avail[y]+=alloc[i][y];
f[i]=1;
}
}
}
}
int flag=1;
for(i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("\n The following system is not safe");
break;
}
}
if(flag==1)
{
printf("\n Request granted immediately\n\n Safe sequence are:");
for(i=0;i<n-1;i++)
printf("p%d->",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",&pno);
printf("Enter resource request of process p%d:\n",pno-1);
for(j=0;j<m;j++)
{
scanf("%d",&req[j]);
}
for(j=0;j<m;j++)
{
if(req[j]>need[pno][j])
break;
}
if(j==m)
{
for(j=0;j<m;j++)
{
if(req[j]>avail[j])
break;
}
if(j==m)
{
for(j=0;j<m;j++)
{
avail[j]-=req[j];
alloc[pno][j]+=req[j];
need[pno][j]-=req[j];
print();
bankers();
}
}
}
else
{
printf(" Request of process p%d cannot granted immediately\n",pno-1);
}
}

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.
176, 79, 34, 60, 92, 11, 41, 114
Starting Head Position: 65
Direction: Left
Answer:
#include<stdio.h>
#include<stdlib.h>

int main()
{
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;i<n;i++)
scanf("%d",&RQ[i]);

printf("Enter initial head position: ");


scanf("%d",&initial);

printf("Enter total disk size: ");


scanf("%d",&size);

printf("Enter the head movement direction for high 1 and for low 0: ");
scanf("%d",&move);

for (i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}

int index;
for (i=0;i<n;i++)
{
if (initial<RQ[i])
{
index=i;
break;
}
}

if (move==1)
{
for (i=index;i<n;i++)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial=size-1;
for (i=index-1;i>=0;i--)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}

else
{
for (i=index-1;i>=0;i--)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial=0;
for (i=index;i<n;i++)
{
printf("Serving Request: %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}

printf("Total head movement is %d",TotalHeadMoment);


return 0;
}

Slip 14:

Q.1 Write a program to simulate Sequential (Contiguous) file allocation method.


Assume disk with n 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
• Show Directory
• Delete File
• Exit
Answer:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200
typedef 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);
fb=n;
for(i=0;i<10;i++)
{

if(bit[i]!=1)
{
bit[i]=1;
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\tstart\tlength\n");
p=first;
while(p!=NULL)
{
printf("%s\t%d\t%d\n",p->fname,p->start,p->length);

p=p->next;

}
}
void create()
{
NODE *p;
char fname[20];
int i=0,j=0,nob,start;
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;i<n;i++)
{
if(bit[i]==0)
break;
}
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->fname,fname);
p->start=i;
p->length=nob;
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 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(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.exit\n");
printf("Enter your choice(1-5):\n");
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: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: 50
Answer:
#include<stdio.h>
#include<stdlib.h>
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=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head position:\n");
scanf("%d",&initial);
printf("\n\nSequence of request:\n");
while(count!=n)
{
int min=1000,diff,index;
for(i=0;i<n;i++)
{
diff=abs(RQ[i]-initial);
if(min>=diff)
{
min=diff;
index=i;
}
}

TotalHeadMov=TotalHeadMov+min;
initial=RQ[index];
printf("%d\n",RQ[index]);
RQ[index]=1000;
count++;
}
printf("Total Head Moment=%d",TotalHeadMov);
}
Slip 19:

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
s
A B C D A B C D A B C D
P0 0 3 2 4 6 5 4 4 3 4 4 2
P1 1 2 0 1 4 4 4 4
P2 0 0 0 0 0 0 1 2
P3 3 3 2 2 3 9 3 4
P4 1 4 3 2 2 5 3 3
P5 2 4 1 4 4 6 3 4
a) Calculate and display the content of need matrix?
b) Is the system in safe state? If display the safe sequence.

Answer:
#include<stdio.h>
int
n,i,p[10],m,alloc[10][10],j,max[10][10],avail[10],need[10][10],f[10],ans[10],ind=
0,k;
void accept()
{
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;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
for(k=0;k<n;k++)
{
f[k]=0;
}
int need[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
}
void print()
{
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
}
void bankers()
{
int y=0;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(f[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0)
{
ans[ind++]=i;
for(y=0;y<m;y++)
avail[y]+=alloc[i][y];
f[i]=1;
}
}
}
}
int flag=1;
for(i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("\n The following system is not safe");
break;
}
}
if(flag==1)
{
printf("\n Request granted immediately\n\n Safe sequence are:");
for(i=0;i<n-1;i++)
printf("p%d->",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",&pno);
printf("Enter resource request of process p%d:\n",pno-1);
for(j=0;j<m;j++)
{
scanf("%d",&req[j]);
}
for(j=0;j<m;j++)
{
if(req[j]>need[pno][j])
break;
}
if(j==m)
{
for(j=0;j<m;j++)
{
if(req[j]>avail[j])
break;
}
if(j==m)
{
for(j=0;j<m;j++)
{
avail[j]-=req[j];
alloc[pno][j]+=req[j];
need[pno][j]-=req[j];
print();
bankers();
}
}
}
else
{
printf(" Request of process p%d cannot granted immediately\n",pno-1);
}
}

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.
23, 89, 132, 42, 187, 69, 36, 55
Start Head Position: 40
Direction: Left

Answer:

#include<stdio.h>
#include<stdlib.h>

int main()
{
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;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position : ");
scanf("%d",&initial);
printf("Enter total disk size : ");
scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0 : ");
scanf("%d",&move);
for (i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}

int index;
for (i=0;i<n;i++)
{
if (initial<RQ[i])
{
index=i;
break;
}
}

if (move==1)
{
for (i=index;i<n;i++)
{
printf("Servicing request : %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Servicing request : %d\n",size);
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
for (i=0;i<index;i++)
{
printf("Servicing request : %d\n",RQ[i]);
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for (i=index-1;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[i];
}
}

printf("\nTotal head movement is %d\n",TotalHeadMoment);


return 0;
}

Slip 21:

Q.1 Write a 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: 50
Answer:

#include<stdio.h>
#include<stdlib.h>
int main()
{
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=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head position:\n");
scanf("%d",&initial);
printf("\n\nSequence of request:\n");
for(i=0;i<n;i++)
{
TotalHeadMov=TotalHeadMov+abs(RQ[i]-initial);

printf("%d\n",RQ[i]);
initial=RQ[i];
}
printf("Total Head Moment=%d",TotalHeadMov);
}

Q.2 Write an MPI program to calculate sum of all even randomly generated 1000
numbers (stored in array) on a cluster

Answer:

#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARRAY_SIZE 1000

int main(int argc, 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);

srand(time(NULL)+rank);
for (int i=0;i<ARRAY_SIZE;i++)
{
array[i]=rand()%100;
}

for (int i=rank;i<ARRAY_SIZE;i+=size)


{
local_sum+=array[i];
}

MPI_REDUCE(&local_sum,&sum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WOR
LD);

if (rank==0)
{
printf("Sum of %d random numbers : %d\n",ARRAY_SIZE,sum);
}
MPI_Finalize();
return 0;
}

Slip 23:
Q.1 Consider a system with ‘m’ processes and ‘n’ resource types. Accept number
of instances for every resource type. For each process accept the allocation and
maximum requirement matrices. Write a program to display the contents of need
matrix and to check if the given request of a process can be granted immediately
or not

Answer:

#include<stdio.h>
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,k;
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;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
for(k=0;k<n;k++)
{
f[k]=0;
}
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
int y=0;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(f[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0)
{
ans[ind++]=i;
for(y=0;y<m;y++)
avail[y]+=alloc[i][y];
f[i]=1;
}
}
}
}
int flag=1;
for(i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("\n The following system is not safe");
break;
}
}
if(flag==1)
{
printf("\n Following system is safe\n\n Safe sequence are:");
for(i=0;i<n-1;i++)
printf("p%d->",ans[i]);
printf("p%d",ans[n-1]);
}
printf("\n");
}

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.
24, 90, 133, 43, 188, 70, 37,
55 Start Head Position: 58

Answer:
#include<stdio.h>
#include<stdlib.h>
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=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head
position:\n");
scanf("%d",&initial);
printf("\n\nSequence of
request:\n");
while(count!=n)
{
int
min=1000,diff,index;
for(i=0;i<n;i++)
{
diff=abs(RQ[i]-
initial);
if(min>=diff)
{
min=diff;
index=i;
}
}

TotalHeadMov=TotalHead
Mov+min;
initial=RQ[index];

printf("%d\n",RQ[index]);
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, 70
Starting Head position= 125
Direction: User Defined

Answer:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,thm=0,initial,size,move;
printf("Enter number of request\n");
scanf("%d",&n);
printf("Enter the request sequence\n");
for(i=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter total disk size\n");
scanf("%d",&size);
printf("Enter the head moment direction for high 1 and for low 0\n");
scanf("%d",&move);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
if(move==1)
{
for(i=index;i<n;i++)
{
thm=thm+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=index-1;i>=0;i--)
{
thm=thm+abs(RQ[i]-initial);
initial=RQ[i];
}
}
else
{
for(i=index-1;i>=0;i--)
{
thm=thm+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=index;i<n;i++)
{
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
with n 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<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 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;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
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(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.exit\n");
printf("Enter your
choice(1-5):\n");
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: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
s
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 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 0 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<stdio.h>
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
,k;
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;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n Enter Max matrix:");
for(i=0;i<n;i++)
{
printf("\n for process p%d:",i);
for(j=0;j<m;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\n Enter Available:");
for(i=0;i<m;i++)
{
scanf("%d",&avail[i]);
}
for(k=0;k<n;k++)
{
f[k]=0;
}
printf("\n Process\n");
for(i=0;i<n;i++)
{
printf("\np%d\t",i);
}
printf("\nAllocation matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
printf("\nMax matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("\nNeed matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
printf("\n Available\n");
for(i=0;i<n;i++)
{
if(i==0)
{
for(j=0;j<m;j++)
{
printf("%d\t",avail[j]);
}
}
}
int y=0;
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(f[i]==0)
{
int flag=0;
for(j=0;j<m;j++)
{
if(need[i][j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0)
{
ans[ind++]=i;
for(y=0;y<m;y++)
avail[y]+=alloc[i][y];
f[i]=1;
}
}
}
}
int flag=1;
for(i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("\n The following system is not safe");
break;
}
}
if(flag==1)
{
printf("\n Following system is safe\n\n Safe sequence are:");
for(i=0;i<n-1;i++)
printf("p%d->",ans[i]);
printf("p%d",ans[n-1]);
}
printf("\n");
}

Q.2 Write a 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<stdio.h>
#include<stdlib.h>
int main()
{
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=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head
position:\n");
scanf("%d",&initial);
printf("\n\nSequence of
request:\n");
for(i=0;i<n;i++)
{

TotalHeadMov=TotalHead
Mov+abs(RQ[i]-initial);

printf("%d\n",RQ[i]);
initial=RQ[i];
}
printf("Total Head
Moment=%d",TotalHeadMo
v);
}

Slip 28:
Q.1 Write a simulation program for disk scheduling using C-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.
56, 59, 40, 19, 91, 161, 151, 39, 185
Start Head Position: 48
Direction: User Defined

Answer:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int
queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],temp1=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",&n);
printf("Enter queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
//queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d \n",queue[j],queue[j+1]);
}
printf("Total seek time is %d\n",seek-max);
}

Q.2 Write an MPI program to calculate sum of randomly generated 1000


numbers
(stored in array) on a cluster

Answer:
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ARRAY_SIZE 1000

int main(int argc, char**


argv)
{
int rank,size;
int array[ARRAY_SIZE];
int sum=0;
int local_sum=0;

MPI_Init(NULL,NULL);

MPI_Comm_rank(MPI_CO
MM_WORLD,&rank);
MPI_Comm_size(MPI_CO
MM_WORLD,&size);

srand(time(NULL)+rank);
for (int
i=0;i<ARRAY_SIZE;i++)
{
array[i]=rand()%100;
}

for (int
i=rank;i<ARRAY_SIZE;i+=
size)
{
local_sum+=array[i];
}

MPI_REDUCE(&local_sum
,&sum,1,MPI_INT,MPI_SU
M,0,MPI_COMM_WORLD
);

if (rank==0)
{
printf("Sum of %d
random numbers :
%d\n",ARRAY_SIZE,sum);
}
MPI_Finalize();
return 0;
}

Slip 29:

Q.1 Write an MPI program to calculate sum of all even randomly generated
1000 numbers
(stored in array) on a cluster.

Answer:

#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ARRAY_SIZE 1000

int main(int argc, 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);

srand(time(NULL)+rank);
for (int i=0;i<ARRAY_SIZE;i++)
{
array[i]=rand()%100;
}

for (int i=rank;i<ARRAY_SIZE;i+=size)


{
local_sum+=array[i];
}

MPI_REDUCE(&local_sum,&sum,1,MPI_INT,MPI_SUM,0,MPI_COMM_WOR
LD);

if (rank==0)
{
printf("Sum of %d random numbers : %d\n",ARRAY_SIZE,sum);
}
MPI_Finalize();
return 0;
}

Q.2 Write a simulation program for disk scheduling using C-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..
80, 150, 60,135, 40, 35, 170
Starting Head Position: 70
Direction: Right
Answer:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,thm=0,initial,curr,move,m=0;
printf("Enter number of request\n");
scanf("%d",&n);
printf("Enter the request sequence\n");
for(i=0;i<n;i++)
{
scanf("%d",&RQ[i]);
}
printf("Enter initial head position\n");
scanf("%d",&initial);
//printf("Enter total disk size\n");
//scanf("%d",&size);
printf("Enter the head moment direction for high 1 and for low 0\n");
scanf("%d",&move);

for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(RQ[j]>RQ[j+1])
{
int temp;
temp=RQ[j];
RQ[j]=RQ[j+1];
RQ[j+1]=temp;
}
}
}
int index;
for(i=0;i<n;i++)
{
if(RQ[i]>initial)
{
m=i;
break;
}
}
curr=initial;
if(move==1)
{
for(j=i;j<n;j++)
{
thm=thm+abs(RQ[j]-initial);
initial=RQ[j];
}
curr=RQ[0];
for(j=1;j<m;j++)
{
thm=thm+abs(RQ[j]-curr);
curr=RQ[j];
}
}
else
{
for(j=i-1;j>=0;j--)
{
thm=thm+abs(RQ[j]-curr);
curr=RQ[j];
}
curr=RQ[n-1];

for(j=n-1;j>=m;j--)
{
thm=thm+abs(RQ[j]-curr);
curr=RQ[j];
}
}

printf("Total head moment=%d",thm);


}

You might also like