You are on page 1of 74

EX NO: 7 (A) IMPLEMENTATION OF INTER PROCESS COMMUNICATION (IPC)

USING PIPES

AIM
To write a c program to develop an application using Inter process Communication (IPC)
using pipes.
ALGORITHM
1. Initialize an array fd[] and child of integer type
2. Initialize array a of character type.
3. Get the string from user to WRITE in to the pipe
4. Get the string from the user using scanf statement.
5. call the function pipe(fd).
6. assign child as fork().
7. Check if not equal to child
8. Then close index fd of 0
9. Write the string in file destination using write () and wait
10. Else close the index 1 of fd
11. Read the index fd(0),b,5
12. Display the string retrieved from the pipe
pipe():
pipe - create pipe
SYNOPSIS
#include <unistd.h>
int pipe(int filedes[2]);
DESCRIPTION
pipe() creates a pair of file descriptors, pointing to a pipe inode, and places them in the
array pointed to by filedes. filedes[0] is for reading, filedes[1] is for writing.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
PROGRAM
#include<stdio.h>
#include<string.h>
int main()
{
int fd[2],child;
char a[10];
printf("Enter the string to enter into the pipe:");
scanf("%s",a);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],a,strlen(a));
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,10);
printf("\n The String retrieved from the pipe is: %s\n",a);
}
return 0;
}
OUTPUT
-bash-4.2$ cc pop.c
-bash-4.2$ ./a.out
Enter the string to enter into the pipe:memory
The String retrieved from the pipe is: memory

RESULT
The C program to develop an application using Inter process Communication (IPC)
using pipes is implemented.

EX NO: 7 (B) IMPLEMENTATION OF INTER PROCESS COMMUNICATION (IPC)


USING SHARED MEMORY
AIM
To implement inter process communication using shared memory.
ALGORITHM
 Create the child process using fork().
 Create the shared memory for parent process using shmget() system call.
 Now allow the parent process to write in shared memory using shmpet pointer
which is return type of shmat().
 Now across and attach the same shared memory to the child process.
 The data in the shared memory is read by the child process using the shmptr
pointer.
shmget():
shmget - allocates a System V shared memory segment
SYNOPSIS         
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg);
DESCRIPTION      
shmget() returns the identifier of the System V shared memory segment associated with the
value of the argument key. A new shared memory segment, with size equal to the value of size
rounded up to a multiple of PAGE_SIZE, is created if key has the value IPC_PRIVATE or key
isn't IPC_PRIVATE, no shared memory segment corresponding to key exists, and
IPC_CREAT is specified in shmflg.
If shmflg specifies both IPC_CREAT and IPC_EXCL and a shared memory segment
already exists for key, then shmget() fails with errno set to EEXIST.
0666|IPC_CREAT:
It sets the permissions for the newly created shared memory segment to S_IRUSR |
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH (read-write permissions for
everyone) if it does not already exist
shmat():
shmat, shmdt - System V shared memory operations
SYNOPSIS         
#include <sys/types.h>
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
DESCRIPTION         
shmat() -
shmat() attaches the System V shared memory segment identified by shmid to the
address space of the calling process. The attaching address is specified by shmaddr with one of
the following criteria:
 If shmaddr is NULL, the system chooses a suitable (unused) address at which to
attach the segment.
 If shmaddr isn't NULL and SHM_RND is specified in shmflg, the attach occurs
at the address equal to shmaddr rounded down to the nearest multiple of
SHMLBA.
 Otherwise, shmaddr must be a page-aligned address at which the attach occurs.
PROGRAM -SENDER:
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#define size 32
int main()
{ int shmid; char *s[100],*str;
printf("\nipc message passing using shared memory sender");
shmid=shmget(60,size,IPC_CREAT|0666);
str=shmat(shmid,0,0);
printf("\nenter the message to be sent");
gets(s);
strcpy(str,s);
printf("\nyour mesage has been sent");
return 0; }
RECEIVER:
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#define size 32
int main()
{
printf("\nipc message passing using shared memory-receiver");
int shmid;
char *str;
shmid=shmget(60,size,IPC_CREAT|0666);
str=shmat(shmid,0,0);
printf("\nreceived message is....");
puts(str);
return 0; }
OUTPUT
-bash-4.2$ vi simmy.c
-bash-4.2$ cc simmy.c
Preparation (5)
-bash-4.2$ ./a.out
Performance (5)
ipc message passing using shared memory
sender Record (5)
enter the message to be sent hello
Viva (5)
your mesage has been sent
-bash-4.2$ vi jimmy.c Total (20)

-bash-4.2$ cc jimmy.c
-bash-4.2$ ./a.out
ipc message passing using shared memory-receiver
received message is.... hello
RESULT
The C program to develop an application for Inter process Communication (IPC) using
shared memory is implemented.
EX NO: 8
BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE

