OS Solution Set

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

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

//Bankers algorithm

#include<stdio.h>
int main()
{
int
m,n,i,j,all[10][10],max[10][10],need[10][10],ava[10],work[10],no=0,safe=0,seq[10];
char p[10],t='t',f='f';
printf("Enter the number of process\n");
scanf("%d",&m);
printf("Enter the number of resources\n");
scanf("%d",&n);

printf("Enter the Allocations\n");


for(i=0;i<m;i++)
{
printf("Enter the allocation for process p%d\t",i);
for(j=0;j<n;j++)
{
scanf("%d",&all[i][j]);
}
}
printf("Enter the Max\n");
for(i=0;i<m;i++)
{
printf("Enter the Max for process p%d\t",i);
for(j=0;j<n;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Process\t\tAllocation\t\tMax\n\n");
for(i=0;i<m;i++)
{
printf("p%d\t",i);
for(j=0;j<n;j++)
{
printf("%d\t",all[i][j]);
}
printf("\t");
for(j=0;j<n;j++)
{
printf("%d\t",max[i][j]);
}
printf("\n");
}
printf("enter availables:\n");
for(j=0;j<n;j++)
{
scanf("%d",&ava[j]);
work[j]=ava[j];
}
printf("\n work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
need[i][j]=max[i][j]-all[i][j];
}
}
printf("\n contents of need table is: \n");
printf("Process\tNeed\n\n");
for(i=0;i<m;i++)
{
printf("P%d\t",i);
for(j=0;j<n;j++)
printf("%d \t",need[i][j]);
printf("\n");
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}

if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else

p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
}

for(i=0;i<m;i++)
{
if(p[i]!=t)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else
p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
}
}
if(safe==m)
{
printf("\n system is in safe state:\n");
printf("safe sequence is:{");
for(i=0;i<m;i++)
printf("p%d",seq[i]);
printf("}");
}
else
printf("\n system is not safe:\n");
}

/*
[root@ugilinux64 Shraddha]# cc banker.c
[root@ugilinux64 Shraddha]# ./a.out
Enter the number of process
5
Enter the number of resources
3
Enter the Allocations
Enter the allocation for process p0 0 1 0
Enter the allocation for process p1 2 0 0
Enter the allocation for process p2 3 0 2
Enter the allocation for process p3 2 1 1
Enter the allocation for process p4 0 0 2
Enter the Max
Enter the Max for process p0 753
Enter the Max for process p1 322
Enter the Max for process p2 902
Enter the Max for process p3 222
Enter the Max for process p4 433
Process Allocation Max

p0 0 1 0 7 5 3
p1 2 0 0 3 2 2
p2 3 0 2 9 0 2
p3 2 1 1 2 2 2
p4 0 0 2 4 3 3
enter availables:
653

work is:
6 5 3
contents of need table is:
Process Need

P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

new work is:


6 5 3
p1 is true

new work is:


8 5 3
p2 is true

new work is:


11 5 5
p3 is true

new work is:


13 6 6
p4 is true

new work is:


13 6 8
p0 is true

new work is:


13 7 8
system is in safe state:
safe sequence is:{p1p2p3p4p0}
*/

*******************************************************

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

//Resource Request

#include<stdio.h>
int main()
{
int
need[10][10],all[10][10],max[10][10],ava[10],i,j,finish[10],m,n,work[10],no=0,safe=
0,seq[10];
char p[10],t='t',f='f';
int pn=0,temp=0,temp1=0,req[10],seq1[10],safe1=0;
printf("Enter the number of process\n");
scanf("%d",&m);
printf("Enter the number of resources\n");
scanf("%d",&n);
printf("enter allocations: \n");
for(i=0;i<m;i++)
{
printf("enter the process %d: ",i);
for(j=0;j<n;j++)
{
scanf("%d",&all[i][j]);
}
}
printf("enter max:\n");
for(i=0;i<m;i++)
{
printf("enter the process %d: ",i);
for(j=0;j<n;j++)
{
scanf("%d",&max[i][j]);
}
}

printf("enter availables:\n");
for(j=0;j<n;j++)
{
scanf("%d",&ava[j]);
work[j]=ava[j];
}

printf("\n work is:\n");


for(j=0;j<n;j++)
printf("%d \t",work[j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
need[i][j]=max[i][j]-all[i][j];
}
}

printf("\n contents of need table is: \n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d \t",need[i][j]);
printf("\n");
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}

if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else

p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);

for(i=0;i<m;i++)
{
if(p[i]!=t)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
//f(no==n)
if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else
p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
}
}
if(safe==m)
{
printf("\n system is in safe state:\n");
printf("safe sequence is:{");
for(i=0;i<m;i++)
printf("p%d",seq[i]);
printf("}");
}
else
printf("\n system is not safe:\n");

