Os Sem 6
Os Sem 6
Os Sem 6
#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
void check()
{
temp=0;
s=0;
for(i=0;i<p;i++) //Calculate need=max-allocation
for(j=0;j<r;j++)
need[i][j]=max[i][j]-allocation[i][j];
for(i=0;i<r;i++)
work[i]=available[i];
while(temp<2)
{
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
if(finish[i]==0 && need[i][j]<=work[j])
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
{
for(j=0;j<r;j++)
work[j]=work[j]+allocation[i][j];
finish[i]=1;
safe[s++]=i;
}
}
temp++;
}
flag=0;
for(i=0;i<p;i++)
{
if(finish[i]==0)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\nSystem is in Deadlock state");
}
else
{
printf("\nSystem is in Safe state");
printf("\nSafe Sequence is:"); for(i=0;i<p;i++)
printf("P%d\t",safe[i]);
}
main()
{
// clrscr();
printf("\n~~~~~~~~~~~~~~~~~~~~~~~BANKER'S
ALGORITHM~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\nEnter the no of resources and processes: ");
scanf("%d%d",&r,&p);
printf("\nEnter the Allocation Table:\n");
for(i=0;i<p;i++) //Accept the allocation table
for(j=0;j<r;j++)
scanf("%d",&allocation[i][j]);
flag=0;
for(i=0;i<r;i++)
if(req[i]<=need[index][i])
flag=1;
else
flag=0;
if(flag==0)
{
printf("\nRequest can not be satisfied...");
//getch();
exit(1);
}
for(i=0;i<r;i++)
if(req[i]<=available[i])
flag=1;
else
flag=0;
if(flag==0)
{
printf("\nRequest can not be satisfied...");
//getch();
exit(1);
}
for(i=0;i<r;i++)
{
allocation[index][i]=allocation[index][i]+req[i];
available[i]=available[i]-req[i];
}
check();
// getch();
}
/
/************************************Output***************************************
[root@localhost Bankers]# vim b.c
[root@localhost Bankers]# gcc b.c
b.c:7:16: warning: built-in function ‘index’ declared as non-function [root@localhost
Bankers]# ./a.out
~~~~~~~~~~~~~~~~~~~~~~~BANKER'S ALGORITHM~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200
int n,fb,bit[MAX];
void init()
{ int
i;
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;
if(nob>fb)
{
printf("Failed to create file %s\n",fname);
return;
}
for(i=0;i<n;i++)
{
if(bit[i]==0) break;
}
p = (NODE*)malloc(sizeof(NODE));
strcpy(p->fname,fname);
p->start=i;
p->next=NULL;
if(first==NULL)
first=p; else
last->next=p;
last=p;
fb-=nob;
j=i+1; nob--
;
while(nob>0)
{
if(bit[j]==0)
{
bit[i]=j;
i=j;
nob--;
}
j++;
}
bit[i]=-1;
printf("File %s created successully.\n",fname);
}
void delete()
{
char fname[20];
NODE *p,*q; int
nob=0,i,j;
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);
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;
}
/**********************************output*****************************************
*
[root@localhost set3]# vim sequential.2.c
[root@localhost set3]# gcc sequential.2.c
[root@localhost set3]# ./a.out
Enter total no.of disk blocks:10
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):1
0 -2 -2 -2 0 -2 -2 -2 0 -2
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):2
Enter file name:abc.txt
Enter no.of blocks:5
File abc.txt created successully.
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):3
File Chain
abc.txt 0->4->8->10->11->NULL
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):4
Enter file name to be deleted:abc.txt File
abc.txt deleted successfully.
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):5
*********************************************************************************/
/*********************************************************************************
C
Assignment No:2 Set: A(2)
Assignment Name: I) Write a program to simulate Linked file allocation method. Assume disk with n
number of blocks. Give value of n as input. Write menu driver program with menu options as
mentioned above and implement each option.
*********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200
NODE *first,*last;
int n,fb,bit[MAX];
void init()
{ int
i;
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;
if(nob>fb)
{
printf("Failed to create file %s\n",fname);
return;
}
for(i=0;i<n;i++)
{
if(bit[i]==0) break;
}
p = (NODE*)malloc(sizeof(NODE));
strcpy(p->fname,fname); p->start=i;
p->next=NULL;
if(first==NULL)
first=p; else
last->next=p;
last=p;
fb-=nob;
j=i+1; nob--
;
while(nob>0)
{
if(bit[j]==0)
{
bit[i]=j;
i=j;
nob--;
}
j++;
}
bit[i]=-1;
printf("File %s created successully.\n",fname);
}
void delete()
{
char fname[20];
NODE *p,*q; int
nob=0,i,j;
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);
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;
}
/**************************************output**************************************
[root@localhost set3]# vim linked.2.c
[root@localhost set3]# gcc linked.2.c
[root@localhost set3]# ./a.out
Enter total no.of disk blocks:10
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):1
0 -2 -2 -2 0 -2 -2 -2 0 -2
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):2
Enter file name:opq.txt
Enter no.of blocks:3
File opq.txt created successully.
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):3
File Chain
opq.txt 0->4->8->NULL
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):2 Enter
file name:xyz.txt
Enter no.of blocks:2
Failed to create file xyz.txt
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):4
Enter file name to be deleted:xyz.txt File
xyz.txt not found.
1.Show bit vector
2.Create new file
3.Show directory
4.Delete file
5.Exit
Enter your choice (1-5):5
*********************************************************************************/
/*********************************************************************************
Assignment No:2 Set: A(3)
Assignment Name: I) Write a program to simulate Indexed file allocation method. Assume disk with
n number of blocks. Give value of n as input. Write menu driver program with menu options as
mentioned above and implement each option.
*********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
void recurse1(){
printf("Enter the index block: "); scanf("%d", &indBlock); if (files[indBlock] != 1){
printf("Enter the number of blocks and the number of files needed for the index %d on the disk:
", indBlock);
scanf("%d", &n);
}
else{
printf("%d is already allocated\n", indBlock);
recurse1();
}
recurse2();
}
void recurse2(){
int ch; int flag
= 0, i,j,k; for (i=0;
i<n; i++){
scanf("%d", &indexBlock[i]);
if (files[indexBlock[i]] == 0)
flag++;
}
if (flag == n){ for (j=0;
j<n; j++){
files[indexBlock[j]] = 1;
}
printf("Allocated\n");
printf("File Indexed\n"); for
(k=0; k<n; k++){
printf("%d ------> %d : %d\n", indBlock, indexBlock[k], files[indexBlock[k]]);
}
}
else{
printf("File in the index is already allocated\n");
printf("Enter another indexed file\n"); recurse2();
}
printf("Do you want to enter more files?\n");
printf("Enter 1 for Yes, Enter 0 for No: ");
scanf("%d", &ch); if (ch == 1) recurse1();
else exit(0); return;
}
int main()
{
int i;
for(i=0;i<50;i++)
files[i]=0;
recurse1();
return 0;
}
/**********************************output*****************************************
*
[root@localhost set3]# vim Indexed.2.c
[root@localhost set3]# gcc Indexed.2.c
[root@localhost set3]# ./a.out
Enter the index block: 10
Enter the number of blocks and the number of files needed for the index 10 on the disk: 5
1
3
6
7
9
Allocated
File Indexed
10 ------> 1 : 1
10 ------> 3 : 1
10 ------> 6 : 1
10 ------> 7 : 1
10 ------> 9 : 1
Do you want to enter more files?
Enter 1 for Yes, Enter 0 for No: 0
*********************************************************************************/
Class:T.Y.BSC. Comp.Sci
Assignment No:2 Set: A(1)
Assignment Name: I) Write a program to simulate easy Sequential (Contiguous) file allocation
method. Assume disk with n number of blocks. Give value of n as input. Write menu driver program
with menu options as mentioned above and implement each option.
*********************************************************************************/
/* Program to simulate sequential file allocation strategy */ //Program
Code:
#include<stdio.h> #include<stdlib.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf("Enter starting block and length of files: ");
scanf("%d%d", &st,&len); for(k=st;k<(st+len);k++)
if(f[k]==0) count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{ f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf("The file is allocated to disk\n");
}
else
printf("The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x; else
exit(0);
}
/*******************************output********************************************
* [root@localhost set3]# vi ssequential.c
[root@localhost set3]# gcc ssequential.c
[root@localhost set3]# ./a.out Files
Allocated are :
Enter starting block and length of files: 0
1
0 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 6 7
6 1
7 1
8 1
9 1
10 1
11 1
12 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 16
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)0
********************************************************************************/
Class:T.Y.BSC. Comp.Sci
Assignment No:2 Set: A(2)
Assignment Name: I) Write a program to simulate easy Linked file allocation method. Assume disk
with n number of blocks. Give value of n as input. Write menu driver program with menu options as
mentioned above and implement each option.
*********************************************************************************/
/* Program to simulate linked file allocation strategy */ //Program
Code:
#include<stdio.h> #include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: "); scanf("%d",&p);
printf("Enter blocks already allocated: "); for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len; if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{ f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;}}} else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x; else exit(0);
}
/************************************output***************************************
[root@localhost set3]# vi slinked.c
[root@localhost set3]# gcc slinked.c
[root@localhost set3]# ./a.out
Enter how many blocks already allocated: 5 Enter
blocks already allocated:
9
16
1
10
25
Enter index starting block and length: 9 4
9 starting block is already allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter index starting block and length: 8
7
8-------->1
9 Block is already allocated
10 Block is already allocated
11-------->1
12-------->1
13-------->1
14-------->1
15-------->1
16 Block is already allocated
17-------->1
Do you want to enter more file(Yes - 1/No - 0)0
*********************************************************************************/
Assignment No:2 Set: A(3)
Assignment Name: I) Write a program to simulate easy Indexed file allocation method. Assume disk
with n number of blocks. Give value of n as input. Write menu driver program with menu options as
mentioned above and implement each option.
*********************************************************************************/
/* Program to simulate indexed file allocation strategy */ //Program
Code:
#include<stdio.h> #include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0; for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind); goto
x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++) f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed"); goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c); if(c==1) goto x; else exit(0);
}
/****************************************output***********************************
* [root@localhost set3]# vim sindexed.c
[root@localhost set3]# gcc sindexed.c
[root@localhost set3]# ./a.out
Enter the index block: 19
Enter no of blocks needed and no of files for the index 19 on the disk :
5
9
16
1
10
25
Allocated
File Indexed
19-------->9 : 1
19-------->16 : 1
19-------->1 : 1
19-------->10 : 1
19-------->25 : 1
Do you want to enter more file(Yes - 1/No - 0)0
*********************************************************************************/
Set: A(1)
Assignment Name: I) Write an OS program to implement FCFS Disk Scheduling algorithm.
*********************************************************************************/
#include<stdio.h>
int main()
{
int i,j,sum=0,n; int
ar[20],tm[20];
int disk;
printf("enter number of location\t"); scanf("%d",&n);
printf("enter position of head\t"); scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
tm[i]=disk-ar[i];
if(tm[i]<0)
{
tm[i]=ar[i]-disk;
}
disk=ar[i];
sum=sum+tm[i];
}
/*for(i=0;i<n;i++)
{
printf("\n%d->",tm[i]);
} */
printf("\nmovement of total cylinders %d",sum); return
0;
}
/*************************************output**************************************
*
[root@localhost DiskScheduling]# vim FCFS.c
[root@localhost DiskScheduling]# gcc FCFS.c
[root@localhost DiskScheduling]# ./a.out
enter number of location 8
enter position of head 50 enter
elements of disk queue
95
180
34
119
11
123
62
64
Set: A(2)
Assignment Name: 2) Write an OS program to implement SSTF algorithm Disk Scheduling algorithm.
*********************************************************************************/
#include<stdio.h> #include<stdlib.h> int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
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("%d-
",initial);
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
printf("------->%d",RQ[index]);
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
*********************************************************************************/
/*********************************************************************************
Name: Roll No:
Class: T.Y.BSC. Comp.Sci Batch:
Assignment No: 3
Set: A(3)
Assignment Name: 3) Write an OS program to implement SCAN Disk Scheduling algorithm.
*********************************************************************************/
#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
total disk size\n"); scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
}
}
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];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
initial = size-1;
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];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
initial =0; for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
// last movement for min size
TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0);
/*movement min to max disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial =size-1;
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
*********************************************************************************/
/*********************************************************************************
Name: Roll No:
Class: T.Y.BSC. Comp.Sci Batch:
Assignment No: 3 Set: A(5)
Assignment Name: 5) Write an OS program to implement LOOK Disk Scheduling algorithm.
*********************************************************************************/
#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
total disk size\n"); scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
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];
}
}
*********************************************************************************/
/*********************************************************************************
Name: Roll No:
Class: T.Y.BSC. Comp.Sci Batch:
Assignment No: 3 Set: A(6)
Assignment Name: 6) Write an OS program to implement C-LOOK Disk Scheduling algorithm.
*********************************************************************************/
#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
total disk size\n"); scanf("%d",&size);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
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];
}
}
MPI_Comm comm=MPI_COMM_WORLD;
int numnodes, myid, ierr;
ierr=MPI_Init(&argc, &argv);
ierr=MPI_Comm_size(comm, &numnodes);
ierr=MPI_Comm_rank(comm, &myid);
int n = 100;
float *x = malloc(sizeof(float)*n);
initialize(x, n);
ierr=MPI_Finalize();
}
/***********************************OUTPUT*************************************
*********************************************************************************/