I
Igna
Hello. I have to write a pipe to joint a GUI in perl and a simulation
program in c. I have read all the docs found in perl.com and now I am
trying to make a test with this simple program. It seems working but it
does not write "bonjour" at the end. Can anybody tell me where my
mistake is? I will be happy for any sample of code for a pipe between c
and perl sent to me too.
Thanks in advance,
Ignazio
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
char chaine[7];
int status;
int main( int argc, char ** argv )
{
/* create the pipe */
int pfd[2];
int pid;
if ((pid = fork()) < 0)
{
printf("fork failed\n");
return 2;
}
if (pid == 0)
{
/* child */
close(pfd[0]);
write(pfd[1],"bonjour",7);
dup2(pfd[1], 1); /* connect the write side with stdout */
close(pfd[1]); /* close the write side */
printf("CHAINE FILS %s\n",chaine);
//return 3;
exit(0);
}
else
{
/* parent */
close(pfd[1]); /* close the unused write side */
dup2(pfd[0], 0); /* connect the read side with stdin */
read (pfd[0],chaine,7);
close(pfd[0]); /* close the read side */
printf("CHAINE PARENT %s\n",chaine);
wait(&status);
}
}
program in c. I have read all the docs found in perl.com and now I am
trying to make a test with this simple program. It seems working but it
does not write "bonjour" at the end. Can anybody tell me where my
mistake is? I will be happy for any sample of code for a pipe between c
and perl sent to me too.
Thanks in advance,
Ignazio
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
char chaine[7];
int status;
int main( int argc, char ** argv )
{
/* create the pipe */
int pfd[2];
int pid;
if ((pid = fork()) < 0)
{
printf("fork failed\n");
return 2;
}
if (pid == 0)
{
/* child */
close(pfd[0]);
write(pfd[1],"bonjour",7);
dup2(pfd[1], 1); /* connect the write side with stdout */
close(pfd[1]); /* close the write side */
printf("CHAINE FILS %s\n",chaine);
//return 3;
exit(0);
}
else
{
/* parent */
close(pfd[1]); /* close the unused write side */
dup2(pfd[0], 0); /* connect the read side with stdin */
read (pfd[0],chaine,7);
close(pfd[0]); /* close the read side */
printf("CHAINE PARENT %s\n",chaine);
wait(&status);
}
}