Re: Way cool



Bill Cunningham wrote:
I don't know what this little program is doing but it's fun to watch.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

main() {
int fd[2],num;
pid_t id;
id=num;
printf("%i",num);
pipe(fd);
id=vfork();
if(id==0) {
printf("success\n");
printf("%i",num);
}
close(fd[1]);
close(fd[0]);
return 0;
}

I wonder what the numbers that are changing is? The number that doesn't change must be the process id.

What numbers change? The first printf prints an uninitialized local variable. On my system (Kubuntu 7.10), this just prints 0, but on other systems it may print any other value, and as this doesn't change until the next printf, this also prints 0, the fact that it's done in the child doesn't matter.
The pid is nowhere output.

It segfaults at the end (unless it's run by gdb ;-) This is because the child returns first rather than _exit(2)s (from man vfork):
"The child must not return from the current function or call exit(3), but may call _exit(2)."

Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
.



Relevant Pages