make a pipe perl2C

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 if it is working I will
use it for the rest of the prgram. 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);
}
}
 
W

Walter Roberson

Hello. I have to write a pipe to joint a GUI in perl and a simulation
program in c.


Pipes and GUIs and perl cannot be done in standard C.
Try comp.unix.programmer for your questions. (But first, read the
replies from when you posted the question the first time.)
 
L

loic-dev

Hello Ignazio,
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 if it is working I will
use it for the rest of the prgram. 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.

This is outside of topic for a NG like comp.lang.c.
comp.unix.programmer would be more appropriate. Anyway, I am missing
the pipe creation in your code.

HTH,
Loic.
#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];

add here:
if ( pipe(pfd)==-1) {
printf("pipe failed\n");
return 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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top