0% found this document useful (0 votes)
48 views

OS LAB Program's

The document discusses various CPU scheduling algorithms like Round Robin, Shortest Job First, First Come First Serve and their implementation in C programs. It also discusses file allocation strategies and file organization techniques.

Uploaded by

divakaran489
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

OS LAB Program's

The document discusses various CPU scheduling algorithms like Round Robin, Shortest Job First, First Come First Serve and their implementation in C programs. It also discusses file allocation strategies and file organization techniques.

Uploaded by

divakaran489
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 108

INDEX

EX.NO. PAGE
DATE NAME OF THEEXPERIMENTS
NO.

1 Basics of UNIX Commands.


2 Shell Programming.
Implement the following CPU scheduling algorithms
a. Round Robin
b. SJF
3 c. FCFS
d. Priority
Implement all file allocation strategies
a. Sequential
b. Indexed
4 c. Linked
5 Implement SEMAPHORE
Implement given File Organization Techniques
a. Single level directory
b. Two level
6 c. Hierarchical
d. DAG
Implement Bankers Algorithm for DeadLock
7 Avoidance.
Implement Bankers Algorithm for DeadLock
8
Detection.
Implement the following page replacement algorithms
a. FIFO
b. LRU
9 c. LFU
10 Implement Shared Memory and IPC.
Implement Paging Technique of Memory
11 Management.
Implement Threading & Synchronization
12 Applications.
Ex. No. 1
DATE:
1(a). PROCESS SYSTEM CALLS

AIM:

To write c program to implement the Process system calls.

ALGORITHM:

1. Start the program.


2. Declare the pid and get the pid by using the getpid() method.
3. Create a child process by calling the fork() system call
4. Check if(pid==0) then print the child process id and then print the
parent process value. Otherwise print
5. Stop the program
PROGRAM:

#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("I am Child Process Id%d\n",getpid());
printf("I am Child Parent Process Id%d\n",getpid());
}
if(pid>0)
{
printf("I am Parent Process Id%d\n",getpid());
printf("I am Parent,Parent (Grand Parent)Process%d\n",getpid());
}
}
OUTPUT:

I am Child Process Id10276


I am Child Parent Process Id10276
I am Parent Process Id10275
I am Parent,Parent (Grand Parent)Process10275
1(b). I/O SYSTEM CALLS

AIM:

To write a ‘c’ program for I/O system calls.

ALGORITHM:

Step 1: Start the program.


Step 2: Open a file for O_WRONLY for Write, O_RDONLY for Read, O_CREAT
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
Step 6: Stop the program
PROGRAM

#include<stdio.h>
#include<fcntl.h>
main()
{
int sz;
int fd=open("foo.txt",O_WRONLY|O_CREAT|O_TRUNC|O_RDONLY,0644);
char*c=(char*)calloc(100,sizeof (char));
if(fd<0)
{
perror("r1");
exit(1);
}
sz=write(fd,"hello\n",strlen("hello\n"));
printf("called write(%d,\"hello\\n\",%d","it returned%d\n",fd,strlen("hello\n"),
sz);
printf("called read(%d,c,10.returned that","%d bytes were read.\n",fd,sz);
close(fd);
}
OUTPUT:

called write(134514266,"hello\n",3,6.it returned 6)


called read(134514310,c,10.returned that 6 bytes were read)
RESULT

Thus the program has been executed successfully.


Ex No: 2
DATE:
2. IMPLEMENT SHELL PROGRAMMING

AIM
To write c program to implement the Shell Programming concept.

ALGORITHM

Step1: Start
Step 2: Declare variables i, a,b , show
Step 3: Initialize the variables, a=0, b=1, and show =0
Step 4: Enter the number of terms of Fibonacci series to be printed
Step 5: Print First two terms of series
Step 6: Use 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: End
PROGRAM :

echo "How many number of terms to be generated ?"


read n
function fib
{
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
}
r=`fib $n`
echo "$r"
OUTPUT:
RESULT

Thus the program has been executed successfully.


