piping to c

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);
}
}
 
J

Jens Thoms Toerring

Igna said:
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
 
L

Lew Pitcher

Igna said:
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

#include <unistd.h>

Non-standard header. Implies POSIX/Unix code; followup in Unix
development forum

char chaine[7];

Uninitialized array - caution required.
int status;

int main( int argc, char ** argv )
{
/* create the pipe */
int pfd[2];

Uninitialized array - caution required.

int pid;
if ((pid = fork()) < 0)

Non-standard function fork() - Implies POSIX/Unix code; followup in
Unix development forum (requires additional include files)
{
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

}

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
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

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

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

printf("CHAINE FILS %s\n",chaine);

Improper argument passed to function printf() - array chaine[] has not
been initialized with any value

//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

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

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
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

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
wait(&status);

Non-standard function wait() - Implies POSIX/Unix code; followup in
Unix development forum (requires additional include files)


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

HTH
 
M

matevzb

char chaine[7];
Uninitialized array - caution required.
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."
 
L

Lew Pitcher

matevzb said:
char chaine[7];
Uninitialized array - caution required.
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
char chaine[7];
Array not explicitly initialized - caution required as implicit
initialization may not satisfy your later use of the array

[snip]
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
 
K

Keith Thompson

Lew Pitcher said:
matevzb wrote: [snip]
You are correct. I goofed, and gave an incomplete (and thus inaccurate)
response.

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

[snip]
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 "".
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top