AIM
To implement the bankers Algorithm for Dead Lock Avoidance
ALGORITHM
1. start the program
2. Include the functions for alloc, max, cneed, a ,total.
3. With in main function ,initialize the variables r,p,i,j,k,flag,f,count,x,flag1 of integer type
4. Assign count and flag1 as 0
5. get the values of resources from the user
6. assign f=r.
7. Get the value of total memory for the resources sequentially using for loop
8. Get the number of processes from the user
9. Get the Allocated and Maxneed memory from the user
10. For each process get alloc value and max need memory for each process
11. Calculate the cneed for each process using the formula cneed[i][j]=max[i][j]-alloc[i][j]
12. For each process and resources perform the sequence of execution
13. get the avail value and allocate find and need values
14. check whether it is possible to allocate
15. it is possible then the system is in safe state
16. else system is not in safety state
17. if the new requests comes then the check that the system is in safety
18. stop the program
PROGRAM
#include<stdio.h>
#include<stdlib.h>
int alloc[10][10];
int max[10][10];
int cneed[10][10];
int a[10];
int total[10];
void main()
{
int r,p,i,j,k,flag,f,count,x,flag1;
count=0;
flag1=0;
printf("\nEnter number of resources:");
scanf("%d",&r);
f=r;
printf("\nEnter total memory of given resources sequentially:");
for(i=0;i<r;i++)
scanf("%d",&total[i]);
printf("\nEnter number of processes:");
scanf("%d",&p);
printf("\nEnter Allocated and Maxneed memory…");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf("\nFor Process %d=",i);
scanf("%d%d",&alloc[i][j],&max[i][j]);
//calculating current need
cneed[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\nSequence of execution is…\n");
k=0;
for(j=0;j<r;j++)
{
for(i=0;i<p;i++)
{
a[k]+=alloc[i][j];
}
a[k]=total[k]-a[k];
k++;
}
while(count!=p)
{
for(i=0;i<p;i++)
{
flag=0;
if(cneed[i][f]!=1)
{
for(j=0;j<r;j++)
{
total[j]=a[j]-cneed[i][j];
}
for(k=0;k<r;k++)
{
if(total[k]<0)
{
flag=1;
}
}
}
if((flag==0)&&(cneed[i][f]!=1))
break;
}
x=i;
cneed[x][f]=1;
printf("P%d->",x);
count++;
for(i=0;i<r;i++)
{
total[i]+=max[x][i];
a[i]=total[i];
}
for(i=0;i<p;i++)
{
if((cneed[i][0]<a[0])&&(cneed[i][f]==0))
flag1=1;
}
if(flag1==0)
break;
}
if(flag1==0)
printf("\nUnsafe…");
else
printf("\nSafe…");
getchar();
}
OUTPUT
-bash-4.2$ cc cup.c
-bash-4.2$ ./a.out

//For Safe condition


Enter number of resources:3
Enter total memory of given resources sequentially:10 5 7
Enter number of processes:5
Enter Allocated and Maxneed memory...
For Process 0=0 7
For Process 0=1 5
For Process 0=0 3
For Process 1=2 3
For Process 1=0 2
For Process 1=0 2
For Process 2=3 9
For Process 2=0 0
For Process 2=2 2
For Process 3=2 2
For Process 3=1 2
For Process 3=1 2
For Process 4=0 4
For Process 4=0 3
For Process 4=2 3
Sequence of execution is...
P1->P3->P0->P2->P4->
Safe...

-bash-4.2$ cc cup.c
-bash-4.2$ ./a.out

//For Unsafe condition


Enter number of resources:3
Enter total memory of given resources sequentially:6 5 4
Enter number of processes:3
Enter Allocated and Maxneed memory...
For Process 0=0 7
For Process 0=1 5
For Process 0=0 3
For Process 1=2 3
For Process 1=0 2
For Process 1=0 2
For Process 2=3 9
For Process 2=0 0
For Process 2=2 2
Sequence of execution is...
P1->Unsafe...

Preparation (5)

Performance (5)

Record (5)

Viva (5)

Total (20)
RESULT:
Thus the program to implement the bankers Algorithm for Dead Lock avoidance has
been executed successfully.
EX NO: 9
IMPLEMENTATION OF DEADLOCK DETECTION

AIM
To write a C program to implement the bankers Algorithm for Dead Lock Detection
ALGORITHM
1. Start the program
2.initialize the variables of integer type found , flag,l,p[4][5],tp,tr,c[4][5], i,j,k=1 ,m[5],
r[5],a[5], temp[5],sum=0.
3. Get the total no of process and total no of resources from user.
4. enter the claim matrix from the user
5. using for loop ,assign I as 1 check if i<=tp and increment the value of i, print the
process.
6.Enter the allocation matrix and again using for loop print the process.
7.using for loop and get the resource vector (total resources) from the user.
8.using for loop and get the availability vector(available sources) from the user.
9.assign temp[i]=a[i] using  for(i=1;i<=tp;i++) assign sum=0 and using  for(i=1;i<=tp;i+
+) assign sum+=p[i][j].
10. using if condition check if sum==0 and assign    m[k]=I and increment the k value.
11.using  for assign i=1 and check if i<=tp and using for loop assign l=1 and check if
l<k check whether   if i!=m[l] and assign flag=1.
12.using for loop assign j=1 and check if j<=tr and assign flag=0.
13. check   if flag==1 then assign m[k]=I and increment the k value.
14. using for loop assign j=1 and check if j<=tr and assign temp[j]+=p[i][j]. And then
print the dead lock causing processes.
15.using for loop assign j=1 and check if j<=tp and assign found=0.
16. using for loop assign i=1 and check if i<k and check  if j==m[i] and assign
found=1.
17. then check if found==0, then print the value of j

PROGRAM
#include <stdio.h>
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe = 0;
int counter = 0, i, j, exec, resources, processes, k = 1;
 
int main()
{
printf("\nEnter number of processes: ");
     scanf("%d", &processes);
 
     for (i = 0; i < processes; i++)
{
         running[i] = 1;
         counter++;
     }
 
     printf("\nEnter number of resources: ");
     scanf("%d", &resources);
 
     printf("\nEnter Claim Vector:");
     for (i = 0; i < resources; i++)
{
        scanf("%d", &maxres[i]);
     }
 
   printf("\nEnter Allocated Resource Table:\n");
     for (i = 0; i < processes; i++)
{
        for(j = 0; j < resources; j++)
{
   scanf("%d", &current[i][j]);
         }
     }
 
     printf("\nEnter Maximum Claim Table:\n");
     for (i = 0; i < processes; i++)
{
         for(j = 0; j < resources; j++)
{
             scanf("%d", &maximum_claim[i][j]);
         }
     }
 
printf("\nThe Claim Vector is: ");
     for (i = 0; i < resources; i++)
{
        printf("\t%d", maxres[i]);
}
 
     printf("\nThe Allocated Resource Table:\n");
     for (i = 0; i < processes; i++)
{
        for (j = 0; j < resources; j++)
{
             printf("\t%d", current[i][j]);
         }
printf("\n");
     }
 
     printf("\nThe Maximum Claim Table:\n");
     for (i = 0; i < processes; i++)
{
         for (j = 0; j < resources; j++)
{
        printf("\t%d", maximum_claim[i][j]);
         }
         printf("\n");
     }
 
     for (i = 0; i < processes; i++)
{
         for (j = 0; j < resources; j++)
{
             allocation[j] += current[i][j];
         }
     }
 
     printf("\nAllocated resources:");
     for (i = 0; i < resources; i++)
{
         printf("\t%d", allocation[i]);
     }
 
     for (i = 0; i < resources; i++)
{
        available[i] = maxres[i] - allocation[i];
}
 
     printf("\nAvailable resources:");
     for (i = 0; i < resources; i++)
{
         printf("\t%d", available[i]);
     }
     printf("\n");
 
     while (counter != 0)
{
         safe = 0;
         for (i = 0; i < processes; i++)
{
             if (running[i])
{
                 exec = 1;
                 for (j = 0; j < resources; j++)
{
                     if (maximum_claim[i][j] - current[i][j] > available[j])
{
                         exec = 0;
                         break;
                     }
                 }
                 if (exec)
{
                     printf("\nProcess%d is executing\n", i + 1);
                     running[i] = 0;
                     counter--;
                     safe = 1;
 
                     for (j = 0; j < resources; j++)
{
                         available[j] += current[i][j];
                     }
                break;
                 }
             }
         }
         if (!safe)
{
             printf("\nThe processes are in unsafe state.\n");
             break;
         }
else
{
             printf("\nThe process is in safe state");
             printf("\nAvailable vector:");
 
             for (i = 0; i < resources; i++)
{
                 printf("\t%d", available[i]);
             }
 
        printf("\n");
         }
     }
     return 0;
}

OUTPUT:
-bash-4.2$ vi liv.c
-bash-4.2$ cc liv.c
-bash-4.2$ ./a.out

Safe State Output


Enter number of processes: 2
Enter number of resources: 2
Enter Claim Vector:30 30
Enter Allocated Resource Table:
2
2
1
1
Enter Maximum Claim Table:
20
20
20
20
The Claim Vector is: 30 30
The Allocated Resource Table:
2 2
1 1
The Maximum Claim Table:
20 20
20 20
Allocated resources: 3 3
Available resources: 27 27
Process1 is executing
The process is in safe state
Available vector: 29 29
Process2 is executing
The process is in safe state
Available vector: 30 30

Preparation (5)

Performance (5)

Record (5)

Viva (5)

Total (20)
RESULT:
Thus the program to implement the bankers Algorithm for Dead Lock Detection has been
executed successfully.

EX NO: 10 IMPLEMENTATION OF THREADING AND SYNCHRONIZATION


APPLICATION
AIM
To write a program to implement threading and synchronization applications.
ALGORITHM
1.start the program.
2.include the Header files stdio.h,string,h,pthread.h,stdlib.h,unistd.h.
3. initialize the function pthread_t tid[2].
4.create the main function void* doSomeThing(void *arg).
5.initialize unsigned variable i=0 of long type.
6.assign pthread_t id = pthread_self().
7.using if statement check if(pthread_equal(id,tid[0])) then print first thread processing
else print second thread processing.
8.using for i value 0, check if i less than 0xFFFFFFFF and increment the value of i and
return null
9.create the main function.
11.initialize i=0 of integer type.
12.initialize err of integer type.
13.using while loop check if i<2
14.assign err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL).
15.using if statement check err not equal to 0, then print thread cant created else print
thread created and increment i.
16.use the function sleep for 5 minutes.
17.return 0.
18.Terminate the process.
PROGRAM
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();

