PRODUCER CONSUMER PROBLEM
************************************************************************
AIM
To write a program for verify and execute the consumer problem using semaphores.
ALGORITHM
1. Start the program.
2. To call main function.
3. Get the choice and print it.
4. Declare i value and check whether item->buffer.
5. To call producer function and print now many no of item available.
6. Print the result.
7. Stop the program.
PROGRAM
#include<stdio.h>
void producer();
void consumer();
int i,item=0,buff=4,a[4],ch;
main()
{
printf(" Producer Consumer Problem.");
do
{
printf("\n Size of the buffer is %d.\n",buff);
printf(" Number of items in buffer %d.\n",item);
printf(" [Link]\n [Link]\n [Link]\n Enter your choice: ");
scanf("%d",&ch);
if(ch==1)
producer();
else if(ch==2)
consumer();
}while(ch!=3);
}
void producer()
{
if(i==0)
{
printf("\n Enter how much item to be produced: ");
scanf("%d",&item);
if(item>buff)
{
printf("\n The buffer will overflow!");
printf("\n Re-enter below buffer size.\n");
producer();
}
else
{
for(i=1;i<=item;i++)
{
printf("\n Enter the %d element: ",i);
scanf("%d",&a[i]);
}
i=i-1;
}
}
else if(i!=0)
printf("\n Producer is waiting...");
}
void consumer()
{
if(i>0)
{
printf("\n Consumer consumes %d items.\n",a[i]);
i--;
item--;
}
else
printf("\n No item are available for producer...\n");
}
OUTPUT
[cse27@localhost siva7]$ cc ex22.c
[cse27@localhost siva7]$ ./[Link]
Producer Consumer Problem.
Size of the buffer is 4.
Number of items in buffer 0.
[Link]
[Link]
[Link]
Enter your choice: 1
Enter how much item to be produced: 3
Enter the 1 element: 2
Enter the 2 element: 4
Enter the 3 element: 7
Size of the buffer is 4.
Number of items in buffer 3.
[Link]
[Link]
[Link]
Enter your choice: 2
Consumer consumes 7 items.
Size of the buffer is 4.
Number of items in buffer 2.
[Link]
[Link]
[Link]
Enter your choice: 2
Consumer consumes 4 items.
Size of the buffer is 4.
Number of items in buffer 1.
[Link]
[Link]
[Link]
Enter your choice: 2
Consumer consumes 2 items.
Size of the buffer is 4.
Number of items in buffer 0.
[Link]
[Link]
[Link]
Enter your choice: 2
No item are available for producer...
Size of the buffer is 4.
Number of items in buffer 0.
[Link]
[Link]
[Link]
Enter your choice: 3
RESULT
Thus the program for the consumer problem using semaphores algorithm is executed and
verified successfully.
FIRST FIT ALGORITHM
************************************************************************
AIM
To write a c program to implement first fit algorithm.
ALGORITHM
1. Start the program.
2. Read the number of processes.
3. Read the amount of space.
4. Read the request.
5. For i=1,i<n;i++
a. If(a[i]>k)
b. If(a[i]==1)
6. Print the result.
7. Stop the program.
PROGRAM
#include<stdio.h>
main()
{
int n,c,temp,st[20],sz[10],i,j=0,psz,df[20],e=0,k=0;
zz:
printf("\n Enter the number of unused space: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Starting address and Unused space size: %d\n",i+1);
scanf("%d%d",&st[i],&sz[i]);
}
zz1:
printf("\n Enter the process size: ");
scanf("%d",&psz);
printf("\n OUTPUT:");
printf("\n Before allocation\n");
printf(" *****************");
printf("\n Starting address \tUnused space \n");
for(i=0;i<n;i++)
printf(" %d \t\t %d\n",st[i],sz[i]);
for(i=0;i<n;i++)
{
if(psz<=sz[i])
{
e=1;
break;
}
}
if(e==1)
{
for(i=0;i<n;i++)
df[i]=sz[i]-psz;
for(i=0;i<n;i++)
{
if(df[i]>=0)
{
st[i]=st[i]+psz;
sz[i]=sz[i]-psz;
j=j+1;
if(j==1)
break;
}
}
printf("\n\n After allocation\n");
printf(" ****************");
printf("\n Starting address \tUnused space\n");
for(i=0;i<n;i++)
printf("\n %d \t\t %d ",st[i],sz[i]);
}
else
{
printf("\n Memory cannot be allocated.");
printf("\n press 1 to retry...");
scanf("%d",&k);
if(k==1)
goto zz1;
}
printf("\n Enter 1 for continue or any key to disconnect...");
scanf("%d",&c);
if(c==1)
goto zz;
OUTPUT
[cse27@localhost siva7]$ cc ex23.c
[cse27@localhost siva7]$ ./[Link]
Enter the number of unused space: 4
Enter the Starting address and Unused space size: 1
1000
5
Enter the Starting address and Unused space size: 2
1020
20
Enter the Starting address and Unused space size: 3
1050
10
Enter the Starting address and Unused space size: 4
1070
5
Enter the process size: 10
OUTPUT:
Before allocation
**************
Starting address Unused space
1000 5
1020 20
1050 10
1070 5
After allocation
*************
Starting address Unused space
1000 5
1030 10
1050 10
1070 5
Enter 1 for continue or any key to disconnect..
RESULT
Thus the program to implement the first fit algorithm was executed successfully.
WORST FIT ALGORITHM
************************************************************************
AIM
To write a c program to implement worst fit algorithm.
ALGORITHM
1. Start the program.
2. Read the number of processes.
3. Read the amount of space and starting address.
4. Read the request.
5. For i=1,i<n;i++
a. If(a[i]>=b)
i. b=I;
ii. l=k;
b. If(c==1)
6. Print the result.
7. Stop the program.
PROGRAM
#include<stdio.h>
struct process
{
int baseadd,blksize;
}p[20];
main()
{
int i,j,n,x,b=0,e,temp,temp1;
printf("\n Enter the number ssof unused space: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the base address and size:\n");
scanf("%d%d",&p[i].baseadd,&p[i].blksize);
}
do
{
printf("\n Enter the size of new process: ");
scanf("%d",&x);
printf("\n OUTPUT:");
printf("\n Before allocation:");
printf("\n **************\n");
printf("\n\t Base address \t\t Block size\n");
for(i=0;i<n;i++)
{
printf("\n\t%d \t\t\t %d\n",p[i].baseadd,p[i].blksize);
}
for(i=0;i<n;i++)
{
if(p[i].blksize<p[i].blksize)
{
temp=p[i].blksize;
p[i].blksize=p[j].blksize;
p[j].blksize=temp;
temp1=p[i].baseadd;
p[i].baseadd=p[j].baseadd;
p[j].baseadd=temp1;
}
}
}
for(j=0;j<n;j++)
{
if(p[j].blksize>=x)
{
p[i].baseadd+=x;
p[j].blksize=x;b=1;
break;
}
}
for(i=0;i<n;i++)
{
for (j=i+1;j<n;j++)
{
if (p[i].baseadd>p[j].baseadd)
{
temp=p[j].baseadd;
p[i].baseadd=p[j].baseadd;
p[j].baseadd=temp;
temp1=p[i].blksize;
p[i].blksize=p[j].blksize;
p[i].blksize=temp1;
}
}
}
if(b==1)
{
printf("\n After Allocation:");
printf("\n **************");
printf("\n\t Base address \t\t Block size\n");
for(i=0;i<n;i++)
{
printf("\n\t%d \t\t\t %d\n",p[i].baseadd,p[i].blksize);
}
}
else
printf("\n The process is not fitter \n");
printf("\n Enter 1 to continue or 0 to exit\n");
scanf("%d",&e);
}
while(e==1);
return 0;
}
OUTPUT
[cse27@localhost siva7]$ cc ex24.c
[cse27@localhost siva7]$ ./[Link]
Enter the number of unused space: 4
Enter the base address and size:
1000
5
Enter the base address and size:
1020
20
Enter the base address and size:
1050
10
Enter the base address and size:
1070
5
Enter the size of new process: 10
OUTPUT:
Before allocation:
***************
Base address Block size
1000 5
1020 20
1050 10
1070 5
After Allocation
*****************
Base address Block size
1000 5
1020 10
1050 10
1070 5
Enter 1 to continue or 0 to exit
RESULT
Thus the program to implement the worst fit algorithm was executed successfully.
BEST FIT ALGORITHM
************************************************************************
AIM
To write a c program to implement best fit algorithm.
ALGORITHM
1. Start the program.
1. Read the number of processes.
2. Read the amount of space and starting address.
3. Read the request.
4. If i=1,i<n;i++
a. If(a[i].y=k)
b. If(c==1)
5. Print the result.
6. Stop the program.
PROGRAM
#include<stdio.h>
main()
{
int n,c=0,temp,st[20],sz[10],i,j,psz,df[20],e,k=0;
char ch;
zz:
do
{
printf("\n Enter the number of unused space: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&st[i],&sz[i]);
}
printf("\n Enter the process size: ");
scanf("%d",&psz);
printf("\n OUTPUT:");
printf("\n Before allocation:");
printf("\n **************");
printf("\n Starting address \t\t Unused space \n");
for(i=0;i<n;i++)
{
printf(" %d \t\t\t %d\n",st[i],sz[i]);
}
for(i=0;i<n;i++)
{
if(psz<=sz[i])
c=1;
if(c==1)
{
for(i=0;i<n;i++)
df[i]=sz[i]-psz;
for(i=0;i<n;i++)
{
for(j=i+1;j<i;j++)
{
if (df[i]>df[j])
df[i]=df[j];
df[j]=temp;
st[j]=temp;
temp=sz[i];
temp=sz[i];
sz[i]=sz[j];
sz[j]=temp;
}
}
}
j=0;
for(i=0;i<n;i++)
if(df[i]>=0)
{
st[i]=st[i]+psz;
if(j==1)
break;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<i;j++)
{
if(df[i]>df[j])
{
temp=df[i];
df[i]=df[j];
df[j]=temp;
temp=st[i];
st[i]=st[j];
st[j]=temp;
temp=sz[i];
sz[i]=sz[j];
sz[j]=temp;
}
}
}
printf("\n After allocation:");
printf("\n *************");
printf("\n Starting address \t\t Unused space\n");
for(i=0;i<n;i++)
{
printf("\n %d \t\t\t %d ",st[i],sz[i]);
}
}
else
{
printf("\n Memory cannot be allocated...");
printf("\n press 1 to retry...");
scanf("%d",&k);
if(k==1)
goto zz;
else
goto yy;
}
yy:
printf("\n Enter 1 for continue or any key to disconnect...");
scanf("%d",&e);
}
}
while(e==1);
return 0;
}
OUTPUT
[cse27@localhost siva7]$ cc ex25.c
[cse27@localhost siva7]$ ./[Link]
Enter the number of unused space: 4
Enter the Starting address and Unused space size: 1
1000
15
Enter the Starting address and Unused space size: 2
1020
20
Enter the Starting address and Unused space size: 3
1050
5
Enter the Starting address and Unused space size: 4
1070
10
Enter the process size: 10
OUTPUT:
Before allocation:
**************
Starting address Unused space
1000 15
1020 20
1050 5
1070 10
After allocation:
*************
Starting address Unused space
1010 5
1020 20
1050 5
1070 10
Enter 1 for continue or any key to disconnect...0
RESULT
Thus the program to implement the best fit algorithm was executed successfully.
INTER PROCESS COMMUNICATION
************************************************************************
AIM
To implementation IPC in UNIX.
ALGORITHM
Step 1: Start the program.
Step 2: Get the number of segments.
Step 3: Get the base address and length for each segment.
Step 4: Get the logical address.
Step 5: Check whether the segment number is within the limit, if not display the
error message.
Step 6: Check whether the byte reference is within the limit, if not display the error
message.
Step 7: Stop the program.
PROGRAM
#include<stdio.h>
#include<sys/types.h>
int main()
{
int fd[2];
pid_t pid;
char buff[30];
pipe(fd);
pid=fork();
if(pid=0)
{
close(fd[0]);
printf("Client is writing PIPE!!!\n");
write(fd[1],"ALL IS WELL",sizeof(buff));
}
else
{
close(fd[1]);
printf("Parent is reading reading the PIPE!!!\n");
read(fd[0],buff,sizeof(buff));
printf("%s",buff);
}
}
OUTPUT
[cse27@localhost siva7]$ cc ex27.c
[cse27@localhost siva7]$ ./[Link]
Parent is reading reading the PIPE!!!
Parent is reading reading the PIPE!!!
RESULT
Thus the program to implement IPC was executed successfully.
PAGING
************************************************************************
AIM
To implement the Memory management policy- Paging.
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>
#include<conio.h>
#include<math.h>
void main()
{
struct
{
int fno, valid;
}p_table[10];
int mainsize, framesize, numframe,numpage ,logicsize, i, j, baseaddr;
int logicaddr, physaddr, pno, disp;
clrscr();
printf("\n\n\t Program for Paging techniques - Fixed Size Partition");
printf("\n\n\t Enter the Base Address of Physical Memory : ");
scanf("%d", &baseaddr);
printf("\n\t Enter the size of Main Memory : ");
scanf("%d", &mainsize);
printf("\n\t Enter the size of Main Memory Frame : ");
scanf("%d", &framesize);
numframe= (int) mainsize/framesize;
printf("\n\t Total no. frames in Main memory is %d", numframe);
printf("\n\t Enter the size of Logical Memory : ");
scanf("%d", &logicsize);
numpage= (int) logicsize/framesize;
printf("\n\t Total no. pages in Logical Memory is %d", numpage);
for(i=0; i<numpage; i++)
{ p_table[i].fno=-1; p_table[i].valid=-1; }
printf("\n\n\t Enter the frame values in Page Table");
for(i=0; i<numpage; i++)
{
while(1)
{
printf("\n\t Page %d is stored in frame no. -> ",i);
scanf("%d", &p_table[i].fno);
if(f_table[p_table[i].fno].valid==-1 && p_table[i].fno < numframe)
{
p_table[i].valid=1;
f_table[p_table[i].fno].valid=1;
f_table[p_table[i].fno].pno=i;
break;
}
else
printf("\n\t Already Allocated / Invalid Frame number...\n\n");
}
}
printf("\n\t PAGE TABLE");
printf("\n\t| Index | Frame_no | Valid_bit \n\n");
for(i=0; i<numpage; i++)
printf("\n\t\t %2d \t %2d \t %2d", i, p_table[i].fno, p_table[i].valid);
printf("\n\n\t Enter the logical address for mapping process : ");
scanf("%d", &logicaddr);
pno= (int)logicaddr / framesize;
disp=(int)logicaddr % framesize;
physaddr= baseaddr + ((p_table[pno].fno-1) * framesize)+ disp;
printf("\n\t Physical Address value is %d \n\n", physaddr);
getch();
}
Program for Paging techniques - Fixed Size Partition
Enter the Base Address of PHysical Memory : 1000
Enter the size of Main Memory : 64
Enter the size of Main Memory Frame : 4
Total no. frames in Main memory is 16
Enter the size of Logical Memory: 16
Total no. pages in Logical Memory is 4
Enter the frame values in Page Table
Page 0 is stored in frame no. -> 3
Page 1 is stored in frame no. -> 7
Page 2 is stored in frame no. -> 11
Page 3 is stored in frame no. -> 15
PAGE TABLE
| Index | Frame_no | Valid_bit |
0 3 1
1 7 1
2 11 1
3 15 1
Enter the logical address for mapping process: 11
Mapped Physical Address value is 1043
RESULT
Thus the implementation of memory allocation with pages is successfully completed.
************************************************************************
SEGMENTATION
************************************************************************
AIM
To implement the memory management policy-segmentation.
ALGORITHM
Step 1: Start the program.
Step 2: Get the number of segments.
Step 3: get the base address and length for each segment.
Step 4: Get the logical address.
Step 5: check whether the segment number is within the limit, if not display the error
message.
Step 6: Check whether the byte reference is within the limit, if not display the error
message.
Step 7: Calculate the physical memory and display it.
Step 8: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int noseg, i, physaddr, temp1=9, digits=1, logicaddr, segno, disp, flag=0;
struct
{
int num, baseaddr, limit;
}segment[5];
clrscr();
printf("\n\n\t Program for Segmentation Implementation");
printf("\n\n\t Enter the No. of segments");
scanf("%d", &noseg);
for(i=1; i<=noseg; i++)
{
segment[i].num=i;
printf("\n\n\t\t Segment Number: %d : ", i);
printf("\n\t Base of Segment In Physical Memory : ");
scanf("%d", &segment[i].baseaddr);
printf("\n\t Limit of Segment : ");
scanf("%d", &segment[i].limit);
}
printf("\n\n\t SEGMENT TABLE");
printf("\n\t| Seg_No. | Base_addr | Limit |\n\n");
for(i=1; i<=noseg; i++)
{
printf("\t %d", segment[i].num);
printf("\t\t %d", segment[i].baseaddr);
printf("\t %d\n", segment[i].limit);
}
do{
while(1)
{
printf("\n\n\t Enter the Logical Address : ");
scanf("%d", &logicaddr);
if(logicaddr > 0)
{
while(temp1 < logicaddr)
{
temp1= (temp1 * 10) + 9;
digits++;
}
segno= (int) logicaddr / pow(10, digits-1);
disp= (int) logicaddr % (int) pow(10, digits-1);
printf("\n\n\t The segment number is %d", segno);
printf("\n\t The displacement is %d", disp);
if(disp< segment[segno].limit)
{
physaddr= segment[segno].baseaddr + disp;
printf("\n\n\t Result");
printf("\n\n\t The logical Address is %d", logicaddr);
printf("\n\n\t Mapped Physical Address is %d", physaddr);
}
else
printf("\n\n\t Error: Invalid Logical Address\n");
break;
}
else
printf("\n\n\t Invalid Logical Address");
}
printf("\n\n\t To Continue, Press 1 : ");
scanf("%d", &flag);
}while(flag==1);
}
OUTPUT
No. of segments: 5
Segment Number: 1:
Base of Segment In Physical Memory : 1000
Limit of Segment : 900
Segment Number: 2 :
Base of Segment In Physical Memory : 2000
Limit of Segment : 300
Segment Number: 3 :
Base of Segment In Physical Memory : 2500
Limit of Segment : 700
Segment Number: 4 :
Base of Segment In Physical Memory : 3400
Limit of Segment : 600
Segment Number: 5 :
Base of Segment In Physical Memory : 4200
Limit of Segment : 600
SEGMENT TABLE
| Seg_No. | Base_addr | Limit |
1 1000 900
2 2000 300
3 2500 700
4 3400 600
5 4200 600
Enter the Logical Address: 3500
The segment number is 3
The displacement is 500
Result
The logical Address is 3500
Mapped Physical Address is 3000
To Continue, Press 1 :1
Enter the Logical Address: 3800
The segment number is 3
The displacement is 800
Error: Invalid Logical Address
To Continue, Press 1 : 0
RESULT
Thus the segmentation concept was successfully simulated.
************************************************************************
FILE ALLOCATION
************************************************************************
AIM
To implement the page replacement algorithm
ALGORITHM
Step 1: Start the program.
Step 2: Get the number of segments.
Step 3: get the base address and length for each segment.
Step 4: Get the logical address.
Step 5: check whether the segment number is within the limit, if not display the error
message.
Step 6: Check whether the byte reference is within the limit, if not display the error
message.
Step 7: Calculate the physical memory and display it.
Step 8: Stop the program.
PROGRAM
#include<stdio.h>
int m,n,i,j,k,flag,fault=0,min,noframes,count,refer[10],pageframe[10][10];
void replace(int x)
{
for(i=0;i<n;i++)
{
flag=1;
for(j=0;j<noframes;j++)
if(refer[i]==pageframe[j][0])
{
m=j;
flag=0;
}
if(flag)
{
fault++;
min=32000;
for(j=0;j<noframes;j++)
if(pageframe[j][1]<min)
{
min=pageframe[j][1];
k=j;
}
pageframe[k][0]=refer[i];
pageframe[k][1]=++count;
for(j=0;j<noframes;j++)
printf("%d",pageframe[j][0]);
printf("\n");
}
else
{
if(x==2)
pageframe[m][1]=++count;
}}
printf("\n no page fault%d",fault);
}
int main()
{
printf("enter the no of reference:");
scanf("%d",&n);
printf("\n enter the no of frames:");
scanf("%d",&noframes);
printf("\n enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&refer[i]);
printf("\t\t fifo algorithm");
for(i=0;i<n;i++)
{
pageframe[i][0]==-1;
pageframe[i][1]=count;
}
replace(1);
fault=0;
count=0;
printf("\t\t\n lru algorithm\n");
for(i=0;i<n;i++)
{
pageframe[i][0]=0;
pageframe[i][1]=count;
}
replace(2);
}
OUTPUT
enter the no of reference:8
enter the no of frames:3
enter the reference string:1
2
0
3
2
1
2
1
fifo algorithm
100
120
123
no page fault3
lru algorithm
100
120
320
321
no page fault4
RESULT
Thus the page replacement algorithm was successfully executed.
************************************************************************