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.
Hi all,
i want to write a code to spawn 3 processess from a process which does 3 different tasks.... and to test that, i wil kill the spawned process and again it has to be spawned.. how to do this
thanks....
Hi,
when a subprocess dies the kernel sends the SIGCHLD signal to the parent process. So, in the parent process you can catch that signal, and then act upon it. In this case, you want to catch the signal and mark some flag so that later in your main program, you can fork a new process to replace the recently died process.
Check the "signal" and "fork" man page.
Thanks wjevans_7d1,
ya this is not a homework, in my project i have to initiate 3 processess and have to keep track of those processess(i.e. whether they are dead or alive ) and i know only basics of linux...so asking help....
Making 3 children is the same as making 1 child, but that you do it three times, and NOT in the child processes.
Pseudo code:
Code:
main (...)
{
spawn3();
child_process_workflow();
}
spawn3()
{
result = fork();
if ( result == we are in the child )
return;
result = fork();
if ( result == we are in the child )
return;
result = fork();
if ( result == we are in the child )
return;
parent_process_after_fork_workflow();
}
parent_process_after_fork_workflow ()
{
...
}
child_process_workflow ()
{
...
}
When you make a call to fork, Linux creates a new version of the process which is a child of the original. Both process have the same code in them. After the fork, they are both at the same point in the program - returning from the fork call. All the code is shared. Almost all state is shared. There are only a few differences, for example the process ID and parent process ID, and importantly, the return value from the fork call. In the parent process, the return value of the fork() call is the PID of the child process, but in the child process, 0 is returned.
Please read the fork manual page carefully - it seems you have some pretty big mis-conceptions about what fork is doing. You may or may not have to make sure some documentation package is installed to get these manual pages. Try this:
Code:
man 2 fork
And if it doesn't work, browse fedora's s/w installer and try to find them. You won't get far with C without a good reference handy, and the manual pages are a very convenient, searchable resource.
Thanks for the suggestion matthewg42,
i know these details what u told about fork() but my doubt was how to spawn 3 different processess from the parent say.....
in this code the output is hello is printed 14 times....because after the first fork till the end everything is clonned(including other 2 fork calls)....
my question is how to make the fork stop clonning...
is this possible ????
or else i need to call 3 functions seperately
Code:
spawn_child1()
spawn_child2()
spawn_child3()
and inside these function definitions i have to fork()
so, from the parent process can i spawn 3 different processess ??
After the first fork, you have two processes, each of which will execute the next instruction (writing a message), then you ask them both to fork, making 4 etc... You must test the result of the fork call, and in the parent, not fork again.
Thanks for the suggestion matthewg42,
i know these details what u told about fork() but my doubt was how to spawn 3 different processess from the parent say.....
in this code the output is hello is printed 14 times....because after the first fork till the end everything is clonned(including other 2 fork calls)....
my question is how to make the fork stop clonning...
is this possible ????
You have to check to make sure you’re not in the child process before forking again. You can easily do this either in a loop or a separate function. For a loop, e.g.,
Code:
#include<unistd.h>
#define N_FORKS 3
int main()
{
int i;
for(i = 0; i < N_FORKS; i++) {
if(!fork()) // i.e., if we’re in the child
break;
}
write(1, "hello\n", 7);
return 0;
}
should give you four “hello”s (one for the parent, and one for each of three children). The function approach was shown by matthew, and is probably more useful if you’re actually interested in doing something differently in each process.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.