ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void)
{
int i = 0;
for (i = 0; i < 2; i++) {
fork();
if (i == 0)
printf("After f %d\n", getpid());
sleep(1);
}
return 0;
}
Code:
acker@dido /tmp $ ./a.out
After f 19961
After f 19960
acker@dido /tmp $ ./a.out | cat
After f 19966
After f 19966
After f 19964
After f 19964
If i redirect the output to a file I get the same output as with the pipeline. Any ideas or pointers (to a specialised list/forum) are welcomed.
Last edited by acker; 12-03-2006 at 12:39 PM.
Reason: RESOLVED!
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().
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().
What things am I doing so wrong?
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.
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.
I don't want to know if i'm the parent or in the child. For me they are just processes. (in this example). I may have other mechanisms to synchronize but that is beyond the problem presented here.
Quote:
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.
Educational.
Quote:
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.
For example
Code:
int
main(void)
{
3; 4; 5;
return 0;
}
compiles ok because is a valid C program (warnings if -Wall is used). Does nothing but does not have "strange effects" although it may seem strange.
Ok, code rewritten
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void)
{
int i = 0;
int res = 0;
for (i = 0; i < 2; i++) {
res = fork();
if (res == -1) {
printf("Error, can't create the little baby\n");
exit(1);
} else if (res == 0) {
if (i == 0) {
printf("Kid: after f %d\n", getpid());
}
} else {
if (i == 0)
printf("Parrent: after f %d\n", getpid());
}
sleep(1);
}
return 0;
}
Approximately same output...
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 )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.