Académique Documents
Professionnel Documents
Culture Documents
www.virtual-tutor.co.cc
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)
{
www.virtual-tutor.co.cc
}
www.virtual-tutor.co.cc
if(pid1==0&&pid2==0&&pid3==0)
{
printf("Process 3 id is %d and its parent id is %d\n",getpid(),getppid());
}
}
Sample Output
$ cc process1.c
$ a.out
Parent id is 3553 and root id is 2495
Process 1 id is 3554 and its parent id is 3553
Process 2 id is 3555 and its parent id is 3554
Process 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;
www.virtual-tutor.co.cc
pid1=fork();
if(pid1==0)
{
printf("Process id is %d ",getpid());
printf("and its parent id is %d”,getppid());
execl("/bin/who","who",0);
}
}
www.virtual-tutor.co.cc
Sample Output
$ cc program2.c
$ a.out
Process id is 3553 and parent id is 2495
Root ttyp2 jun25 03.30
sit ttyp1 jun25 03.30
Aim:
To write a C program to display the files in the given directory
Algorithm:
1. Start the program
2. Declare the variable to the structure dirent (defines the file system-independent
directory) and also for DIR
3. Specify the directory path to be displayed using the opendir system call
4. 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 listed
6. 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)
{
www.virtual-tutor.co.cc
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
1049278.
442373..
1049279.kde
1049364.aa.c.swp
1049285.balan.sh.swp
1049387ar.sh
1049404firstfit.c
1049403wai.c
1049406sta1.c
1049405di.c
RESULT:
www.virtual-tutor.co.cc
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)
www.virtual-tutor.co.cc
write(fd1,ch,n);
close(fd2);
stat(source,&s);
printf("Source file size=%d\n",s.st_size);
fstat(fd1,&t);
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
Enter the file to be Copied
sta1.c
Source file size=670
Destination File size before closing=670
Destination File Size after closing=3
[test1@localhost test1]$
RESULT:
www.virtual-tutor.co.cc
Algorithm :
1. Import dir.h header file
2. 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)
{
www.virtual-tutor.co.cc
GREP
Algorithm :
1. Create a file with some content.
2. Get any one pattern / string as a input
3. Read file content as strings and compare it with the input pattern
4. 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();
www.virtual-tutor.co.cc
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);
}
www.virtual-tutor.co.cc
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);
i=-1;
}
i++;
}
fclose(fptr);
getch();
}
Sample Output:
well
RESULT:
Algorithm :
Program:
#include<stdio.h>
#include <conio.h>
#include<graphics.h>
struct pro
CS2257- OS Lab P a g e | 10
Virtual Tutor
{
www.virtual-tutor.co.cc
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;
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);
www.virtual-tutor.co.cc
awt=(float)twt/n;
atat=(float)ttat/n;
printf("AVERAGE WAITING TIME %f\n",awt);
printf("AUERAGE TURN AROUND TIME %f\n",atat);
CS2257- OS Lab P a g e | 11
Virtual Tutor
printf(" %c",'-');
www.virtual-tutor.co.cc
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
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
www.virtual-tutor.co.cc
---------------
0 4 9 15
---------------
Algorithm :
CS2257- OS Lab P a g e | 12
Virtual Tutor
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 time
5. 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 time
7. 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;
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++)
www.virtual-tutor.co.cc
if(p[i].pt>=p[j].pt)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
p[1].wt=0;
CS2257- OS Lab P a g e | 13
Virtual Tutor
p[1].tat=p[1].pt;
www.virtual-tutor.co.cc
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(" %*dp",j,p[i].pid);
}
printf("\n");
for(i=1;i<=p[n].wt+p[n].pt;i++)
printf(" %c",'-');
getch();
}
CS2257- OS Lab P a g e | 14
Virtual Tutor
www.virtual-tutor.co.cc
Sample Output
Enter 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
Gantt chart
----------------
0 3 9 16
2p 1p 3p
----------------
RESULT:
Aim:
To compute average waiting time and average turnaround time and to draw Gantt
chart using priority and round robin scheduling algorithms.
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,
CS2257- OS Lab P a g e | 15
Virtual Tutor
Waiting time of process(n) = waiting time of process (n-1) + process time of process(n-1)
www.virtual-tutor.co.cc
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);
}
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];
www.virtual-tutor.co.cc
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++)
CS2257- OS Lab P a g e | 16
Virtual Tutor
{
www.virtual-tutor.co.cc
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 :
3 8 2 4 6
1 8 3 6 9
2 9 4 9 13
CS2257- OS Lab P a g e | 17
Virtual Tutor
Algorithm :
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()
{
www.virtual-tutor.co.cc
CS2257- OS Lab P a g e | 18
Virtual Tutor
scanf("%d",&ts);
www.virtual-tutor.co.cc
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;
www.virtual-tutor.co.cc
p[q[f]].et=ct+ts;
p[q[f]].rt=p[q[f]].rt-ts;
if (p[q[f]].rt>0)
{
r++;
q[r]=q[f];
}
CS2257- OS Lab P a g e | 19
Virtual Tutor
ct=ct+ts;
www.virtual-tutor.co.cc
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 : 2
Enter the value of time slice : 5
Enter the processing time of process 1 : 10
Enter 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
RESULT:
www.virtual-tutor.co.cc
CS2257- OS Lab P a g e | 20
Virtual Tutor
Algorithm :
1. Start
2. Create a child and parent using fork()
3. Allow communication between both the process
4. 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);
}
else
write(p[1],”Says hello to grandpa”,29);
}
}
Sample Output;
Says hello to grandpaX@
www.virtual-tutor.co.cc
RESULT:
CS2257- OS Lab P a g e | 21
Virtual Tutor
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 buffer
int s_semaphore=1; // to enforce mutual exclusion
char s;
void producer()
{
s_semaphore=0; // set semaphore to avoid access to consumer
if(!s_semaphore)
printf("Now producer can add data to buffer\n");
else
printf("Critical Region \n");
s_semaphore=1; // release semaphore
signal_c(); // call to consumer
}
void consumer()
{
buffer_check(); // check buffer is empty or not
s_semaphore=0; // set semaphore to avoid access to producer
if(!s_semaphore)
printf("Consumer takes from the buffer\n");
else
www.virtual-tutor.co.cc
signal_c()
{
CS2257- OS Lab P a g e | 22
Virtual Tutor
n_semaphore=n_semaphore+1;
www.virtual-tutor.co.cc
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 buffer
Consumer takes from the buffer
Enter n to stop
www.virtual-tutor.co.cc
RESULT:
CS2257- OS Lab P a g e | 23
Virtual Tutor
www.virtual-tutor.co.cc
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();
www.virtual-tutor.co.cc
void display();
void main()
{
int ch;
clrscr();
CS2257- OS Lab P a g e | 24
Virtual Tutor
while(1)
www.virtual-tutor.co.cc
{
printf("\n \nEnter the choice: \n 1.Partition \n 2.Allocate \n 3.Display \n 4.Exit \n");
scanf("%d",&ch);
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));
www.virtual-tutor.co.cc
newnode->start=addr;
newnode->end=addr+size[no];
newnode->size=size[no];
newnode->busy=0;
newnode->pid=0;
newnode->next=NULL;
if(freenode==NULL)
CS2257- OS Lab P a g e | 25
Virtual Tutor
freenode=newnode;
www.virtual-tutor.co.cc
else
{
temp1=freenode;
while(temp1->next!=NULL)
temp1=temp1->next;
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]);
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;
www.virtual-tutor.co.cc
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)
CS2257- OS Lab P a g e | 26
Virtual Tutor
allotnode=anode;
www.virtual-tutor.co.cc
else
{
temp2=allotnode;
while(temp2->next!=NULL)
temp2=temp2->next;
temp2->next=anode;
}
}
}
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;
}
}
www.virtual-tutor.co.cc
Sample Output:
CS2257- OS Lab P a g e | 27
Virtual Tutor
1
www.virtual-tutor.co.cc
CS2257- OS Lab P a g e | 28
Virtual Tutor
30
www.virtual-tutor.co.cc
70
Proces id 1
Start addr 2310
End addr 2324
Proces id 2
Start addr 2350
End addr 2379
Proces id 2
Start addr 2350
End addr 2379
Internal Fragmentation = 15
External Fragmentation = 60
RESULT:
www.virtual-tutor.co.cc
CS2257- OS Lab P a g e | 29
Virtual Tutor
www.virtual-tutor.co.cc
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
{
www.virtual-tutor.co.cc
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;
};
CS2257- OS Lab P a g e | 30
Virtual Tutor
void partition();
www.virtual-tutor.co.cc
void allocate();
void Deallocate(int);
void Merge();
void print();
int block_size,*addr,t;
double up_size;
void main()
{
int id;
int choice;
clrscr();
printf("Enter the Block size \n");
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();
www.virtual-tutor.co.cc
break;
case 3:
print();
break;
case 4:
printf("Enter the process id which you want to Deallocate \n");
scanf("%d",&id);
CS2257- OS Lab P a g e | 31
Virtual Tutor
Deallocate(id);
www.virtual-tutor.co.cc
break;
case 5:
exit(0);
break;
}
}
}
void partition()
{
int size;
node1 = (struct allotnode *)malloc(sizeof(struct allotnode));
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;
}
www.virtual-tutor.co.cc
node2=node1;
t=node2->endaddr;
freenode->startaddr=t+1;
}
else
CS2257- OS Lab P a g e | 32
Virtual Tutor
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;
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)
{
www.virtual-tutor.co.cc
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;
CS2257- OS Lab P a g e | 33
Virtual Tutor
}
www.virtual-tutor.co.cc
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;
}
}
else
printf("\nProcess not available\n");
temp=temp->next;
}
Merge();
}
www.virtual-tutor.co.cc
void Merge()
{
int f=0;
temp=headallot;
while(temp!=NULL)
{
if(temp->flag==0 && temp->next->flag==0)
CS2257- OS Lab P a g e | 34
Virtual Tutor
{
www.virtual-tutor.co.cc
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 | 35
Virtual Tutor
5.Exit pid 1
3 end addr 2315
3.To Print
4.To Deallocate start addr 2296
5.Exit pid 1
3 end addr 2315
CS2257- OS Lab P a g e | 36
Virtual Tutor
CS2257- OS Lab P a g e | 37