Ex. No. 3
DATE:
CPU SCHEDULING ALGORITHMS
A. ROUND ROBIN SCHEDULING

AIM:
Write a C program to implement the Round Robin Scheduling.

ALGORITHM:

Step 1: Start the process


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: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time
of process(n-1 ) + the time difference in getting the CPU from process(n-1)
(b) Turnaround time for process(n) = waiting time of process(n) + burst
time of process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
(g) Average waiting time = Total waiting Time / Number of process
(h) Average Turnaround time = Total Turnaround Time / Number of
process
Step 8: Stop the process.
PROGRAM:

#include<stdio.h>
int main()
{
int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
printf("\t\t ROUND ROBIN SCHEDULING \n");
printf("Enter the number of Processors:");
scanf("%d",&n);
n1=n;
printf("\n Enter the Timeslice:");
scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("\n Enter the process ID %d:",i);
scanf("%d",&pid[i]);
printf("\n Enter the Burst Time for the process:");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0;
n--;
}
}
else
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");
printf("\n\n Process \t Process ID \t BurstTime \t Waiting Time \t TurnaroundTime
\n ");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}
printf("\n The average Waiting Time=%4.2f",awt);
printf("\n The average Turn around Time=%4.2f",atat);
return 0;
}
OUTPUT:
RESULT

Thus the program has been executed successfully.


B. SHORTEST JOB FIRST

AIM:
Write a C program to implement SJF Scheduling.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
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 6: For each process in the ready queue,
Calculate
(c) Waiting time for process(n)= waiting time of process (n-1) + Burst time
of process(n-1)
(d) Turn around time for Process(n)= waiting time of Process(n)+ Burst time
for process(n)
Step 6: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process.
PROGRAM:

#include<stdio.h>
void main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
printf("SHORTEST JOB FIRST ");
printf("\n Enter number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of Process %d:",i);
scanf("\n %d",&t[i]);
}
for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(t[i]>t[k])
{
int temp;
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
}
printf("\n\n\t SHORTEST JOB FIRST SCHEDULING ALGORITHM");
printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \tTURNAROUND
TIME \n\n");
wt[0]=0;
for(i=0;i<n;i++)
{
sum=0;
for(k=0;k<i;k++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d \t\t%5d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);
}
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n AVERAGE WAITING TIME %4.2f",awt);
printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);
return 0;
}
}
OUTPUT:
RESULT

Thus the program has been executed successfully.


C. FIRST COME FIRST SERVE

AIM:
Write a C program to implement FCFS Scheduling.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU
burst time.
Step 4: Set the waiting of the first process as ‘0’ and its burst time as its turn
around time.
Step 5: For each process in the Ready Q calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time
of process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time
for process(n)
Step 6: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process.
PROGRAM:

#include<stdio.h>
int main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
printf("FIRST COME FIRST SERVE");
printf("\n Enter number of processors:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the process %d:",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("\t1 \t\t 0 \t\t %d \n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n\t %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n Average Waiting Time: %4.2f",awt);
printf("\n Average Turnaround Time: %4.2f",atat);
return 0;
}
OUTPUT:
RESULT

Thus the program has been executed successfully.


D. PRIORITY SCHEDULING

AIM:
Write a C program to implement Priority Scheduling.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU
burst time.
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‘0’ and its burst time as its turn
around time.
Step 6: For each process in the Ready Q calculate
(e) Waiting time for process(n)= waiting time of process (n-1) + Burst time
of process(n-1)
(f) Turn around time for Process(n)= waiting time of Process(n)+ Burst time
for process(n)
Step 7: Calculate
(e) Average waiting time = Total waiting Time / Number of process
(f) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process.
PROGRAM:

#include <stdio.h>
int main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt,atat;
printf("\n-----------PRIORITY SCHEDULING-------------\n");
printf("Enter the No of Process: ");
scanf("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
// Sorting start
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i] > pr[j] )
{
t = pr[i];
pr[i] = pr[j];
pr[j] = t;
t = bt[i];
bt[i] = bt[j];
bt[j] = t;
t = pid[i];
pid[i] = pid[j];
pid[j] = t;
}
}
// Sorting finished
tat[0] = bt[0];
wt[0] = 0;
for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i];
twt = twt + wt[i];
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time:%f\n",awt,atat);
return 0;
}
OUTPUT:
RESULT