if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
} sleep(5);
return 0; }
OUTPUT
-bash-4.2$ vi edit.c
-bash-4.2$ cc editt.c -lpthread
First thread processing
Thread created successfully
Second thread processing

Preparation (5)

Performance (5)

Record (5)

Viva (5)

Total (20)
RESULT
Thus the program has been executed and verified successfully.

EX NO: 11 IMPLEMENTATION OF MEMORY ALLOCATION METHODS FOR


FIXED PARTITION
AIM
To allocate a memory in a fixed sized partition to various processes based on the first fit,
best fit and worst fit algorithms and to find which algorithm makes the most efficient use of
memory.
DESCRIPTION
One of the simplest methods for memory allocation is to divide memory into several
fixed sized partitions. Each partition may contain exactly one process. The degree of
multiprogramming is bounded by the number of partitions. Thus a free list of memory blocks of
varying sizes is maintained.
First fit:
The free list is searched in order until a block of memory is found that is as least as large
as the one desired by the allocation. The block is then carved into two pieces: one large enough
to hold the desired allocation, and one containing the remainder of the block after the allocation
is done. The remainder block is returned to the free list for the next allocation.
Best fit:
Allocate the smallest hole that is big enough. We must search the entire list,unless the list
is ordered by size. This strategy produces the smallest left over hole.
Worst fit:
Allocate the large hole. Again we must search the entire list, unless it is sorted by size.
This strategy produces the largest left over hole, which may be more useful than the smaller
leftover hole from a best-fit approach.
ALGORITHM
1. Initialize the integer variables ch,l,t1,z,z1,z2,t2[5],i,j,n,k,p[5],bt[5],fs,sfs[5],t[5].
2. Get the no of process and process no of each process and size from the user .
3. Get the details of no of free partitions from the user
4. Get the size of each free partition s from the user.
5. Get the size of the segment from the user .
6. Switch case statement is used to select among three choices first fit, best fit and
worst fit.
7. If case 1 ,assign k value to zero and check if bt[i]<=sfs[j] then increment k value
by 1
8. If k is 1 then, calculate sfs[j]=sfs[j]-bt[i];
9. Assign sfs[j]=0
10. Display the value of i,bt[i],j
11. If case 2, then check if t2[k]>t2[l] and perform swapping using temp variable
t1.
12. Assign z,z2 to 0 and check if t2[j]>=0 and increment the value of z by 1 and
assign z1=t2[j]
13. Display the values of i,bt[i],k.
14. If case is 3 then ,calculate t[j]=sfs[j]-bt[i] and check if t2[k]<t2[l] then swap the
t1[k] and t1[l] using temp variable t1 ,and repeat the step 12 and 13
15. If entered choice is wrong then display as enter choice between 1 to 3
16. Stop the program
PROGRAM
#include<stdio.h>
int main()
{
int ch,l,t1,z,z1,z2,t2[5],i,j,n,k,p[5],bt[5],fs,sfs[5],t[5];
printf("Enter the no of processes:\t");
scanf("%d",&n);
printf("Enter the process numbers:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
}
for(i=1;i<=n;i++)
{
printf("Enter the size of %d process\t",i);
scanf("%d",&bt[i]);
}
printf("\nEnter the number of free partitions");
scanf("%d",&fs);
printf("\nEnter the size of each free partitions\n");
for(i=1;i<=fs;i++)
{
printf("Enter the size of %d segment",i);
scanf("%d",&sfs[i]);
}
printf("Enter your choice:\n1.FirstFit\n2.BestFit\n3.WorstFit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
k=0;
printf("\nProcessNo\tPro.Size\tAll.Seg.No\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=fs;j++)
{
if(bt[i]<=sfs[j])
{
k++;
if(k==1)
{
sfs[j]=sfs[j]-bt[i];
sfs[j]=0;
printf("%d\t\t%d\t\t%d\n",i,bt[i],j);
goto x;
}
}
}
x:k=0;
}
break;
case 2:
printf("\nProcessNo\tPro.Size\tAll.Seg.No\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=fs;j++)
{
t[j]=sfs[j]-bt[i];
}
for(j=1;j<=fs;j++)
{ t2[j]=t[j];
}
for(k=1;k<fs;k++)
{
for(l=k+1;l<=fs;l++)
{
if(t2[k]>t2[l])
{
t1=t2[k];
t2[k]=t2[l];
t2[l]=t1;
}
}
}
z=0;
z2=0;
for(j=1;j<=fs;j++)
{
if(t2[j]>=0)
{
z++;
z1=t2[j];
}
if(z==1)
{
for(k=1;k<=fs;k++)
{
if(z1==t[k])
{
z2++;
if(z2==1)
{
sfs[k]=sfs[k]-bt[i];
sfs[k]=0;
printf("%d\t\t%d\t\t%d\n",i,bt[i],k);
}
}
}
}
}
}
break;
case 3:
printf("\nProcessNo\tPro.Size\tAll.Seg.No\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=fs;j++)
{
t[j]=sfs[j]-bt[i];
}
for(j=1;j<=fs;j++)
{
t2[j]=t[j];
}
for(k=1;k<fs;k++)
{
for(l=k+1;l<=fs;l++)
{
if(t2[k]<t2[l])
{
t1=t2[k];
t2[k]=t2[l];
t2[l]=t1;
}
}
}
z=0;
z2=0;
for(j=1;j<=fs;j++)
{
if(t2[j]>=0)
{
z++;
z1=t2[j];
}
if(z==1)
{
for(k=1;k<=fs;k++)
{
if(z1==t[k])
{
z2++;
if(z2==1)
{
sfs[k]=sfs[k]-bt[i];
sfs[k]=0;
printf("%d\t\t%d\t\t%d\n",i,bt[i],k);
}
}
}
}
}
}
break;
default: printf("Enter correct choice between 1 and 3");
}
return 0;
}
OUTPUT
First Fit:
-bash-4.2$ vi typing.c
-bash-4.2$ cc typing.c
-bash-4.2$ ./a.out
Enter the no of processes: 3
Enter the process numbers:
1
2
3
Enter the size of 1 process 100
Enter the size of 2 process 250
Enter the size of 3 process 50
Enter the number of free partitions4
Enter the size of each free partitions
Enter the size of 1 segment50
Enter the size of 2 segment150
Enter the size of 3 segment200
Enter the size of 4 segment25
Enter your choice:
1.FirstFit
2.BestFit
3.WorstFit
1
Process No. Pro.size All.Seg.No
1 100 2
3 50 1
Best Fit:
Enter the no of processes: 3
Enter the process numbers:
1
2
3
Enter the size of 1 process 11
Enter the size of 2 process 22
Enter the size of 3 process 33
Enter the number of free partitions4
Enter the size of each free partitions
Enter the size of 1 segment44
Enter the size of 2 segment33
Enter the size of 3 segment22
Enter the size of 4 segment11
ProcessNo Pro.Size All.Seg.No
1 11 4
2 22 3
3 33 2
Worst Fit:
Enter the no of processes: 3
Enter the process numbers:
1
2
3
Enter the size of 1 process 11
Enter the size of 2 process 22
Enter the size of 3 process 33
Enter the number of free partitions4
Enter the size of each free partitions
Enter the size of 1 segment44
Enter the size of 2 segment33
Enter the size of 3 segment22
Enter the size of 4 segment11
ProcessNo Pro.Size All.Seg.No
1 11 1
2 22 2

