Programación General > Visual C++
Rutinas En Visual C++
Diodo:
--- Citar ---en realidad no termina el proceso del segundo programa porque no tengo el pid para poder matarlo, en realidad el proceso que termina es el del segundo programa
--- Fin de la cita ---
:blink: ahi me mataste ;)
bcasadorodriguez:
Perdón por la confusión al explicarme. A ver, ya he conseguido lo que queria, las pipes me funcionan perfectamente, el problema es que esta parte del código la tengo que insertar en otro código que es un módulo para que el primer programa que es el que llama al segundo lo entienda. Ahora bien, cuando ejecuto el programa singular.exe con este código me funciona aparentemente bien, excepto cuando tiene que leer varias líneas al hacer get() pero cuando lo inserto en el módulo, al leer se me queda como bloqueado. Si no teneis el programa singular lo podeis simular con el ejecutable de cat. No sé porque no funciona cuando lo introduzco en el módulo, a ver si teneis alguna solución y sobre todo , porque no me funciona cuando intento leer varias lineas??
Espero haberme explicado bien. ahi os dejo el código.
#include "stdafx.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <Windows.h>
#define PIPE_READ 0
#define PIPE_WRITE 1
#define MAXMESG 0x1000
#define _pipe(phandles) _pipe(phandles,MAXMESG,O_TEXT)
int p1[2] ; // pipe1
int p2[2] ; // pipe2
int m_in ;
int m_out ;
int state;
int endstate;
char prog[] = "Singular.exe" ;
int fd1,fd2;
//*****************************************************************************
//* my error
//*
//*****************************************************************************
void myerror(int errorCode)
{
fprintf(stderr, "Return with error code %d\n", errorCode) ;
fprintf(stderr, "Errno: %d\n", errno) ;
exit(errorCode) ;
}
//*****************************************************************************
//* put
//*
//*****************************************************************************
void put(int fd)
{
//char buf[256];
//fprintf(stderr, "type some Singular code :\n");
//fflush(stderr);
//gets(buf);
//buf[sizeof(buf)] = '\n' ;
//buf[sizeof(buf)+1] = '\0' ;
//int bytes = write(fd,buf,sizeof(buf));
int bytes = write(fd,"4+5;\n",sizeof("4+5);\n"));
//write(fd,"\n", sizeof("\n"));
fprintf(stderr, "to singular is : 4+5;", bytes);
fflush(stderr);
}
/*
void put(int fd)
{
write(fd,"4+5;\n",strlen("4+5;\n"));
fprintf(stderr, "to singular is : %s \n","4+5; \n");
fflush(stderr);
}
*/
//*****************************************************************************
//* get
//*
//*****************************************************************************
void get (int fd)
{
char buf[60000];
int bytes = read ( fd, buf,sizeof(buf));
buf[bytes] = '\0' ;
fprintf(stderr, "from singular is : %s \n",buf);
fflush(stderr);
//Sleep(3000);
//bytes = read ( fd, buf,sizeof(buf));
//fprintf(stderr, "from singular is : %s %d\n",buf, bytes);
//fflush(stderr);
}
/*
void get (int fd)
{
char buf[60000];
read ( fd, buf,sizeof(buf));
fprintf(stderr, "from singular is : %s \n",buf);
fflush(stderr);
}
*/
//*****************************************************************************
//* main
//*
//*****************************************************************************
int main(int argc, char* argv[])
{
fprintf(stderr, "Starting slave program ....\n");
fflush(stderr);
char otherProgramm[] = "Singular.exe";
//*create two pipes p1 and p2, return true if the pipes could not be created.
//if( _pipe(p1,256,O_TEXT || _pipe(p2,256,O_TEXT)
if( _pipe(p1)==-1 || _pipe(p2)==-1 )
{
perror("pipe failed") ;
}
/*close(0);
dup2(p2[PIPE_READ],0);
close(1);
dup2(p1[PIPE_WRITE],1);*/
//char rpipe[20];
//char wpipe[20];
// Convert read side of pipe to string and pass as an argument to the child process. in spawnl function
// itoa( p1[PIPE_READ], rpipe, 10 );
// Convert read side of pipe to string and pass as an argument to the child process. in spawnl function
//itoa( p2[PIPE_WRITE], wpipe, 10 );
// Preparing the redirection of stdin and stdout
int s1, s2 ;
s1 = dup(0) ;
s2 = dup(1) ;
close( 0 ); /*** slave - stdin ***/
if( dup2(p1[PIPE_READ], 0) == -1 )
myerror(4) ;
close( 1 ); /*** slave - stdout ***/
if( dup2(p2[PIPE_WRITE], 1) == -1 )
myerror(5) ;
/*
close( 2 ); // slave - stderr
if( dup2(fd2, 2) == -1 )
myerror(6) ;
*/
fprintf(stderr, "Starting the program singular.exe\n");
fflush(stderr);
//Sleep(10000) ;
int pid ;
//char buff[256]="2+3;\n";
//pid = spawnl(_P_NOWAIT,"C:\\Archivos de programa\\bin\\cat.exe","cat.exe",NULL) ;
pid = spawnl(_P_NOWAIT,"C:\\Archivos de programa\\usr\\local\\Singular\\2-0-5\\ix86-Win\\Singular.exe", prog, "-q",NULL) ;
if ( pid == -1 )
myerror(7) ;
close( 0 ); /*** slave - stdin ***/
if( dup2(s1, 0) == -1 )
myerror(40) ;
close(s1);
close( 1 ); /*** slave - stdout ***/
if( dup2(s2, 1) == -1 )
myerror(50) ;
close(s2);
close(p1[PIPE_READ]);
put(p1[PIPE_WRITE]);
close(p2[PIPE_WRITE]);
get(p2[PIPE_READ]);
//sleep(5000);
printf("This is never reached\n");
printf("PID: %d\n", pid);
TerminateProcess( (HANDLE) pid, 1 ) ;
CloseHandle( (HANDLE) pid ) ;
printf("Process %d is terminated\n", pid);
return 0;
}
Navegación
[*] Página Anterior
Ir a la versión completa