Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links



How to use pipe between programs in C?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-01-2010
Member
 
Join Date: Dec 2009
Posts: 12
How to use pipe between programs in C?

I have Asus desktop computer at home. I doing studies on programming language like C, C++, Core Java and DBMS. Actually i just love to do programming. I am beginner in programming. Few days back i stuck in pipes in C. I was doing programming in C and i got problem in piping, i want a pipe to share between child and parent process as well as also wants to know how to do same between client and server programs. Please if anyone could help me out or give me right direction.
Reply With Quote
  #2  
Old 08-01-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
Re: How to use pipe between programs in C?

As we know that there many multiple processes may be running on a system and maybe be controlled (spawned by fork() by one of the programs). And in many applications there is a need for this processes to communicate with each exchanging data or control information. This function is achieved by pipe(piping) method. Piping is a process where the input of one process is made the input of another. And piping function can be done by using two pipe functions i.e popen(formatting pipe) and pipe(low level piping).
Reply With Quote
  #3  
Old 08-01-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
Re: How to use pipe between programs in C?

I have done one piping program and it will be perfect example for you to clear doubt about how to use pipe in C programs:

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>

int d1[2];

void open_pipe(void)
{
/*It is the same as pipe() */
if(socketpair(PF_UNIX,SOCK_STREAM,0,d1) < 0)
{
printf(stderr,"File : %s , Line : %d , socketpair error:",
__FILE__,
__LINE__);
perror("");
abort();
}
}

void *do_task(void *arg)
{
int m;

printf("\t Comes into thread \n\n");

m = read(d1[0],(void *) &iValue,sizeof(iValue1));
printf("\t child1 ,Line1 = %d \n", iValue1);

return (void *) NULL;
}

void main(void)
{
int iRetSts1,iValue1;
pthread_t iThreadId;
pthread_attr_t vThreadAttr;

open_pipe();

pthread_attr_init(&vThreadAttr);
pthread_create(&iThreadId,NULL,do_task,(void *)NULL);

iRetSts1 = write(d1[1],(void *) &iValue1,sizeof(iValue1));
if(iRetSts1 == -1)
{
printf(stderr,"File : %s , Line1 : %d , write failed \n",
__FILE__,
__LINE__);
perror("");
}
}
Hope this will clear your query of using pipes
Reply With Quote
  #4  
Old 08-01-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: How to use pipe between programs in C?

The basic way to create a pipe is by using the pipe function. The pipe function will make both the reading as well as writing ends of the pipe. The pipe is not useful for the one(single) process and it is only use for two or more processes. We can say that a process will make a pipe just before it forks one or more child process. The pipe is always used in a communication between the parent and child processes, or between 2 sibling processes. The pipe function should be declared in the header file unistd.h.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 08-01-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: How to use pipe between programs in C?

Good to know that people are still learning programming language like C till now. The pipe is a function which is use to connect or inter-process communication between two or more processes or programs. The syntax/example of creating a pipe for reading and writing are as follow:
int _pipe(
int *pfds,
unsigned int psize,
int textmode
);
The parameter pfds is a array which can hold read and write file descriptors, the parameter psize is used to maintain the amount of memory to reserve and parameter textmode denotes it is a File mode. Hope your problem get solve.

Last edited by Reegan : 08-01-2010 at 10:07 AM.
Reply With Quote
  #6  
Old 08-01-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: How to use pipe between programs in C?

According to me, to make use of the pipe function to do communication between a parent and a child process, then each process should have only one descriptor to open on the pipe. The descriptors must be opposite i.e if the parent has a write descriptor open, then the child must have a open descriptor opened. The easiest way to do this is to use OR (|) the _O_NOINHERIT flag with textmode. The pipe is said to be an artificial Input/Output channel that a program uses to pass information to other programs.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use pipe between programs in C?"
Thread Thread Starter Forum Replies Last Post
AD query, pipe, and batch file halo_7 Active Directory 3 13-02-2011 12:59 AM
dsquery pipe to dsmove dpav Active Directory 10 23-02-2010 11:06 PM
USB:Pipe not Opening! sunwins Software Development 2 27-05-2009 09:33 AM
Named Pipe lsass.exe Marbles Windows Security 7 28-05-2007 05:31 AM
MSI P35 Platinum - Circus Pipe dr.nil Motherboard Processor & RAM 0 04-05-2007 02:59 AM


All times are GMT +5.5. The time now is 11:55 AM.