Preparation (5)

Performance (5)

Record (5)

Viva (5)

Total (20)
RESULT
Thus the program was executed and output was verified successfully

EX NO: 12 IMPLEMENTATION OF PAGING TECHNIQUE OF MEMORY


MANAGEMENT
AIM
To write a C program to implement the concept of Paging technique of memory
management.
ALGORITHM
1.initialize the variables of integer type ,j,arr[100],pt[20],val,pgno,offset,phymem,fs,nf;
2.get the value for Memory Management paging and enter the size of physical memory
3..using for loop get the physical memory for each processes.
4.Asssin arr[i]=j in the process
5.get the size of frame or page
6. assign nf=phymem/fs
7.display the No of frame available in the page
8. ..using for loop Enter the page table
9. get the value for page no and the offset
10. calculate the physical address val=(fs*pt[pgno])+offset;
11. display the result of physical addres
12. stop the program
PROGRAM
#include<stdio.h>
#define MAX 50
int main()
{
int page[MAX],i,n,f,ps,off,pno;
int choice=0;
printf("\nEnter the no of peges in memory: ");
scanf("%d",&n);
printf("\nEnter page size: ");
scanf("%d",&ps);
printf("\nEnter no of frames: ");
scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any frame)\n\n");
printf("\npageno\tframeno\n-------\t-------");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i);
scanf("%d",&page[i]);
}
do
{
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
return 1;
}
OUTPUT
-bash-4.2$ cc doitt.c
-bash-4.2$ ./a.out
Enter the no of pages in memory: 4
Enter page size: 10
Enter no of frames: 10
Enter the page table
(Enter frame no as -1 if that page is not present in any frame)
pageno frameno
------- -------
0 -1
1 8
2 -1
3 6
Enter the logical address(i.e,page no & offset):2 200
The required page is not available in any of frames
Do you want to continue(1/0)?:1
Enter the logical address(i.e,page no & offset):1 500
Physical address(i.e,frame no & offset):8,500
Do you want to continue(1/0)?:0

