Operating Systems
Operating Systems
OS LAB Manual
LAB MANUAL
OBJECTIVES
LIST OF EXPERIMENTS
Aim:
Result
Thus the study of basic UNIX commands has been executed successfully.
To write a program using the following system calls of UNIX operating system fork, exec, getpid, exit,
wait, close, stat, opendir, readdir
ALGORITHM:
1)Start
2)Declare the ‘pid’ and get the pid value using getpid() method.
5)Else if pid=0 then print parent process value otherwise print the id of child process.
Coding:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void main(intargc,char *arg[])
{
intpid;
pid=fork();
if(pid<0)
{
printf("fork failed");
exit(1);
}
else if(pid==0)
{
execlp("whoami","ls",NULL);
exit(0);
}
else
{
printf("\n Process id is -%d\n",getpid());
wait(NULL);
exit(0);
}
}
OUTPUT:
Cc fork.c
./a.out
Aim:
Algorithm:
Step 2: Open a file for O_RDWR for R/W,O_CREATE for creating a file ,O_TRUNC for truncate a file.
Step 3: Using getchar(), read the character and stored in the string[] array.
Step 5: Then the first is opened for read only mode and read the characters and displayed it and close the
file.
Coding:
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[256];
DIR *dirp;
printf("\n\n Enter directory Name");
scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("Error");
exit(1);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
} closedir(dirp);
}
Output:
Enter directory Name
inf
in
eng
Result:
Thus the invoking system calls of readdir(), opendir(), closedir() has been successfully executed and
completed
To write a c program to implement the file operation using i/o system calls
Algorthim:
Step5: While reading contents, if a new line character occurs, increase the number of lines.
Coding:
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main()
{
int fd,sz;
char buffer[50];
fd=open("info9",O_WRONLY|O_TRUNC);
if(fd<0)
{
printf("ERROR");
scanf("%s",buffer);
sz=write(fd,buffer,strlen(buffer));
close(fd);
}
}
Output:
[user@linuxserver ~]$ ./a.out
information
[user@linuxserver ~]$ cat info9
information
Result:
Thus the file operations of I/O system calls has been successfully executed and completed.
8
Aim:
To write a program to simulate UNIX commands like cp, ls, grep using UNIX operating system
Algorithm:
Step 1 : Start
Step 2: Use O_CREATE for creating a file ,O_RDWR for opening in read and write mode and
O_TRUNC for truncating a file.
Step 5: Write the stored array in the file and close it.
Step 6: Then open the file in read only mode and display the file.
Step7: Stop.
Coding:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
main( )
{
int fd[2];
char buf1[25]= "just a test\n";
char buf2[50];
fd[0]=open("file1",O_RDWR);
fd[1]=open("file2",O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf("\n Enter the text now….");
scanf("\n %s",buf1);
printf("\n Cat file1 is \n hai");
write(fd[0], buf1, strlen(buf1));
lseek(fd[0], SEEK_SET, 0);
read(fd[0], buf2, sizeof(buf1));
write(fd[1], buf2, sizeof(buf2));
close(fd[0]);
close(fd[1]);
printf("\n");
return 0;
}
SIMULATE LS COMMAND
The ls command lists all the contents of the directory including filse and sub-directories. The
following c program in simulates the ls command.
9
#include<stdio.h>
#include<dirent.h>
#include<stdlib.h>
int main()
{
char dirname[10];
DIR*p;
struct dirent *d;
printf("Enter directory name\n");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find directory");
exit(-1);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
return 0;
}
#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
10
Aim:
Algorithm:
Coding
echo " Enter the number for which factorial is to be found "
read n
f=1
a=0
i=1
if [ $n -eq $a ]
then
echo $f
else
while [ $i -le $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done
fi
echo " The factorial of the number is "
echo $f
Output:
Result:
Thus the program for factorial using shell script has been executed and verified successfully.
11
Aim:
Algorithm:
Step 2: Declare the variables and initialize the variables, a=0, b=1, and show =0
Step 3: Enter the number of terms of Fibonacci series to be printed
Step 4: Print first two terms of series
Step 5: Using loop for the following steps
show=a+b
a=b
b=show
increase value of i each time by 1
print the value of show
Step 6 : Stop the program.
Coding:
12
Output:
Result:
Thus the program for Fibonacci series using shell script has been executed and verified
successfully.
13
Aim:
Algorithm:
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process (n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Calculate Average waiting time = Total waiting Time / Number of process
Coding:
#include<stdio.h>
int main()
{
int bursttime[10][10],bt[10][10];
int i,j,nop,k=0,tq,sum=0;
float avg;
printf("\nEnter the Number of processes:");
scanf("%d",&nop);
printf("\n\tEnter the time quantum;");
scanf("%d",&tq);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
bursttime[i][j]=0;
bt[i][j]=0;
}
for(i=0;i<nop;i++)
{ j=0;
printf("\n\tEnter the process time for process %d;",i+1);
scanf("%d",&bursttime[i][j]);}
for(j=0;j<10;j++)
for(i=0;i<nop;i++)
{
bt[2*j][i]=k;
if((bursttime[i][j]<=tq)&&(bursttime[i][j]!=0))
14
{
k=k+bursttime[i][j];
bt[2*j+1][i]=k;
}
else if(bursttime[i][j]!=0)
{
bursttime[i][j+1]=bursttime[i][j]-tq;
k=k+tq;
bt[2*j+1][i]=k;
}
else
{
bt[2*j][i]=0;
bt[2*j+1][i]=0;
} }
for(i=0;i<nop;i++)
sum=sum+bt[0][i];
for(i=0;i<nop;i++)
for(j=1;j<10;j++)
{
if((bt[j][i]!=0)&&(bt[j+1][i]!=0)&&((j+1)%2==0))
{
sum=sum+((bt[j+1][i]-bt[j][i]));
} }
avg=(float)sum/nop;
printf("\n\n\t\tAverage waiting time:%0.2f millisecs",avg);
sum=avg=0;
for(j=0;j<nop;j++)
{
i=1;
while(bt[i][j]!=0)
i++;
sum=sum+bt[i-1][j];
}
avg=(float)sum/nop;
printf("\n\n\t\tAverage Turnaround time: %0.2f ms\n",avg);
return 0;
}
15
Output:
Enter the Number of processes: 3
Result:
Thus the Round Robin Scheduling algorithm has been successfully executed and verified.
16
Aim:
Algorthim:
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest to highest
burst time.
Step 5: Set the waiting time of the first process as ‗0‘and its turnaround time as its burst time.
Step 7: For each process in the ready queue, calculate Waiting timess(n)= waiting time (n-1) + Burst time
(n-1),Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate Average waiting time = Total waiting Time / Number of process
Coding:
#include<stdio.h>
int main()
{
int nop,i,j,t;
int bt[10],wt=0,tat=0;
float totwt=0.0,tottat=0.0;
float avgwt=0.0,avgtat=0.0;
printf("\n\tEnter the number of processes:");
scanf("%d",&nop);
for(i=1;i<=nop;i++){
printf("\n\tEnter the burst time for process %d in ms:",i);
scanf("%d",&bt[i]);
}
for(i=1;i<=nop;i++)
for(j=1;j<=nop;j++)
{
if(bt[i] < bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
}
17
for(i=0;i<nop;i++)
{
bt[0]=0;
wt=wt + bt[i];
totwt = totwt + wt;
}
printf("\n\t\tTotal waiting time: %0.2f ms",totwt);
avgwt=(float)totwt / (float)nop;
printf("\n\t\tAverage waiting time: %0.2f ms",avgwt);
for(i=1;i<=nop;i++)\
{
tat=tat + bt[i];
tottat=tottat + tat;
}
printf("\n\t\tTotal turn around time: %0.2f ms",tottat);
avgtat=(float)tottat / (float)nop;
printf("\n\t\tAverage Turn around time: %0.2f ms",avgtat);
return 0;
}
Output:
[user@linuxserver ~]$ ./a.out
Result:
Thus the SJF Scheduling algorithm has been successfully executed and verified.
18
Aim:
To write a c program to implement the scheduling algorithm first come first serve.
Algorthim:
Step4: Calculate the average waiting time and average turn around time
Coding:
#include<stdio.h>
int main()
{
int nop,wt=0,tat=0,i;
int bt[10];
float totwt=0.0,tottat=0.0;
float avgwt=0.0,avgtat=0.0;
printf("\n\tEnter the number of process:");
scanf("%d",&nop);
for(i=1;i<=nop;i++)
{
printf("\n\tEnter the burst time for the process%d in msecs:",i);
scanf("%d",&bt[i]);
}
for(i=0;i<nop;i++)
{
bt[0]=0;
wt = wt + bt[i];
totwt=totwt + wt;
}
printf("\n\t\tTotal waiting time: %0.2f ms",totwt);
avgwt=(float)totwt/(float)nop;
printf("\n\t\tAverage waiting time:%0.2f ms",avgwt);for(i=0;i<=nop;i++)
{
tat=tat + bt[i];
tottat=tottat + tat;
}
printf("\n\t\tTotal Turn around time: %0.2f ms",tottat);
avgtat=(float)tottat/(float)nop;
printf("\n\t\tAverage Turn around time: %0.2f ms",avgtat);
return 0;
}
19
Output:
[user@linuxserver ~]$ ./a.out
Result:
Thus the FCFS Scheduling algorithm has been successfully executed and verified
20
Aim:
Algorithm:
Coding:
#include<stdio.h>
#include<string.h>
int main()
{
float avgwt,avgtt;
char pname[10][10],c[10][10];
int wt[10],pt[10],tt[10],bt[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=10;
printf("\n\n Enter the no of process");
scanf("%d",&n);
printf("\n\nEnter the name and bursttime");
for(i=0;i<n;i++)
{
printf("\n\nNAME:");
scanf("%s",pname[i]);
printf("\n\n BURSTTIME");
scanf("%d",&bt[i]);
}
printf("\n\n enter the priority of the process:");
for(i=0;i<n;i++)
{
printf("\n\n priority of the process");
scanf("%d",&pt[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
t=pt[i];
pt[i]=pt[j];
pt[i]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
21
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
} }
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
sum=sum+wt[i];
sbt=sbt+wt[i+1];
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];
}
printf("\n\nGENTT CHART");
printf("\n^^^^^^^^^^^^^^\n");
printf("\n TOTAL WAITING TIME OF THE PROCESS=%d",sum);
printf("\n TOTAL TURN AROUNT TIME OF THE PROCESS=%d",sbt);
avgwt=(float)sum/n;
avgtt=(float)sbt/n;
printf("\n AVERAGE WAITING TIME=%f",avgwt);
printf("\n AVERAGE TURN AROUND TIME=%f",avgtt);
return 0;}
Output:
[user@linuxserver ~]$ ./a.out
Result:
Thus the Priority Scheduling algorithm has been successfully executed and verified.
22
Aim:
Algorithm:
Mutex initialised to 0 which allows only one process to execute at any time.
Two variables to indicate the limit of buffer.
Step 3: Wait and signal are two functions to implement the semaphore.
Step 4: The reader process, checks if any process is writing. If so it waits else it reads the content of
shared variable and then signals.
Step 4: The Writer process checks if any other process is accessing the shared variable. If not it changes
the value of shared variable and then signals.
Coding;
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
static int full,empty,mutex;
int buffer[5],in=0,out=0;
void wait(int *a);
void signal(int *b);
void producer()
{
int nextp;
printf("producer\n");
wait(&empty);
wait(&mutex);
nextp=rand()%10+1;
buffer[in]=nextp;
printf("produced item is %d\n",nextp);
in=(in+1)%5;
signal(&mutex);
signal(&full);
printf("full=%d\t empty=%d\n",full,empty);
}
void consumer()
{
int nextc;
23
printf("consumer\n");
wait(&full);
wait(&mutex);
nextc=buffer[out];
printf("consumerd item is %d\n",nextc);
out=(out+1)%5;
signal(&mutex);
signal(&empty);
printf("full=%d\t empty=%d\n",full,empty);
}
void wait(int *a)
{
while(*a<=0)
*a=*a-1;
}
void signal(int *b)
{
*b=*b+1;
}
main()
{
int c;
mutex=1;
empty=5;
full=0;
clrscr();
while(1)
{
printf("1.producer\t 2.consumer\t 3.both\t 4.Exit\n");
printf("choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
if(empty==0)
printf("producer has to wait\n");
else
{
producer();
}
break;
case 2:
if(full==0)
printf("consumer has to wait");
else
{
consumer();
}
break;
case 3:
if(!empty)
24
{
printf("producer has to wait\n");
consumer();
}
else if(!full)
{
printf("consumer has to wait\n");
producer();
}
else {
consumer();
producer();
}
break;
case 4:
exit(0);
break;
} }
getch();
return 0;
}
Output:
Result:
Thus the implementation of producer –consumer problem using Semaphore has been successfully
executed and verified.
25
Aim:
Algorithm:
Step 2: Create the shared memory for parent process using shmget() system call.
Step 3: Now allow the parent process to write in shared memory using shmptr pointer which is return
type.
Step 4: Now across and attach the same shared memory to the child process.
Step 5: The data in the shared memory is read by the child process using shmptr pointer.
Coding:
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
int child,shmid,i;
char * shmptr;
child=fork();
if(!child)
shmid=shmget(2041,32,0666 | IPC-CREAT);
shmptr=shmat(shmid,0,0);
for(i=0;i<10;i++)
shmptr[i]=’a’+i;
putchar(shmptr[i]);
} }
26
printf(“\n\n%s”,shmptr);
wait(NULL);
else {
shmid=shmget(2041,32,0666);
shmptr=shmat(shmid,0,0);
for(i=0;i<10;i++)
putchar(shmptr[i]);
shmdt(NULL);
return 0;
Output:
PARENT WRITING:
ghghg
CHILD IS READING:
ghghg
Result:
Thus scheme to implement inter process communication using shared memory has been successfully
executed and verified
27
Aim:
Algorithm:
Step 3: if a process request for resources, the avoidance algorithm checks before the allocation of
resources about the state of system.
Step 4: If the state is safe, the system allocate the resources to the requesting process otherwise
(unsafe) do not allocate the resources.
Step 6: Check the resources in all the processes with available resources and find safe or unsafe.
Coding:
#include<stdio.h>
void main()
int max[20],all[20],need[20],seq[20],avail,work,i,j=0,d,m,n,e;
clrscr();
printf("Enter no of process");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("max of p %d is",i);
scanf("%d",&max[i]);
printf("\nAllocation of p %d is",i);
scanf("%d",&all[i]);
need[i]=max[i]-all[i];
printf("\nneed of p %d is : %d",i,need[i]);
28
scanf("%d",&avail);
for(i=0;i<n;i++)
printf("%d %d %d",max[i],all[i],need[i]);
m=d=n;
e=m+1;
while(m>0)
if(d==e)
m=0 ;
else
d=e;
e=0;
for(i=0;i<n;i++)
if((need[i]>0)&&(need[i]<=avail))
need[i]=0;
work=all[i]+avail;
avail=work;
seq[j]=i;
m--;
j++;
else
e++;
29
if(d==e)
else
for(i=0;i<n;i++)
printf("p %d\t",seq[i]);
Output:
Enter the no of process 3
Allocation of p 0 is 1
Need of p0 is : 1
Max of p1 is 5
Allocation of p1 is2
Need of p1 is : 3max of p 2 is 4
Allocation of p2 is 2
Need of p2 is : 2
2 1 15 2 3 4 2 2
Safe sequence
Result:
Thus the banker’s algorithm has been created to prevent the deadlock.
30
Aim:
Algorithm:
Step 2: initialize the values for the number of process and resource.
Step 3: if a process request for resources, the detection algorithm checks before the allocation of
resources about the state of system.
Step 4: If the state is safe, the system allocate the resources to the requesting process otherwise
(unsafe) do not allocate the resources.
Step 6: Check the resources in all the processes with available resources and find safe or unsafe.
Coding:
#include <stdio.h>
#include <conio.h>
void main()
{
int 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;
clrscr();
31
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++) {
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
} }
32
for(j=1;j<=tp;j++)
{ found=0;
for(i=1;i<k;i++)
{ if(j==m[i])
found=1;
} if(found==0)
printf("%d\t",j);
}
getch();
}
Output:
Enter total no. of processes : 4
Enter total no. of resources : 5
Enter claim (Max. Need) matrix :
01001
00101
00001
10101
Enter allocation matrix :
10110
11000
00010
00000
Enter resource vector (Total resources) :
21121
Enter availability vector (available resources) :
00001
deadlock causing processes are : 2 3
Result:
Thus the C program to detect the deadlock has been created and executed.
33
WORST FIT
AIM:
To write a C Program for the Implementation of Best fit, First fit, Worst fit in Memory Management
Scheme.
Algorithm:
Step 1 : start the program
Step 2 : Initialze Structure with pointer node to create a memory block.
Step 3 : In Best fit, the memory will be allocated to the exact block size of memory to the processes
created.
Step 4 : In First fit, the memory will be allocated where the block size is received first by the processes.
Step 5 : In worst fit, the memory will be allocated to large block of memory for the process initiated.
Step 6 : stop the program.
Coding:
#include<stdio.h>
struct node;
typedef struct node *ptrtonode;
typedef ptrtonode memory;
typedef struct node nodetype;
int pro_id,pro_size;
memory create();
struct node
{
int id;
int start;
int hole;
int end;
ptrtonode next;
};
memory create()
{
memory mem;
mem=(ptrtonode)malloc(sizeof(nodetype));
mem->next=NULL;
return mem;
}
void insert(memory mem,int s,int e)
{
ptrtonode tmp;
tmp=(ptrtonode)malloc(sizeof(nodetype));
tmp->next=mem->next;
tmp->start=s;
tmp->end=e;
tmp->hole=e-s;
tmp->id=0;
mem->next=tmp;
}
void firstfit(memory mem)
34
{
int diff;
ptrtonode tmp;
tmp=mem->next;
while(tmp!=NULL)
{
if(tmp->hole>=pro_size&&tmp->id==0)
{
tmp->id=pro_id;
diff=tmp->hole-pro_size;
tmp->hole=0;
tmp->end=tmp->start+pro_size;
if(tmp->next!=NULL&&tmp->next->hole==0)
{
tmp=tmp->next;
tmp->hole+=diff;
tmp->start-=diff;
}
else
{
ptrtonode t;
t=(ptrtonode)malloc(sizeof(nodetype));
t->next=tmp->next;
tmp->next=t;
t->id=0;
t->hole=diff;
t->start=tmp->end;
t->end=t->start+t->hole;
}
break;
}
Else
tmp=tmp->next;
}
}
void best_fit(memory mem)
{
int diff,count=0,c,min;
ptrtonode tmp;
tmp=mem->next;
min=3200;
while(tmp!=NULL)
{
count++;
if(tmp->hole>=pro_size&&tmp->id==0)
if(tmp->hole<min)
{
min=tmp->hole;
c=count;
}
tmp=tmp->next;
35
}
tmp=mem->next;
count=0;
while(tmp!=NULL)
{
count++;
if(c==count)
{
tmp->id=pro_id;
diff=tmp->hole-pro_size;
tmp->hole=0;
tmp->end=tmp->start+pro_size;
if(tmp->next!=NULL&&tmp->next->hole==0)
{
tmp=tmp->next;
tmp->hole+=diff;
tmp->start-=diff;
}
else
{
prtonode t;
t=(ptrtonode)malloc(sizeof(nodetype));
t->next=tmp->next;
tmp->nex=t;
t->id=0;
t->hole=diff;
t->start=tmp->end;
t->end=t->start+t->hole;
}
Break;
}
tmp=tmp->next;
}
}
void worst_fit(memory mem)
{
int diff,count=0,c,max;
ptrtonode tmp;
tmp=mem->next;
max=-1;
while(tmp!=NULL)
{
count++;
if(tmp->hole>=pro_size&&tmp->id==0)
if(tmp->hole>max)
{
max=tmp->hole;
c=count;
}
tmp=tmp->next;
{
36
count++;;
if(c==count)
{
tmp->id=pro_id;
diff=tmp->hole-pro_size;
tmp->hole=0;
tmp->end=tmp->start+pro_size;
if(tmp->next!=NULL&&temp->next->hole==0)
{
tmp=tmp->next;
tmp->hole+=diff;
tmp->start-=diff;
}
Else
{
Ptrtonode t;
t=(ptrtonode)malloc(sizeof(nodetype));
t->next=tmp->next;
tmp->next=t;
t->hole=diff;
t->id=0;
t->start=tmp->end;
t->end=t->start+t->hole;
}
Break;
}
tmp=tmp->next;
}
}
void allocate(memory mean)
{
Int opt;
printf(“\nEnter the process ID:”);
scanf(“%d”,&pro_id);
printf(“\n Enter the process size:”);
scanf(“%d”,&pro_size);
printf(“\nMENU”);
printf(“\n1.FIRST FIT”);
printf(“\n2.BEST FIT”);
printf(“\n3.WORST FIT”);
printf(“\nEnter your option”);
scanf(“%d”,opt);
switch(opt)
{
case 1:
first_fit(mem);
break;
case 2:
best_fit(mem);
break;
}
37
}
void deallocate(memory mem)
{
prtonode tmp;
int diff;
printf(“\nEnter the process ID:”);
scanf(“%d”,&pro_id);
tmp=mem;
while(tmp->next!=NULL)
{
if(tmp->next->id==pro_id)
{
Ptrtonode
t=tmp->next;
t->id=0;
t->next->start=t->start;
t->next->hole=t->next->end-t->next->start;
tmp->next=t->next;
free(t);
}
else
{
t->id=0;
t->hole=t->end-t->start;
}
break;
}
tmp=tmp->next;
}
}
void display(memory mem)
{
ptrtonode tmp;
tmp=mem->next;
while(tmp!=NULL)
{
tmp=tmp->next;
}
}
void del(memory mem)
{
ptrtonode tmp,tmp,t;
tmp=mem;
while(tmp!=NULL)
{
t=tmp->next;
free(t);
tmp=t;
}
free(mem);
}
38
int main()
{
int i,opt,mem_end=8000,mem_start;
memory mem;
mem=create();
insert(mem,0,10000);
for(;;)
{
printf(“\nMENU”);
printf(“\n1.Allocate memory”);
printf(“\n2.Deallocate Memory”);
printf(“\n3.Display”);
printf(“\n4.Exit”);
printf(“\nEnter your Option”);
scanf(“%d”,&opt);
switch(opt)
{
case 1:
allocate(mem);
break;
case 2:
deallocate(mem);
break;
case 3:
display(mem);
break;
case 4:
del(mem);
exit(0);
}
}
Return 0;
OUTPUT:
[examuser35@localhost Jebastin]$ cc fit.c
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
MENU
39
1.FIRST FIT
2.BEST FIT
3.WORST FIT
Enter your Option1
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
Enter your option 3
id=1 start= 0 hole= 0 end=1500
id=0 start= 1500 hole= 8500 end=10000
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
Enter your option 1
Enter the Process ID:2
Enter the Process size:2500
MENU
1.FIRST FIT
2.BEST FIT
3.WORST FIT
Enter your Option 3
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
Enter your option 3
id=1 start= 0 hole= 0 end=1500
id=2 start= 1500 hole= 0 end=4000
id=0 start= 4000 hole= 6000 end=10000
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
Enter your option 2
MENU
1.Allocate Memory
2.Deallocate Memory
40
3.Display
4.Exit
Enter your option 3
id=0 start= 0 hole= 1500 end=1500
id=2 start= 1500 hole= 0 end=4000
id=0 start= 4000 hole= 6000 end=10000
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
Enter your option 1
Enter the Process ID:4
Enter the Process size:1000
MENU
1.FIRST FIT
2.BEST FIT
3.WORST FIT
Enter your Option 2
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
Enter your option 3
id=4 start= 0 hole= 0 end=1000
id=2 start= 1000 hole= 500 end=4000
id=0 start= 4000 hole= 6000 end=10000
MENU
1.Allocate Memory
2.Deallocate Memory
3.Display
4.Exit
Enter your option 4
Result:
Thus the program for best fit, first fit, worst fit has been successfully executed and verified.
41
Aim:
Algorithm:
Coding:
#include <stdio.h>
#include <conio.h>
struct pstruct
int fno;
int pbit;
}ptable[10];
int pmsize,lmsize,psize,frame,page,ftable[20],frameno;
void info()
scanf("%d",&pmsize);
scanf("%d",&lmsize);
scanf("%d",&psize);
42
void assign()
int i;
for (i=0;i<page;i++)
ptable[i].fno = -1;
ptable[i].pbit= -1;
for(i=0; i<frame;i++)
ftable[i] = 32555;
for (i=0;i<page;i++)
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
ptable[i].fno = frameno;
ptable[i].pbit = 1;
getch();
// clrscr();
printf("\n\nPAGE TABLE\n\n");
43
for (i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
void cphyaddr()
int laddr,paddr,disp,phyaddr,baddr;
getch();
clrscr();
scanf("%d",&baddr);
scanf("%d",&laddr);
if(ptable[paddr].pbit == 1 )
void main()
clrscr();
info();
assign();
44
cphyaddr();
getch();
Output:
PAGE TABLE
0 5 1
1 6 1
2 7 1
3 2 1
FRAME TABLE
FrameAddress PageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
45
7 2
Result:
Thus the Program to implement paging for memory management has been executed and verified
successfully.
46
Aim:
Algorithm:
Step 4. Check the need of replacement from old page to new page in memory
Coding:
#include<stdio.h>
int main()
int I,j,n,a[50],frame[10],no,k,avail,count=0;
scanf(“%d”,&n);
for(i=1;i<=n;i++)
scanf(“%d”,&a[i]);
scanf(“%d”,&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
47
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”);
return 0;
Output:
2 2 -1 -1
3 2 3 -1
48
4 2 3 4
5 5 3 4
1 5 1 4
7 5 1 7
2 2 1 7
3 2 3 7
9 2 3 9
10 10 3 9
Page Fault Is 10
Result:
Thus the c program to implement FIFO page replacement algorithm has been successfully executed and
completed.
49
Aim:
Algorithm :
Coding:
#include<stdio.h>
#include<conio.h>
int n,ref[100],fs,frame[100],count=0;
void input();
void show();
void cal();
void main()
input();
cal();
show();
getch();
void input()
50
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&ref[i]);
scanf("%d",&fs);
void cal()
int i,j,k=0,c1,c2[100],r,temp[100],t;
frame[k]=ref[k];
count++;
k++;
for(i=1;i<n;i++)
c1=0;
for(j=0;j<fs;j++)
if(ref[i]!=frame[j])
c1++;
if(c1==fs)
count++;
if(k<fs)
frame[k]=ref[i];
51
k++;
else
for(r=0;r<fs;r++)
c2[r]=0;
for(j=i-1;j<n;j--)
if(frame[r]!=ref[j])
c2[r]++;
else
break;
for(r=0;r<fs;r++)
temp[r]=c2[r];
for(r=0;r<fs;r++)
for(j=r;j<fs;j++)
if(temp[r]<temp[j])
t=temp[r];
temp[r]=temp[j];
temp[j]=t;
52
for(r=0;r<fs;r++)
if(c2[r]==temp[0])
frame[r]=ref[i];
} } } } }
void show()
Output:
Press 1 to continue: 2
Page Faults = 9
Result:
Thus the C program to implement LRU page replacement algorithm has been successfully executed and
completed.
53
Aim:
Algorithm:
Step 3. Check for Page Hit. If yes then Goto step 7 else Goto step 4
Step 5. find the least frequently used page from the pages in FRAME.
Coding:
#include<stdio.h>
int main()
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
scanf("%d",&f);
scanf("%d",&p);
for(i=0;i<f;i++)
frame[i]=-1;
for(i=0;i<50;i++)
54
count[i]=0;
for(i=0;i<p;i++)
{ scanf("%d",&pages[i]);
printf("\n");
for(i=0;i<p;i++)
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
if(frame[j]==-1 || frame[j]==pages[i])
if(frame[j]!=-1)
{ hit++;
flag=0;
frame[j]=pages[i];
break;
if(count[least]>count[frame[j]])
{ least=frame[j];
} }
if(flag)
{ minTime=50;
55
for(j=0;j<f;j++)
temp=j;
minTime=time[frame[j]];
} }
count[frame[temp]]=0;
frame[temp]=pages[i];
for(j=0;j<f;j++)
printf("%d ",frame[j]);
printf("\n");
return 0;
56
Output:
Enter no of frames : 3
Enter no of pages : 10
Enter page no :2 3 4 5 1 2 1 7 8 2
2 -1 -1
2 3 -1
234
534
514
512
512
712
718
Result:
Thus the C program to implement LFU page replacement algorithm has been successfully executed and
verified.
57
Aim:
Algorithm:
Step 1: Start
Step 7: End
Coding:
#include<stdio.h>
#include<conio.h>
main()
int master,s[20];
char f[20][20][20];
char d[20][20];
int i,j;
clrscr();
scanf("%d",&master);
for(i=0;i<master;i++)
scanf("%s",&d[i]);
for(i=0;i<master;i++)
58
scanf("%d",&s[i]);
for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" directory\tsize\tfilenames\n");
printf("*************************************************\n");
for(i=0;i<master;i++)
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
printf("\n");
printf("\t\n");
getch();
59
Output:
***************************************
a 2 as
bs
b 3 cs
ad
fg
c 1 as
Result:
Thus the File organization technique using single level directory has been successfully executed and
completed
60
Algorithm:
Step 1: Start
Initialize integer variables x, y, ftype, lx, rx, nc, level; struct tree_element
*link[5];}typedef structure tree_element node;
Coding:
#include<stdio.h>
#include<conio.h>
struct st
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
int i,j,k,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",&dir[i].dname);
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
scanf("%s",&dir[i].fname[j][k]);
} } }
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
} printf("\n");
62
getch();
Output:
****************************************
a 0
b 2 b 2 as sd
c 1 af
n 2 aq 1 file1
as 1 file2
Result:
Thus the File organization technique using two level directory has been successfully executed and
completed
63
Aim:
Algorithm:
Step 1: Start
Step 3: start main and declare variables Node *root Root = NULL
Step 5:create a directory tree structure If check *root==NULL Display dir/file name.
Step 7: display the directory tree in graphical mood Display nood *root
Step 8: foe i=0 to i<root->nc Line of root->x, root->y, root->link[i]->x, root- >link[i]-y
Coding:
#include<stdio.h>
#include<graphics.h>
struct tree_element
char name[20];
int x,y,ftype,lx,rx,nc,level;
};
node;
void main()
{
64
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
printf("\n");
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
close graph();
int i,gap;
if(*root==NULL)
(*root)=(node *)malloc(sizeof(node));
fflush(stdin);
gets((*root)->name);
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++)
65
(*root)->link[i]=NULL;
if((*root)->ftype==1)
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
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
66
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 the File organization technique using hierarchical directory has been successfully executed and
completed
67
Aim:
Algorithm:
Step 1: Start
Step 3: Start main and declare variables Node *root Root = NULL
Step 4: Draw link lines for shared files Search root find the first and second
Step 5: if check root !-NULL If check string comparing root ->name, s==0
Step 6: creates a directory tree structure If check *root==NULL Display directory name or file
name
Step 7: display the directory tree in graphical mode Display node *root
Coding:
#include<stdio.h>
#include<graphics.h>
struct tree_element
char name[20];
int x,y,ftype,lx,rx,nc,level;
68
};
void main()
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
display(root);
getch();
closegraph();
int i,gap;
if(*root==NULL)
(*root)=(node*)malloc(sizeof(node));
fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1)
(*root)->ftype=1;
else
(*root)->ftype=2;
(*root)->level=lev;
69
(*root)->y=50+lev*50;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
if(lev==0||lev==1)
if((*root)->level==0)
else
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
else (*root)->nc=0;
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
else (*root)->nc=0;
display(node *root)
int i;
70
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);
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:
71
Result:
Thus the File organization technique using DAG level directory has been successfully executed and
completed.
72
Aim:
Algorithm:
Coding:
#include<stdio.h>
#include<conio.h>
main()
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
scanf("%d",&n);
73
for(i=0;i<n;i++)
scanf("%d",&b[i]);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
scanf("%d",&x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
74
Output:
1 1 3
2 2 2
Result:
Thus the C Program for sequential file allocation has been executed and verified successfully
75
Aim:
Algorithm:
Coding:
#include<stdio.h>
#include<conio.h>
main()
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&sb[i],&s[i]);
scanf("%d",&m[i]);
76
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
scanf("%d",&x);
i=x-1;
printf("Index is:%d",sb[i]);
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
77
Output:
2 5 4 6 7 2 6 4 7
1 2 10
2 3 5
Result:
Thus the C Program for Indexed file allocation strategy has been executed and verified
Successfully.
78
Aim:
Algorithm:
Coding:
#include<stdio.h>
#include<conio.h>
struct file
char fname[10];
int start,size,block[10];
}f[10];
main()
79
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",&f[i].fname);
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
scanf("%d",&f[i].size);
for(j=1;j<=f[i].size;j++)
scanf("%d",&f[i].block[j]);
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
getch();
80
Output:
12
15
45
32
25
file1 20 6 4--->12--->15--->45--->32--->25
file2 12 5 6--->5--->4--->3--->2
Result:
Thus the C Program for linked allocation strategy has been executed and verified successfully.
81