aos-lab manual corrected

25
Lab Manual AdvancedOperatingSystems COMP-332 Academic Year 1434-35 H CourseCoordinator: Mr.Mohammed Shuaib Department of Computer Science Jazan University, KSA

Upload: others

Post on 05-Apr-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

LabManual

AdvancedOperatingSystems

COMP-332

AcademicYear1434-35H

CourseCoordinator:

Mr.MohammedShuaib

DepartmentofComputerScience

JazanUniversity,KSA

S.No

Program

Page

1

Introduction to C program under unix operating system

3

2

Program of Fork and Exit system call

5

3

Program using Exec and Wait system call

6

4

Program to demonstrate Orphan process

7

5

Program to demonstrate Zombie process

8

6

Program to demonstrate Thread Synchronization using MUTEX

9

7

Demonstration of Banker’s Algorithm for Deadlock Avoidance

10

8

Producer-Consumer problem using Semaphore

14

9

Dynamic File Allocation First-fit and Best-fit Algorithm

17

10

Contiguous File Allocation technique

22

11

Page Replacement Algorithm FIFO and LRU

24

12

Program to simulate Paging technique

27

Lab 1: Introduction to C Programming under UNIX OS Objectives: To gain experience with:

[1] Writing simple c programs.

[2] Compile a C program in UNIX OS [3] Execute the C program.

A Simple C Program

The following program named “odd” reads an integer number from the user and it prints all the positive odd integers which are less than the given integer:

#include <stdio.h> int main ( ) { int x, n; printf("Enter an Integer: "); scanf("%d",&n); printf("The list of all positive odd integers less than %d:\n",n); for(x=1;x<n;x++) { if (x%2 != 0) printf("%d\n",x); } return(0); }

1.2 Compiling and Executing a C Program

Running the last C program in UNIX Environment will be as following: 1. Open gedit text editor and save the file in the current working directory with “c” file extension so that; it appears as “odd.c”. 2. Compile the program by typing and executing in the terminal command line either: gcc odd.c –o outputFileName OR gcc –o outputFileName odd.c 3. Run the program by typing and executing in the terminal command line: ./outputFileName Notes: (1) You can freely choose any output file name. For example, we can put as “test” or “sample”…etc. (2) One can just compile a C program under Linux without specifying the name of the output file as follows:

gcc odd.c

However, here the system will assign an automatic name for the output file which is always “a.out”. Running the program will be as follows: ./a.out

Beware: Such automatic-generated output file will pertain to the latest compiled C program. For instance, if we compiled a C program named “prog1.c” and then, compiled one named “prog2.c” then, “a.out” will pertain to the latter.

2.1 System Calls

2.1.1 What is a System Call? A system call is just what its name implies -- a request for the operating system to do something on behalf of the user's program. The system calls are functions used in the kernel itself. To the programmer, the system call appears as a normal C function call. However since a system call executes code in the kernel, there must be a mechanism to change the mode of a process from user mode to kernel mode.

2.1.2 Use of System Calls

UNIX system calls are used to manage the file system, control processes, and to provide inter-process communication.

2.Simulation of Fork & Exit system call Aim: To write a C-Program to simulate the fork () & exit() system call. Algorithm: 1. Create a new child using fork () system call. 2. Check for error condition and display error message. 3. If return value of fork is not equal to 0 and display it all parent and getpid using getpid () systemcall. 4. If return value of fork equal to 0 then display it as child process and getpid () system call. 5. Print the corresponding result.

Program:

#include<stdio.h> #include<stdlib.h> #include<unistd.h> int main () { int pid,p1,p2; pid=fork(); if(pid==-1) { printf(\n Error in creation:”); exit(1); } if (pid!=0) { printf(“Parent process”); p1=getpid(); printf(“\n parent process is :% d”,p1); } else {

printf(“\n Child process\n”); p2=getpid(); printf(“\n child process is :% d”,p2); } }

3. Program using exec and wait System Call Aim