printf("Resourse process");
printf("enter the process no");
scanf("%d",&pn);
printf("enter the request");
for(i=0;i<=n;i++)
{
scanf("%d",&req[i]);

}
for(i=0;i<=n;i++)
{
if(req[i]<=need[pn][i])
temp++;
}
if(temp==n)
{
for(i=0;i<=n;i++)
{
if(req[i]<=ava[i])
temp1++;
}
}
printf("temp1=%d",temp1);
if(temp1==n)
{
for(i=0;i<=n;i++)
{
ava[i]=ava[i]-req[i];
all[pn][i]=all[pn][i]+req[i];
need[pn][i]=need[pn][i]-req[i];
}
}
printf("\n content of need table is:\n");
for(i=0;i<=m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",need[i][j]);
printf("\n");
}

printf(" \n content of allocation table is:\n");


for(i=0;i<=m;i++)
{
for(j=0;j<=n;j++)

printf("%d\t",all[i][j]);
printf("\n");
}
for(i=0;i<n;i++)

work[i]=ava[i];
for(i=0;i<m;i++)
{
printf("new work is:\n");
for(j=0;j<n;j++)
printf("%d\t",work[j]);
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n p%d id true\n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq1[safe1]=i;
safe1++;
}

else
p[i]=f;
no=0;
printf("\n new work is \n");
for(j=0;j<n;j++)
printf("%d\t",work[j]);
}
for(i=0;i<m;i++)
{

if(p[i]!=t)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n P%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq1[safe1]=i;
safe1++;
}
else
p[i]=f;
no=0;
printf("\n new work is");
for(j=0;j<n;j++)
printf("%d\t",work[j]);
}
}

if(safe1==m)
{
printf("\n sysytem is in safe state");
printf("\n safe sequece is:");
for(i=0;i<m;i++)

printf("\n p%d",seq1[i]);
}

else
printf("\n sysytem is not safe \n");
}
*******************************************************

/* Que. 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 */

//Linked File Allocation

#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);

for(i=0;i<n;i++)
{
bit[i] =1;
fb = n;
int k = rand()%n;
if(bit[k]!=0)
{
bit[k]=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\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]==1) 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 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.Exit\n");
printf("Enter your choice (1-5):");
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;
}

********************************************************

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

//Sequential (Contiguous) file allocation

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

int bit[256],n,fb;

typedef struct d
{
char fname[25];
int length,start;
struct d *next;
}NODE;
NODE *last,*first;

