fork(), different output with/without redirection. RESOLVED!
hello,
[LATER EDIT:] Better check for a shorter and cleaner example: http://www.linuxquestions.org/questi...84#post2528684 and the answer. [/LATER] Code:
#include <stdio.h> Code:
acker@dido /tmp $ ./a.out |
You're doing several things completely incorrect, its hard to know where to start. May I suggest a book that I like to tell people about, it covers in great detail this subject with lots of examples.
Advanced Programming in the UNIX Environment by W. Richard Stevens. Your main problem is you are not checking the return code of fork(). |
Quote:
My example is short and I think it should do the following: fork the main process in two (parent, and child_1) and then each process should fork in two also: parent, child_2 + child_1, child_child_1). If it will return (-1) I will not have this number of processes but I don't care cause in this simple example is not the case. |
Hi,
With fork(), it returns 0 in the child and the pid of the child in the parent. If you don't read the return code of fork() there is no reason to call it because your program doesn't know if you are the parent or the child. That's why I suggested the book because it goes into the theory of what the useful applications of fork() are, and there is no useful purpose if your software doesn't know when you are running as parent or child process after forking. Calling fork() in a loop like that is very strange, and I'm not surprised strange results happen. I've never seen that done before and can't understand what purpose it serves because it doesn't do anything useful. |
Quote:
Quote:
Quote:
Code:
int Ok, code rewritten ;) Code:
#include <stdio.h> Thanks for trying to help me but I think the "problem" is somewhere else (in my incorrect understanding of something or/and a race condition of some sort/buffer issue). I think I will try the gcc list (although this is not a compiler issue; hope I won't get too burned :) ) --ack |
GOT THE ANSWER.. yuppy!! :)
Code:
#include <stdio.h> Code:
acker@dido /tmp $ ./a.out This code has the same output with or without the "| cat| in the command: Code:
int [LATER EDIT] After realizing this I've found http://www.linuxforums.org/forum/147741-post6.html . I do have the book but it's almost 700 pages so.. :) Thanks and have a fluffy day ;) |
Yes indeed, after reading the other example that makes sense. Good find.
|
All times are GMT -5. The time now is 06:20 PM. |