Thus the program has been executed successfully.


Ex. No. 4
DATE:
IMPLEMENT FILE ALLOCATION STRATEGIES

A.SEQUENTIAL

AIM:

Write a C Program to implement Sequential File Allocation method.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which
Is big enough is encountered. It allocates that memory block for the
requesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which
Can be allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and
allocates it to the process.
Step 7: Analyses all the three memory management techniques and display the
Best algorithm which utilizes the memory resources effectively and
efficiently.
Step 8: Stop the program.
PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
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]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%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();
}
OUTPUT:

Enter no.of files: 2

Enter no. of blocks occupied by file1 4


Enter the starting block of file1 2
Enter no. of blocks occupied by file2 10
Enter the starting block of file2 5
Filename Start block length
1 2 4
2 5 10

Enter file name: rajesh

File name is: 12803 length is:0 blocks occupied:


RESULT

Thus the program has been executed successfully.


B.INDEXED

AIM:
Write a C Program to implement Indexed File Allocation method.

ALGORITHM:

Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.
PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
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]);
}printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
getch();
}
OUTPUT:

Enter no. of files: 2


Enter starting block and size of file1: 2 5

Enter blocks occupied by file1:10


enter blocks of file1:3 2 5 4 6 7 2 6 4 7
Enter starting block and size of file2: 3 4
Enter blocks occupied by file2:5
enter blocks of file2: 2 3 4 5
6 File index length
1 2 10
2 3 5
Enter file name: venkat
file name is:12803

Index is:0 Block occupied are:


RESULT

Thus the program has been executed successfully.


C.LINKED FILE ALLOCATION

AIM:
Write a C Program to implement Linked File Allocation method.

ALGORITHM:

Step 1: Create a queue to hold all pages in memory


Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack
Step 6: Stop the allocation.
PROGRAM:

#include<stdio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
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");
}
return 0;
}
OUTPUT:
RESULT

Thus the program has been executed successfully.


Ex. No. 5
DATE:

5. PRODUCER CONSUMER PROBLEM USING SEMAPHORES

AIM:

To write a C-program to implement the producer – consumer problem using


semaphores.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the required variables.
Step 3: Initialize the buffer size and get maximum item you want to produce.
Step 4: Get the option, which you want to do either producer, consumer or exit
from the operation.
Step 5: If you select the producer, check the buffer size if it is full the producer
should not produce the item or otherwise produce the item and increase the value
buffer size.
Step 6: If you select the consumer, check the buffer size if it is empty the consumer
should not consume the item or otherwise consume the item and decrease the value
of buffer size.
Step 7: If you select exit come out of the program.
Step 8: Stop the program.
PROGRAM:

#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume,
choice=0; in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf("\n 1. Produce \t 2. Consume \t3. Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");
else
{
printf("\nEnter the value:" );
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\n The consumed value is %d", consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT:
RESULT

Thus the program has been executed successfully.


Ex. No. 6
DATE:
6. IMPLEMENT ALL FFILE ORGANIZATION TECHNIQUES
A. SINGLE LEVEL DIRECTORY

AIM:

To write program to stimulate single level directory file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the directory.

Step 3: get the number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of filled circle

Step 6: Every file is connected with the given directory

Step 7: Display the connected gragh along with name using graphics

Step 8: Stop the program.


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int
gd=DETECT,gm,count,i,j,mid,cir_x; char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice();
setbkcolor(GREEN);
puts("Enter no of files do u have?");
scanf("%d",&count);
for(i=0;i<count;i++) {
cleardevice();
setbkcolor(GREEN);
printf("Enter file %d
name",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify
(1,1);
outtextxy(320,125,"Root Directory");
setcolor(BLUE);
for(j=0;j<=i;j++,cir_x+=mid ){
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[j]); }
getch();
}
}
OUTPUT:
RESULT