void init()
{
printf("Enter the no. of block\n");//10
scanf("%d",&n);
for (int i = 0; i < n; i++)
bit[i] =1;
fb=n;//free block
}
void BitVector(){
for (int i = 0; i < n; i++)
printf("%d\t\n",bit[i]);

void createFile(){
NODE *f;
int i,j;
int len;
char fname[24];

printf("Enter file name and length : ");


scanf("%s%d",&fname,&len);
f=(NODE*)malloc(sizeof(NODE));

if(fb>len){
strcpy(f->fname,fname);
f->length=len;

for (i = 0; i < n; i++)


{
if (bit[i]==1)
break;
}
f->start=i;
f->next=NULL;
if (first==NULL)
{
first=f;
}
else{
last->next=f;
}
last=f;
fb-=len;
j=i-1;

while (len>0)
{
if(bit[j]==1){
bit[i]=0;
i=j;
len--;
}
j++;
}

bit[i]=f->start+f->length;
printf("File %s created successully.\n",fname);

else{
printf("Insufficent Space");
}

}
void show_dir(){
NODE *f;
f=first;
printf("\tFile\tStart\tLength\n");
while (f!=NULL)
{
printf("%7s\t%8d\t%9d\n",f->fname,f->start,f->length);
f=f->next;
}
}
void fdelete(){
char fname[100];
NODE *fp,*fn;
int i,j;

printf("Enter the file name to delete :\n");


scanf("%s",&fname);

fp=fn=first;
while (fn!=NULL)
{
if(strcmp(fname,fn->fname)==0)
break;
fp=fn;
fn=fn->next;
}
if (fn==NULL)
{
printf("File not found\n");
}
else{
i=fn->start;
while (bit[i]!=(fn->start+fn->length)){
bit[i++]=1;
}
bit[i]=1;
fb+=fn->length;

if(fn==first){
first=first->next;
}
else if(fn==last){
last=fp;
last->next=NULL;
}
else{
fp->next=fn->next;
}
free(fn);
printf("File %s deleted successfully.\n",fname);

}
}
void main()
{
int choice;
init();

do
{
printf("1 :Show bit vector\n");
printf("2 :Create new file \n");
printf("3 :Show Directry\n");
printf("4 :Delete file\n");
printf("5 :Exit\n");
printf("\nEnter Your choice\n :");
scanf("%d",&choice);
switch (choice)
{
case 1:
BitVector();
break;
case 2:
createFile();
break;
case 3:
show_dir();
break;
case 4:
fdelete();
break;
default:
break;
}
} while (choice<5);
}

*******************************************************

/* 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
*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int rq[199],initial,i,headm=0,n;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);
for(i=0;i<n;i++)
{
headm +=abs(rq[i]-initial);
initial=rq[i];
}
printf("\ntotal moment %d",headm);
}

/* OUTPUT
[root@ugilinux64 Shraddha]# cc FCFS.c
[root@ugilinux64 Shraddha]# ./a.out

Enter the No Request 9


Enter the sequence 55 58 39 18 90 160 150 38 184
Enter the initial 50
total moment 458 */
/* 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
*/
#include<stdio.h>
#include<stdlib.h>
int min(int q[],int n)
{
int i,min=1000,index,d;
return index;
}
void main()
{
int rq[199],initial,i,headm=0,n,min=1000,index,d,count=0;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);

while (count!=n)
{
min=1000;
for(i=0;i<n;i++)
{
d=abs(rq[i]-initial);
if(min>=d)
{
min=d;
index=i;
}
}
headm+=min;
initial=rq[index];
rq[index]=1000;
count++;
}
printf("\ntotal moment %d",headm);
}

/*OUTPUT
[root@ugilinux64 Shraddha]# cc SSTF.c
[root@ugilinux64 Shraddha]# ./a.out

Enter the No Request 9


Enter the sequence 55 58 39 18 90 160 150 38 184
Enter the initial 50
total moment 214

*/
*******************************************************

/* 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.
33, 99, 142, 52, 197, 79, 46, 65
Start Head Position: 72
Direction: Right
*/

#include<stdio.h>
#include<stdlib.h>
int max=199;
void main(){
int direction,rq[max],initial,i,headm=0,n,min,m,headb;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);
printf("\nEnter head direction\n1:Left\n2:Right\n");
scanf("%d",&direction);
if(direction==1){
m=rq[0];
for(i=1;i<n;i++){
if(m<=rq[i])
m=rq[i];
}
headb=initial+m;
printf("\nTotal left HeadMovement %d ",headb);
}else{
min=rq[0];
for(i=1;i<n;i++){
if(min>=rq[i])
min=rq[i];
}
headm=max-initial+max-min;
printf("\nTotal right HeadMovement %d ",headm);
}
}

/*OUTPUT
[root@ugilinux64 Shraddha]# cc SCAN.c
[root@ugilinux64 Shraddha]# ./a.out
Enter the No Request 9
Enter the sequence 55 58 39 18 90 160 150 38 184
Enter the initial 50
Enter head direction
1:Left
2:Right
1
Total left HeadMovement 234

[root@ugilinux64 Shraddha]# cc SCAN.c


[root@ugilinux64 Shraddha]# ./a.out
Enter the No Request 9
Enter the sequence 55 58 39 18 90 160 150 38 184
Enter the initial 50
Enter head direction
1:Left
2:Right
2
total right HeadMovement 330

*/
*******************************************************

/* 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
#include<stdio.h>
#include<stdlib.h>
int max=199;
void main(){
int rq[max],initial,i,headm=0,n,j,m,headb;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);
m=0;
for(i=0;i<n;i++){
if (rq[i] < initial && m<=rq[i])
{
m=rq[i];
}
}
headm=max-initial+max+m;
printf("\ntotal head movement %d %d",headm,m);
}
/*OUTPUT
[root@ugilinux64 Shraddha]# cc CSCAN.c
[root@ugilinux64 Shraddha]# ./a.out
Enter the No Request 9
Enter the sequence 55 58 39 18 90 160 150 38 184
Enter the initial 50
total head movement 387 39
*/
*******************************************************

/* Write a simulation program for disk scheduling using LOOK algorithm. Accept
totalnumber 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 numberof head moments.
86, 147, 91, 170, 95, 130, 102, 70
Starting Head position= 125
Direction: User Defined
*/
#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\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);

// logic for look disk scheduling

/*logic for sort the request array */


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 movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

}
}

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


return 0;
}

*******************************************************

/* 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 */
#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\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);

// logic for C-look disk scheduling

/*logic for sort the request array */


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 movement is towards high value


if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];

}
}

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