Preparation (5)

Performance (5)

Record (5)

Viva (5)

Total (20)
RESULT
Thus the program was executed and output was verified successfully.

EX NO: 13 (A) IMPLEMENTATION OF FIFO PAGE REPLACEMENT


ALGORITHM
AIM:
To write a C program to implement FIFO Page Replacement Algorithm.
ALGORITHM
Step 1: Start the
program. Step 2: Get the
page as string. Step 3:
Get the frame size.
Step 4: Replace the first come page with the new page till all pages are
used. Step 5: Display the chart along with Faults and Hits.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
            printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
            printf("\n ENTER THE PAGE NUMBER :\n");
            for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
            printf("\n ENTER THE NUMBER OF FRAMES :");
            scanf("%d",&no);
for(i=0;i<no;i++)
            frame[i]= -1;
                        j=0;
                        printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
                        {
                                    printf("%d\t\t",a[i]);
                                    avail=0;
                                    for(k=0;k<no;k++)
if(frame[k]==a[i])
                                                avail=1;
                                    if (avail==0)
                                    {
                                                frame[j]=a[i];
                                                j=(j+1)%no;
                                                count++;
                                                for(k=0;k<no;k++)
                                                printf("%d\t",frame[k]);
}
                                    printf("\n");
}
                        printf("Page Fault Is %d",count);
                        return 0;
}