To write a C-Program for displaying a command using exec() and wait() system call. Algorithm: 1. Get the command from user 2. Check the process id values is equal to zero after creating a childprocess using wait()system call function 3. It is equal to print the command for using exec()system call 4. Else print the file is not in existence.. Program: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> int main() { int pid; printf("\n\n\t\tUNIX system Calls\n\n"); pid=fork(); if(pid<0) { perror("\nFork Failed-insufficient memory"); exit(-1); } else if(pid==0) { printf("\nThis is Child Process\n"); printf("Process id of child process = %d\n",getpid()); printf("process id of parent process = %d\n",getpid()); execlp("/bin/ls","ls",NULL); } else if(pid>0) { wait(NULL); printf("\nChild Completed"); printf("\n\nThis is Parent process\n"); printf("process id of Parent process =%d\n",getpid()); exit(0); } }

4. Program to Demonstrate Orphan Processes

When a parent dies before its child, the child is automatically adopted by the original “init” process whose PID is 1. To, illustrate this insert a sleep statement into the child’s code. This ensured that the parent process terminated before its child.

#include <stdio.h> #include<unistd.h> int main ( ) { int pid ; printf ("I'am the original process with PID %d and PPID %d.\n", getpid ( ), getppid ( ) ) ; pid = fork ( ) ; /* Duplicate. Child and parent continue from here */ if ( pid != 0 ) /* pid is non-zero, so I must be the parent */ { printf ("I'am the parent process with PID %d and PPID %d.\n", getpid ( ), getppid ( ) ) ; printf ("My child's PID is %d\n", pid ) ; } else /* pid is zero, so I must be the child */ { sleep (4) ; /* make sure that the parent terminates first */ printf ("I'am the child process with PID %d and PPID %d.\n", getpid ( ), getppid ( ) ) ; } printf ("PID %d terminates.\n", getpid ( ) ) ; }

OUTPUT:

I'am the original process with PID 5100 and PPID 5011. I'am the parent process with PID 5100 and PPID 5011. My child's PID is 5101 PID 5100 terminates. /* Parent dies */ I'am the child process with PID 5101 and PPID 1. /* Orphaned, whose parent process is “init” with pid 1 */ PID 5101 terminates.

5. Program to Demonstrate Zombie Processes

A process that terminates cannot leave the system until its parent accepts its return code. If its parent process is already dead, it’ll already have been adopted by the “init” process, which always accepts its children’s return codes. However, if a process’s parent is alive but never executes a wait ( ), the process’s return code will never be accepted and the process will remain a zombie.

The following program created a zombie process, which was indicated in the output from the ps utility. When the parent process is killed, the child was adopted by “init” and allowed to rest in peace. #include <stdio.h> #include<unistd.h> #include<stdlib.h> int main ( ) { int pid ; pid = fork ( ) ; /* Duplicate. Child and parent continue from here */ if ( pid != 0 ) /* pid is non-zero, so I must be the parent */ { while (1) /* Never terminate and never execute a wait ( ) */ sleep (100) ; /* stop executing for 100 seconds */ } else /* pid is zero, so I must be the child */ { exit (42) ; /* exit with any number */ } }

OUTPUT:

[student@localhost ~]$ gcc p1.c –o kk [student@localhost ~]$ ./kk & execute the program in the background [1] 5186 [student@localhost ~]$ ps obtain process status PID TT STAT TIME COMMAND 5187 p0 Z 0:00 <exiting> the zombie child process 5149 p0 S 0:01 -csh (csh) the shell 5186 p0 S 0:00 kk the parent process 5188 p0 R 0:00 ps [student@localhost ~]$ kill 5186 kill the parent process [1] Terminated kk [student@localhost ~]$ ps notice that the zombie is gone now PID TT STAT TIME COMMAND 5149 p0 S 0:01 -csh (csh) 5189 p0 R 0:00 ps

6. A Program to Demonstrate Thread Synchronization using MUTEX Technique

#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *functionC( ); pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; int main( )

{ int rc1, rc2; pthread_t thread1, thread2; /* Create independent threads each of which will execute functionC */ if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) ) { printf("Thread creation failed: %d\n", rc1); } if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) ) { printf("Thread creation failed: %d\n", rc2); } /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); pthread_join( thread2, NULL); exit(0); } void *functionC() { pthread_mutex_lock( &mutex1 ); counter++; printf("Counter value: %d\n",counter); pthread_mutex_unlock( &mutex1 ); }

How to run this program. cc -pthread filename.c ./a.out

OUTPUT:

Counter value: 1 Counter value: 2

