Os Lab 6,7 Exp
Os Lab 6,7 Exp
Write C programs to illustrate the following IPC mechanisms a) Pipes b) FIFOs c) Message Queues
d) Shared Memory
OBJECTIVE:
PROGRAM
#include <stdio.h>
#include <unistd.h>
#define MSGSIZE 16
int main()
char inbuf[MSGSIZE];
int p[2], i;
if (pipe(p) < 0)
exit(1);
/* continued */
/* write pipe */
write(p[1], msg1, MSGSIZE);
/* read pipe */
return 0;
Output:
hello, world #1
hello, world #2
hello, world #3
PROGRAM
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
// mkfifo(<pathname>, <permission>)
mkfifo(myfifo, 0666);
while (1)
fd = open(myfifo, O_WRONLY);
// 80 is maximum length
// and close it
close(fd);
fd = open(myfifo, O_RDONLY);
close(fd);
return 0;
output:
c).Message Queue:
int main()
{
key_t key;
int msgid;
return 0;
}
MESSAGE QUEUE FOR READER PROCESS
// C Program for Message Queue (Reader Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
return 0;
}
Output:
PROGRAM:
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
gets(str);
shmdt(str);
return 0;
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
shmdt(str);
shmctl(shmid,IPC_RMID,NULL);
return 0;
Segmentation
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset; int s[10], fno[10][20];
clrscr();
nop = ms/ps;
rempages = nop;
for(i=1;i<=np;i++)
{
if(s[i] >rempages)
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
else
pa=fno[x][y]*ps+offset;
getch();
INPUT
OUTPUT
Memory is Full
SOURCE CODE:
#include<stdio.h>
#include <conio.h>
#include<math.h>
int sost;
void gstinfo();
void ptladdr();
struct segtab
{ int sno;
int baddr;
int limit;
int val[10];
}st[10];
void gstinfo()
{ int i,j;
scanf("%d",&sost);
for(i=1;i<=sost;i++)
st[i].sno = i;
scanf("%d",&st[i].baddr);
scanf("%d",&st[i].limit);
for(j=0;j<=sost;i++)
printf("\t\t%d \t\t%d\t\t%d\n\n",st[i].sno,st[i].baddr,st[i].limit);
scanf("%d",&swd);
n=swd;
while (n != 0)
n=n/10; d++;
Dept. of CSE 53
}
s = swd/pow(10,d-1);
disp = swd%(int)pow(10,d-1);
if(s<=sost)
Else
else
void main()
{ char ch;
clrscr();
gstinfo();
do
ptladdr();
printf("\n\t Do U want to Continue(Y/N)");
flushall();
scanf("%c",&ch);
getch();
Dept. of CSE 54
Enter the 8 address Value: 51
145
254
334
Do U want to Continue(Y/N)
145
254
334
PROGRAM:
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k])
{
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
printf("%d\t", frames[j]);
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
OUTPUT:
Enter number of frames: 3
Enter number of pages: 10
Enter page reference string: 2 3 4 2 1 3 7 5 4 3
2 -1 -1
2 3 -1
234
234
134
134
734
534
534
534
Total Page Fault=6
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);