Aakash Os
Aakash Os
NAME : AAKASH K
YEAR/SEM/SEC : III/V/A
AAKASH K 211191101001
BONAFIDE CERTIFICATE
AAKASH K 211191101001
TABLE OF CONTENTS
STAFF SIGN
S.NO DATE NAME OF THE PROGRAM PAGE.NO
AAKASH K 211191101001
Ex.No:1
UNIX INTRODUCTION
DATE:29/06/2023
AIM:
UNIX Commands
General Commands:
2. who: Gives the details of the user who have logged into the system.
$ who abc
tty() oct 15 11:17
Xyz tty4 oct 15 11:30
3. whoami: Gives the details regarding the login time and system’s namefor
the connection being used.
$ whoami.
Raghu
4. man: It displays the manual page of our terminal with the command
‘man’ command name.
$ man who
5. head and tail: ‘head’ is used to display the initial part of the text fileand
‘tail’ is used to display the last part of the file.
$ head [-count] [filename]
$ tail [-count] [filename]
6. pwd: It displays the full path name for the current directory we are
working in.
$ pwd
/home/raghu
1
AAKASH K 211191101001
Options Description
known as a long format that displays detailed information about files and
-l
directories.
-a Represent all files Include hidden files and directories in the listing.
Sort files and directories by their last modification time, displaying the most
-t
recently modified ones first.
-r known as reverse order which is used to reverse the default order of listing.
-S Sort files and directories by their sizes, listing the largest ones first.
known as inode which displays the index number (inode) of each file and
-i
directory.
known as group which displays the group ownership of files and directories
-g
instead of the owner.
2
AAKASH K 211191101001
$ ls –t = lists in order of last modification time.
$ ls –a = lists all entries , including the hidden files.
$ ls –d = lists directory files instead of its contents.
$ ls –p = puts a slash after each directory.
$ ls –u = lists in order of last access time.
10. rmdir: It is use to remove the directory specified in the command line.
$ rmdir directory name
13.mv: This command is used to rename and move ordinary and directory
files.
$ mv file1 file2
$ mv directory1 directory2
15. rm: This command is used to remove one or more files from the
directory.This can be used to delete all files as well as directory.
$ rm [option…..] file
3
AAKASH K 211191101001
who = a – all users
g – group
o – others
u –user
[+/-/=] + adds
- removes
= assigns
[permission] r = read
w =write
x =execute
Ex.:: $ chmod 754 prog1.
18. wc:counts and displays the lines,words and characters in the files
specified.
$ wc
$ wc
$ wc
$ wc
Ex:: $ wc prog2.
3 9 60 prog2.
4
AAKASH K 211191101001
21. paste: merges the corresponding lines of the given files.
$ paste –d file1 file2
Option – d allows replacing tab character by one or more alternate
characters.
Ex:: paste prog1 prog2
Ex :: $ ls – l | sort –n
5
AAKASH K 211191101001
The Vi Editor
The vi editor is a line-oriented editor and is not very easy to use. But it is
simple and you can learn enough commands to use it for editing your Java
programs.
You need some way to open an existing file , to save your work , to
save and exit and to abandon an attempt to edit a file without making any
changes(just in case you really mess up). Precede these with an Esc.
6
AAKASH K 211191101001
OUTPUT:
7
AAKASH K 211191101001
8
AAKASH K 211191101001
9
AAKASH K 211191101001
Result:
Thus the Basics of Unix operating system , commands and vi have been
studied successfully.
10
AAKASH K 211191101001
Ex.No:2
SHELL PROGRAMMING
DATE:06/07/2023
Aim:
To find the sum of two given numbers.
Algorithm:
Step 1: Start
Step 4: Add the two numbers and assign the result to sum.Step
5: Display sum
Step 6: Stop
11
AAKASH K 211191101001
Program
12
AAKASH K 211191101001
INPUT:
OUTPUT:
13
AAKASH K 211191101001
RESULT:
Thus the sum of two numbers were implemented and output was
verified successfully.
14
AAKASH K 211191101001
2(B) - FIBONACCI SERIES
Aim:
To find the Fibonacci series of the given number.
Algorithm:
Step 1: Start
5.1: temp←second_term
5.3 : first_term←temp
Step 6: Stop
15
AAKASH K 211191101001
Program:
16
AAKASH K 211191101001
INPUT:
[student@localhost student]$ sh fibo
enter a number
5
OUTPUT:
17
AAKASH K 211191101001
RESULT:
Thus the Fibonacci series were implemented and output was verified
successful
18
AAKASH K 211191101001
2(C) - POSITIVE OR NEGATIVE
Aim:
To find whether the given number is positive or negative.
Algorithm:
Step 1: Start
4: If Number is >0
Then
Print “positive”else
if Number is<0
Print “Negative”Else
Print “zero”.
Step 5: Stop
19
AAKASH K 211191101001
Program:
20
AAKASH K 211191101001
INPUT:
[student@localhost student]$ sh positive
enter a number
-5
OUTPUT:
21
AAKASH K 211191101001
RESULT:
22
AAKASH K 211191101001
2(D) - GREATEST OF THREE NUMBERS
Aim:
To find the Greatest of the given three numbers.
Algorithm:
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b and If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
23
AAKASH K 211191101001
Program:
24
AAKASH K 211191101001
INPUT:
[student@localhost student]$ sh greatest
enter the three numbers
5
8
9
OUTPUT:
25
AAKASH K 211191101001
RESULT:
Thus the greatest of three numbers were implemented and ouput was
verified successfully.
26
AAKASH K 211191101001
2(E) - ARMSTRONG NUMBER
Aim:
To find whether the given number is an Armstrong number or not.
Algorithm:
Step 1: Start
Step 2: Input the value N
Step 3: Set SUM = 0
Step 4: Number = N
Step 5: Repeat While Number ≠ 0
SUM = (Number%10)**3 + SUM
Number = Number/10
27
AAKASH K 211191101001
Program:
28
AAKASH K 211191101001
INPUT:
[student@localhost student]$ sh armstrong
enter the number
153
OUTPUT:
29
AAKASH K 211191101001
RESULT:
Thus the Armstrong number were implemented and output was
verified successfully.
30
AAKASH K 211191101001
2(F) - FACTORIAL NUMBER
Aim :
To write a program to generate the factorial of given number.
Algorithm:
Step 1: Start
Step 5: Repeat the steps until i=n 5.1: fact←fact* i 5.2: i←i+1
Step 7: Stop.....
31
AAKASH K 211191101001
Program:
n=0
fact=1
g=0
y=1
echo "enter no to find the factorial:"
read n
g=$n
while [ $n -ge $y ]
do
fact=` expr $fact \* $n`
n=` expr $n - 1 `
done
echo "factorial for $g is $fact"
32
AAKASH K 211191101001
INPUT:
[student@localhost student]$ sh fact
enter no to find the factorial:
5
OUTPUT:
33
AAKASH K 211191101001
RESULT:
Thus the shell Programs were implemented and output was verified
successfully.
34
AAKASH K 211191101001
Ex.No:3
SYSTEM CALLS FORK
DATE:13/07/2023
Aim:
Algorithm:
35
AAKASH K 211191101001
Program :
#include<iostream.h>
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t child;
cout<<"pid:"<<getpid()<<"patent:"<<getppid()<<endl;
switch(child=fork())
{
case (pid_t) -1: perror ("fork");
break;
case (pid_t) 0: cout<<"child
created:pid:"<<getpid()<<"patent:"<<getppid()<<endl;
exit(0);
default:cout<<"parent after fork pid:"<<"child pid:"<<child<<endl;
}
return 0;
}
36
AAKASH K 211191101001
INPUT:
[student@localhost student]$ cc proc.c
OUTPUT:
37
AAKASH K 211191101001
RESULT:
Thus the Program was implemented and output was verified
successfully
38
AAKASH K 211191101001
Ex.No:4
CPU SCHEDULING POLICIES
DATE:27/07/2023
AIM:
39
AAKASH K 211191101001
Ex.No:4(A)
FIRST COME FIRST SERVED CPU SCHEDULING
DATE:27/07/2023
Aim:
To Implement CPU Scheduling Algorithms using first come first served
Algorithm:
1. Start the process.
2. Declare the array size.
3. Get the number of elements to be inserted.
4. Select the process that first arrived in the ready queue
5. Make the average waiting the length of next process.
6. Start with the first process from it’s selection as above and let other
process to be in queue.
7. Calculate the total number of burst time.
8. Display the values.
9. Stop the process.
40
AAKASH K 211191101001
Program:
#include<stdio.h>
main()
{
int i,n,w[10],e[10],b[10];
float wa=0,ea=0;
printf("\nEnter the no of jobs: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the burst time of job %d :",i+1);
scanf("%d",&b[i]);
if(i==0)
{
w[0]=0;
e[0]=b[0];
}
else
{
e[i]=e[i-1]+b[i];
w[i]=e[i-1];
}
}
printf("\n\n\tJobs\tWaiting time \tBursttime\tExecution time\n");
printf("\t \n");
for(i=0;i<n;i++)
{
printf("\t%d\t\t%d\t\t%d\t\t%d\n",i+1,w[i],b[i],e[i]);
wa+=w[i];
ea+=e[i];
}
wa=wa/n;
ea=ea/n;
printf("\n\nAverage waiting time is :%2.2f ms\n",wa);
printf("\nAverage execution time is:%2.2f ms\n\n",ea);}
41
AAKASH K 211191101001
INPUT:
OUTPUT:
42
AAKASH K 211191101001
RESULT:
Thus the Program was implemented and output was verified
successfully.
43
AAKASH K 211191101001
Ex.No:4(B) SHORTEST JOB FIRST CPU SCHEDULING
DATE:27/07/2023
Aim:
Algorithm:
44
AAKASH K 211191101001
Program:
#include<stdio.h>
struct sjfs
{
char pname[10];
int btime;
}proc[10],a;
void main( )
{
int temp=0,temp1=0,temp2;
char name[20];
float tt,awt;
printf("Enter the number of processes:\n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the process name: \n");
scanf("%s",&proc[i].pname);
printf("Enter the Burst time:\n");
scanf("%d",&proc[i].btime);
}
for(i=0;i<n;i++)
{
45
AAKASH K 211191101001
for(j=0;j<n;j++)
if(proc[i].btime<proc[j].btime)
{
a=proc[i];
proc[i]=proc[j];
proc[j]=a;
}
}
}
printf("-------------------- CPU SCHEDULING ALGORITHM - SJFS ---
");
temp=0;
for(i=0;i<n;i++)
{
temp=temp1+temp+proc[i].btime;
temp1=temp1+proc[i].btime;
temp2=temp1-proc[i].btime;
printf("\n\t %s \t %d ms \t %d ms \t %d
ms\n",proc[i].pname,proc[i].btime,temp2,temp1);
}
printf("
");
awt=(temp-temp1)/n;
46
AAKASH K 211191101001
tt=temp/n;
47
AAKASH K 211191101001
INPUT:
Enter the number of processes:
4
Enter the process name:
p1
Enter the Burst time:
5
Enter the process name:
p2
Enter the Burst time:
5
Enter the process name:
p3
Enter the Burst time:
6
Enter the process name:
p4
Enter the Burst time:
2
OUTPUT:
48
AAKASH K 211191101001
RESULT:
Thus the Program was implemented and output was verified
successfully.
49
AAKASH K 211191101001
Ex.No:4(C)
ROUND ROBIN CPU SCHEDULING
DATE:27/07/2023
Aim:
To Implement CPU Scheduling Algorithms using Round Robin
Algorithm:
1. Start the process.
2. Declare the array size.
3. Get the number of elements to be inserted.
4. Get the value.
5. Set the time sharing system with preemption.
6. Define quantum is defined from 10 to 100ms.
7. Declare the queue as a circular.
8. Make the CPU scheduler goes around the ready queue allocating CPUto
each
50
AAKASH K 211191101001
Program:
#include<stdio.h>
#include<malloc.h>
void line(int i)
{
int j;
for(j=1; j<=i; j++)
printf("-");
printf("\n");
}
struct process
{
int p_id;
int etime, wtime, tatime;
};
struct process *p, *tmp;
int i, j, k, l, n, time_slice, ctime;
float awtime=0, atatime=0;
int main()
{
printf("Process Scheduling - Round Robin \n");
line(29);
printf("Enter the no.of processes : ");
scanf("%d", &n);
printf("Enter the time slice : ");
scanf("%d", &time_slice);
printf("Enter the context switch time : ");
scanf("%d", &ctime);
p=(struct process*) calloc(n+1, sizeof(struct process));
tmp=(struct process*) calloc(n+1, sizeof(struct process));
for(i=1; i<=n; i++)
{
printf("Enter the execution time of process %d : ", i-1);
scanf("%d", &p[i].etime);
p[i].wtime=(time_slice+ctime)*i-1;
awtime += p[i].wtime;
p[i].p_id=i-1;
tmp[i]=p[i];
}
i=0; j=1; k=0;
while(i<n)
51
AAKASH K 211191101001
{
for(j=1; j<=n; j++)
{
if(tmp[j].etime <= time_slice && tmp[j].etime!=0)
{
k=k+tmp[j].etime;
tmp[j].etime=0;
p[j].tatime=k;
atatime += p[j].tatime;
k=k+ctime;
i++;
}
if(tmp[j].etime>time_slice && tmp[j].etime!=0)
{
k=k+time_slice+ctime;
tmp[j].etime -= time_slice;
}
}
}
awtime=awtime/n;
atatime=atatime/n;
printf("\nShedule \n");
line(60);
printf("Process\t\tExecution\tWait\t\tTurnaround\n");
printf("Id No\t\ttime\t\ttime\t\ttime\n");
line(60);
for(i=1;i<=n;i++)
printf("%7d\t%14d\t%8d\t%14d \n", p[i].p_id, p[i].etime,
p[i].wtime,
p[i].tatime);
line(60);
printf("Avg waiting time :\t%2f \n",awtime);
printf("Avg turn around time :\t%2f\n",atatime);
line(60);
return(0);
}
52
AAKASH K 211191101001
Input :
Output:
53
AAKASH K 211191101001
RESULT:
Thus the Program was implemented and output was verified
successfully.
54
AAKASH K 211191101001
Ex.No:4(D)
PRIORITY CPU SCHEDULING
DATE:27/07/2023
Aim:
To Implement Priority CPU Scheduling Algorithms
Algorithm:
1. Start the process.
2. Declare the array size.
3. Get the number of elements to be inserted.
4. Get the priority for each process and value
5. start with the higher priority process from it’s initial position let other
process to be queue.
6. Calculate the total number of burst time.
7. Display the values
8. Stop the process.
55
AAKASH K 211191101001
Program:
#include<stdio.h>
#include<malloc.h>
void line(int i)
{
int j;
for(j=1;j<=i;j++)
printf("-");
printf("\n");
}
struct process
{
int p_id,priority;
int etime,wtime,tatime;
};
struct process *p,temp;
int i,j,k,l,n;
float awtime=0,atatime=0;
int main()
{
printf("Priority Scheduling\n");
line(29);
printf("Enter the no of processes : ");
scanf("%d",&n);
p=(struct process *) calloc(n+1,sizeof(struct process));
p[0].wtime=0;
p[0].tatime=0;
for(i=1;i<=n;i++)
{
printf("Enter the execution time of process %d:",i-1);
scanf("%d",&p[i].etime);
printf("Enter the priority of process %d:",i-1);
scanf("%d",&p[i].priority);
p[i].p_id=i;
}
for(i=1;i<=n;i++)
for(j=1;j<=n-i;j++)
if(p[i].priority>p[j+1].priority)
{
temp=p[j];
p[j]=p[j+1];
56
AAKASH K 211191101001
p[j+1]=temp;
}
for(i=1;i<=n;i++)
{
p[i].wtime=p[i-1].tatime;
p[i].tatime=p[i-1].tatime+p[i].etime;
awtime+=p[i].wtime;
atatime+=p[i].tatime;
}
awtime=awtime/n;
awtime=atatime/n;
printf("\nSchedule\n");
line(60);
printf("Process\t\texection\twait\t\tturnaround\n");
printf("Id No\t\t time\t\ttime\t\ttime\n");
line(60);
for(i=1;i<=n;i++)
printf("%7d\t%14d\t%8d\t%14d\n",p[i].p_id,p[i].etime,p[i].tatime);
line(60);
printf("Avg waiting time:\t %2f\n",awtime);
printf("Avg turnaround time:\t %2f\n",atatime);
line(60);
return(0);
}
57
AAKASH K 211191101001
Input:
Priority Scheduling
Enter the no of processes : 3
Output:
58
AAKASH K 211191101001
RESULT:
Thus the Program was implemented and output was verified
successfully
59
AAKASH K 211191101001
Ex.No:5 INTERPROCESS COMMUNICATION USING SHARED
DATE:05/08/2023 MEMORY
Aim:
To implement Inter Process Communication using Message queue.
Algorithm:
60
AAKASH K 211191101001
Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define SHMKEY 75
#define K 1024
int main()
{
int shmid;
char *addr1;
printf("\n\t\tSENDING---USING SHARED MEMORY\n");
printf("\t\t`````````````````````````````````");
shmid=shmget(SHMKEY,128*K,IPC_CREAT|0777);
addr1=shmat(shmid,0,0);
printf("\n the address is:0x%x\n",addr1);
printf("\n enter the message to send:");
scanf("%s",addr1);
return(0);
}
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define SHMKEY 75
#define K 1024
int main()
{
int shmid;
char *addr1;
printf("\n\t\treceiving--USING SHARED MEMORY\n");
printf("\t\t`````````````````````````````````");
shmid=shmget(SHMKEY,128*K,IPC_CREAT|0777);
addr1=shmat(shmid,0,0);
printf("\n the address is:0x%x\n",addr1);
printf("\n the received message is:%s\n",addr1);
return(0);
}
61
AAKASH K 211191101001
OUTPUT:
62
AAKASH K 211191101001
RESULT:
The IPC using Shared memory segment is implemented and
verified successfully.
63
AAKASH K 211191101001
Ex.No:6
IMPLEMENTATION OF BANKER’S ALGORITHM
DATE:10/08/2023
Aim:
To write a C program to implement Bankers Algorithm.
Algorithm:
1. Start
2. Declare available(k) where is the instances of resource type , max[i,j]=k
where process i request at most k instances of resource type j and
allocation[i,j]=k where for process i k instances of resource type j are
allocated
3. Compute Need[i,j]=Max[i,j]-allocation[i,j]
4. Let work and finish be vectors of length i and j respectively.
5. Initialize Work=available
6. if finish[i]=false for all processes
7. Find a process i such that Finish [i] = false and Needi Work and if no
such i exists go to step 10
8. Work := Work + Allocationi
9. If Finish[i]=true go to step 7
10 . If finish[i]=true for all processes then display the system is in safe stateelse
unsafe state
64
AAKASH K 211191101001
Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,z;
int res[10];
int resource,process,boolean=0;
int
allocation[10][10],sel[10],max[10][10],need[10][10],work[10],work1[10]
;
int check=0;
int adpro;
int ar[10];
printf("Welcome to Bankers Algorithms");
printf("\n Enter the no .of processes:");
scanf("%d",&process);
printf("\n Enter the number of Resources");
scanf("%d",&resource);
for(i=0;i<resource;i++)
{
printf("Enter the instances of Resource %d:",i+1);
scanf("%d",&res[i]);
}
for(i=0;i<process;i++)
{
printf("\n Enter allocated resources for process %d:",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&allocation[i][j]);
}
}
for(i=0;i<10;i++)
sel[i]=-1;
for(i=0;i<process;i++)
{
printf("Enter maximum need of process: %d",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&max[i][j]);
65
AAKASH K 211191101001
}
}
for(i=0;i<process;i++)
for(j=0;j<resource;j++)
need[i][j]=max[i][j]-allocation[i][j];
printf("\n The Need is \n");
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
printf("\t%d",need[i][j]);
printf("\n");
}
printf("\n The Avalilable is ");
for(i=0;i<resource;i++)
{
work[i]=0;
for(j=0;j<process;j++)
work[i]=work[i]+allocation[j][i];
work[i]=res[i]-work[i];
}
for(z=0;z<process;z++)
{
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(work[j]>=need[i][j]&&sel[i]==-1)
{
boolean=1;
}
else
{
boolean=0;
break;
}
}
if(boolean==1)
{
sel[i]=1;
for(j=0;j<resource;j++)
{
work[j]=work[j]+allocation[i][j];
}
66
AAKASH K 211191101001
}
}
}
if(check==1)
printf("\n System is in Unsafe mode");
else
printf("\n System is in safe mode");
}
67
AAKASH K 211191101001
Input:
68
AAKASH K 211191101001
Output:
69
AAKASH K 211191101001
Result:
The Banker’s Algorithm was implemented and verified successfully.
70
AAKASH K 211191101001
Ex.No:7
IMPLEMENTAION OF DINING PHILOSOPHER’S PROBLEM
DATE:17/08/2023
Aim:
Write a C program to implement Dining Philosophers problem.
Program:
#include<stdio.h>
char state[10],self[10],spoon[10];
void test(int k)
{
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e'))
{
state[k]='e';
self[k]='s';
spoon[k]='n';
spoon[(k+4)%5]='n';
}
}
void pickup(int i)
{
state[i]='h'; test(i);
if(state[i]=='h')
{
self[i]='w';
}
}
void putdown(int i)
{
state[i]='t';
spoon[i]='s';
spoon[i-1]='s';
test((i+4)%5);
test((i+1)%5);
}
int main()
{
71
AAKASH K 211191101001
int ch,a,n,i;
printf("\t\t Dining Philosopher Problem\n");
for(i=0;i<5;i++)
{
state[i]='t';
self[i]='s';
spoon[i]='s';
}
printf("\t\t Initial State of Each Philosopher\n");
printf("\n\t Philosopher No.\t Think/Eat \tStatus \tspoon");
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit \n 2.Hungry\n3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
while(ch!=1)
{
switch(ch)
{
case 2:
{
printf("\n\t Enter which philosopher is hungry\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
pickup(n);
break;
}
case 3:
{
printf("\n\t Enter which philosopher is thinking\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
putdown(n);
break;
}
}
printf("\n\t State of Each philosopher\n\n");
printf("\n\t Philosoper No.\t Thinking\t Hungry");
72
AAKASH K 211191101001
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit\n 2.Hungry\n 3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
}}
73
AAKASH K 211191101001
Output:
74
AAKASH K 211191101001
Result:
The Dining philosophers Problem was implemented and verified.
75
AAKASH K 211191101001
Ex.No:8(A)
MEMORY ALLOCATION FIRST FIT MEMORY ALLOCATION
DATE:07/09/2023
Aim:
To write a C++ program to implement First Fit Memory allocation
technique.
Algorithm:
1. Start
2. Declare the no.of free blocks(holes) and their sizes.
3. Get the size of the file to be loaded.
4. Have to allocatet the first blocks that is big enough to hold the file.
5. Start search from the beginning of the set of holes.
6. If hole is large enough is found then stop searching and allocate the holeto
the file.
7. Otherwise display file that cannot be allocated.
76
AAKASH K 211191101001
Program:
#include<stdio.h>
#include<string.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
printf("\t\t MEMORY MANAGEMENT \n");
printf("\t\t =================\n");
printf("\tEnter the total no of blocks: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the size of blocks: ");
scanf("%d",&size[i]);
}
cho=0;
while(cho==0)
{
printf("\n Enter the size of the file: ");
scanf("%d",&m);
x=0;
for(i=1;i<=n;i++)
{
if(size[i]>=m)
{
printf("\n size can occupy %d",size[i]);
size[i]-=m;
x=i;
break;
}
}
if(x==0)
{
printf("\n\nBlock can't occupy\n\n");
}
printf("\n\nSNO\t\tAvailable block list\n") ;
for(i=1;i<=n;i++)
printf("\n\n%d\t\t\t%d",i,size[i]);
printf("\n\n Do u want to continue. ... (0-->yes/1-->no): ");
scanf("%d",&cho);
}}
77
AAKASH K 211191101001
Input:
Enter the total no of blocks: 4
Enter the size of blocks: 50
Enter the size of blocks: 20
Enter the size of blocks: 30
Enter the size of blocks: 40
Enter the size of the file: 25
Output:
78
AAKASH K 211191101001
RESULT:
Thus the memory allocation techniques first fit was implemented.
79
AAKASH K 211191101001
Ex.No:8(B)
BEST FIT MEMORY ALLOCATION
DATE:07/09/2023
Aim:
To write a C program to implement Best Fit Memory allocation technique.
Algorithm:
1. Start
2. Declare the no.of free blocks(holes) and their sizes.
3. Get the size of the file to be loaded.
4. Find the hole that is sufficiently enough for the file.
5. Start search from the beginning of the set of holes.
6. If the hole that is large enough is found then st6op searching and
allocate the hole to the file.
7. Otherwise display file cannot be allocated.
8. Stop.
80
AAKASH K 211191101001
Program:
#include<stdio.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
81
AAKASH K 211191101001
for(j=i+1;j<=n;j++)
if(sub[i]>sub[j])
{
t=sub[i];
sub[i]=sub[j];
sub[j]=t;
t=f[i];f[i]=f[j];
f[j]=t;
}
}
for(i=1;i<=n;i++)
{
if(size[f[i]]>=m)
{
printf("size can occupy %d : ",size[f[i]]);
size[f[i]]-=m;
x=i;
break;
}
}
if(x==0)
{
printf("block can't occupy");
}
printf("\n\nSNO\t\t Available Block size\n") ;
for(i=1;i<=n;i++)
printf("\n%d\t\t%d",i,size[i]);
printf("\n\n Do u want to continue. ... (0-->yes\t/1-->no)");
scanf("%d",&cho);
82
AAKASH K 211191101001
Input:
ENTER THE TOTAL NO OF blocks : 4
Enter the size of blocks : 50
Enter the size of blocks : 100
Enter the size of blocks : 200
Enter the size of blocks : 150
Enter the size of the file : 95
Output:
83
AAKASH K 211191101001
RESULT:
Thus the best fit memory allocation technique was implemented.
84
AAKASH K 211191101001
Ex.No:8(C)
WORST FIT MEMORY ALLOCATION
DATE:07/09/2023
Aim:
To write a C program to implement Worst Fit Memory allocation
technique.
Algorithm:
1. START
2. Declare the number of free blocks(holes) and their size.
3. Get the file size to be loaded.
4. Among the holes find the largest hole that is enough for the file.
5. Start search from the beginning of the set of holes.
6. If the hole is large enough is found then stoop searching and allocate the holeto
the file.
7. Otherwise display file cannot be allocated.
8. stop
85
AAKASH K 211191101001
Program:
#include<stdio.h>
int p[10]={0},m=0,x=0,b[10]={0},a[10]={0};
main()
{
int j=0,n=0,pra[10]={0},pro[10]={0},z[10],ch,flag,flag2,sum=0,sum2=0;int
c=0,i=0,k=0;
printf("\n\t\tMEMORY MANAGEMENT POLICIES\n");
printf("\n enter the no of process:\t");
scanf("%d",&n);
printf("\n enter the no of partition:\t");
scanf("%d",&m);
printf("\nprocess information\n");
for(i=0;i<n;i++)
{
printf("\n enter the memory required for process P%d:",i+1);
scanf("\t%d",&a[i]);
pro[i]=a[i];
}
printf("\n memory partition information\n");
for(j=0;j<m;j++)
{
printf("\n enter the block size of block B%d:",j+1);
scanf("\t%d",&p[j]);
pra[j]=p[j];
}
arrange();
printf("\n process partition\n\n");
86
AAKASH K 211191101001
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]<p[j])
{
printf("%d\t%d\n",a[i],p[j]);
p[j]=0;
flag=i;
break;
}
}
if(flag!=i)
printf("%d\t%s\n",a[i],"waiting");
arrange();
}
}
arrange()
{
int i,j,t;
for(i=0;i<m-1;i++)
for(j=0;j<m-i-1;j++)
{
if(p[j]<p[j+1])
{
t=p[j+1];
p[j+1]=p[j];
p[j]=t;
}}}
87
AAKASH K 211191101001
Input:
process information
88
AAKASH K 211191101001
Output:
89
AAKASH K 211191101001
RESULT:
Thus the Worst fit memory allocation technique was implemented.
90
AAKASH K 211191101001
Ex.No:9(A) PAGE REPLACEMENT
DATE:14/09/2023 FIRST IN FIRST OUT PAGE REPLACEMENT
Aim:
To write a C Program to implement FIFO page replacement algorithm.
Algorithm:
1. Start
2. Declare the no.of blocks and no. of page references.
3. If the referred page is in the main memory then refer it.
4. If the referred page is not in main memory and free blocks are availablethen
more the referred page to the existing free blocks.
5. Otherwise replace the first page that entered in main memory with the
referred page and increment page fault.
6. Stop
91
AAKASH K 211191101001
Program:
#include<stdio.h>
main()
{
int main_mem,cur=0,i=0,j,fault=0;
static int page[100],page_mem[100],flag,num;
for(i=0;i<100;i++)
page_mem[i]=-2;
printf("\n\t\t\t\t paging->fifo\n");
printf("\n\n Enter the number of pages in main memory ");
scanf("%d",&main_mem);
printf("\n enter no of page references");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n Enter page reference:");
scanf("%d",&page[i]);
}
printf("\n\t\t\t\t Fifo-> paging \n\n\n");
for(i=0;i<main_mem;i++)
printf("\t page %d",i+1);
for(i=0;i<num;i++)
{
for(j=0;j<main_mem;j++)
if(page[i]==page_mem[j])
{
flag=1;
break;
}
92
AAKASH K 211191101001
if(!flag)
{
page_mem[cur]=page[i];
fault++;
}
printf("\n\n");
for(j=0;j<main_mem;j++)
printf("\t%d",page_mem[j]);
if(!flag&&cur<main_mem-1)
cur++;
else if(!flag)
cur=0;
flag=0;
}
printf("\n\n-2 refers to empty blocks\n\n")
printf("\n\n No of page faults:%d\n",fault);
}
93
AAKASH K 211191101001
Input:
Enter the number of pages in main memory: 3
enter no of page references: 8
Enter page reference: 2
Enter page reference: 0
Enter page reference: 3
Enter page reference: 0
Enter page reference: 2
Enter page reference: 3
Enter page reference: 5
Enter page reference: 9
Output:
94
AAKASH K 211191101001
RESULT:
95
AAKASH K 211191101001
Ex.No:9(B) LEAST RECENTLY USED PAGE
DATE:14/09/2023 REPLACEMENT
Aim:
To write a C Program to implement LRU page replacement algorithm.
Algorithm:
1. Start
2. Declare the no.of blocks and no.of page references.
3. If the referred page is in the main memory then refer it.
4. If the referred page is not in main memory and free blocks are availablethen
more the referred page to the existing free blocks.
5. Otherwise replace the page which is not referred for a long time with the
referred page and increment page fault.
6. Stop
96
AAKASH K 211191101001
Program:
#include<stdio.h>
#include<stdlib.h>
#define max 100
int frame[10],count[10],cstr[max],tot,nof,fault;
main()
{
getdata();
push();
}
getdata()
{
int pno,i=0;
printf("\n\t\tL R U - Page Replacement Algorithm\n");
printf("\nEnter No. of Pages in main memory:");
scanf("%d",&nof);
printf("\nEnter the no of page references:\n");
scanf("%d",&pno);
for(i=0;i<pno;i++)
{printf("Enter page reference%d:",i);
scanf("%d",&cstr[i]);
}
tot=i;
for(i=0;i<nof;i++)
printf("\tpage%d\t",i);
}
push()
{
int x,i,j,k,flag=0,fault=0,nc=0,mark=0,maximum,maxpos=-1;
for(i=0;i<nof;i++)
{
frame[i]=-1;
count[i]=mark--;
}
for(i=0;i<tot;i++)
{
flag=0;
x=cstr[i];
nc++;
for(j=0; j<nof; j++)
{
for(k=0; k<nof;k++)
97
AAKASH K 211191101001
count[k]++;
if(frame[j]==x)
{
flag=1;
count[j]=1;
break;
}
}
if(flag==0)
{
maximum = 0;
for(k=0;k<nof;k++)
{
if(count[k]>maximum && nc>nof)
{
maximum=count[k];
maxpos = k;
}
}
if(nc>nof)
{
frame[maxpos]=x;
count[maxpos]=1;
}
else
frame[nc-1]=x;
fault++;
dis();
}
}
printf("\nTotal Page Faults :%d",fault);
}
dis()
{
int i=0;
printf("\n\n");
while(i<nof)
{
printf("\t%d\t",frame[i]);
i++;
}
}
98
AAKASH K 211191101001
Input:
99
AAKASH K 211191101001
Result:
Thus the page replacement algorithm LRU was implemented.
100
AAKASH K 211191101001