0% found this document useful (0 votes)
12 views35 pages

OS Lab

Operating system lab codetantra

Uploaded by

shinchonee
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)
12 views35 pages

OS Lab

Operating system lab codetantra

Uploaded by

shinchonee
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/ 35

S.No: 1 Exp.

Name: Write the code for I/O system calls Date: 2022-05-21

Page No:
Aim:
Write a c program for I/O system calls.
ALGORITHM:

ID: 2002220100079
1. Start the program.
2. open a file for O_RDWR for R/W, O_CREATE for creating a file, O_TRUNC for truncate a file
3. Using getchar(), read the character and store it in the string[]array
4. The string [] array is written into a file close to it.
5. Then the first is opened for read-only mode and read the characters and displayed It and close the file
6. Stop the program
Source Code:

systemCalls.c

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int fd[2];
char buf1[25]= "just a test\n";
char buf2[50];
fd[0]=open("file1",O_RDWR);
fd[1]=open("file2",O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf("Enter the textnow: ");
scanf(" %s", buf1);
printf("Cat file1 is");
printf("\nhai");
write(fd[0], buf1, strlen(buf1));
lseek(fd[0],SEEK_SET, 0);
read(fd[0], buf2, sizeof(buf1));
write(fd[1], buf2, sizeof(buf2));
close(fd[0]);
close(fd[1]);
printf("\n");
return 0;
}

Execution Results - All test cases have succeeded!


CSE_4_B1

Test Case - 1

User Output
ITS Engineering College

Enter the textnow: Hello


Cat file1 is
hai
Exp. Name: Write the code to implement CPU scheduling algorithm for shortest job first
S.No: 2 Date: 2022-05-21
scheduling.

Page No:
Aim:
Write a C program to implement a CPU scheduling algorithm for the shortest job first scheduling.

ID: 2002220100079
ALGORITHM:
1. Start the program. Get the number of processes and their burst time.
2. Initialize the waiting time for process 1 as 0.
3. The processes are stored according to their burst time.
4. The waiting time for the processes are calculated a follows: for(i=2;i<=n;i++).wt.p[i]=p[i=1]+bt.p[i-1].
5. The waiting time of all the processes summed and then the average time is calculated
6. The waiting time of each process and average time are displayed.
7. Stop the program.
Source Code:

sjfs.c