OUTPUT
-bash-4.2$ cc pagee.c
-bash-4.2$ ./a.out
ENTER THE NUMBER OF PAGES:
10
ENTER THE PAGE NUMBER :
5
2
5
6
8
9
1
2
4
6
ENTER THE NUMBER OF FRAMES :2
ref string page frames
5 5 -1
2 5 2
5
6 6 2
8 6 8
9 9 8
1 9 1
2 2 1
4 2 4
6 6 4
Page Fault Is 9
RESULT
Thus C program to implement FIFO Page Replacement Algorithm was written, executed
and output is verified successfully.

EX NO: 13 (B) IMPLEMENTATION OF LRU PAGE REPLACEMENT


ALGORITHM
AIM
To write a C program to implement LRU Page Replacement Algorithm.
ALGORITHM
Step 1: Start the program.
Step 2: Get the pages as string.
Step 3: Get the frame size.
Step 4: Replace the least recently used page with the new page till all the pages are
processed.
Step 5: Display the page faults and hits along with chart.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
 int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
  for(i = 1; i < n; ++i){
if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}
 int main()
{
 int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j,
pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
     scanf("%d", &pages[i]);
    }
    for(i = 0; i < no_of_frames; ++i){
     frames[i] = -1;
    }
   for(i = 0; i < no_of_pages; ++i){
     flag1 = flag2 = 0;
        for(j = 0; j < no_of_frames; ++j){
     if(frames[j] == pages[i]){
     counter++;
     time[j] = counter;
   flag1 = flag2 = 1;
   break;
   }
     }
       if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
     if(frames[j] == -1){
     counter++;
     faults++;
     frames[j] = pages[i];
     time[j] = counter;
     flag2 = 1;
     break;
     }
     }
     }
         if(flag2 == 0){
     pos = findLRU(time, no_of_frames);
     counter++;
     faults++;
     frames[pos] = pages[i];
     time[pos] = counter;
     }
      printf("\n");
         for(j = 0; j < no_of_frames; ++j){
     printf("%d\t", frames[j]);
     }
}
printf("\n\nTotal Page Faults = %d", faults);
        return 0;
}
OUTPUT

-bash-4.2$ vi doitt2.c
-bash-4.2$ cc doitt2.c
-bash-4.2$ ./a.out
Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5
4
3
6
7
3

5 -1 -1
5 4 -1
5 4 3
6 4 3
6 7 3
6 7 3
Total Page Faults = 5
RESULT
Thus C program to implement LRU Page Replacement Algorithm was written, executed
and output is verified successfully.

EX NO: 13 (C) IMPLEMENTATION OF LFU PAGE REPLACEMENT