return 0;
}
/*Write an MPI program to calculate sum of randomly generated 1000 numbers
(stored in array) on a cluster*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

#define SIZE 1000

int main(int argc, char** argv) {


int rank, size, i;
double sum = 0.0, local_sum = 0.0;
double data[SIZE];

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

for (i = 0; i < SIZE; i++) {


data[i] = (double) rand() / RAND_MAX;
}

for (i = 0; i < SIZE/size; i++) {


local_sum += data[i];
}

MPI_Reduce(&local_sum, &sum, 1, MPI_DOUBLE, MPI_SUM, 0,


MPI_COMM_WORLD);

if (rank == 0) {
printf("Sum = %d\n", (int)sum);
}

MPI_Finalize();
return 0;
}

/* OUTPUT

[root@ugilinux205 Shraddha]# mpicc sum.c


[root@ugilinux205 Shraddha]# mpirun -np 5 ./a.out
Sum=526
[root@ugilinux205 Shraddha]#
*/
/*Write an MPI program to calculate sum of all even randomly generated 1000
numbers (stored in array) on a cluster. */
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

#define SIZE 1000

int main(int argc, char** argv) {


int rank, size, i;
double sum = 0.0, local_sum = 0.0;
double data[SIZE];

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

for (i = 0; i < SIZE; i++) {


data[i] = (double) 2*rand() / RAND_MAX;
}
for (i = 0; i < SIZE/size; i++) {
local_sum += data[i];
}
MPI_Reduce(&local_sum,&sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);

if (rank == 0) {
printf("Sum = %d\n", (int)sum);
}

MPI_Finalize();
return 0;
}
/*Write an MPI program to calculate sum of all odd randomly generated 1000
numbers(stored in array) on a cluster.*/

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

#define SIZE 1000

int main(int argc, char** argv) {


int rank, size, i;
double sum = 0.0, local_sum = 0.0;
double data[SIZE];

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

for (i = 0; i < SIZE; i++)


{
data[i] = (double) 2*rand()+1 / RAND_MAX;
}

for (i = 0; i < SIZE/size; i++)


{
local_sum += data[i];
}

MPI_Reduce(&local_sum,&sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
if (rank == 0) {
printf("Sum = %d\n", (int)sum);
}

MPI_Finalize();
return 0;
}

/*Write an MPI program to calculate sum and average randomly generated 1000
numbers (stored in array) on a cluster.*/

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

#define SIZE 1000


int main(int argc,char*argv[]){
int rank,size,i;

double sum=0.0,local_sum=0.0;
double average=1.0;
double data[SIZE];
MPI_Init(&argc ,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);

for(i=0;i<SIZE;i++)
{
data[i]=(double) rand()/RAND_MAX;
}
for(i=0;i<SIZE;i++)
{
local_sum= local_sum + data[i];
}
MPI_Reduce(&local_sum,&sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD
);
average=sum/SIZE;
if(rank==0)
{
printf("Sum=%d\n",(int)sum);
printf("average=%f\n",(double)average);
}
MPI_Finalize();
return 0;
}

/*OUTPUT
[root@ugilinux205 Shraddha]# mpicc sum.c
[root@ugilinux205 Shraddha]# mpirun -np 5 ./a.out
Sum=526
Average=0.526
[root@ugilinux205 Shraddha]#
/*Write an MPI program to find the max number from randomly generated 1000
numbers(stored in array) on a cluster (Hint: Use MPI_Reduce) */

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

#define ARRAY_SIZE 1000

int main(int argc, char** argv)


{
int rank, size, i;
int local_max = INT_MIN;
int global_max;
int array[ARRAY_SIZE];

MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

for (i = 0; i < ARRAY_SIZE; i++)


{
array[i] = rand();
}

for (i = 0; i < ARRAY_SIZE / size; i++)


{
if (array[i] > local_max)
{
local_max = array[i];
}
}

MPI_Reduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, 0,


MPI_COMM_WORLD);
if (rank == 0)
{
printf("Global maximum value = %d\n", global_max);
}

MPI_Finalize();
return 0;
}
/*Write an MPI program to find the min number from randomly generated 1000
numbers(stored in array) on a cluster (Hint: Use MPI_Reduce)*/

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

#define ARRAY_SIZE 1000


int main(int argc, char** argv)
{
int rank, size, i;
int local_min = INT_MAX;
int global_min;
int array[ARRAY_SIZE];

MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

for (i = 0; i < ARRAY_SIZE; i++)


{
array[i] = rand();
}

for (i = 0; i < ARRAY_SIZE / size; i++)


{
if (array[i] < local_min)
{
local_min = array[i];
}

MPI_Reduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, 0,


MPI_COMM_WORLD);

if (rank == 0)
{
printf("Global minimum value = %d\n", global_min);
}

MPI_Finalize();
return 0;
}

/* OUTPUT
[root@ugilinux205 Shraddha]# mpicc MIN.c
[root@ugilinux205 Shraddha]# mpirun -np 5 ./a.out
Global minimum value = 8936987
[root@ugilinux205 Shraddha]#
*/

You might also like