Link to home
Start Free TrialLog in
Avatar of JCW2
JCW2

asked on

Pipes and character arrays; Grandchild processes

How would you send a character array through a pipe in the C programming language?

Also, how do you have a process wait for processes that are children of -its- processes?

 I would prefer it if you wrote it out here rather than just posted a link.

Update:

I happen to be using this kind of code:

http://en.wikibooks.org/wiki/Distributed_Systems#Pipes
ASKER CERTIFIED SOLUTION
Avatar of Let_Me_Be
Let_Me_Be
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JCW2
JCW2

ASKER

What would cause me to get output from the pipes, albeit random trash?
Did you use the code I provided ? I tested it and it works flawlessly for me ? Actually a lot of things could cause the pipe to not work properly. If you're not using my code, please provide the one you use. And could you tell me which compiler you're using ?
Avatar of phoffric
Hi mr-cax,
I got your code to work on cygwin. I was just wondering why the waitpid works with only one argument. In my (old) book, and in this reference: http://www.opengroup.org/onlinepubs/009695399/functions/wait.html , I see that the waitpid takes 3 arguments. Could you provide a more modern link for my reference that shows the waitpid with only one argument. Thanks. Also, could you let me know what OS and compiler are you using?
Hi phoffric,

you're right! This is what happens when you don't specify all #include <...> statements and use some copy/pasted code as a basis :)

waitpid takes 3 parameters, but as I forgot to add #include <wait.h> statement, the compiler didn't warn be about that. And because calling convention is __cdecl by default and that implies it's the caller that cleans the calling stack, the code was executing, but not really waiting for the child (as the third parameter was just some random memory on the stack) plus this could probably cause some segfault because status pointer is random too.

Here is the corrected version of the code (I introduce a delay in child to check if waitpid was working)  :

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>

int main() {
int p[2];
int cpid;
char buf1[64] = "Simple string";
char buf2[64];
int len1, len2;
int status;

len1 = strlen(buf1);

pipe(p); /* call by address. */
cpid = fork();
if (cpid < 0) exit(1);

/* child process */
if (cpid == 0) {
	close(p[1]); /* child won't need to write, so close. */
	read(p[0], &len2, sizeof(int));
	read(p[0], buf2, len2 + 1);
	sleep(1);
	printf("%s\n", buf2);
	close(p[0]);
	_exit(0); /* read man 3 exit and man _exit to understand the difference. */
}

/* parent */
else {
	close(p[0]); /* parents can be so close-minded! */
	write(p[1], &len1, sizeof(int));
	write(p[1], buf1, len1 + 1);
	close(p[1]);
	waitpid(cpid, &status, WUNTRACED | WCONTINUED);
}
}

Open in new window

By the way I'm using gcc 4.4.1 under ubuntu 9.10
Avatar of JCW2

ASKER

Thank you for your help.