7. Program to Demonstrate Banker’s Algorithm for Deadlock Avoidance #include<stdio.h> #include<stdlib.h> #include<unistd.h>

int main() { int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7]; int first,p,r,i,j,prc,count,t; count=0; for(i=1;i<=7;i++) comp[i]=0;

printf("\t BANKERS ALGORITHM IN C \n\n"); printf("Enter the no of processes : "); scanf("%d",&p); printf("\n\nEnter the no of resources : "); scanf("%d",&r); printf("\n\nEnter the claim for each process : "); for(i=1;i<=p;i++) { printf("\nFor process %d : ",i); for(j=1;j<=r;j++) { scanf("%d",&clm[i][j]); } } printf("\n\nEnter the allocation for each process : "); for(i=1;i<=p;i++) { printf("\nFor process %d : ",i); for(j=1;j<=r;j++) { scanf("%d",&alloc[i][j]); } } printf("\n\nEnter total no of each resource instances : "); for(j=1;j<=r;j++) scanf("%d",&rsrc[j]); for(j=1;j<=r;j++) { int total=0; avail[j]=0; for(i=1;i<=p;i++) {total+=alloc[i][j];} avail[j]=rsrc[j]-total; } do { for(i=1;i<=p;i++) { for(j=1;j<=r;j++) { req[i][j]=clm[i][j]-alloc[i][j]; } } printf("\n\nAvailable resources are : "); for(j=1;j<=r;j++)

{ printf("%d\t",avail[j]); } printf("\nClaim matrix:\tAllocation matrix:\n"); for(i=1;i<=p;i++) { for(j=1;j<=r;j++) { printf("%d\t",clm[i][j]); } printf("\t\t\t"); for(j=1;j<=r;j++) { printf("%d\t",alloc[i][j]); } printf("\n"); } prc=0; for(i=1;i<=p;i++) { if(comp[i]==0)//if not completed { prc=i; for(j=1;j<=r;j++) { if(avail[j]==0) { prc=0; break; } } } if(prc!=0) break; } if(prc!=0) { printf("\nProcess %d ",prc); printf(" runs to completion!"); count++; for(j=1;j<=r;j++) { avail[j]+=alloc[prc][j]; alloc[prc][j]=0; clm[prc][j]=0; comp[prc]=1; } } } while(count!=p &&prc!=0);

if(count==p) printf("\nThe system is in a safe state!!"); else printf("\nThe system is in an unsafe state!!"); return (0); } OUTPUT:

Bankers Algorithm in C

Enter the no. of processes: 5

Enter the no. of resources: 3 Enter the claim for each processes: For process1: 7 5 3 For process2: 3 2 2 For process3: 9 0 2 For process4: 2 2 2 For process5: 4 3 3 Enter the allocation for each processes: For process1: 0 1 0 For process2: 2 0 0 For process3: 3 0 2 For process4: 2 1 1 For process5: 0 0 2 Enter total no. of each resource instances: 10 5 7 Available resources are: Claim Matrix: Allocation Matrix 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 3 3 0 0 2 Process 5 runs to completion System is in Safe State!

8 Program to Demonstrate Producer-Consumer problem using Semaphore

Aim:

To write a program for producer/consumer problem using semaphores.

DESCRIPTION This is an illustration of a solution to the classic producer-consumer (bounded-buffer) problem using semaphores.

CONCEPT: Producers produce items to be stored in the buffer. Consumers remove and consume items which have been stored. Mutual exclusion must be enforced on the buffer itself. Moreover, producers can store only when there is an empty slot, and consumers can remove only when there is a full slot. Three semaphores are used. The binary semaphore mutex controls access to the buffer itself. The counting semaphore empty keeps track of empty slots, and the counting semaphore full keeps track of full slots. In this example, the buffer is implemented as an array of size MAX treated as a circular (ring) buffer. Variables in and out give the index of the next position for putting in and taking out (if any). Variable count gives the number of items in the buffer.

Algorithm: 1. Start the program. 2. Declare the variables in the type of pthread_t as tid_produce tid_consume. 3. Declare a structure for semaphore variables. 4. During run time read the number of items to be produced and consumed. 5. Declare and define semaphore function for creation and destroy. 6. Define producer function. 7. Define consumer function. 8. Call producer and consumer function. 9. Stop the execution.

Program:

#include<stdio.h> #include<semaphore.h> #include<pthread.h> #include<stdlib.h> #define buffersize 10 pthread_mutex_t mutex; pthread_t tidP[20],tidC[20]; sem_t full,empty; int counter; int buffer[buffersize]; void initialize() { pthread_mutex_init(&mutex,NULL); sem_init(&full,1,0); sem_init(&empty,1,buffersize); counter=0; } void write(int item) { buffer[counter++]=item; }

int read() { return(buffer[--counter]); } void * producer (void * param) { int waittime,item,i; item=rand()%5; waittime=rand()%5; sem_wait(&empty); pthread_mutex_lock(&mutex); printf("\nProducer has produced item: %d\n",item); write(item); pthread_mutex_unlock(&mutex); sem_post(&full); } void * consumer (void * param) { int waittime,item; waittime=rand()%5; sem_wait(&full); pthread_mutex_lock(&mutex); item=read(); printf("\nConsumer has consumed item: %d\n",item); pthread_mutex_unlock(&mutex); sem_post(&empty); } int main() { int n1,n2,i; initialize(); printf("\nEnter the no of producers: "); scanf("%d",&n1); printf("\nEnter the no of consumers: "); scanf("%d",&n2); for(i=0;i<n1;i++) pthread_create(&tidP[i],NULL,producer,NULL); for(i=0;i<n2;i++) pthread_create(&tidC[i],NULL,consumer,NULL); for(i=0;i<n1;i++) pthread_join(tidP[i],NULL); for(i=0;i<n2;i++) pthread_join(tidC[i],NULL); //sleep(5); exit(0); }

OUTPUT:

cc -pthread prod_cons.c ./a.out

OUTPUTEnter the no of producers: 2 Enter the no of consumers: 2 Producer has produced item: 3 Producer has produced item: 2

Consumer has consumed item: 2 Consumer has consumed item: 3

9 Program to Demonstrate Dynamic File Allocation FirstFit and BestFit Algorithm

Aim: To write a program for first fit and best fit algorithm for memory management.

DESCRIPTION

Memory Management Algorithms In an environment that supports dynamic memory allocation, the memory manager must keep a record of the usage of each allocatable block of memory. This record could be kept by using almost any data structure that implements linked lists. An obvious implementation is to define a free list of block descriptors, with each descriport containing a pointer to the next descriptor, a pointer to the block, and the length of the block. The memory manager keeps a free list pointer and inserts entries into the list in some order conducive to its allocation strategy. A number of strategies are used to allocate space to the processes that are competing for memory.

First Fit Another strategy is first fit, which simply scans the free list until a large enough hole is found. Despite the name, first-fit is generally better than best-fit because it leads to less fragmentation. Small holes tend to accumulate near the beginning of the free list, making the memory allocator search farther and farther each time.

Best Fit The allocator places a process in the smallest block of unallocated memory in which it will fit. It requires an expensive search of the entire free list to find the best hole. More importantly, it leads to the creation of lots of little holes that are not big enough to satisfy any requests. This situation is called fragmentation, and is a problem for all memory-management strategies, although it is particularly bad for best-fit. One way to avoid making little holes is to give the client a bigger block than it asked for. For example, we might round all requests up to the next larger multiple of 64 bytes. That doesn't make the fragmentation go away, it just hides it.

Unusable space in the form of holes is called external fragmentation Unusable space in the form of holes is called external fragmentation

Algorithm: 1. Start the program. 2. Get the number of segments and size.

3. Get the memory requirement and select the option. 4. If the option is ‘2’ call first fit function. 5. If the option is ‘1’ call best fit function. 6. Otherwise exit. 7. For first fit, allocate the process to first possible segment which is free. 8. For best fit, do the following steps. a. Sorts the segments according to their sizes. b. Allocate the process to the segment which is equal to or slightly greater than the process size. 9. Stop the program.

Program: #include<stdio.h> #define MAXSIZE 25 void printlayout(int[],int ); int firstfit(int[],int, int); int bestfit(int[],int, int); int main() { int i,a[25],n,req,choice,pos,ch; printf("How MAny Segments"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Segment Size"); scanf("%d",&a[i]); } loop: printf("How Much Is Your Memory Requirment"); scanf("%d",&req); printf("\n1.Bestfit\n2.Firstfit\n3.Exit\n"); printf("Enter Your Choice\n"); scanf("%d",&choice); switch(choice) { case 1: pos=bestfit(a,n,req); break; case 2: pos=firstfit(a,n,req);

break; } printf("\tBestfit and Firstfit Algorithm\n"); printf("\t_ \n\n"); printlayout(a,n); printf("Your Memory Requirment is :%d\n\n",req); printf("Alloted Memory Region is:%d\n\n",a[pos]); a[pos]=0; printf("Do You Want To Continue 1/0");

scanf("%d",&ch); if(ch==1) goto loop; return 0; } void printlayout(int a[],int n) { int i,j; printf("\t\tMemory Free List"); printf("\n\t\t_ \n\n"); printf("\t\t|~~~|\n"); for(i=0;i<n;i++) { if(a[i]!=0) { for(j=1;j<=(a[i]/100);j++) printf("\t\t| |\n"); printf("\t\t| %d|\n",a[i]); printf("\t\t|---|\n"); }} printf("\n\n"); }

