0% found this document useful (0 votes)
4K views6 pages

Write C Programs To Illustrate The Following IPC Mechanisms: A) Pipes

The document provides code snippets in C to illustrate three different inter-process communication (IPC) mechanisms: (1) pipes for transferring data between related processes, (2) first-in first-out (FIFO) page replacement algorithm for managing memory pages, and (3) shared memory for sharing data between unrelated processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views6 pages

Write C Programs To Illustrate The Following IPC Mechanisms: A) Pipes

The document provides code snippets in C to illustrate three different inter-process communication (IPC) mechanisms: (1) pipes for transferring data between related processes, (2) first-in first-out (FIFO) page replacement algorithm for managing memory pages, and (3) shared memory for sharing data between unrelated processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Write C programs to illustrate the following IPC mechanisms

a) Pipes

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MSG_LEN 64
int main()
{
int result;
int fd[2];
char message[MSG_LEN];
char recvd_msg[MSG_LEN];
result = pipe (fd);
if (result < 0)
{
perror("pipe ");
exit(1);
}
strncpy(message,"Linux World!! ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
{
perror("write");
exit(2);
}
strncpy(message,"Understanding ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
{
perror("write");
exit(2);
}
strncpy(message,"Concepts of ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
{
perror("write");
exit(2);
}
strncpy(message,"Piping ", MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
{
perror("write");
exit(2);
}
result=read (fd[0],recvd_msg,MSG_LEN);
if (result < 0)
{
perror("read");
exit(3);
}
printf("%s\n",recvd_msg);
return 0;
}
Out Put:
Linux World!! Understanding Concepts of Piping
B) FIFO

#include<stdio.h>

#include<conio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

void 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);

getch();

Output:
FIFO PAGE REPLACEMENT ALGORITHM
Enter no.of frames....3
Enter number of reference string..
6
Enter the reference string..3
5
6
2
7
8

The given reference string: 3 5 6 2 7 8

Reference np3-> 3 -1 -1
Reference np5-> 3 5 -1
Reference np6-> 3 5 6
Reference np2-> 2 5 6
Reference np7-> 2 7 6
Reference np8-> 2 7 8

No.of pages faults...6

c)Shared Memory

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc, char *argv[ ])
{
int shmid,cntr;
key_t key;
char *segptr;
char buff[]="pooja......";
key=ftok(".",'s');
if((shmid=shmget(key, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666))== -1)
{
if((shmid=shmget(key,SEGSIZE,0))==-1)
{
perror("shmget");
exit(1);
}
}
else
{
printf("Creating a new shared memory seg \n");
printf("SHMID:%d",shmid);
}
system("ipcs –m");

if((segptr=(char*)shmat(shmid,0,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
printf("Writing data to shared memory...\n");
strcpy(segptr,buff);
printf("DONE\n");
printf("Reading data from shared memory...\n");
printf("DATA:-%s\n",segptr);
printf("DONE\n");
printf("Removing shared memory Segment...\n");
if(shmctl(shmid,IPC_RMID,0)== -1)
printf("Can‟t Remove Shared memory Segment...\n");
else
printf("Removed Successfully");
}

OutPut:

Reading data from shared memory...


DATA:-pooja......
DONE
Removing shared memory Segment...
Removed Successfully

You might also like