Connecting Tech Pros Worldwide Forums | Help | Site Map

piping to c

Igna
Guest
 
Posts: n/a
#1: Jan 16 '07
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);
}
}


Lane Straatman
Guest
 
Posts: n/a
#2: Jan 16 '07

re: piping to c



"Igna" <ignleotta@gmail.comwrote in message
news:1168966393.984440.296500@l53g2000cwa.googlegr oups.com...
Quote:
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
Non-standard stuff. LS



Jens Thoms Toerring
Guest
 
Posts: n/a
#3: Jan 16 '07

re: piping to c


Igna <ignleotta@gmail.comwrote:
Quote:
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.
Sorry, but you ended up in the wrong place. fork(), dup2() etc.
aren't (standard) C functions but UNIX extensions and for this
reason off-topic here. You will find a much more receptive
audience for your problems in e.g. comp.unix.programmer.

<OT>
You don't even create a pipe using pipe(), so there's no pipe to
write to at all, just an uninitialized array of two integers. And
on top of that you first write to pfd[1] before the dup2() to make
it the standard output of the program. But for more details please
ask in comp.unix.programmer.
</OT>
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Lew Pitcher
Guest
 
Posts: n/a
#4: Jan 16 '07

re: piping to c



Igna wrote:
Quote:
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>
Non-standard header. Not used in code

Quote:
#include <unistd.h>
Non-standard header. Implies POSIX/Unix code; followup in Unix
development forum

Quote:
char chaine[7];
Uninitialized array - caution required.
Quote:
int status;
>
int main( int argc, char ** argv )
{
/* create the pipe */
int pfd[2];
Uninitialized array - caution required.

Quote:
>
int pid;
if ((pid = fork()) < 0)
Non-standard function fork() - Implies POSIX/Unix code; followup in
Unix development forum (requires additional include files)
Quote:
{
printf("fork failed\n");
return 2;
Non-standard return value from main() - only 0, EXIT_SUCCESS and
EXIT_FAILURE are acceptable - value implies POSIX/Unix code; followup
in Unix development forum

Quote:
}
>
if (pid == 0)
{
/* child */
close(pfd[0]);
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[0] has not been
initialized with any value
Quote:
write(pfd[1],"bonjour",7);
Non-standard function write() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function write() - pfd[1] has not been
initialized with any value

Quote:
dup2(pfd[1], 1); /* connect the write side with stdout */
Non-standard function dup2() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function dup2() - pfd[1] has not been
initialized with any value

Quote:
close(pfd[1]); /* close the write side */
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[1] has not been
initialized with any value

Quote:
printf("CHAINE FILS %s\n",chaine);
Improper argument passed to function printf() - array chaine[] has not
been initialized with any value

Quote:
//return 3;
exit(0);
}
else
{
/* parent */
close(pfd[1]); /* close the unused write side */
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[1] has not been
initialized with any value

Quote:
dup2(pfd[0], 0); /* connect the read side with stdin */
Non-standard function dup2() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function dup2() - pfd[0] has not been
initialized with any value

Quote:
read (pfd[0],chaine,7);
Non-standard function read() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function read() - pfd[0] has not been
initialized with any value
Quote:
close(pfd[0]); /* close the read side */
Non-standard function close() - Implies POSIX/Unix code; followup in
Unix development forum
Improper argument passed to function close() - pfd[0] has not been
initialized with any value

Quote:
printf("CHAINE PARENT %s\n",chaine);
Improper argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with any value
Quote:
wait(&status);
Non-standard function wait() - Implies POSIX/Unix code; followup in
Unix development forum (requires additional include files)

Quote:
}
Caution: Missing return value from main() - only 0, EXIT_SUCCESS and
EXIT_FAILURE are acceptable
Quote:
}
HTH
--
Lew

matevzb
Guest
 
Posts: n/a
#5: Jan 16 '07

re: piping to c


On Jan 16, 7:29 pm, "Lew Pitcher" <lpitc...@sympatico.cawrote:
<snip>
Quote:
Quote:
char chaine[7];
Uninitialized array - caution required.
<snip>
Quote:
Quote:
printf("CHAINE FILS %s\n",chaine);
Improper argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with any value
Are you sure about these two? "chaine" is not automatic, it falls under
external linkage and thus (quotes from C89):
1. "An object declared with external or internal linkage, or with the
storage-class specifier static has static storage duration."
2. "If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant."
--
WYCIWYG - what you C is what you get

Lew Pitcher
Guest
 
Posts: n/a
#6: Jan 16 '07

re: piping to c



matevzb wrote:
Quote:
On Jan 16, 7:29 pm, "Lew Pitcher" <lpitc...@sympatico.cawrote:
<snip>
Quote:
Quote:
char chaine[7];
Uninitialized array - caution required.
<snip>
Quote:
Quote:
printf("CHAINE FILS %s\n",chaine);
Improper argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with any value
Are you sure about these two? "chaine" is not automatic, it falls under
external linkage and thus (quotes from C89):
1. "An object declared with external or internal linkage, or with the
storage-class specifier static has static storage duration."
2. "If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant."
You are correct. I goofed, and gave an incomplete (and thus inaccurate)
response.

I should have said
Quote:
char chaine[7];
Array not explicitly initialized - caution required as implicit
initialization may not satisfy your later use of the array

[snip]
Quote:
printf("CHAINE FILS %s\n",chaine);
Questionable argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with a printable string

--
Lew

Martin Ambuhl
Guest
 
Posts: n/a
#7: Jan 16 '07

re: piping to c


Lew Pitcher wrote:
Quote:
Igna wrote:
Quote:
Quote:
>char chaine[7];
>
Uninitialized array - caution required.
Not true. This array is declared outside of any function, so is
initialized to all zeros. Improper 'correction' - caution required.
Keith Thompson
Guest
 
Posts: n/a
#8: Jan 16 '07

re: piping to c


"Lew Pitcher" <lpitcher@sympatico.cawrites:
Quote:
matevzb wrote:
[snip]
Quote:
You are correct. I goofed, and gave an incomplete (and thus inaccurate)
response.
>
I should have said
>
Quote:
>char chaine[7];
Array not explicitly initialized - caution required as implicit
initialization may not satisfy your later use of the array
>
[snip]
Quote:
> printf("CHAINE FILS %s\n",chaine);
Questionable argument passed to function printf() - unless non-standard
function read() has altered it's contents, array chaine[] has not been
initialized with a printable string
If chaine[] has not been altered, it *does* contain a printable
string. It happens to be "".

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Closed Thread