ALGORITHM
AIM
To write a C program to implement LFU Page Repalcement Algorithm.
ALGORITHM
Step 1: Start the program.
Step 2: Get the pages as
string. Step 3: Get the
frame size.
Step 4: Replace the pages that are least frequently used with the new page that
has arrived and continue this till all the pages are processed.
Step 5: Display the chart along with page faults and page
hits. Step 6: Stop the program.
PROGRAM
#include<stdio.h>
int main()
{
int frames[10], temp[10], pages[10];
int total_pages, m, n, position, k, l, total_frames;
int a = 0, b = 0, page_fault = 0;
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frames[m] = -1;
}
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Values for Reference String:\n");
for(m = 0; m < total_pages; m++)
{
printf("Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
for(n = 0; n < total_pages; n++)
{
a = 0, b = 0;
for(m = 0; m < total_frames; m++)
{
if(frames[m] == pages[n])
{
a = 1;
b = 1;
break;
}
}
if(a == 0)
{
for(m = 0; m < total_frames; m++)
{
if(frames[m] == -1)
{
frames[m] = pages[n];
b = 1;
break;
}
}
}
if(b == 0)
{
for(m = 0; m < total_frames; m++)
{
temp[m] = 0;
}
for(k = n - 1, l = 1; l <= total_frames - 1; l++, k--)
{
for(m = 0; m < total_frames; m++)
{
if(frames[m] == pages[k])
{
temp[m] = 1;
}
}
}
for(m = 0; m < total_frames; m++)
{
if(temp[m] == 0)
position = m;
}
frames[position] = pages[n];
page_fault++;
}
printf("\n");
for(m = 0; m < total_frames; m++)
{
printf("%d\t", frames[m]);
}
}
printf("\nTotal Number of Page Faults:\t%d\n", page_fault);
return 0;
}

OUTPUT
-bash-4.2$ cc doitt3.c
-bash-4.2$ ./a.out
Enter Total Number of Frames: 4
Enter Total Number of Pages: 5
Enter Values for Reference String:
Value No.[1]: 5
Value No.[2]: 3
Value No.[3]: 5
Value No.[4]: 2
Value No.[5]: 1
5 -1 -1 -1
5 3 -1 -1
5 3 -1 -1
5 3 2 -1
5 3 2 1
Total Number of Page Faults: 0

Preparation (5)

Performance (5)

Record (5)

Viva (5)

Total (20)
RESULT
Thus C program to implement LFU Page Replacement Algorithm was written, executed
and output is verified successfully.

EX NO: 14 (A) IMPLEMENTATION OF SINGLE-LEVEL FILE


ORGANIZATION TECHNIQUE
AIM
To write a C program to implement Single-Level File Organization Technique.
ALGORITHM
1.Start the program
2.initialize integer variables as nf ,I,j,ch and assign them to 0.
3.Get the dir name and no of files from the user.
4.Using for loop get the names of files to be created from the user .
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter
your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}
OUTPUT
-bash-4.2$ cc queen.c
-bash-4.2$ ./a.out
Enter name of directory success
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- su.c
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice -- 1
Enter the name of the file -- sue.c

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice -- 4
The Files are -- su.c sue.c
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice -- 2
Enter the name of the file -- su.c
File su.c is deleted
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice -- 4
The Files are -- sue.c
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice -- 3
Enter the name of the file -- sue.c
File sue.c is found
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice -- 5

RESULT:
Thus C program to implement Single-Level File Organization Technique was written,
executed and output is verified successfully.

EX NO: 14 (B) IMPLEMENTATION OF TWO-LEVEL FILE ORGANIZATION


TECHNIQUE
AIM
To write a C program to implement Two-Level file Organization Technique.

ALGORITHM

Step 1: Start the program.


Step 2: Create a structure to generate levels.
Step 3: Intialize the graphics window.
Step 4: Display the root directory based on which get the sub directories and files.
Step 5: Based on the files and directories create a 2 level file orgranization and display it.
Step 6: Stop the program.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\t Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}

OUTPUT
-bash-4.2$ cc king.c
-bash-4.2$ ./a.out
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice1
Enter name of directory -- flower
Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice2
Enter name of the directory -- flower
Enter name of the file -- rose.c
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice2
Enter name of the directory -- flower
Enter name of the file -- jasmine.c
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice3
Enter name of the directory -- flower
Enter name of the file -- rose.c
File rose.c is deleted
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice5

Directory Files
flower jasmine.c
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice4
Enter name of the directory -- flower
Enter the name of the file -- rose.c
File rose.c not found
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice6

RESULT
Thus C program to implement Two-Level File Organization Technique was written,
executed and output is verified successfully.

EX NO: 14 (C) IMPLEMENTATION OF HIERARCHIAL FILE


ORGANIZATION TECHNIQUE
AIM
To write a C program to implement Hierarchial File Organization Technique.
ALGORITHM
Step 1: Start the program.
Step 2: Create a structure to generate levels.
Step 3: Intialize the graphics window.
Step 4: Display the root directory based on which get the sub directories and files.
Step 5: Based on the files and directories create a hierarchical file organization and
display it.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level; struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root; root=NULL;
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\Turboc3\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap; if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("\nEnter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
printf("\nEnter 1 for Dir/2 for File:");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("\nNo of Sub Directories/Files(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc; for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i; settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
}
}

