Os Lab Programs for 4th Sem
-
Upload
smalldon22 -
Category
Documents
-
view
4.034 -
download
3
Transcript of Os Lab Programs for 4th Sem
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX:NO: 1 WRITE PROGRAMS USING THE FOLLOWING SYSTEM CALLS OF UNIX OPERATING SYSTEM: Fork, exec, getpid, exit, wait, close, stat, opendir, readdir.
FORK & GETPID
Aim : To Create a process in the following hierarchy
Parent Child1
Child2
Child3
Algorithm 1. Declare the necessary variables.2. Parent process is the process of the program which is running.3. Create the child1 process using fork() When parent is active.4. Create the child2 process using fork() when child1 is active.5. Create the child3 process using fork() when child2 is active.
Program //process1.c#include<stdio.h>
void main(){int pid1,pid2,pid3;printf("Parent id is %d and root id is %d\n",getpid(),getppid());pid1=fork();if(pid1==0){printf("Process 1 id is %d and its parent id is %d\n",getpid(),getppid());pid2=fork();}if(pid2==0){printf("Process 2 id is %d and its parent id is %d\n",getpid(),getppid());pid3=fork();}if(pid1==0&&pid2==0&&pid3==0)
CS2257- OS Lab P a g e | 1
R.M.K. College of Engg. & Technology Dept. of CSE & IT
{printf("Process 3 id is %d and its parent id is %d\n",getpid(),getppid());}}
Sample Output $ cc process1.c$ a.outParent id is 3553 and root id is 2495Process 1 id is 3554 and its parent id is 3553Process 2 id is 3555 and its parent id is 3554Process 3 id is 3556 and its parent id is 3555
EXECl
Aim To Execute a Unix Command (Who) in a ‘C’ program using execl() system call.
Problem Description The Unix system call transfers an executable binary files into a process that are the
exec family of sytem calls.General Format
Execl(filename,arg0,arg1,………argn,0)Char *filename *Arg0* , arg1* ,…….argn*Where filenames are the executable binary files to be transferred into a process. arg0
through argn define the argument to be passed to the process.
Program //program1.c
#include<stdio.h>#include<sys/types.h>#include<unistd.h>void main(){int pid1;pid1=fork();if(pid1==0){printf("Process id is %d ",getpid());printf("and its parent id is %d”,getppid());execl("/bin/who","who",0);}}Sample Output
$ cc program2.c$ a.out
CS2257- OS Lab P a g e | 2
R.M.K. College of Engg. & Technology Dept. of CSE & IT
Process id is 3553 and parent id is 2495Root ttyp2 jun25 03.30sit ttyp1 jun25 03.30
OPENDIR & READDIR
Aim:To write a C program to display the files in the given directory
Algorithm:1. Start the program2. Declare the variable to the structure dirent (defines the file system-independent
directory) and also for DIR3. Specify the directory path to be displayed using the opendir system call4. Check for the existence of the directory and read the contents of the directory using
readdir system call (returns a pointer to the next active directory entry)5. Repeat the above step until all the files in the directory are listed6. Stop the program
Program#include<stdio.h>#include<dirent.h>main(){
DIR *p;struct dirent *dp;p=opendir("."); //p=opendir("./shantha");if(p==NULL){perror("opendir");exit(0);}dp=readdir(p);while(p!=NULL){printf("%d%s\n",dp->d_ino,dp->d_name);dp=readdir(p);}
}Output:"di.c" [New] 21L, 239C written[test1@localhost test1]$ cc di.c[test1@localhost test1]$ ./a.out1049278.442373..1049279.kde1049364.aa.c.swp1049285.balan.sh.swp
CS2257- OS Lab P a g e | 3
R.M.K. College of Engg. & Technology Dept. of CSE & IT
1049387ar.sh1049404firstfit.c1049403wai.c1049406sta1.c1049405di.c
RESULT:
CS2257- OS Lab P a g e | 4
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX.NO: 2. UNIX I/O SYSTEM CALLS - STAT, OPEN, CLOSE, EXIT
Aim :To implement UNIX I/O system calls open, read , write etc.
Algorithm:1. Create a new file using creat command (Not using FILE pointer).2. Open the source file and copy its content to new file using read and write command.3. Find size of the new file before and after closing the file using stat command.
Program:#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>main(){ int fd1,fd2,n; char source[30],ch[5]; struct stat s,t,w; fd1=creat("test.txt",0644); printf("Enter the file to be Copied\n"); scanf("%s",source); fd2=open(source,0); if(fd2==-1) { perror("file doesnot exist"); exit(0); } while((n=read(fd2,ch,1))>0) write(fd1,ch,n);
close(fd2); stat(source,&s); printf("Source file size=%d\n",s.st_size);
fstat(fd1,&t); printf("Destination File size before closing=%d\n",t.st_size); close(fd1); fstat(fd1,&w); printf("Destination File Size after closing=%d\n",w.st_size);}
Sample Output: [test1@localhost test1]$ cc sta1.c[test1@localhost test1]$ ./a.out
CS2257- OS Lab P a g e | 5
R.M.K. College of Engg. & Technology Dept. of CSE & IT
Enter the file to be Copiedsta1.cSource file size=670Destination File size before closing=670Destination File Size after closing=3[test1@localhost test1]$
RESULT:
CS2257- OS Lab P a g e | 6
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX: NO: 3. SIMULATION OF UNIX COMMANDS USING CAim :
To simulate the UNIX command ls.
Algorithm :1. Import dir.h header file2. Create directory structure variable 3. Using findfirst and findnext methods display the files available in the current
directory.
Program:#include <stdio.h>#include <dir.h>#include<conio.h>char dir[MAXDIR];void main(void){ struct ffblk ffblk; int done; clrscr(); printf("Directory listing of *.*\n"); done = findfirst("*.*",&ffblk,0); while (!done) { printf(" %s\n", ffblk.ff_name); done = findnext(&ffblk); }getch();}
CS2257- OS Lab P a g e | 7
R.M.K. College of Engg. & Technology Dept. of CSE & IT
GREP
Aim : To simulate the UNIX command grep.
Algorithm :1. Create a file with some content.2. Get any one pattern / string as a input3. Read file content as strings and compare it with the input pattern4. If pattern match with file string or part of the string then print that string / line.5. Close the file.
Program:#include<stdio.h>#include<conio.h>#include<string.h>void main(){ FILE *fptr; char ch; int i; char p[10], a[50]; clrscr(); fptr=fopen("input.txt","w"); printf("Enter the data to be stored in the file\n"); scanf("%c",&ch); while(ch!='$') { fprintf(fptr,"%c",ch); scanf("%c",&ch); } fclose(fptr); printf("Enter the pattern to be searched"); scanf("%s",p); fptr=fopen("input.txt","r");
i=0; while(!feof(fptr)) {
ch=getc(fptr);if( ch!='\n') a[i] = ch;else { a[i]='\0'; if(strnicmp(a,p,strlen(p))==0) printf("%s\n",a);
CS2257- OS Lab P a g e | 8
R.M.K. College of Engg. & Technology Dept. of CSE & IT
i=-1; } i++;
} fclose(fptr); getch(); }
Sample Output:
Enter the data to be stored in the fileIndiaCountrywelcomesitwell$Enter the pattern to be searchedwelwelcomewell
RESULT:
CS2257- OS Lab P a g e | 9
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX: NO: 4 SCHEDULING ALGORITHMS – FCFS AND SJFAim:
To compute average waiting time and average turnaround time and to draw Gantt chart using FCFS and SJF
FCFS SCHEDULING ALGORITHM
Algorithm :
1. Get the no of processes.2. For each process assign the process id and get the process time.3. Set the waiting time of the first process as 0 and its turn around time as process time.4. For each process calculate, Waiting time of process(n) = waiting time of process (n-1) + process time of process (n-1) Turn around time of process(n) = waiting time of process (n) + process time of process (n)5. Calculate the average waiting time and turn around time.6. Print the Gantt chart.
Program:
#include<stdio.h> #include <conio.h> #include<graphics.h> struct pro { int pid,pt,wt,tat; }; void main() { struct pro p[10]; int n,i,twt,ttat,j,k; float awt,atat; char s[5],e[5];
clrscr(); printf("Enter the no of process\n"); scanf("%d",&n); for(i=1;i<=n;i++) { p[i].pid=i; printf("Enter the processing time of %d process\n",i); scanf("%d",&p[i].pt); } p[1].wt=0;
CS2257- OS Lab P a g e | 10
R.M.K. College of Engg. & Technology Dept. of CSE & IT
p[1].tat=p[1].pt; twt=p[1].wt; ttat=p[1].tat; for(i=2;i<=n;i++) { p[i].wt=p[i-1].wt+p[i-1].pt; p[i].tat=p[i].wt+p[i].pt; twt=twt+p[i].wt; ttat=ttat+p[i].tat; } printf("PROCESSID PROCESSTIME WAITINGTIME TURNTIME\n"); for(i=1;i<=n;i++) { printf("%d\t %d\t %d \t%d\n",p[i].pid,p[i].pt,p[i].wt,p[i].tat); } printf("TOTAL WAITING TIME %d\n",twt); printf("TOTAL TURN AROUND TIME %d\n",ttat); awt=(float)twt/n; atat=(float)ttat/n; printf("AVERAGE WAITING TIME %f\n",awt); printf("AUERAGE TURN AROUND TIME %f\n",atat);
printf(“\nGantt chart \n”);for(i=1;i<=p[n].wt+p[n].pt;i++)
printf(" %c",'-');printf("\n");
for(i=1;i<=n;i++){ j=p[i].wt; printf(" %*d",j,p[i].wt);}for(i=1;i<=p[n].wt;i++)printf(" ");printf("%d",p[n].wt+p[n].pt);
printf("\n");for(i=1;i<=p[n].wt+p[n].pt;i++) printf(" %c",'-');
getch(); }
Sample Output:Enter the no of process 3 Enter the processing time of 1 process
CS2257- OS Lab P a g e | 11
R.M.K. College of Engg. & Technology Dept. of CSE & IT
4 Enter the processing time of 2 process 5 Enter the processing time of 3 process 6 PROCESSID PROCESSTIME WAITINGTIME TURNTIME 1 4 0 4 2 5 4 9 3 6 9 15 TOTAL WAITING TIME 13 TOTAL TURN AROUND TIME 28 AVERAGE WAITING TIME 4.333333 AUERAGE TURN AROUND TIME 9.333333
Gant Chart - - - - - - - - - - - - - - - 0 4 9 15 - - - - - - - - - - - - - - -
SJF SCHEDULING ALGORITHM
Algorithm :1. Get the no of processes.2. For each process assign the process id and get the process time.3. Sort the processes according to the process time.4. Set the waiting time of the first process as 0 and its turn around time as process time5. For each process calculate, Waiting time of process(n) = waiting time of process (n-1) + process time of pocess(n-1) Turn around time of process(n) = waiting time of process (n) + process time of process (n)6. Calculate the average waiting time and turn around time7. Print the Gantt chart.
Program:
#include <stdio.h>#include<conio.h>struct pro{int pid;int pt;int wt; int tat;};void main(){struct pro p[10],t;
CS2257- OS Lab P a g e | 12
R.M.K. College of Engg. & Technology Dept. of CSE & IT
int n,i,j,twt,ttat,k;float awt,atat;clrscr(); printf("enter the no of process\n");scanf("%d",&n);for(i=1;i<=n;i++){p[i].pid=i;printf("enter the processing time of %d process\n",i);scanf("%d",&p[i].pt);}for(i=1;i<n;i++)for(j=i+1;j<=n;j++)if(p[i].pt>=p[j].pt){t=p[i];p[i]=p[j];p[j]=t;}p[1].wt=0;p[1].tat=p[1].pt;twt=p[1].wt;ttat=p[1].tat;for(i=2;i<=n;i++){p[i].wt=p[i-1].wt+p[i-1].pt;p[i].tat=p[i].wt+p[i].pt;twt=twt+p[i].wt;ttat=ttat+p[i].tat;}printf("PROCESSID PROCESSTIME WAITINGTIME TURNTIME\n");for(i=1;i<=n;i++){ printf("%d %d %d %d\n",p[i].pid,p[i].pt,p[i].wt,p[i].tat);}printf("TOTAL WAITING TIME %d\n",twt);printf("TOTAL TURN AROUND TIME %d\n",ttat);awt=(float)twt/n;atat=(float)ttat/n;printf("AVERAGE WAITING TIME %f\n",awt);printf("AUERAGE TURN AROUND TIME %f\n",atat);
printf("\nGantt chart \n");for(i=1;i<=p[n].wt+p[n].pt;i++)
printf(" %c",'-');printf("\n");
CS2257- OS Lab P a g e | 13
R.M.K. College of Engg. & Technology Dept. of CSE & IT
for(i=1;i<=n;i++){ j=p[i].wt; printf("%*d",j,p[i].wt);}for(i=1;i<=p[n].wt;i++)printf(" ");printf("%d",p[n].wt+p[n].pt); printf("\n"); for(i=1;i<=n;i++){ j=p[i].wt; printf(" %*dp",j,p[i].pid); }printf("\n");for(i=1;i<=p[n].wt+p[n].pt;i++) printf(" %c",'-');getch();
}
Sample OutputEnter the no of process 3 Enter the processing time of 1 process 6 Enter the processing time of 2 process 3 Enter the processing time of 3 process 7
PROCESSID PROCESSTIME WAITINGTIME TURNTIME 2 3 0 3 1 6 3 9 3 7 9 16 TOTAL WAITING TIME 12 TOTAL TURN AROUND TIME 28 AVERAGE WAITING TIME 4.000000 AUERAGE TURN AROUND TIME 9.333333 Gantt chart - - - - - - - - - - - - - - - - 0 3 9 16 2p 1p 3p - - - - - - - - - - - - - - - - RESULT:
CS2257- OS Lab P a g e | 14
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX: NO: 5 SCHEDULING ALGORITHMS – PRIORITY AND ROUND ROBIN
Aim: To compute average waiting time and average turnaround time and to draw Gantt
chart using priority and round robin scheduling algorithms.
PRIORITY SCHEDULING ALGORITHM
Algorithm :1. Get the no of processes.2. For each process assign the process id and get the process time and the priority value.3. Sort the processes according to the priority value.4. Set the waiting time of the first process as 0 and its turn around time as process time.5. For each process calculate,Waiting time of process(n) = waiting time of process (n-1) + process time of process(n-1)Turn around time of process(n) = waiting time of process(n) + process time of process (n)6. Calculate the average waiting time and turn around time.
Program :
#include <stdio.h>#include <conio.h>struct pro{int pid;int pr;int pt;int wt;int tat;};void main(){struct pro p[10],t;int n,i,j,twt,ttat;float awt,atat;clrscr();printf("enter the no of process\n");scanf("%d",&n); for(i=1;i<=n;i++){p[i].pid=i;printf("enter the processing time & priority of %d process\n",i);scanf("%d %d",&p[i].pt,&p[i].pr);}
CS2257- OS Lab P a g e | 15
R.M.K. College of Engg. & Technology Dept. of CSE & IT
for(i=1;i<n;i++)for(j=i+1;j<=n;j++)if(p[i].pr<=p[j].pr){t=p[i];p[i]=p[j];p[j]=t;}p[1].wt=0;p[1].tat=p[1].pt;twt=p[1].wt;ttat=p[1].tat; for(i=2;i<=n;i++){p[i].wt=p[i-1].wt+p[i-1].pt;p[i].tat=p[i].wt+p[i].pt;twt=twt+p[i].wt;ttat=ttat+p[i].tat;} printf("PROCESSID PROCESSTIME PRIORITY WAITINGTIME TURNTIME\n");for(i=1;i<=n;i++){printf("%d %d %d %d %d\n",p[i].pid,p[i].pt,p[i].pr,p[i].wt,p[i].tat);}printf("TOTAL WAITING TIME %d\n",twt);printf("TOTAL TURN AROUND TIME %d\n",ttat);awt=(float)twt/n;atat=(float)ttat/n;printf("AVERAGE WAITING TIME %f\n",awt);printf("AUERAGE TURN AROUND TIME %f\n",atat);getch();}
Sample Output :
enter the no of process : 4enter the burst time for process 1 : 3enter the priority for process 1 : 8enter the burst time for process 2 : 4enter the priority for process 2 : 9enter the burst time for process 3 : 2enter the priority for process 3 : 8enter the burst time for process 4 : 4enter the priority for process 4 : 1
CS2257- OS Lab P a g e | 16
R.M.K. College of Engg. & Technology Dept. of CSE & IT
process id priority burst time waiting time turn around time 4 1 4 0 4
3 8 2 4 6
1 8 3 6 9
2 9 4 9 13
total waiting time : 19total turn around time : 32average waiting time : 4.75average turn around time : 8
ROUND ROBIN SCHEDULING ALGORITHM
Algorithm :
1. Accept the no of processes in the ready queue and time slice.2. For each process in the ready queue accept the burst time.3. Calculate the no of time slices required for each process.4. If the burst time is less than the time slice then the no of time slice is 1.5. Considering the ready queue as a circular queue, calculate Total waiting time for process(n) = waiting time for process (n-1) + burst time of
process (n-1) + the time difference in getting the CPU from process(n). Total turn around time for process(n) = waiting time for process (n) + burst time of
process (n) + the time difference in getting the CPU from process(n).6. Calculate the average waiting time and turn around time.
Program :
#include <stdio.h>#include <conio.h>struct pro{int pid;int pt;int rt;int st;int et;int seen;int wt;int tat;};void main()
CS2257- OS Lab P a g e | 17
R.M.K. College of Engg. & Technology Dept. of CSE & IT
{struct pro p[10];int n,i,ct=0,ts,twt,ttat,q[30],r=0,f=1;float awt,atat;clrscr();printf("\nenter the no of process:");scanf("%d",&n);printf("\nenter the value of time slice:");scanf("%d",&ts);for(i=1;i<=n;i++){p[i].pid=i;printf("\nenter the processing time of %d process:",i);scanf("%d",&p[i].pt);p[i].rt=p[i].pt;p[i].seen=0; r++;q[r]=p[i].pid;}printf("PRID PT ST ET RT\n");p[q[f]].wt=0;p[q[f]].st=ct;p[q[f]].et=ct+ts;p[q[f]].rt=p[q[f]].rt-ts;p[q[f]].seen=1;if (p[q[f]].rt>0){r++;q[r]=q[f];}printf("%d %d %d %d %d\n",q[f],p[q[f]].pt,p[q[f]].st, p[q[f]].et,p[q[f]].rt);f++;ct=ts; while (r>=f){if(p[q[f]].seen==0){p[q[f]].wt=p[q[f]-1].wt+ts;p[q[f]].seen=1;}else{p[q[f]].wt=p[q[f]].wt+ct-p[q[f]].et;}p[q[f]].st=ct;p[q[f]].et=ct+ts;
CS2257- OS Lab P a g e | 18
R.M.K. College of Engg. & Technology Dept. of CSE & IT
p[q[f]].rt=p[q[f]].rt-ts;if (p[q[f]].rt>0){r++;q[r]=q[f];}ct=ct+ts; printf("%d %d %d %d %d\n",q[f],p[q[f]].pt,p[q[f]].st,
p[q[f]].et,p[q[f]].rt);f++;}for(i=1;i<=n;i++){printf("\nwaiting time of %d process is %d:",i,p[i].wt);} getch();}
Sample Output :Enter the no of process : 2Enter the value of time slice : 5Enter the processing time of process 1 : 10Enter the processing time of process 2 : 5
PRID PT ST ET RT 1 10 0 5 5 2 5 5 10 0 1 10 10 15 0
Average Waiting time = 7.5
RESULT:
CS2257- OS Lab P a g e | 19
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX:NO: 6 INTERPROCESS COMMUNICATION
Aim : To implement interprocess communication using pipe command.
Algorithm :1. Start2. Create a child and parent using fork()3. Allow communication between both the process4. Stop
Program :#include<stdio.h>main(){ int p[2],pid,pid1; char msg[25],msg1[25]; pipe(p); pid=fork(); if(pid!=0) { sleep(2); read(p[0],msg1,21); printf(“%s”,msg1); }else{ pid1=fork(); if(pid1!=0); { sleep(1); read(p[0],msg1,21); write(p[1],”Grand child says hello”,21); }elsewrite(p[1],”Says hello to grandpa”,29);}}Sample Output; Says hello to grandpaX@
RESULT:
CS2257- OS Lab P a g e | 20
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX:NO :7 PRODUCER- CONSUMER PROBLEM
Aim : To implement the Producer- Consumer problem using semaphores.
Algorithm :5. Create two functions called producer and consumer.6. Set semaphore variable as 1.7. When producer active set the semaphore variable as 1 and allow producer to put data
into the buffer and don’t allow consumer to consume anything.8. After producer complete the process release the semaphore and signal the consumer.9. When consumer active again set the semaphore variable as 1 and allow the consumer
to get data from buffer and don’t allow the producer to add data.10. After the consumer taken release the semaphore variable and signal the producer.
Program :#include<stdio.h>#include<conio.h>int n_semaphore=0; // keep track of no of item in the bufferint s_semaphore=1; // to enforce mutual exclusionchar s;
void producer(){s_semaphore=0; // set semaphore to avoid access to consumerif(!s_semaphore)
printf("Now producer can add data to buffer\n");else
printf("Critical Region \n");s_semaphore=1; // release semaphoresignal_c(); // call to consumer}
void consumer(){buffer_check(); // check buffer is empty or nots_semaphore=0; // set semaphore to avoid access to producerif(!s_semaphore)
printf("Consumer takes from the buffer\n");else
printf("Critical Region \n");s_semaphore=1; // release semaphoresignal_p(); // call to producer}
signal_c(){
CS2257- OS Lab P a g e | 21
R.M.K. College of Engg. & Technology Dept. of CSE & IT
n_semaphore=n_semaphore+1;consumer();return 0;}signal_p(){n_semaphore=n_semaphore-1;printf("Enter n to stop\n");scanf("%c",&s);if(s=='n') exit(); return 0;}buffer_check(){if(n_semaphore<=0) {
printf("Buffer is empty\n");exit();
} return 0;}
void main(){ clrscr(); n_semaphore=0; while(1) { producer(); }}
Sample Output;Now producer can add data to bufferConsumer takes from the bufferEnter n to stopNow producer can add data to bufferConsumer takes from the bufferEnter n to stopn
RESULT:
EX:NO :8 MEMORY MANAGEMENT – FIXED PARTITION
CS2257- OS Lab P a g e | 22
R.M.K. College of Engg. & Technology Dept. of CSE & IT
Aim:To implement Fixed Partition Memory Management Scheme.
Algorithm:1. Get the number of pages and their sizes.2. Create pages with corresponding sizes using linked list as free nodes.3. Get the number of programs <= number of pages to be allocated and their sizes. 4. Start with the first page, if the first program size fits with this page then allocate first
page for the first program. Otherwise check with next program and so on.5. Continue the process with next pages.6. Maintain the allotted pages as a separate linked list.7. Calculate internal fragmentation – balance spaces in each page and external
fragmentation – sizes of unallocated pages.8. Display the starting and ending address of Free nodes and allotted nodes and also the
internal and external fragmentations.
Program:
#include<stdio.h>#include<conio.h>struct node{int size;int busy;int pid;struct node *start, *end, *next;};struct node *freenode=NULL, *allotnode=NULL, *newnode, *temp1=NULL, *temp2=NULL, *anode;int internal=0, external=0;int no_of_pages, size[20], allot[10]={0};int no,np,x,y,t=0;
void partition();void allocate();void display();
void main(){int ch;clrscr();
while(1){printf("\n \nEnter the choice: \n 1.Partition \n 2.Allocate \n 3.Display \n 4.Exit \n");scanf("%d",&ch);
CS2257- OS Lab P a g e | 23
R.M.K. College of Engg. & Technology Dept. of CSE & IT
switch(ch){case 1:
partition();break;
case 2:allocate();break;
case 3:display();break;
case 4:exit(0);break;
default:printf("Wrong Choice");break;
} }
}
void partition(){int addr;printf("\nEnter the no of pages\n");scanf("%d",&no_of_pages);printf("\nEnter the page sizes in Bytes\n ");for(no=0;no<no_of_pages;no++){scanf("%d",&size[no]);addr=(int *)malloc(size[no]);newnode=(struct node *)malloc(sizeof(struct node));newnode->start=addr;newnode->end=addr+size[no];newnode->size=size[no];newnode->busy=0;newnode->pid=0;newnode->next=NULL;if(freenode==NULL) freenode=newnode;else { temp1=freenode; while(temp1->next!=NULL)
temp1=temp1->next;
CS2257- OS Lab P a g e | 24
R.M.K. College of Engg. & Technology Dept. of CSE & IT
temp1->next=newnode; }}}void allocate(){int no_of_prog, sizep[20];anode=(struct node *)malloc(sizeof(struct node));do{printf("\nEnter the number of programs<= %d\n",no_of_pages);scanf("%d",&no_of_prog);}while(no_of_prog > no_of_pages); printf("\nEnter the program sizes in Bytes\n");for(np=0;np<no_of_prog;np++)scanf("%d",&sizep[np]);
//Program to allocate the pages to the programs using firstfit
temp1=freenode;while(temp1 != NULL) {
for(y=0;y<no_of_prog;y++){
if(temp1->size>=sizep[y] && temp1->busy==0 && allot[y]==0){
temp1->busy=1;allot[y]=1;anode->pid=y+1;anode->busy=1;anode->size=temp1->size;anode->start=temp1->start;t=(int)temp1->start;anode->end=t+sizep[y]-1;anode->next=NULL;internal+=temp1->size-sizep[y];if(allotnode==NULL) allotnode=anode;else{ temp2=allotnode; while(temp2->next!=NULL) temp2=temp2->next; temp2->next=anode;}
CS2257- OS Lab P a g e | 25
R.M.K. College of Engg. & Technology Dept. of CSE & IT
}}
printf("Proces id %d \n",allotnode->pid);printf("Start addr %u \n",allotnode->start);printf("End addr %u \n\n",allotnode->end);if(temp1->busy==0)
external+=temp1->size;temp1=temp1->next;
} printf("Internal Fragmentation = %d\n",internal); printf("External Fragmentation = %d\n",external);}
void display(){temp1=freenode;printf("\n\nFree nodes are\n\n\n"); while(temp1!=NULL) {
if((temp1->busy)==0){printf("Start addr %u \n",temp1->start);printf("End addr %u \n",temp1->end);printf("Size %d \n",temp1->size);}temp1=temp1->next;
}}
Sample Output:
Enter the choice: 1.Partition 2.Allocate 3.Display 4.Exit1
Enter the no of pages3
Enter the page sizes in Bytes 204060
CS2257- OS Lab P a g e | 26
R.M.K. College of Engg. & Technology Dept. of CSE & IT
Enter the choice: 1.Partition 2.Allocate 3.Display 4.Exit3
Free nodes areStart addr 2310Endaddr 2330Size 20
Start addr 2350Endaddr 2390Size 40
Start addr 2410Endaddr 2470Size 60
Enter the choice: 1.Partition 2.Allocate 3.Display 4.Exit2
Enter the number of programs<= 33
Enter the program sizes in Bytes153070Proces id 1Start addr 2310End addr 2324
Proces id 2Start addr 2350End addr 2379
Proces id 2Start addr 2350
CS2257- OS Lab P a g e | 27
R.M.K. College of Engg. & Technology Dept. of CSE & IT
End addr 2379
Internal Fragmentation = 15External Fragmentation = 60
Enter the choice: 1.Partition 2.Allocate 3.Display 4.Exit4
RESULT:
CS2257- OS Lab P a g e | 28
R.M.K. College of Engg. & Technology Dept. of CSE & IT
EX:NO :9 MEMORY MANAGEMENT – VARIABLE PARTITIONAim:
To implement Variable Partition Memory Management Scheme.
Algorithm:1. Get the number of bytes for the memory and allocate a common space.2. Partition the memory according to the process id and the request memory size from
the user one by one until the memory is full.3. After the process finishes, the memory is deallocated and any other process can use
this.4. When deallocating the memory, if any two consecutive partitions are free merge them
as a single partition.5. Allocate the process in partitions according to the process sizes. Balance space in that
partition is treated as a separate partition or merge with the nearby partition if that is free.
6. Display the starting and ending address of Free nodes and allotted nodes.
Program:
#include<stdio.h>#include<conio.h>struct allotnode{ int *startaddr; int pid; int flag; //it indicates whether the block is empty or not , if it is 1 then the block is busy int *endaddr; struct allotnode *next;};
void partition();void allocate();void Deallocate(int);void Merge();void print();int block_size,*addr,t;double up_size;
struct allotnode *headallot=NULL,*node1=NULL,*node2=NULL,*temp,*freenode;
void main(){int id;int choice;clrscr();printf("Enter the Block size \n");
CS2257- OS Lab P a g e | 29
R.M.K. College of Engg. & Technology Dept. of CSE & IT
scanf("%d",&block_size);addr = (int *) calloc(block_size,1);freenode = (struct allotnode *)malloc(sizeof(struct allotnode));freenode->flag=0;freenode->pid=0;freenode->startaddr=addr;freenode->endaddr=addr+(block_size/2);freenode->next=NULL;
while(1){printf("Enter the choice \n 1.Partition \n 2.To Allocate \n 3.To Print \n 4.To Deallocate \n 5.Exit \n");scanf("%d",&choice);switch(choice){ case 1 :
partition();break;
case 2:allocate();break;
case 3:print();break;
case 4:printf("Enter the process id which you want to Deallocate \n");scanf("%d",&id);Deallocate(id);break;
case 5:exit(0);break;
}}}
void partition(){ int size; node1 = (struct allotnode *)malloc(sizeof(struct allotnode));
printf("Enter the process id\n"); scanf("%d",&node1->pid); printf("Enter the size of the proces"); scanf("%d",&size);
CS2257- OS Lab P a g e | 30
R.M.K. College of Engg. & Technology Dept. of CSE & IT
node1->next=freenode; node1->flag=1;
if(((int)freenode->endaddr-(int)freenode->startaddr)>=size){ if(headallot==NULL) {
headallot=node1;node1->startaddr=addr;t=addr+(size/2);node1->endaddr=t-1;
} else {
node2->next=node1;t=(int)node2->endaddr;node1->startaddr=t+1;node1->endaddr=t+size;
} node2=node1; t=node2->endaddr; freenode->startaddr=t+1;}
elseprintf("Less Memory ! \n\n\n");
}
void allocate(){ int pid,size,allot=0; node1 = (struct allotnode *)malloc(sizeof(struct allotnode)); printf("Enter the process id\n"); scanf("%d",&pid); printf("Enter the size of the proces"); scanf("%d",&size); temp=headallot;
while(temp != NULL) {if(temp->flag==0 && ((int)temp->endaddr - (int)temp->startaddr)>=size && allot==0)
{temp->flag=1;temp->pid=pid;allot=1;
CS2257- OS Lab P a g e | 31
R.M.K. College of Engg. & Technology Dept. of CSE & IT
node1->flag=0;node1->endaddr=temp->endaddr;temp->endaddr=(int)temp->startaddr+size-1;node1->startaddr=(int)temp->endaddr+1;node1->next=temp->next;temp->next=node1;}temp=temp->next;
}}
void print(){temp=headallot;printf("\n\nAllocated nodes are\n\n\n"); while(temp != NULL) {
if(temp->flag==1){printf("start addr %d \n",temp->startaddr);printf("pid %d \n",temp->pid);printf("end addr %d \n\n\n",temp->endaddr);}temp=temp->next;
} temp=headallot; printf("\n\nFree nodes are\n\n\n");
while(temp != NULL) {
if(temp->flag==0){ if(temp->startaddr == temp->endaddr)
printf("\n No Free nodes \n"); else { printf("start addr %d \n",temp->startaddr);
printf("end addr %d \n\n\n",temp->endaddr); }}temp=temp->next;
}}
void Deallocate(int id){temp=headallot;
CS2257- OS Lab P a g e | 32
R.M.K. College of Engg. & Technology Dept. of CSE & IT
while(temp!=NULL){if(temp->pid==id){
temp->pid=0;temp->flag=0;
}
else printf("\nProcess not available\n");temp=temp->next;}Merge();}
void Merge(){int f=0; temp=headallot; while(temp!=NULL) { if(temp->flag==0 && temp->next->flag==0) { temp->endaddr=temp->next->endaddr; temp->next=temp->next->next; f=1; } temp=temp->next; } if(f==1) Merge();}Sample Output:
CS2257- OS Lab P a g e | 33
R.M.K. College of Engg. & Technology Dept. of CSE & IT
Enter the Block size100Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit1Enter the process id1Enter the size of the process20
Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit1Enter the process id2Enter the size of the process40Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit3
Allocated nodes arestart addr 2296pid 1end addr 2315
start addr 2216pid 2end addr 2355
Free nodes are
start addr 2256end addr 2396
CS2257- OS Lab P a g e | 34
R.M.K. College of Engg. & Technology Dept. of CSE & IT
Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit2
Enter the process id3Enter the size of the proces30
Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit3
Allocated nodes are
start addr 2296pid 1end addr 2315
start addr 2216pid 2end addr 2355
start addr 2256pid 3end addr 2385
Free nodes are
start addr 2286end addr 2396
Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate
CS2257- OS Lab P a g e | 35
R.M.K. College of Engg. & Technology Dept. of CSE & IT
5.Exit4
Enter the process id which you want to Deallocate2Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit3
Allocated nodes are
start addr 2296pid 1end addr 2315
start addr 2256pid 3end addr 2385
Free nodes are
start addr 2216end addr 2355start addr 2286end addr 2396
Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit2
Enter the process id2Enter the size of the process10
Enter the choice 1.Partition 2.To Allocate
CS2257- OS Lab P a g e | 36
R.M.K. College of Engg. & Technology Dept. of CSE & IT
3.To Print 4.To Deallocate 5.Exit3
Allocated nodes are
start addr 2296pid 1end addr 2315
start addr 2316pid 2end addr 2325
start addr 2256pid 3end addr 2385
Free nodes areRESULT:
start addr 2226end addr 2355
start addr 2286end addr 2396
CS2257- OS Lab P a g e | 37