int firstfit(int a[],int n,int r) { int i; for(i=0;i<n;i++) if(a[i]>=r) break; return i; } int bestfit(int a[],int n,int r) { int b[25],i,j,temp,val; for(i=0;i<n;i++) b[i]=a[i]; for(i=0;i<n-1;i++) for(j=i;j<n-1;j++) if(b[i]>b[j]) { temp=b[i]; b[i]=b[j]; b[j]=temp; } for(i=0;i<n;i++) if(b[i]>=r) break; val=b[i]; for(i=0;i<n;i++) if(a[i]==val) break;

return i; }

OUTPUT:

How Many Segments4 Segent Size500 Segent Size200 Segent Size300 Segent Size400 How Much Is Your Memory Requirment200 1.Bestfit 2.Firstfit 3.Exit Enter Your Choice 1 Bestfit and Firstfit Algorithm

Memory Free List

|~~~| | | | | | | | | |500| | --- | | | | | |200| | --- | | | | | | | |300| | --- | | | | | | | | | |400| | --- | Your Memory Requirment is :200 Alloted Memory Region is:200 Do You Want To Continue 1/0 1 How Much Is Your Memory Requirment250 1.Bestfit 2.Firstfit 3.Exit

Enter Your Choice 2 Bestfit and Firstfit Algorithm

