RE:Comunicacion mediante pipes
A ver si me puedo explicar mejor. Necesito hacer una comunicación entre dos programas mediante pipes en Windows. Para ello creo dos pipes una para transmitir los datos hacia un programa y otra para devolver los datos hacia el otro programa. Una vez que he creado las dos pipes, tengo que crear un nuevo proceso para que abra el segundo programa.
tengo que hacer algo asi, este es el servidor:
#include "stdafx.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h> // to use _pipe function
#include <stdio.h>
#include <stdlib.h> // to use itoa function.
#include <process.h> //to use NO_WAIT argument of spawn function, execl
#define PIPE_READ 0
#define PIPE_WRITE 1
#define pipe(phandles) _pipe(phandles,MAXMESG,O_BINARY)
int p1[2] ; // pipe1
int p2[2] ; // pipe2
int m_in ;
int m_out ;
int state;
int fd1,fd2;
int main(int argc, char* argv[])
{
char otherProgramm[10] = "Link2.exe" ;
//*create two pipes p1 and p2, return true if the pipes could not be created.
if( _pipe(p1,256,O_TEXT|O_NOINHERIT )==-1 || _pipe(p2,256,O_TEXT|O_NOINHERIT )==-1 ){
return false ;
}
char rpipe[20];
char wpipe[20] ;
itoa( p1[PIPE_READ], rpipe, 10 );
itoa( p2[PIPE_WRITE], wpipe, 10 );
// starting another program
if( (state = spawnl(P_NOWAIT, otherProgramm, rpipe, wpipe, NULL))==-1 )
{
// error closing the pipes
close(p1[PIPE_READ]) ; // close 0
close(p1[PIPE_WRITE]) ; // close 1
close(p2[PIPE_READ]) ; // close 0
close(p2[PIPE_WRITE]) ; // close 1
return false ;
}
m_in = p2[PIPE_READ] ;
m_out = p1[PIPE_WRITE] ;
close(p2[PIPE_WRITE]) ;
close(p1[PIPE_READ]) ;
return true;
}
y luego al crear un proceso tendria que hacer algo asi:
#include "stdafx.h"
#include <stdio.h>
#include <io.h>
#include <process.h>
int fd1,fd2;
int main(int argc, char* argv[])
{
// Preparing the redirection of stdin and stdout
// assign the file descriptor f1 to the standar input. fd1 = p1[READ]
close( 0 ); /*** slave - stdin ***/
sscanf(argv[1],"%d",&fd1);
dup2(fd1, 0);
// assign the file descriptor f2 to the standar output. fd2 = p1[WRITE]
close( 1 ); /*** slave - stdout ***/
sscanf(argv[1],"%d",&fd2);
dup2(fd2, 1);
/*
// assign the file descriptor f2 to the standar error fd2 = p1[WRITE]
close( 2 ); // slave - stderr
if( dup2(fd2, 2) == -1 )
myerror(6) ;
*/
fprintf(stderr, "Starting the program Link2.exe\n");
fflush(stderr);
execl( "Singular.exe","Singular.exe", NULL );
return true;
El problema es que yo tengo el primer codigo fuente "link.cpp" y al crear un nuevo proceso, no se si tengo que hacer otro programa "link.cpp" que luego a su vez llame al programa que deseo abrir, o simplemente poner un else y así si al hacer el spawn() no da error, ejecuta el código del hijo. de esta forma:
#include "stdafx.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h> // to use _pipe function
#include <stdio.h>
#include <stdlib.h> // to use itoa function.
#include <process.h> //to use NO_WAIT argument of spawn function, execl
#define PIPE_READ 0
#define PIPE_WRITE 1
#define pipe(phandles) _pipe(phandles,MAXMESG,O_BINARY)
int p1[2] ; // pipe1
int p2[2] ; // pipe2
int m_in ;
int m_out ;
int state;
int fd1,fd2;
int main(int argc, char* argv[])
{
char otherProgramm[10] = "singular.exe" ;
//*create two pipes p1 and p2, return true if the pipes could not be created.
if( _pipe(p1,256,O_TEXT|O_NOINHERIT )==-1 || _pipe(p2,256,O_TEXT|O_NOINHERIT )==-1 ){
return false ;
}
char rpipe[20];
char wpipe[20] ;
itoa( p1[PIPE_READ], rpipe, 10 );
itoa( p2[PIPE_WRITE], wpipe, 10 );
// starting another program
if( (state = spawnl(P_NOWAIT, otherProgramm, rpipe, wpipe, NULL))==-1 )
{
// error closing the pipes
close(p1[PIPE_READ]) ; // close 0
close(p1[PIPE_WRITE]) ; // close 1
close(p2[PIPE_READ]) ; // close 0
close(p2[PIPE_WRITE]) ; // close 1
return false ;
}
else
{
// Preparing the redirection of stdin and stdout
// assign the file descriptor f1 to the standar input. fd1 = p1[READ]
close( 0 ); /*** slave - stdin ***/
sscanf(argv[1],"%d",&fd1);
dup2(fd1, 0);
// assign the file descriptor f2 to the standar output. fd2 = p1[WRITE]
close( 1 ); /*** slave - stdout ***/
sscanf(argv[1],"%d",&fd2);
dup2(fd2, 1);
/*
close( 2 ); // slave - stderr
if( dup2(fd2, 2) == -1 )
myerror(6) ;
*/
fprintf(stderr, "Starting the program Link2.exe\n");
fflush(stderr);
execl( "Singular.exe","Singular.exe", NULL );
}
m_in = p2[PIPE_READ] ;
m_out = p1[PIPE_WRITE] ;
close(p2[PIPE_WRITE]) ;
close(p1[PIPE_READ]) ;
return true;
}
te pongo aqui una breve explicacion por si lo entiendes mejor:
// Handling pipes under Windows
//
// The following is code fragment which starts another programm
// with spawnl and communcates via pipes with this programm
// This is just a frame of code (taken and slightly modified from
// another bigger project). So it is not directly tested in this way
// but the important technical components are demonstrated
// Syntactically everything should be fine.
#define MAXMESG 0x1000
#define PIPE_READ 0
#define PIPE_WRITE 1
#define pipe(phandles) _pipe(phandles,MAXMESG,O_BINARY)
int p1[2] ; // pipe1
int p2[2] ; // pipe2
int m_in ;
int m_out ;
/**
* the init() function creates a child process and prepares it
* for pipe communication. The pipe communication
* is as follows:
*
* --------- ---------
* | THIS | | OTHER |
* | 1 0 0 1 | | 0 1 1 0 |
* --------- ---------
* v ^ ^ v ^ v v ^
* | | | | ------- | | | |
* | | | *======>| pipe1 |>======* | | |
* | | *-CLOSED-<| |<-CLOSED-* | |
* | | ------- | |
* | | | |
* | | ------ | |
* | *==========<| pipe2 |<==========* |
* *-CLOSED----->| |>--CLOSED----*
* -------
*
* THIS process uses pipe2[0] for reading and pipe1[1] for writing.
* Therefore the OTHER programm must use pipe2[1] for writing and
* pipe1[0] for reading. This means in THIS process the channels
* pipe2[1] and pipe1[0] must be closed and in the OTHER process
* the channels pipe2[0] and pipe1[1] must be closed.
El problema es que haga como lo haga siempre me dan errores y no sé como hacerlo
Espero que me entiendas y puedas ayudarme.
gracias.