#include "stdafx.h"
#include <fcntl.h>
#include <io.h> // to use _pipe function
#include <stdlib.h> // to use itoa function.
#include <process.h> //to use NO_WAIT argument of spawn function, execl
#include <Windows.h>
#define PIPE_READ 0
#define PIPE_WRITE 1
int p1[2]; // pipe1
int p2[2]; // pipe2
int m_in;
int m_out;
int state;
int endstate;
int main(int argc, char* argv[])
{
fprintf(stderr, "Starting slave program ....\n");
fflush(stderr);
char otherProgramm[] = "muff.exe";
//*create two pipes p1 and p2, return true if the pipes could not be created.
if( _pipe(p1,256,O_TEXT)==-1 || _pipe(p2,256,O_TEXT)==-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 );
// starting another program
if( (state = spawnl(P_NOWAIT, otherProgramm, rpipe, wpipe, NULL))==-1 )
//if( (state = spawnl(P_NOWAIT, otherProgramm, otherProgramm, 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
perror("The process can not be created....\n Spawn failed");
fflush(stderr);
}
fprintf(stderr, "creating a new process....\n");
fflush(stderr);
//assign to m_in and m_out the file descriptors of the pipe for the parent process.
m_in = p2[PIPE_READ];
m_out = p1[PIPE_WRITE];
if (cwait(&endstate, state, WAIT_CHILD)== -1)
{
perror("cwait failed");
}
// close descriptors from the child process.
close(p2[PIPE_WRITE]);
close(p1[PIPE_READ]);
TerminateProcess( (HANDLE) state, 1 );
CloseHandle( (HANDLE) state );
fprintf(stderr, "End of slave program....\n");
fflush(stderr);
return 0;
}
[color=red]
muff.exe[/color]
#include "stdafx.h"
#include <stdio.h>
#include <io.h>
#include <process.h>
#include <stdlib.h>
int fd1,fd2;
void error(int errorCode)
{
fprintf(stderr, "Return with error code %d\n", errorCode);
exit(errorCode);
}
int main(int argc, char* argv[])
{
fprintf(stderr, "Starting muff program ....\n");
fflush(stderr);
fprintf(stderr, "the number of arguments are %d \n",argc);
fflush(stderr);
if (argc !=2)
{
perror("the number of arguments are <> 2");
exit(2);
}
// Preparing the redirection of stdin and stdout
// assign the file descriptor f1 to the standar input. fd1 = p1[READ]
//close( 0 ); /*** slave - stdin ***/
if(close(0) == -1 )
{
perror("can not close standard input");
}
//sscanf(argv[0],"%d",&fd1);
//dup2(fd1, 0);
if (sscanf(argv[0],"%d",&fd1) != 1)
{perror("sscanf does not work");}
if (dup2(fd1, 0)==-1){
perror("dup2 does not work");}
// assign the file descriptor f2 to the standar output. fd2 = p1[WRITE]
//close( 1 ); /*** slave - stdout ***/
if(close(1) == -1 )
{
perror("can not close standard output");
}
//sscanf(argv[1],"%d",&fd2);
//dup2(fd2, 1);
if (sscanf(argv[1],"%d",&fd2) != 1)
{perror("sscanf does not work");}
if (dup2(fd2, 1)==-1){
perror("dup2 does not work");}
/*
// 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 Singular.exe\n");
fflush(stderr);
int nexec;
//nexec = execl("Singular.exe","Singular.exe",NULL);
nexec = execl("C:\\Archivos de programa\\usr\\local\\Singular\\2-0-5\\ix86-Win\\Singular.exe","Singular.exe",NULL);
fprintf(stderr, "Ending the muff program \n");
fflush(stderr);
return 0;
}