#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
}
p[10],temp;
int main()
{
int i,j,n,totwt,tottt;
float avg1,avg2;
printf("Enter the number of process: ");
scanf("%d",&n); for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("Enter the burst time: ");
scanf("%d",&p[i].bt);
}
for(i=1;i<n;i++) {
for(j=i+1;j<=n;j++)
{
if(p[i].bt>p[j].bt)
{
CSE_4_B1

temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
ITS Engineering College

p[i].bt=p[j].bt;
p[j].bt=temp.bt;
}
}
}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n) {
p[i].wt=p[i-1].bt+p[i-1].wt;

Page No:
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;

ID: 2002220100079
totwt=tottt=0;
printf("Process id\tbt\twt\ttt\n");
while(i<=n){
printf("%d\t%d\t%d\t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
avg1=totwt / n;
avg2=tottt / n;
printf("AVG1 = %f\nAVG2 = %f\n",avg1,avg2);
return 0;
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the number of process: 3
Enter the burst time: 2
Enter the burst time: 4
Enter the burst time: 6
Process id bt wt tt
1 2 0 2
2 4 2 6
3 6 6 12
AVG1 = 2.000000
AVG2 = 6.000000
CSE_4_B1

Test Case - 2

User Output
ITS Engineering College

Enter the number of process: 6


Enter the burst time: 10
Enter the burst time: 59
Enter the burst time: 34
Enter the burst time: 51
Enter the burst time: 26
Test Case - 2
Enter the burst time: 87

Page No:
Process id bt wt tt
1 10 0 10
5 26 10 36
3 34 36 70

ID: 2002220100079
4 51 70 121
2 59 121 180
6 87 180 267
AVG1 = 69.000000
AVG2 = 114.000000

CSE_4_B1
ITS Engineering College
S.No: 3 Exp. Name: Write the code to perform priority scheduling Date: 2022-05-21

Page No:
Aim:
Write a ‘C’ program to perform Priority Scheduling.

ALGORITHM:

ID: 2002220100079
1. Start the program.
2. Read burst time, waiting time, turn the around time, and priority.
3. Initialize the waiting time for processes 1 and 0.
4. Based upon the priority process are arranged
5. Thewaitingtimeofalltheprocessesissummedandthentheaveragewaitingtime
6. The waiting time of each process and average waiting time are displayed based on the priority.
7. Stop the program.
Source Code:

priorityScheduling.c

#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
int prior;
}
p[10],temp;
int main()
{
int i,j,n,totwt,tottt,arg1,arg2;
printf("Enter the number of process: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("Enter the burst time: ");
scanf("%d",&p[i].bt);
printf("Enter the priority: ");
scanf("%d",&p[i].prior);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
CSE_4_B1

{
if(p[i].prior>p[j].prior)
{
temp.pid=p[i].pid;
ITS Engineering College

p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp.bt;
temp.prior=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=temp.prior;
}
}

Page No:
}
p[i].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;

ID: 2002220100079
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("process id\tbt\twt\ttt");
while(i<=n)
{
printf("\n%d\t%d\t%d\t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;}arg1=totwt / n;arg2=tottt / n;
printf("\nAvg1 = %d",arg1);
printf("\nAvg2 = %d\n",arg2);
return 0;

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the number of process: 3
Enter the burst time: 2
Enter the priority: 3
Enter the burst time: 4
Enter the priority: 1
CSE_4_B1

Enter the burst time: 6


Enter the priority: 2
process id bt wt tt
ITS Engineering College

2 4 0 4
3 6 4 10
1 2 10 12
Avg1 = 4
Avg2 = 8
Test Case - 2

User Output

Page No:
Enter the number of process: 6
Enter the burst time: 48
Enter the priority: 4

ID: 2002220100079
Enter the burst time: 62
Enter the priority: 2
Enter the burst time: 98
Enter the priority: 1
Enter the burst time: 27
Enter the priority: 5
Enter the burst time: 116
Enter the priority: 3
Enter the burst time: 58
Enter the priority: 6
process id bt wt tt
3 98 0 98
2 62 98 160
5 116 160 276
1 48 276 324
4 27 324 351
6 58 351 409
Avg1 = 201
Avg2 = 269

CSE_4_B1
ITS Engineering College
Exp. Name: Write the code to implement CPU & scheduling algorithm for first come first
S.No: 4 Date: 2022-05-21
serve scheduling

Page No:
Aim:
Write the program to implement CPU & scheduling algorithm for first come first serve scheduling.

ID: 2002220100079
ALGORITHM:
1. Start the program.
2. Get the number of processes and their burst time.
3. Initialize the waiting time for processes 1 and 0.
4. Process for(i=2;i<=n;i++),wt.p[i]=p[i-1]+bt.p[i-1].
5. Thewaitingtimeofalltheprocessesissummedthenaveragevaluetimeiscalculated.
6. The waiting time of each process and average times are displayed
7. Stop the program
Source Code:

firstComeFirstServe.c

#include<stdio.h>
int main()
{
int n, wt=0;
printf("Enter the no of process: ");
scanf("%d",&n);
int bt[n];
for(int i=0;i<n;i++)
{
printf("Enter the burst time: ");
scanf("%d", &bt[i]);
}
int avg1=0, avg2=0;
printf("process id\tbt\twt\ttt\n");
for(int i=0;i<n;i++)
{
avg1+=wt;
printf("%d\t%d\t%d\t%d\n", i+1, bt[i],wt,wt+bt[i]);
wt+=bt[i];
avg2+=wt;
}
avg1=avg1/n, avg2/=n;
printf("Avg1 = %d\n",avg1);
printf("Avg2 = %d\n",avg2);
}
CSE_4_B1
ITS Engineering College

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the no of process: 3
Test Case - 1
Enter the burst time: 2

Page No:
Enter the burst time: 4
Enter the burst time: 6
process id bt wt tt
1 2 0 2

ID: 2002220100079
2 4 2 6
3 6 6 12
Avg1 = 2
Avg2 = 6

Test Case - 2

User Output
Enter the no of process: 5
Enter the burst time: 4
Enter the burst time: 6
Enter the burst time: 8
Enter the burst time: 70
Enter the burst time: 29
process id bt wt tt
1 4 0 4
2 6 4 10
3 8 10 18
4 70 18 88
5 29 88 117
Avg1 = 24
Avg2 = 47

CSE_4_B1
ITS Engineering College
S.No: 5 Exp. Name: Write the code for implementing the sequential file allocation method Date: 2022-05-21

Page No:
Aim:
Write a C program for implementing the sequential file allocation method.

ALGORITHM:

ID: 2002220100079
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations to each in sequential order a). Randomly select a location from available
location s1= random(100);
a) Check whether the required locations are free from the selected location.
if (b[s1].flag == 0) {
for (j=s1;j<s1+p[i];j++) {
if ((b[j].flag) == 0)
count++;
}
if (count == p[i])
break;
}
b) Allocate and set flag=1 to the allocated locations.
for (s = s1; s < (s1 + p[i]); s++) {
k[i][j] = s;
j = j + 1;
b[s].bno = s;
b[s].flag = 1;
}
Step 5: Print the results file no, length, Blocks allocated.
Step 6: Stop the program
Source Code:

sequentialFileAllocation.c

#include<stdio.h>
int main()
{
int f[50],i,st,j,len,c,k;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("Enter the starting block & length of file: ");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
CSE_4_B1

{
f[j]=1;
printf("%d->%d\n",j,f[j]);
}
ITS Engineering College

else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("The file is allocated to disk");
printf("\nIf u want to enter more files?(y-1/n-0): ");
scanf("%d",&c);
if(c==1)

Page No:
goto X;
}

ID: 2002220100079
Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the starting block & length of file: 4 10
4->1 1
5->1 1
6->1 1
7->1 1
8->1 1
9->1 1
10->1 1
11->1 1
12->1 1
13->1 1
The file is allocated to disk 1
If u want to enter more files?(y-1/n-0): 1
Enter the starting block & length of file: 8 6
Block already allocated 0
If u want to enter more files?(y-1/n-0): 0

CSE_4_B1
ITS Engineering College
S.No: 6 Exp. Name: Write the code to implement linked file allocation technique Date: 2022-05-21

Page No:
Aim:
Write a C program to implement a linked file allocation technique.

ALGORITHM:

ID: 2002220100079
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q= random(100);
a) Check whether the selected location is free.
b) If the location is free allocate and set flag=1 to the allocated locations.
While allocating next location address to attach it to the previous location
for (i = 0; i < n; i++) {
for (j = 0; j < s[i]; j++) {
q = random(100);
if (b[q].flag == 0)
b[q].flag = 1;
b[q].fno = j;
r[i][j] = q;
if (j > 0) {
}
}
p = r[i][j - 1];
b[p].next = q;
}
Step 5: Print the results file no, length, Blocks allocated.
Step 6: Stop the program.
Source Code:

linkedFileAllocation.c

#include<stdio.h>
int main()
{
int f[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated: ");
scanf("%d",&p);
printf("Enter the blocks no's that are already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
CSE_4_B1

f[a]=1;
}
X:
printf("Enter the starting index block & length: ");
ITS Engineering College

scanf("%d%d", &st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d->%d\n",j,f[j]);
}
else

Page No:
{
printf("%d->file is already allocated\n",j);
k++;
}

ID: 2002220100079
}
printf("If u want to enter one more file? (yes-1/no-0): ");
scanf("%d",&c);
if(c==1)
goto X;
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter how many blocks that are already allocated: 3
Enter the blocks no's that are already allocated: 4 7 9
Enter the starting index block & length: 3 7
3->1 1
4->file is already allocated 1
5->1 1
6->1 1
7->file is already allocated 1
8->1 1
9->file is already allocated 1
10->1 1
11->1 1
12->1 1
If u want to enter one more file? (yes-1/no-0): 1
Enter the starting index block & length: 8 5
8->file is already allocated 0
9->file is already allocated 0
10->file is already allocated 0
11->file is already allocated 0
CSE_4_B1

12->file is already allocated 0


13->1 0
14->1 0
ITS Engineering College

15->1 0
16->1 0
17->1 0
If u want to enter one more file? (yes-1/no-0): 0

Test Case - 2
Test Case - 2

User Output

Page No:
Enter how many blocks that are already allocated: 7
Enter the blocks no's that are already allocated: 15 89 32 14 56 21 38
Enter the starting index block & length: 1 15

ID: 2002220100079
1->1 1
2->1 1
3->1 1
4->1 1
5->1 1
6->1 1
7->1 1
8->1 1
9->1 1
10->1 1
11->1 1
12->1 1
13->1 1
14->file is already allocated 1
15->file is already allocated 1
16->1 1
17->1 1
If u want to enter one more file? (yes-1/no-0): 1
Enter the starting index block & length: 19 6
19->1 1
20->1 1
21->file is already allocated 1
22->1 1
23->1 1
24->1 1
25->1 1
If u want to enter one more file? (yes-1/no-0): 1
Enter the starting index block & length: 34 8
34->1 1
35->1 1
36->1 1
37->1 1
38->file is already allocated 1
39->1 1
40->1 1
41->1 1
CSE_4_B1

42->1 1
If u want to enter one more file? (yes-1/no-0): 1
Enter the starting index block & length: 40 12
ITS Engineering College

40->file is already allocated 0


41->file is already allocated 0
42->file is already allocated 0
43->1 0
44->1 0
45->1 0
46->1 0
Test Case - 2
47->1 0

Page No:
48->1 0
49->1 0
50->file is already allocated 0
51->file is already allocated 0

ID: 2002220100079
52->file is already allocated 0
53->1 0
54->file is already allocated 0
55->file is already allocated 0
56->file is already allocated 0
57->file is already allocated 0
58->file is already allocated 0
59->file is already allocated 0
60->1 0
61->1 0
62->file is already allocated 0
63->file is already allocated 0
64->file is already allocated 0
65->file is already allocated 0
66->file is already allocated 0
67->file is already allocated 0
68->file is already allocated 0
69->file is already allocated 0
70->file is already allocated 0
71->file is already allocated 0
72->file is already allocated 0
73->1 0
74->file is already allocated 0
75->file is already allocated 0
76->1 0
If u want to enter one more file? (yes-1/no-0): 0

CSE_4_B1
ITS Engineering College
S.No: 7 Exp. Name: Write the code to implement allocation method using chained method Date: 2022-05-21

Page No:
Aim:
Write a C program to implement the allocation method using the chained method.

ALGORITHM:

ID: 2002220100079
Step 1: Start the program.
Step 2: Get the number of files.
Step 3: Get the memory requirement of each file.
Step 4: Allocate the required locations by selecting a location randomly q= random(100);
a) Check whether the selected location is free.
b) If the location is free allocate and set flag=1 to the allocated locations.
q = random(100);
if (b[q].flag == 0)
b[q].flag = 1;
b[q].fno = j;
r[i][j] = q;
Step 5: Print the results file no, length, Blocks allocated.
Step 6: Stop the program
Source Code:

chainedMethod.c

#include<stdio.h>
int f[50],i,j,k,inde[50],n,c,count=0,p;
int main()
{
for(i=0;i<50;i++)
f[i]=0;
x:
printf("Enter index block: ");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("Enter no of files on index: ");
scanf("%d",&n);
}
else
{
goto x;
}
printf("Enter the files: ");
for(i=0;i<n;i++)
CSE_4_B1

scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
ITS Engineering College

printf("Block already allocated\n");


goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("Allocated");
printf("\nFile indexed\n");
for(k=0;k<n;k++)
printf("%d->%d:%d\n",p,inde[k],f[inde[k]]);
printf("Enter 1 to enter more files and 0 to exit: ");

Page No:
scanf("%d",&c);
if(c==1)
goto
x;

ID: 2002220100079
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter index block: 9
Enter no of files on index: 3
Enter the files: 1 2 3
Allocated 1
File indexed 1
9->1:1 1
9->2:1 1
9->3:1 1
Enter 1 to enter more files and 0 to exit: 1
Enter index block: 5
Enter no of files on index: 5
Enter the files: 5 6 7 23 14
Block already allocated 12
Enter index block: 12
Enter no of files on index: 6
Enter the files: 24 36 78 21 54 46
Allocated 0
File indexed 0
12->24:1 0
12->36:1 0
12->78:1 0
12->21:1 0
12->54:1 0
CSE_4_B1

12->46:1 0
Enter 1 to enter more files and 0 to exit: 0

Test Case - 2
ITS Engineering College

User Output
Enter index block: 5
Enter no of files on index: 6
Enter the files: 14 69 32 17 35 48
Allocated 1
Test Case - 2
File indexed 1

Page No:
5->14:1 1
5->69:1 1
5->32:1 1
5->17:1 1

ID: 2002220100079
5->35:1 1
5->48:1 1
Enter 1 to enter more files and 0 to exit: 1
Enter index block: 7
Enter no of files on index: 5
Enter the files: 18 37 49 24 39
Allocated 0
File indexed 0
7->18:1 0
7->37:1 0
7->49:1 0
7->24:1 0
7->39:1 0
Enter 1 to enter more files and 0 to exit: 0

CSE_4_B1
ITS Engineering College
Exp. Name: Write the code to implement Worst fit, best fit, first fit algorithm for memory
S.No: 8 Date: 2022-05-21
management

Page No:
Aim:
Write a C program to implement the Worst fit, best fit, first fit algorithm for memory management.