Thus the program has been executed successfully.


B. TWO LEVEL DIRECTORY

AIM:

To write a program to stumilate two level directory file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the directory.

Step 3: get the number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of filled circle

Step 6: Every file is connected with the given directory

Step 7: Display the connected gragh along with name using graphics

Step 8: Stop the program.


PROGRAM:

#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct
tree_element *link[5];
};
typedef struct
tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1)
(*root)->ftype=1;
else
(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)-lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("How manyusers");
else
printf("how many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else (*root)->nc=0;
if((*root)->nc==0)
gap=rxlx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc; i++)
create(&((*root)->link[i]),lev+1,(*root)-
>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else (*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}}
OUTPUT:
RESULT:

Thus the program has been executed and verified successfully.


C. HIERARCHICAL

AIM:

To write a program to stimulate hierarchical level file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the directories.

Step 3: get the number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of fill circle

Step 6: Every file is connected with respective directory

Step 7: Display the connected graph along with name in hierarchical way using
graphics .

Step 8: Stop the program.


PROGRAM:

#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct
tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("Enter name of dir/file(under%s) :",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else gap=(rxlx)/(*root)- >nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)-
>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else (*root)- >nc=0;
}}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root- >name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}
OUTPUT
RESULT

Thus the program has been executed successfully.


D. DAG

AIM:

To write a program to stimulate Direct Acyclic Grph level file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the directories.

Step 3: get the number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of fill circle

Step 6: Every file is connected with respective directory

Step 7: Display the connected graph along with name using graphics

Step 8: Stop the program.


PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element*link[5];
};
typedef struct
tree_element node;
typedef struct
{
char from[20];
char to[20];
}link;
Link L[10];
Int nofl;
node * root;
void main()
{
int gd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("how many links");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");
fflush(std in);
gets(L[i].to); }}
draw_link_lines() {
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++) {
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1); }}
search(node *root,char *s,int *x,int *y) {
int i;
if(root!=NULL ){
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return; }
else {
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}}}
create(node **root,int lev,char * dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL ){
(*root)=(node*)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for dir/2 for file:");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("no of sub directories /files (for %s):",(*root)->name); scanf("%d",&(*root)-
>nc);
if((*root)->nc==0) gap=rxlx;
else
gap=(rxlx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create( & ( (*root)->link[i] ),lev+1,(*root)-
>name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}}
OUTPUT
RESULT:

Thus the program has been executed and verified successfully.


Ex. No.7
DATE:

7. BANKER S ALGORITHM FOR DEADLOCK AVOIDANCE

AIM:

To write a C program to implement bankers algorithm for dead lock


avoidance.

ALGORITHM:

STEP 1: Start the Program.


STEP 2: Obtain the required data through char and int datatypes.
STEP 3: Enter the filename, index block.
STEP 4: Print the file name index loop.
STEP 5: File is allocated to the unused index blocks.
STEP 6: This is allocated to the unused linked allocation.
STEP 7: Stop the execution.
PROGRAM:

#include<stdio.h>
struct process
{
int allocation[3];
int max[3];
int need[3];
int finish;
}p[10];
int main()
{
int n,i,I,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf("\nEnter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %dth process allocated resources:",i);
scanf("%d%d%d",&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);
printf("\nEnter the %dth process maximum resources:",i);
scanf("%d%d%d",&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[1]=p[i].max[1]-p[i].allocation[1];
p[i].need[2]=p[i].max[2]-p[i].allocation[2];
}
printf("\nEnter the available vector:");
scanf("%d%d%d",&avail[0],&avail[1],&avail[2]);
for(i=0;i<3;i++)
work[i]=avail[i];
while(count!=n)
{
count=0;
for(i=0;i<n;i++)
{
flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j];
p[i].finish=1;
sequence[k++]=i;
flag=0;
}
if(flag==1)
count++;
}
}
count=0;
for(i=0;i<n;i++)
if(p[i].finish==1)
count++;
printf("\n The safe sequence is:\t");
if(count++==n)
for(i=0;i<k;i++)
printf("%d\n",sequence[i]);
else
printf("SYSTEM IS NOT IN A SAFE STATE \n\n");
return 0;
}
OUTPUT:
RESULT:

Thus the program has been executed and verified successfully.


Ex. No. 8
DATE:
8. AN ALGORITHM FOR DEADLOCK DETECTION

AIM:

To write a C program to implement Deadlock Detection algorithm.

ALGORITHM:

STEP 1: Start the Program.


STEP 2: Obtain the required data through char and in data types.
STEP 3: Enter the filename, index block.
STEP 4: Print the file name index loop.
STEP 5: File is allocated to the unused index blocks.
STEP 6: This is allocated to the unused linked allocation.
STEP 7: Stop the execution.
PROGRAM:

#include<stdio.h>
int main()
{
int found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
printf("enter total no of processes: \n");
scanf("%d",&tp);
printf("enter clain matrix: \n");
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&c[i][j]);
}
}
printf("enter allocation matrix: \n");
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&p[i][j]);
}
}
printf("enter resource vector: \n");
for(i=0;i<5;i++)
{
scanf("%d",&r[i]);
}
printf("enter availability vector: \n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=0;i<4;i++)
{
sum=0;
for(j=0;j<5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=0;i<4;i++)
{
for(l=1;l<k;l++)
{
if(i!=m[l])
{
flag=1;
for(j=0;j<5;j++)
{
if(c[i][j]>temp[j])
{
flag=0;
break;
}
}
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=0;j<5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are: \n");
for(j=0;j<tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
return 0;
}
}
OUTPUT:
RESULT:

Thus the program has been executed and verified successfully.


Ex. No. 9
DATE:

9. IMPLEMENT ALL PAGE REPLACEMENT ALGORITHMS

A.)FIFO

AIM:

To implement page replacement algorithms FIFO (First In First Out).

ALGORITHM:

STEP 1: Create a queue to hold all pages in memory.


STEP 2: When the page is required replace the page at the head of the queue.
STEP 3: Now the new page is inserted at the tail of the queue.
PROGRAM:

#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int main()
{
printf("\n \t\t\t FIFO PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames....");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t No.of pages faults...%d",pf);
return 0;
}
OUTPUT:
RESULT:

Thus the program has been executed and verified successfully.


B LRU PAGE REPLACEMENT ALGORITHM
AIM:
To implement page replacement algorithm LRU (Least Recently Used).
ALGORITHM:
STEP 1: Create a queue to hold all pages in memory.
STEP 2: When the page is required replace the page at the head of the queue.
STEP 3: Now the new page is inserted at the tail of the queue.
STEP 4: Create a stack.
STEP 5: When the page fault occurs replace page present at the bottom of the
stack.
PROGRAM:

#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf);
getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
OUTPUT:
RESULT:

Thus the program has been executed successfully.


C OPTIMAL(LFU) PAGE REPLACEMENT ALGORITHM
AIM:
To implement page replacement algorithms Optimal (The page which is not
used for longest time).
ALGORITHM:
STEP 1: Create a array.
STEP 2: When the page fault occurs replace page that will not be used for the
Longest period of time.
STEP 3: Create a stack.
STEP 4: When the page fault occurs replace page present at the bottom of the
stack.
PROGRAM:

#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
int main()
{
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\nEnter the no.of frames:");
scanf("%d",&nof);
printf("Enter the no.of reference string:");
scanf("%d",&nor);
printf("Enter the reference string:\n");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\t OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\nThe given string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int i,j,temp,notfound;
for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0; }
OUTPUT:

OPTIMAL PAGE REPLACEMENT ALGORITHM

Enter no.of Frames 3


Enter no.of reference string 6

Enter reference string.. 6 5 4 2 3 1

OPTIMAL PAGE REPLACEMENT ALGORITHM

The given reference string 6 5 4 2 3 1

Reference NO 6-> 6 -1 -1
Reference NO 5-> 6 5 -1
Reference NO 4-> 6 5 4
Reference NO 2-> 2 5 4
Reference NO 3-> 2 3 4
Reference NO 1-> 2 3 1

No.of page
faults...6
RESULT:

Thus the program has been executed successfully.


Ex. No. 10
DATE:
10. SHARED MEMORY AND IPC

AIM:
To write a c program to implement IPC using shared memory.

ALGORITHM:

Step 1: Start the process


Step 2: Declare the segment size
Step 3: Create the shared memory
Step 4: Read the data from the shared memory
Step 5: Write the data to the shared memory
Step 6: Edit the data
Step 7: Stop the process
PROGRAM:

Process 1 (Write data into memory)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
void write_shared();
int main()
{
int i;
void *shared_memory;
int shmid;
shmid = shmget((key_t)2345, 1024, 0666 | IPC_CREAT);
printf("+ Key of shared memory is %d\n", shmid);
shared_memory = shmat(shmid, NULL, 0);

printf("+ Process attached at %p\n", shared_memory);


printf("** Writting buffer into shared memory \n");
write_shared(shared_memory);
}

void write_shared(void *shared_memory)


{
char buff[100];
printf("++ Enter some data to write to shared memory:");
scanf("%s",&buff);
strcpy(shared_memory, buff);
printf("++ You wrote : %s\n", (char *)shared_memory);
}
Process 2 (Read data from memory)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>

void read_shared();
int main()
{
int i;
void *shared_memory;
int shmid;

shmid = shmget((key_t)2345, 1024, 0666 | IPC_CREAT);


printf("+ Key of shared memory is %d\n", shmid);

shared_memory = shmat(shmid, NULL, 0);

printf("+ Process attached at %p\n", shared_memory);

printf("** Reading buffer from shared memory \n");


read_shared(shared_memory);
}

void read_shared(void *shared_memory){


printf("- Process attached at %p\n",shared_memory);
printf("- Data read from shared memory is : %s\n",(char *)shared_memory);
}
OUTPUT:
RESULT:

Thus the program has been executed successfully.


Ex. No. 11
DATE:

11. PAGING TECHNIQUE OF MEMORY MANAGEMENT

AIM:

To write a c program to implement paging technique of memory management.

ALGORITHM:

STEP 1: Read all the necessary input from the keyboard.


STEP 2: Pages - Logical memory is broken into fixed - sized blocks.
STEP 3: Frames – Physical memory is broken into fixed – sized blocks.
STEP 4: Calculate the physical address using the following
Physical address = ( Frame number * Frame size ) + offset
STEP 5: Display the physical address.
STEP 6: Stop the process.
PROGRAM:

#include<stdio.h>
int main()
{
int i,ps,ms,np,nf,pt[20],I,page,offset,id,ph_add;
printf("\n Enter page size, Memorysize, No.of.pages:");
scanf("%d%d%d",&ps,&ms,&np);
nf=ms/ps;
for(i=0;i<np;i++)
{
printf("\n Enter the size of local address:");
scanf("%d",&id);
page=id%ps;
ph_add=pt[page]*ps+offset;
printf("\n Physical address is %d",ph_add);
printf("\n No of frames=%d \n Pages=%d \n Offset %d",nf,page,offset);
return 0;
}
}
OUTPUT:
RESULT:

Thus the program has been executed successfully.


Ex. No. 12
DATE:
12. THREADING AND SYNCHRONIZATION

AIM:

To write a c to implement threading and synchronization.

ALGORITHM:

Step 1: Start the process


Step 2: Declare process thread, thread-id.
Step 3: Read the process thread and thread state.
Step 4: Check the process thread equals to thread-id by using if condition.
Step 5: Check the error state of the thread.
Step 6: Display the completed thread process.
Step 7: Stop the process
PROGRAM:

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void *arg)
{
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(5);
return 0;
}
OUTPUT:
RESULT:

Thus the program has been executed successfully.

You might also like