Os Sem 6

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

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

Assignment No:1 Set: A(1)


Assignment Name: Q.I) Add the following functionalities in your program a)
Accept Available
b) Display Allocation, Max
c) Display the contents of need matrix
d) Display Available
Q.2) I) Modify above program so as to include the following:
a) Accept Request for a process
b) Resource request algorithm
c) Safety algorithm
Consider a system with ‘n’ processes and ‘m’ 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.
*********************************************************************************/

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

int allocation[20][20],max[20][20],available[20],need[20][20],safe[10],s=0; int


finish[10],work[10],cnt=0,flag=0,temp=0;
int p,r,i,j,ch,index,req[10];

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];

printf("\nAllocation Table is:\n");


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

printf("\nNeed Table is:\n");


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

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

printf("\nEnter the Max Table:\n");


for(i=0;i<p;i++) //Accept the max table
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);

printf("\nEnter the vector Available :");


for(i=0;i<r;i++)
scanf("%d",&available[i]); check();

printf("\nDo U want to add new request:(0/1)");


scanf("%d",&ch);
if(ch==0)
exit(1);

printf("\nEnter the process no:");


scanf("%d",&index);
printf("\nEnter the request:");
for(i=0;i<r;i++)
scanf("%d",&req[i]);

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~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter the no of resources and processes: 3 5

Enter the Allocation Table:


0
1
0
2
0
0
3
0
2
2
1
1
0
0
2

Enter the Max Table:


7
5
3
3
2
2
9
0
2
2
2
2
4
3
3

Enter the vector Available :3


3
2

Allocation Table is:


0 1 0
2 0 0
3 0 2
2 1 1
0 0 2

Need Table is:


7 4 3
1 2 2
6 0 0
0 1 1
4 3 1

System is in Safe state


Safe Sequence is:P1 P3 P4 P0 P2
Do U want to add new request:(0/1)1

Enter the process no:1

Enter the request:1 0 2

Allocation Table is:


0 1 0
3 0 2
3 0 2
2 1 1
0 0 2

Need Table is:


7 4 3
0 2 0
6 0 0
0 1 1
4 3 1

System is in Safe state


Safe Sequence is:P1 P3 P4 P0 P2 [root@localhost Bankers]#
********************************************************************************/
/*********************************************************************************
Name: Roll No:
Class: T.Y.BSC. Comp.Sci Batch
Assignment No:2 Set: A(1)
Assignment Name: I) Write a program to simulate 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.
*********************************************************************************/

#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 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]==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;

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

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 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]==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;

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

int files[50], indexBlock[50], indBlock, n;


void recurse1(); void recurse2();

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

movement of total cylinders 644


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

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

// logic for sstf disk scheduling


/* loop will execute until all process is completed*/
while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}

}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
printf("------->%d",RQ[index]);
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}

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


return 0;}
/**********************************output***************
*************************** [ctbora@localhost ~]$ cc SSTF.c
[ctbora@localhost ~]$ ./a.out
Enter the number of Requests
8
Enter the Requests sequence
95
180
34
119
11
123
62
64
Enter initial head position
50
50-------->62------->64------->34------->11------->95------->119------->123------->180
Total head movement is 236

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

// logic for Scan 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];
}
// 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];

}
}

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


return 0;
}
/*************************************output**************************************
* [ctbora@localhost ~]$ gcc scan.c
[ctbora@localhost ~]$ ./a.out
Enter the number of Requests
8
Enter the Requests sequence
95
180
34
119
11
123
62
64
Enter initial head position
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0 1
Total head movement is 337
*********************************************************************************/
/*********************************************************************************
Name: Roll No:
Class: T.Y.BSC. Comp.Sci Batch:
Assignment No: 3 Set: A(4)
Assignment Name: 4) Write an OS program to implement C-SCAN algorithm 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);

// logic for C-Scan 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];
}
// last movement for max size
TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);
/*movement max to min disk */
TotalHeadMoment=TotalHeadMoment+abs(size-1-0);
initial=0;
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];
}
// 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];
}
}

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


return 0;
}
/************************************output***************************************
* [ctbora@localhost ~]$ gcc CSCAN.c
[ctbora@localhost ~]$ ./a.out
Enter the number of Requests
8
Enter the Requests sequence
95
180
34
119
11
123
62
64
Enter initial head position
50
Enter total disk size
200
Enter the head movement direction for high 1 and for low 0 1
Total head movement is 382

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

// 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;
}
/*************************************output**************************************
*
[root@localhost DiskScheduling]# vim LOOK.c
[root@localhost DiskScheduling]# gcc LOOK.c
[root@localhost DiskScheduling]# ./a.out
Enter the number of Requests
8
Enter the Requests sequence
95
180
34
119
11
123
62
64
Enter initial head position
50
Enter total disk size
199
Enter the head movement direction for high 1 and for low 0 0
Total head movement is 208

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

// 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;
}
/********************************OUTPUT******************************************
[root@localhost ctbora]# gcc clook.c
[root@localhost ctbora]# ./a.out
Enter the number of Requests
8
Enter the Requests sequence
95
180
34
119
11
123
62
64
Enter initial head position
50
Enter total disk size
199
Enter the head movement direction for high 1 and for low 0 0
Total head movement is 326
*******************************************************************************/
/*********************************************************************************
Name: Roll No:
Class: T.Y.BSC. Comp.Sci Batch:
Assignment No: 4 Set: A(1)
Assignment Name: 1) Write an MPI program to calculates sum of randomly generated 1000
numbers (stored in array) on a cluster.
*********************************************************************************/
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
void initialize(float* inarray, int n)
{ int
i;
float sum=0;
for (i=0; i<n; i++)
{
inarray[i] = random() / (float)RAND_MAX;
printf("%f\t",inarray[i]);
sum=sum+inarray[i];
}
printf("\nTotal sum is : %f \n",sum);
}

void main(int argc, char* argv[])


{

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

[ctbora@localhost ~]$ mpicc test1


[ctbora@localhost ~]$mpirun -np 1 ./test1
0.840188 0.394383 0.783099 0.798440
0.911647 0.197551 0.335223 0.768230
0.277775 0.553970 0.477397 0.628871
0.364784 0.513401 0.952230 0.916195
0.635712 0.717297 0.141603 0.606969
0.016301 0.242887 0.137232 0.804177
0.156679 0.400944 0.129790 0.108809
0.998924 0.218257 0.512932 0.839112
0.612640 0.296032 0.637552 0.524287
0.493583 0.972775 0.292517 0.771358
0.526745 0.769914 0.400229 0.891529
0.283315 0.352458 0.807725 0.919026
0.069755 0.949327 0.525995 0.086056
0.192214 0.663227 0.890233 0.348893
0.064171 0.020023 0.457702 0.063096
0.238280 0.970634 0.902208 0.850920
0.266666 0.539760 0.375207 0.760249
0.512535 0.667724 0.531606 0.039280
0.437638 0.931835 0.930810 0.720952
0.284293 0.738534 0.639979 0.354049
0.687861 0.165974 0.440105 0.880075
0.829201 0.330337 0.228968 0.893372
0.350360 0.686670 0.956468 0.588640
0.657304 0.858676 0.439560 0.923970
0.398437 0.814767 0.684219 0.910972

Total sum is : 54.682476

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

You might also like