ID: 2002220100079
ALGORITHM:
1. Start the program.
2. Get the segment size, the number of processes to be allocated, and their corresponding size.
3. Get the options. If the option is ‘2’ call the first fit function.
4. If the option is ‘1’ call the best fit function. Otherwise exit.
5. For the first fit, allocate the process to the first possible segment which is free, and set the personnel slap
as ‘1’. So that none of process to be allocated to segment which is already allocated and vice versa.
6. For best fit, do the following steps.
7. Sorts the segments according to their sizes.
8. Allocate the process to the segment which is equal to or slightly greater than the process size and set the
flag as the ‘1’.So that none of the process to be allocated to the segment which is already allocated and vice
versa.
9. Stop the program
Source Code:

memoryManagement.c

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("Enter the number of blocks: ");
scanf("%d",&nb);
printf("Enter the number of files: ");
scanf("%d",&nf);
printf("Enter the size of the blocks\n");
for(i=1;i<=nb;i++)
{
printf("Block %d: ",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files\n");
for(i=1;i<=nf;i++)
{
printf("File %d: ",i);
CSE_4_B1

scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
ITS Engineering College

for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;

Page No:
}
}
}
frag[i]=highest;

ID: 2002220100079
bf[ff[i]]=1;
highest=0;
}
printf("File_no\tFile_size\tBlock_no\tBlock_size\tFragement\n");
for(i=1;i<=nf;i++)
printf("%d\t%d\t%d\t%d\t%d\n",i,f[i],ff[i],b[ff[i]],frag[i]);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks 5
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files 1
File 1: 1
File 2: 4
File_no File_size Block_no Block_size Fragement
1 1 3 7 6
2 4 1 5 1

Test Case - 2

User Output
Enter the number of blocks: 7
Enter the number of files: 7
CSE_4_B1

Enter the size of the blocks 24


Block 1: 24
Block 2: 31
ITS Engineering College

Block 3: 49
Block 4: 28
Block 5: 36
Block 6: 19
Block 7: 45
Enter the size of the files 26
File 1: 26
Test Case - 2
File 2: 47

Page No:
File 3: 63
File 4: 40
File 5: 35
File 6: 10

ID: 2002220100079
File 7: 90
File_no File_size Block_no Block_size Fragement
1 26 3 49 23
2 47 0 144 0
3 63 0 144 0
4 40 7 45 5
5 35 5 36 1
6 10 2 31 21
7 90 0 144 0

CSE_4_B1
ITS Engineering College
S.No: 9 Exp. Name: Write the code to implement and simulate the MFT algorithm Date: 2022-05-22

Page No:
Aim:
Write a C program to implement and simulate the MFT algorithm.

ALGORITHM:

ID: 2002220100079
Step1: Start the process.
Step2: Declarevariables.
Step3: Enter total memory size ms.
Step4: Allocate memory for os. Ms = ms - os
Step5: Read the no partition to be divided n Partition size = ms / n.
Step6: Read the process no and process size.
Step 7: If process size is less than partition size allot alseblocke the process. While allocating update memory
wastage-external fragmentation.
if (pn[i] == pn[j])
f=1;
if (f == 0) {
if (ps[i] <= size) {
extft = extft + size - ps[i];
avail[i] = 1;
count++;
}
}
Step 8: Print the results
Source Code:

mft.c

