Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
50 views7 pages

Single Argument, Which Is An Array of Two Integers. FD (0) Is Set Up For Reading, FD (1) Is Set Up For Writing

The document discusses several programs for inter-process communication using pipes and shared memory in C. It includes programs to: 1) Create a pipe between a parent and child process, with the parent writing a message to the pipe and the child reading it. 2) Write data to a pipe using popen() by writing to a process and fwrite(). 3) Read data from a pipe using popen() by reading from a process and fread(). 4) Create inter-process communication using named pipes with three programs - one to create the named pipe, one to write to it, and one to read from it. 5) Create shared memory between two programs using shmget() to create a shared memory

Uploaded by

aniket sriwastva
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)
50 views7 pages

Single Argument, Which Is An Array of Two Integers. FD (0) Is Set Up For Reading, FD (1) Is Set Up For Writing

The document discusses several programs for inter-process communication using pipes and shared memory in C. It includes programs to: 1) Create a pipe between a parent and child process, with the parent writing a message to the pipe and the child reading it. 2) Write data to a pipe using popen() by writing to a process and fwrite(). 3) Read data from a pipe using popen() by reading from a process and fread(). 4) Create inter-process communication using named pipes with three programs - one to create the named pipe, one to write to it, and one to read from it. 5) Create shared memory between two programs using shmget() to create a shared memory

Uploaded by

aniket sriwastva
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/ 7

Q. Program to send a message from parent process to child process.

Note: To create a simple pipe with C, we make use of the pipe() system call. It takes a
single argument, which is an array of two integers.
fd[0] is set up for reading, fd[1] is set up for writing.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int fd[2],n;
char buffer[100];
pid_t p;
pipe(fd);
p=fork();
if(p>0) //parent
{
close(fd[0]);
printf("Passing value to child\n");
write(fd[1],"hello\n",6);
wait();

}
else // child
{
close(fd[1]);
n=read(fd[0],buffer,100);
write(1,buffer,n);
}
}
Program for inter-process communication using popen
and pclose
//This is the structure stored in 'stdio.h' under the name FILE.This file pointer is just to store
the composite information about a file subject to manipulation. C has a special "data type" for
handling files which is defined in the standard library 'stdio.h '.
It is called the file pointer and has the syntax FILE*.

Q. Program to write into a pipe

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
FILE *rd;
char buffer[50];
sprintf(buffer,"name first");
/*The C library function int sprintf(char *str, const char *format, ...) sends
formatted output to a string pointed to, by str. ... int sprintf(char *str, const
char *format, ...) str − This is the pointer to an array of char elements where
the resulting C string is stored. */
rd=popen("wc -c","w"); // wc -c -> is the process which counts the number
of characters
//passed. 2nd parameter is "w" which means
pipe is opened in writing
//mode
fwrite(buffer,sizeof(char),strlen(buffer),rd); // to write the data into the pipe

/* The second argument of fwrite() is the size of each object, and the third
argument is ...fwrite(buffer, sizeof(char), sizeof(buffer), file);. is a simple ....
functions, such as strlen , strcpy . printf("%s", buffer); */
pclose(rd);
}

Q. Program to read from a pipe

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
FILE *rd;
char buffer[50];
rd=popen("ls","r");
fread(buffer, 1, 50, rd);
printf("%s\n", buffer);
pclose(rd);
}

Program for inter process communication using named


pipes

In computing, a named pipe (also known as a FIFO for its behavior) is an extension to
the traditional pipe concept on Unix and Unix-like systems, and is one of the methods
of inter-process communication (IPC). The concept is also found in OS/2 and Microsoft
Windows, although the semantics differ substantially. A traditional pipe is "unnamed" and
lasts only as long as the process. A named pipe, however, can last as long as the system
is up, beyond the life of the process. It can be deleted if no longer used. Usually a named
pipe appears as a file, and generally processes attach to it for inter-process
communication.

This will require three different programs to work


Program 1: Creating fifo/named pipe ( 1.c )
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int res;
res = mkfifo("fifo1",0777); //creates a named pipe with the
name fifo1
printf("named pipe created\n");
}
Now compile and run this program.
Program 2: Writing to a fifo/named pipe ( 2.c )
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int res,n;
res=open("fifo1",O_WRONLY);
write(res,"written",7);
printf("Process %d finished\n",getpid());
}
Compile this program as
gcc -o 2 2.c
Note: If you run this you will not see any output

Program 3: Reading from the named pipe ( 3.c )


#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int res,n;
char buffer[100];
res=open("fifo1",O_RDONLY);
n=read(res,buffer,100);
printf("Total bytes read: %d\n",n);
printf("%s was passed to me \n",buffer);
printf("Process %d finished\n",getpid());

}
Compile the program as
gcc -o 3 3.c
Now run both the object files simultaneously as
./2 & ./3

IPC:Shared Memory
Shared Memory is an efficeint means of passing data between programs.
One program will create a memory portion which other processes (if
permitted) can access.
A process creates a shared memory segment using shmget()
Once created, a shared segment can be attached to a process address space
using shmat(). It can be detached using shmdt()

1. Program 1: This program creates a shared memory segment, attaches itself to


it and then writes some content into the shared memory segment.

 shm_id = shmget(
 key_t k, /* the key for the
segment */
 int size, /* the size of the
segment */
 int flag); /* create/use flag
*/
 In the above definition, k is of type key_t or IPC_PRIVATE. It is
the numeric key to be assigned to the returned shared memory
segment. size is the size of the requested shared memory. The
purpose of flagis to specify the way that the shared memory will be
used

NAME
shmat - shared memory attach operation
SYNOPSIS

#include <sys/shm.h>

void *shmat(int shmid, const void *shmaddr, int shmflg);

DESCRIPTION
The shmat() function attaches the shared memory segment associated with
the shared memory identifier specified by shmid to the address space of the
calling process. The segment is attached at the address specified by one of
the following criteria:
 If shmaddr is a null pointer, the segment is attached at the first
available address as selected by the system.
 If shmaddr is not a null pointer and (shmflg&SHM_RND) is non-
zero, the segment is attached at the address given by (shmaddr-
((uintptr_t)shmaddr%SHMLBA)) The character % is the C-language
remainder operator.
 If shmaddr is not a null pointer and (shmflg&SHM_RND) is 0, the
segment is attached at the address given by shmaddr.
 The segment is attached for reading if (shmflg&SHM_RDONLY) is
non-zero and the calling process has read permission; otherwise, if it
is 0 and the calling process has read and write permission, the
segment is attached for reading and writing.

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1000,0666|IPC_CREAT); \\creates shared
memory segment \\IPC_CREAT | 0666 for a server (i.e., creating and
granting read and write access to the server)

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


shared_memory=shmat(shmid,NULL,0); \\process attached to shared memory
segment
printf("Process attached at %X\n",(int)shared_memory);
printf("Enter some data to write to shared memory\n");
read(0,buff,100); \\get some input from user
strcpy(shared_memory,buff); \\data written to shared memory
printf("You wrote : %s\n",shared_memory);
}

Program 2: This program attaches itself to the shared memory segment created in
Program 1 and then reads whatever was written in the shared memory via Program 1

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1000,0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); \\process attached to shared memory
segment
printf("Process attached at %X\n",(int)shared_memory);
printf("Data read from shared memory is : %s\n",shared_memory);
}

You might also like