Memory Free List

|~~~| | | | | | | |500| | --- | | | | | |300| | --- | | | | | | | |400| | --- | Your Memory Requirment is :250 Alloted Memory Region is:500 Do You Want To Continue 1/0 0

10 Program on FILE ALLOCATION TECHNIQUE-

CONTIGUOUS AIM: To Write a C Program to implement file allocation technique.

DESCRIPTION File Allocation Methods One main problem in file management is how to allocate space for files so that disk space is utilized effectively and files can be accessed quickly. Three major methods of allocating disk space are: * Contiguous Allocation * Linked Allocation * Indexed Allocation. Each method has its advantages and disadvantages. Accordingly, some systems support all three (e.g. Data General's RDOS). More commonly, a system will use one particular method for all files.

Contiguous File Allocation

Each file occupies a set of contiguous block on the disk Allocation using first fit/best fit. A need for compaction Only starting block and length of file in blocks are needed to work with the file Allows random access Problems with files that grow

ALGORITHM:

1. Start the process 2. Declare the necessary variables 3. Get the number of files 4.Get the total no. of blocks that fit in to the file 5. Display the file name, start address and size of the file. 6.Stop the program.

PROGRAM: #include<stdio.h> int main() { int i,j,n,block[20],start; printf(“Enter the no. of file:\n”); scanf(“%d”,&n); printf(“Enter the number of blocks needed for each file:\n”); for(i=0;i<n;i++) scanf(“%d”,&block[i]); start=0; printf(“\t\tFile name\tStart\tSize of file\t\t\n”); printf(“\n\t\tFile1\t\t%d\t\t\t%d\n”,start,block[0]); for(i=2;i<=n;i++) { start=start+block[i-2]; printf(“\t\tFile%d\t\t%d\t\t%d\n”,i,start,block[i-1]); } return 0; }

To run the program. gcc filename.c -o abx ./abx

OUTPUT:

Enter the number of file:4 Enter the number of blocks needed for each file: 3 5 6 1 Filename start size of file File 1 0 3 File 2 3 5 File 3 8 6 File 4 14 1

11 Program to Demonstrate FIFO and LRU page replacement algorithm

Aim: To write a program for page replacement algorithms (FIFO and LRU) for memory management.

DESCRIPTION Page replacement algorithms are used to decide what pages to page out when a page needs to be allocated. This happens when a page fault occurs and free page cannot be used to satisfy allocation

FIFO: The frames are empty in the beginning and initially no page fault occurs so it is set to minus one. When a page fault occurs the page reference string is brought into memory. The operating system keeps track of all the pages in memory, herby keeping track of the most recently arrived and the oldest one. If the page in the page reference string is not in memory, the page fault is incremented and the oldest page is replaced. If the page in the page reference string is in the memory, take the next page without calculating the page fault. Take the next page in the page reference string and check if the page is already present in the memory or not. Repeat the process until all the pages are referred and calculate the page fault for all those pages in the page reference string for the number of available frames.

LRU : A good approximation to the optimal algorithm is based on the observation that pages that have been heavily used in the last few instructions will probably be heavily used again in the next few. Conversely, pages that have not been used for ages will probably remain unused for a long time. This idea suggests a realizable algorithm: when a page fault occurs, throw out the page that has been unused for the longest time. This strategy is called LRU (Least Recently Used) paging.

Algorithm: 1. Start the program 2. Obtain the number of sequences, number of frames and sequence string from the user 3. Now when a page is not in the frame comes, increment the number of page fault and remove the page that come in the first in FIFO algorithm

4. In LRU algorithm, when a page fault occurs, the page which most recently used is removed 5. Display the number of faults 6. Stop the program

Program: #include<stdio.h> int m,n,i,j,k,flag,count=0,refer[100],page_frame[100][2],fault=0,min,no_frames; void replace(int z) { for(i=0;i<n;i++) { flag=1; for(j=0;j<no_frames;j++) if(refer[i]==page_frame[j][0]) { m=j; flag=0; } if(flag) { fault++; min=32000; for(j=0;j<no_frames;j++) if(page_frame[j][1]<min) { min=page_frame[j][1]; k=j; } page_frame[k][0]=refer[i]; page_frame[k][1]=++count; for(j=0;j<no_frames;j++) printf("%d",page_frame[j][0]); printf("\n"); } else

{ printf("no page fault\n"); if(z==2) page_frame[m][1]=++count; } } printf("number of page fault is:%d\n",fault); } int main() { printf("\nEnter the number of reference:"); scanf("%d",&n); printf("\nEnter the number of frames:"); scanf("%d",&no_frames); printf("\nEnter the reference string:");

for(i=0;i<n;i++) scanf("%d",&refer[i]); printf("\t\t\tFIFO ALGORITHM \n"); for(i=0;i<no_frames;i++) { page_frame[i][0]=-1; page_frame[i][1]=count; } replace(1); fault=0; count=0; printf("\t\t\tLRU ALGORITHM \n"); for(i=0;i<no_frames;i++) { page_frame[i][0]=-1; page_frame[i][1]=count;

} replace(2); return 0; }

OUTPUT:

Enter the number of reference:10 Enter the number of frames:3 Enter the reference string:7 0 1 2 0 3 0 4 2 6 FIFO ALGORITHM 7 -1 -1 7 0 -1 7 0 1 2 0 1 no page fault 2 3 1 2 3 0 4 3 0 4 2 0 4 2 6 number of page fault is:9 LRU ALGORITHM 7 -1 -1 7 0 -1 7 0 1 2 0 1 no page fault 2 0 3 no page fault 4 0 3 4 0 2 4 6 2 number of page fault is:8

Program to simulate Paging technique of memory management.

PROGRAM:

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

int main()

{

int np,ps,i;

int *sa;

printf("enter how many pages\n");

scanf("%d",&np);

printf("enter the page size \n");

scanf("%d",&ps);

sa=(int*)malloc(2*np);

for(i=0;i<np;i++)

{

sa[i]=(int)malloc(ps);

printf("page%d\t address %u\n",i+1,sa[i]);

} return 0;

}

OUTPUT:

Enter how many page

3

Enter the page size

3

Page1 address1918

Page2 address1840 Page3 address181