#include<stdio.h>
#include<conio.h>
int main()
{
int ms,bs,nob,ef,n,mp[10],tif=0;
int i,p=0;
printf("Enter the total memory available (in Bytes): ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes): ");
scanf("%d",&bs);
nob=ms/bs;
ef=ms-nob*bs;
printf("Enter the number of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
CSE_4_B1

printf("Enter memory required for process %d (in Bytes): ",i+1);


scanf("%d",&mp[i]);
}
printf("No. of blocks available\tin memory %d\n",nob);
ITS Engineering College

printf("Process\tMemory Required\tAllocated\tInternal Fragmentation\n");


for(i=0;
i<n && p<nob;i++)
{
printf("%d\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\tNO\t------\n");
else
{
printf("\tYES\t%d\n",bs-mp[i]);

Page No:
tif = tif+bs-mp[i];
p++;
}
}

ID: 2002220100079
if(i<n) printf("Memory is Full, Remaining Processes cannot be accomodated
\n");
printf("Total Internal Fragmentation is %d\n",tif);
printf("Total External Fragmentation is %d\n",ef);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the total memory available (in Bytes): 1000
Enter the block size (in Bytes): 300
Enter the number of processes: 5
Enter memory required for process 1 (in Bytes): 257
Enter memory required for process 2 (in Bytes): 400
Enter memory required for process 3 (in Bytes): 290
Enter memory required for process 4 (in Bytes): 293
Enter memory required for process 5 (in Bytes): 100
No. of blocks available in memory 3
Process Memory Required Allocated Internal Fragmentation
1 257 YES 43
2 400 NO ------
3 290 YES 10
4 293 YES 7
Memory is Full, Remaining Processes cannot be accomodated
Total Internal Fragmentation is 60
Total External Fragmentation is 100

Test Case - 2

User Output
CSE_4_B1

Enter the total memory available (in Bytes): 6000


Enter the block size (in Bytes): 1000
Enter the number of processes: 9
ITS Engineering College

Enter memory required for process 1 (in Bytes): 245


Enter memory required for process 2 (in Bytes): 1354
Enter memory required for process 3 (in Bytes): 234
Enter memory required for process 4 (in Bytes): 43
Enter memory required for process 5 (in Bytes): 1423
Enter memory required for process 6 (in Bytes): 486
Test Case - 2
Enter memory required for process 7 (in Bytes): 12

Page No:
Enter memory required for process 8 (in Bytes): 321
Enter memory required for process 9 (in Bytes): 465
No. of blocks available in memory 6
Process Memory Required Allocated Internal Fragmentation

ID: 2002220100079
1 245 YES 755
2 1354 NO ------
3 234 YES 766
4 43 YES 957
5 1423 NO ------
6 486 YES 514
7 12 YES 988
8 321 YES 679
Memory is Full, Remaining Processes cannot be accomodated
Total Internal Fragmentation is 4659
Total External Fragmentation is 0

CSE_4_B1
ITS Engineering College
S.No: 10 Exp. Name: Write the code to simulate the MVT algorithm Date: 2022-05-22

Page No:
Aim:
Write a C program to simulate the MVT algorithm.

ALGORITHM:

ID: 2002220100079
Step1: Start the process.
Step2: Declarevariables.
Step3: Enter total memory size ms.
Step4: Allocate memory for os. Ms=ms-os
Step5: Read the no partition to be divided n Partition size = ms / n.
Step6: Read the process no and process size.
Step 7: If process size is less than partition size allot alseblocke the process. While allocating update memory
wastage-external fragmentation.
if (pn[i] == pn[j])
f = 1;
if (f == 0) {
if (ps[i] <= size) {
extft = extft + size - ps[i];
avail[i] = 1;
count++;
}
}
Step 8: Print the results
Step 9: Stop the process.
Source Code:

mvt.c

#include<stdio.h>
#include<conio.h>
int main()
{
int ms,mp[10],i,temp,n=0;
char ch='y';
printf("Enter the total memory available (in Bytes): ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("Enter memory required for process %d (in Bytes): ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
CSE_4_B1

printf("Memory is allocated for Process %d\n",i+1);


temp = temp-mp[i];
}
else
ITS Engineering College

{
printf("Memory is Full\n");
break;
}
printf("Do you want to continue(y/n): ");
scanf(" %c",&ch);
}
printf("Total Memory Available %d\n",ms);
printf("Process\tMemory Allocated\n");
for(i=0;i<n;i++)

Page No:
printf("%d\t%d\n",i+1,mp[i]);
printf("Total Memory Allocated is %d\n",ms-temp);
printf("Total External Fragmentation is %d\n",temp);
}

ID: 2002220100079
Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the total memory available (in Bytes): 1000
Enter memory required for process 1 (in Bytes): 400
Memory is allocated for Process 1 y
Do you want to continue(y/n): y
Enter memory required for process 2 (in Bytes): 275
Memory is allocated for Process 2 y
Do you want to continue(y/n): y
Enter memory required for process 3 (in Bytes): 550
Memory is Full
Total Memory Available 1000
Process Memory Allocated
1 400
2 275
Total Memory Allocated is 675
Total External Fragmentation is 325

Test Case - 2

User Output
Enter the total memory available (in Bytes): 10000
Enter memory required for process 1 (in Bytes): 2546
Memory is allocated for Process 1 y
Do you want to continue(y/n): y
Enter memory required for process 2 (in Bytes): 1543
CSE_4_B1

Memory is allocated for Process 2 y


Do you want to continue(y/n): y
Enter memory required for process 3 (in Bytes): 3456
Memory is allocated for Process 3 y
ITS Engineering College

Do you want to continue(y/n): y


Enter memory required for process 4 (in Bytes): 213
Memory is allocated for Process 4 y
Do you want to continue(y/n): y
Enter memory required for process 5 (in Bytes): 4563
Memory is Full
Test Case - 2
Total Memory Available 10000

Page No:
Process Memory Allocated
1 2546
2 1543
3 3456

ID: 2002220100079
4 213
Total Memory Allocated is 7758
Total External Fragmentation is 2242

CSE_4_B1
ITS Engineering College
S.No: 11 Exp. Name: Write the code to implement a resource allocation graph (RAG) Date: 2022-06-09

Page No:
Aim:
Write a C program to implement a resource allocation graph (RAG)
Source Code:

ID: 2002220100079
rag.c

#include<stdio.h>
static int mark[20];
int i,j,np,nr;
int main()
{
int alloc[10][10],request[10][10],avail[10], r[10],w[10];
printf("Enter the no of process: ");
scanf("%d",&np);
printf("Enter the no of resources: ");
scanf("%d", &nr);
for(i=0;i<nr;i++)
{
printf("Total Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("Enter the request matrix\n");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
printf("Enter the allocation matrix\n");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
CSE_4_B1

if (alloc[i][j]==0)
count++;
else
break;
ITS Engineering College

}
if(count==nr)mark[i]=1;

}
for(j=0;j<nr;j++)w[j]=avail[j];
for(i=0;i<np;i++)
{
int canbeprocessed=0;

Page No:
if (mark[i]!=1)

{
for(j=0;j<nr;j++)

ID: 2002220100079
{
if(request[i][j]<=w[j])
{
canbeprocessed=0;
break;
}
}
if(canbeprocessed){mark[i]=1;
for(j=0;j<nr;j++)w[j]+=alloc[i][j];}}}
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)deadlock=1;
if(deadlock)
printf("Deadlock detected");
else
printf("No Deadlock possible");

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the no of process: 4
Enter the no of resources: 5
Total Amount of the Resource R1: 2
Total Amount of the Resource R2: 1
Total Amount of the Resource R3: 1
Total Amount of the Resource R4: 2
Total Amount of the Resource R5: 1
Enter the request matrix 0 1 0 0 1
CSE_4_B1

0 0 1 0 1 0 0 1 0 1
0 0 0 0 1 0 0 0 0 1
1 0 1 0 1 1 0 1 0 1
Enter the allocation matrix 1 0 1 1 0
ITS Engineering College

1 1 0 0 0 1 1 0 0 0
0 0 0 1 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0
Deadlock detected

Test Case - 2
Test Case - 2

User Output

Page No:
Enter the no of process: 2
Enter the no of resources: 2
Total Amount of the Resource R1: 1

ID: 2002220100079
Total Amount of the Resource R2: 2
Enter the request matrix 1 0
0 1 0 1
Enter the allocation matrix 0 1
1 0 1 0
Deadlock detected

CSE_4_B1
ITS Engineering College
S.No: 12 Exp. Name: Write the code to implement Banker's algorithm Date: 2022-06-09

Page No:
Aim:
Write a C program to implement Banker's algorithm.

ALGORITHM:

ID: 2002220100079
1. Start the program
2. Attacking Mutex condition: never grant exclusive access. But this may not be possible for several
resources.
3. Attacking preemption: not something you want to do.
4. Attacking hold and wait for condition: make a process hold at the most 1resource
5. At a time. Make all the requests at the beginning. Nothing policy. If you feel, retry.
6. Attacking circular wait: Order all the resources. Make sure that the requests are issued in the
7. Correct order so that there are no cycles present in the resource graph. Resources numbered 1 ...n.
8. Resources can be requested only in increasing
9. Order.i.e.you cannot request are source whose noiseless than any you may be holding.
10. Stop the program
Source Code:

bankersAlgorithm.c

#include<stdio.h>
int max[10][10],alloc[10][10], need[10][10];
int avail[10],i,j,p,r,finish[10]={0},flag=0;
int main()
{
printf("Enter no. of processes: ");
scanf("%d",&p);
printf("Enter no. of resources: ");
scanf("%d",&r);
printf("Enter allocation matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("Enter max matrix");
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("Enter available matrix\n");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
CSE_4_B1

fun();
if(flag==0)
{
if(finish[i]!=1)
ITS Engineering College

{
printf("Failing: Mutual exclusion\n");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}
fun();
printf("By allocating required resources to process %d deadlock is prev

Page No:
ented\n",i);
printf("Lack of preemption\n");
for(j=0;j<r;j++)
{

ID: 2002220100079
if(avail[j]<need[i][j])
avail[j]=need[i][j];
alloc[i][j]=0;

}
fun();
printf("Daedlock is prevented by allocating needed resources\n");
printf("Failing: Hold and Wait condition\n");
for(j=0;j<r;j++)
{
if(avail[j]<need[i][j])
avail[j]=need[i][j];
}

fun();
printf("Avoiding any one of the condition, you can prevent deadlock
\n");
}
}
return 0;
}fun()
{
while(1)
{
for(flag=0,i=0;i<p;i++)
{
if(finish[i]==0)
{
for(j=0;j<r;j++)

{
if(need[i][j]<=avail[j])
continue;
else

break;
}
if(j==r)
CSE_4_B1

{
for(j=0;j<r;j++)
avail[j]+=alloc[i][j];
flag=1;
ITS Engineering College

finish[i]=1;
}
}
}
if(flag==0)
break;
}
return 0;
}

Page No:
ID: 2002220100079
Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter no. of processes: 3
Enter no. of resources: 2
Enter allocation matrix 2 4 5
3 4 5 3 4 5
Enter max matrix 4 3 4
5 6 1 5 6 1
Enter available matrix 2 4
Failing: Mutual exclusion
By allocating required resources to process 3 deadlock is prevented
Lack of preemption
Daedlock is prevented by allocating needed resources
Failing: Hold and Wait condition
Avoiding any one of the condition, you can prevent deadlock

Test Case - 2

User Output
Enter no. of processes: 2
Enter no. of resources: 2
Enter allocation matrix 2 6
7 3 7 3
Enter max matrix 4 6
7 1 7 1
Enter available matrix 9 3
Failing: Mutual exclusion
By allocating required resources to process 2 deadlock is prevented
Lack of preemption
CSE_4_B1

Daedlock is prevented by allocating needed resources


Failing: Hold and Wait condition
Avoiding any one of the condition, you can prevent deadlock
ITS Engineering College
S.No: 13 Exp. Name: Write the code to implement producer/consumer problem using semaphore Date: 2022-06-09

Page No:
Aim:
Write a C program to implement producer/consumer problems using semaphore.

ALGORITHM:

ID: 2002220100079
1. Declare a variable for producer & consumer as pthread - t - tid produce tid consume.
2. Declare a structure to add items, semaphore variable set as a struct.
3. Read the number of items to be produced and consumed.
4. Declare and define the semaphore function for the creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer andconsumer.
8. Stop the execution.
Source Code:

producerConsumer.c

#include<stdio.h>
void main()
{
int buffer[10], bufsize, in , out , produce , consume , choice=0;
in=0;
out=0;
bufsize=10;
while(choice!=3)
{
printf("1.Produce\t2.Consume\t3.Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("Buffer is Full\n");
else
{
printf("Enter the value: ");
scanf("%d",&produce);
buffer[in] = produce;
in= (in+1)%bufsize;
}
break;
case 2: if(in==out)
printf("Buffer is empty\n");
CSE_4_B1

else
{
consume = buffer[out];
printf("The consumed value is %d\n",consume);
ITS Engineering College

out = (out+1)%bufsize;
}
break;
}
}}
Page No:
Execution Results - All test cases have succeeded!

ID: 2002220100079
Test Case - 1

User Output
1.Produce 2.Consume 3.Exit 2
Enter your choice: 2
Buffer is empty 1
1.Produce 2.Consume 3.Exit 1
Enter your choice: 1
Enter the value: 100
1.Produce 2.Consume 3.Exit 2
Enter your choice: 2
The consumed value is 100 3
1.Produce 2.Consume 3.Exit 3
Enter your choice: 3

Test Case - 2

User Output
1.Produce 2.Consume 3.Exit 2
Enter your choice: 2
Buffer is empty 1
1.Produce 2.Consume 3.Exit 1
Enter your choice: 1
Enter the value: 54
1.Produce 2.Consume 3.Exit 1
Enter your choice: 1
Enter the value: 65
1.Produce 2.Consume 3.Exit 2
Enter your choice: 2
The consumed value is 54 2
1.Produce 2.Consume 3.Exit 2
Enter your choice: 2
The consumed value is 65 2
1.Produce 2.Consume 3.Exit 2
Enter your choice: 2
CSE_4_B1

Buffer is empty 1
1.Produce 2.Consume 3.Exit 1
Enter your choice: 1
ITS Engineering College

Enter the value: 695


1.Produce 2.Consume 3.Exit 2
Enter your choice: 2
The consumed value is 695 3
1.Produce 2.Consume 3.Exit 3
Enter your choice: 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy