Os 3
Os 3
[ec2-user@ip-172-31-23-10-)$ vigccfirstfit.c
[[ec2-userl
ec2-userCipip-172-31-23-10
Bnter no. of -blocks:3
172-31-23-10-1$ 1$ /Ef firstfit.c-o fi
Enter size of each blocks 12
Enter no 0f
Enter size
processes:3
of each
4
process:
Block no. size
2 12 Process no
size
2 7S
Not allocated [ec2Userci231T23-l0
AIM: -Implementation of BEST FIT
EXPERIMENT-11
algorithm for memory management.
THEORY: -
Best-Fit Allocation is a memory allocation
to a process. In
Best-Fit, the technigueused in onerating systems to allocate memory
to find the block that is operating system searches through the list of free blocks of memory
closest in size to the memory reguest from
block is found, the theprocess. Once a suitabie
to the process, and operating system splits the block into two parts: the
the remaining free
block. portion that Will be alocated
Advantages of
smallest block of Best-Fit Allocation include improved memory utilization, as it allocates the
memory that is sufficient to
Additionally, Best-Fit can also
blocks of memory that are less help to accommodate the memory request from the process.
reduce memory fragmentation, as it tends to allocate smaller
likely to become
Disadvantages of fragmented.
the best-fit block Best-Fit Allocation
of memory can be include increased computational overhead, as the
algorithm. Additionally, Best-Fit may alsotime-consuming and requires a more search for
smaller blocks of memory result in increased complex search
Overall,
scattered throughout the memory
space.fragmentation, as it may leave
but its Best-Fit Allocation is a widely used memory
effectiveness may vary depending on the specifics
executed. allocation
of the technique operating
in
systems,
system and the workload
being
CODE: -
#include<stdio.h>
void main()
int fragment[20],b[20].p[20],ij,nb,np,temp,lowest-9999;
static int barray[20],parray[201;
printf("\n\t\t\tMemory Management Scheme - Best Fit"):
printf("\nEnter number of blocks:"):
thc
scanf("%d",&nb);
printf("Enter thenumber gfprocesses:");
scanf("od",&np);
printf("\nEnter the size of the blocks:-\n"):
for(i=1;i<=nb;it+)
printf("Block no.%d:",):
scanf("%d",&b[i]D;
}
printf("\nEnter the size of the processes :-\n");
for(i=1;i<-np;it+)
{
printf("Process no.%d:",);
scanf("%od",&p[il);
for(i=1;i<=np;it+)
for(j=1;j<-nb;j++)
if(barrayi]!=1)
temp-b[jil-p[i]:
if(temp>-0)
if(lowest>temp)
parray[i]-j:
lowesttemp;
fragment[i]=lowest:
barray[parfay[i]]=l;
lowest=10000;
OUTPUT: -
thesize of theaprocesses
Ente
Process no
PrOcess no2:4S
ProCess no.3:7
Blocksize ragmentL
PrOcess size Block no
Process no
1
1 2 [ec2-user@ip-172-31-2310 1
EXPERIMENT -12
AIM: Implementation of WORST FIT algorithm for memory management.
THEORY:
In this allocation technique, the process traverses the whole memory and always search Tor tne
largest hole/partition, and then the process is nlaced in that hole/partition. It is a slow proeess
because it has to traverse the entire memory to search the largest hole.
Here is an example to understand Worst Fit-Allocation
PROCESSSTATUS INTERNAL
MEMORY MEMORY PROCESS
SIZE
FRAGMENTATION
LOCALN BLOCK SZE NUMBER
45K Busy 5K
12345 50K P3
Busy None
P2 100K
45871 100K
30K Busy 370K
400K P1
1245
175K 375K
550K TOTAL
TOTAL
USED:
AVAILABLE:
void impliment WorstFit(int blockSize[], int blocks, int processSize(], int processes)
blocks
l/pick each process and find suitable
llaccording to its size ad assign to it
for (int i-0; i< processes; it+)
process
block fit to accomodate
1/ptáce it at the first
-1)
if (indexPlaccd==
indexPlaccd =:
/ if any future block is larger than the current block where
l process is placed, change the block and thus indexPlaced
clse if (blockSize[indexPlaced] < blockSize[j])
indexPlaccd = j;
Size\tBlock no.\n");
printf("\nProcess No.\tProcess
processes; it+)
for (int i= 0; i<
i+1, processSize[i):
printf("%d \tltlt %d \t\t\t",
if (allocation[i]!= -1)
printf("%d\n",allocation[i] + );
clsc
printf("Not Allocatcd\n"):
1/Driver code
int main()
OUTPUT:-
[ec2-user@ip-172-31-23-10~]$ vi wOrdtfit.c
Lec2-user@ip-172-31-23-10-]$ gcc Wordtfit.c -o wÊ
[ec2-user@ip-172-31-23-10 -1^/Wf
Process No Process Size Block No.
40
10
3 30 2
60 Not Allocated
EXPERIMENT -13
AIM:-Implementation of READER/WRITER problem using
semaphol
THEORY: -
The readers-writers problem relates to an ahiont euch as a fle that is
processes. Some of shared between muimple
these processes are readers i.e.,,they only want to read the data from the object
and some of the processes are
writers i.e. they want to write into the object.
The readers-writers problem is used to manage
the object data. For example - If two synchronization so that there are no problems Wu
readers access the
However, if two writers or a reader and writer access obiect
the
at the same time there 1s no proo
problems. obiect at the same time, there may Oo
To solve this situation, a writer should get
accessing the object, no reader or writer may exclusive
access it.
access to an object i.e. when a writer 15
object at the same time. However, multiple readers can access tne
This can be implemented using
reader-writer problem are given assemaphores.
follows -
The codes for the reader and writer
process in the
Reader Process
The code that defines the reader
process is given below
wait (mutex);
rc ++;
if (rc == 1)
wait (wrt);
signal(mutex);
wait(mutex);
rc --;
if (rc == 0)
signal (wrt);
signal (mutex);
Writer Process
The code that defines the writer process is given below:
wait(wrt);
signal(wrt);
CODE:-
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
W
Reader reading the shared resource
printf("Reader %d is reading.\n", id);
sleep(rand() %3 t 1);
I/ Reader releasing mutex to update
readCount
sem_wait(&mutex);
readCount--;
if (readCount == 0) {
sem _post(&mutex);
I/ Reader done reading
printf("Reader %d finished reading.\n", id);
sleep(rand() %3 + 1);
return NULL:
sem_post(&writeBlock);
return NULL;
int main() {
srand(time(NULL));
I| Initialize semaphores
sem_init(&mutex, 0, 1):
sem init(&writeBlock, 0, 1);
|/ Create reader threads
pthread t readers[NUM_READERS];
int reader ids[NUM_READERS];
for (int i=0; i< NUM READERS; ++i) {
reader ids[i]= i;
pthread create(&readers[i], NULL, reader, &reader ids•i):
thrcads to finish
1/ Waít for reader
NUM_READERS; ++))
for (int i= 0; i<
pthrcad_join(readers[i], NULL);
1/Wait for writer thrcads to
finish
for (inti=0; i < NUM WRITERS; ++i) {
pthrcad_join(writers[i], NULL);
I/ Destroy semaphores
sem_destroy(&mutex);
scm_destroy(&writcBlock);
return 0:
OUTPUT: -
MeiterHVLELg
Reador 2
EXPERIMENT- 14
deadlock avoidance.
AIM: - Implementation of BANKER'S algorithmfor
THEORY: - avoidance algorithmthat
tests for
deadlock all
The banker's algorithm is a resource allocation and maximum possible amounts of
safety by simulating the allocation for the predetermined deciding whether
possible activities, before
Iresources, then makes an "s-state" check to test for
allocation should be allowed to continue.
Advantages
algorithm:
Following are the cssential characteristics of the Banker's
each proceSS.
resources that meet the requirements of
I. t Contains various for upcoming
resource
2. the operating system be held.
Each process should provide informationtolong
and how the resources will
requests, the number of resources, resource
process requests for each type of
and control
3. It helps the operating system manage
in the computer system. process can hold
attribute that represents indicates each
has a Max resource
4. The algorithm in a systemn.
the maximum number of resources
CODE: -
#include <stdio.h>
int main()
if (flag == 0) {
ans[ind++]= i;
for (y = 0; y <m; y++)
avail[y] += alloc[iJ[y]:
f[i] = 1;
int flag= 1;
for(int i-0;i<n;i++)
if(f[i]-0)
flag-0;
printf("The following systemis-nof safe"):
break;
if(flag-1)
printf("Following
for (i
is the SAFE
Sequence\n");
=0; i
<n-1; it+)
printf(" P%d->", ans[i]);
printf(" P%d", ans[n - 1);
return (0):
OUTPUT: -
CODE:
inciuds <sáioh
#include <sálib.h
#includepthrced.h
inciude unistdh
PHILOSOPHERS 5
#define NUM
3
#dcfine NUM EATING ITERATIONS
forks[UM_PHILOSOPHERS]:
pthrcad rmutez t
ptfhrczdtphilosophers[NUM PHILOSOPHERS]:
"philosophervoid arg) !
void
*jarg:
int id = *(int
= id;
int leftfork NUM_PHILOSOPHERS:
Aa- 1) %
int ríght_fork EATING_ITERATIONS; t+i) !
S0:i- NUM E
fur (int
Thinking n", jd):
thínking.
príntf("Philosopher%d is
sleeplrand)%,3 - l);
|/Pick up left
fork
pthread_mutex lock(&forks[left _fork]);
printf("Philosopher od picked up left fork.\n", id);
|/Pick up right
fork
pthread_mutex lock(&forks[right _fork]);
printf("Philosopher %d picked up right fork.\n", id):
I/ Eating
pthread_mutex_unlock(&forks[right_fork]);
printf("Philosopher %d put down right fork. \n", id);
|/Put down left fork
pthread mutex_unlock(&forks[left_fork]);
printf("Philosopher %d put down left fork.\n", id);
return NULL:
int main() {
srand(time(NULL));
1/ Initialize forks muteXes
thrcads
/Crcatc philosopher
int philosopher_ids|NUM_PHILOSOPHERS):
i<NUM PHILOSOPHERS; +ti) {
forint i = 0;
philosopher_ids[i] -i:
pthread Create(&philosophers[i], NULL, philosopher, &philosopher_ids[):
1/ Wait for
philosopher threads to finish
for (int i= 0; i<NUM
PHILOSOPHERS; +i){
pthread join(philosophers[i], NULL);
pthread mutex_destroy(&forks[i):
return 0:
OUTPUT: -
Osei-172-21-3-10
DR
tec2-serip-172 3
Iec2-ertip-l231
hintig
Filos inàg
2b t h i o
Osopber TE ork
AilosopesG thin
Fhlogopbe ning
ber 0 ork.
ebiloso Dber3
ehiloscpr
tnng
losopber
Philao
ork
Losopher
ork
1 put cht
Rork
legt tor
2 3 - - 1 0 JS
Pbiloe
PhilosopherEhilnsOph
RhilosOpher Ehilosopher
thining sPhilasophe
Ehilogopher
thiRking is Ehlosopher
Ehosopher
thnihg downPhilosopher3
Bhilosopher
PhilosoRhe thinkihg
thìnking
Iis0
pUtPhilosophex
ehilosopher
(ec2-usereipl721-2310
Philosophe
thinkangthinking i /dp
Philosopher
Put 2
Ls
thinRDOS
dowghe Chinking
OR