OUTPUT
RESULT:

Thus C program to implement Hierarchical File Organization Technique was written,


executed and output is verified successfully.

EX NO: 14 (D)
IMPLEMENTATION OF DAG FILE ORGANIZATION TECHNIQUE
AIM
To write a C program to implement DAG File Organization Technique.
ALGORITHM
Step 1: Start the program.
Step 2: Create a structure to generate levels.
Step 3: Intialize the graphics window.
Step 4: Display the root directory based on which get the sub directories and files.
Step 5: Based on the files and directories create a DAG file organization.
Step 6: Get the links from the user and display the structure and the link.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
typedef struct
{
char from[20]; char to[20];
}link;
link L[10]; int nofl; node *root;
void main()
{
int gd=DETECT,gm; root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr(); initgraph(&gd,&gm,"c:\\Turboc3\\BGI");
draw_link_lines();
display(root); getch(); closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap; if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("\nEnter name of dir/file(under %s):",dname); fflush(stdin);
gets((*root)->name);
printf("\nEnter 1 for Dir/2 for File:");
scanf("%d",&(*root)->ftype); (*root)->level=lev;
(*root)->y=50+lev*50; (*root)->x=x;
(*root)->lx=lx; (*root)->rx=rx; for(i=0;i<5;i++)
(*root)->link[i]=NULL; if((*root)->ftype==1)
{
printf("\nNo of Sub Directories/Files(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc; for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
read_links()
{
int i;
printf("\nHow many Links:");
scanf("%d",&nofl); for(i=0;i<nofl;i++)
{
printf("\nFile/Dir:");
fflush(stdin); gets(L[i].from);
printf("\nUser Name:");
fflush(stdin);
gets(L[i].to);
}
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y; return;
}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name); for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}
OUTPUT

Preparation (5)

Performance (5)

Record (5)

Viva (5)

Total (20)
RESULT

Thus C program to implement DAG File Organization Technique was written, executed
and output is verified successfully.

EX NO: 15 (A) IMPLEMENTATION OF SEQUENTIAL FILE ALLOCATION


STRATEGY
AIM
To write a C program to implement sequential File Allocation Strategy.
ALGORITHM
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is big
enough is encountered. It allocates that memory block for the requesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can be
allocated to requesting process and allocates it.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and allocates
it to the process.
Step 7: Analyses all the three memory management techniques and display the best
algorithm which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void recurse(int files[]){
int flag = 0, startBlock, len, j, k, ch;
printf("Enter the starting block and the length of the files: ");
scanf("%d%d", &startBlock, &len);
for (j=startBlock; j<(startBlock+len); j++){
if (files[j] == 0)
flag++;
}
if(len == flag){
for (k=startBlock; k<(startBlock+len); k++){
if (files[k] == 0){
files[k] = 1;
printf("%d\t%d\n", k, files[k]);
}
}
if (k != (startBlock+len-1))
printf("The file is allocated to the disk\n");
}
else
printf("The file is not allocated to the disk\n");
printf("Do you want to enter more files?\n");
printf("Press 1 for YES, 0 for NO: ");
scanf("%d", &ch);
if (ch == 1)
recurse(files);
else
exit(0);
return;
}
int main()
{
int files[50];
int i;
for(i=0;i<50;i++)
files[i]=0;
printf("Files Allocated are :\n");
recurse(files);
getch();
return 0;
}

OUTPUT
RESULT
Thus C program to implement Sequential File Allocation Strategy was written, executed
and output is verified successfully.

EX NO: 15 (B)
IMPLEMENTATION OF LINKED FILE ALLOCATION STRATEGY
AIM
To write a C program to implement Linked File Allocation Strategy.
ALGORITHM
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack
Step 6: Stop the allocation.
PROGRAM
#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int sb, nob;
}ft[30];
void main()
{
int i, j, n;
char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched-- ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
10
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED\n");
printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);
}
getch();
}
OUTPUT
Enter no of files :3
Enter file name 1 :A
Enter starting block of file 1 :85
Enter no of blocks in file 1 :6
Enter file name 2 :B
Enter starting block of file 2 :102
Enter no of blocks in file 2 :4

Enter file name 3 :C


Enter starting block of file 3 :60
Enter no of blocks in file 3 :4
Enter the file name to be searched – B
FILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED
B 102 4 102, 103, 104, 105
RESULT
Thus the program to implement the linked file allocation was executed successfully and
output was verified.

EX NO: 15 (C)
IMPLEMENTATION OF INDEXED FILE ALLOCATION STRATEGY
AIM
To write a C program to implement indexed File Allocation Strategy.
ALGORITHM
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
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);
getch();
}
OUTPUT
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
Preparation (5)
A5llocated
File Indexed Performance (5)

Record (5)

Viva (5)

Total (20)
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0

RESULT
Thus the program to implement the indexed file allocation was executed successfully and
output was verified.

You might also like