Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Lots of reasons, having the Parent PID as the controlling process.. IE: Daemon.. Server/Client model, PPID manages incoming connections, and creates a child process to manage that new connections, to free up the parent to allow new connections.
That's right, but the most common sequence is for the child process to follow a different branch in the code, set up a few things (mainly closing and opening files, etc.), and then call exec(), which completely replaces the parent's image with something new. Aside from process number 1 (init), that's how every process in the system begins life.
Can any one show a code example for generating two or more independent processes w/o children
and another code sample for a father process and a child process .
By the way can a father process have two or more children ?
Can any one show a code example for generating two or more independent processes w/o children
and another code sample for a father process and a child process .
Sure. Here is very rudimentary code to create an independent process by breaking the inheritance chain:
Code:
pid1 = fork();
if(pid1) waitpid(pid1, NULL, 0); /* Parent waits for 1st child */
else { /* Child process continues here */
if(fork()) exit(0); /* 1st child forks again and exits */
/* The orphaned 2nd child process continues here */
.
.
.
}
That orphaned child process gets adopted by the init process (PID 1). Note that the waitpid() call in the original parent is important to keep that short-lived 1st child from hanging around as a zombie. Note: Besides error checking, this code also omits a lot of other things you would want to do to create a truly independent process (setpgrp(), setting up the standard file descriptors, breaking the association with the controlling tty, ...).
The usual parent<->child arrangement is simpler. Just don't do the second fork(), and the parent has the option of (a) waiting for the child immediately, (b) continuing processing and doing the wait() later, or (c) continuing processing and exiting without doing a wait(). For case c, the child process then gets adopted by init, just as in the first example.
Code:
pid1 = fork();
if(pid1 == 0) { /* Child process continues here */
.
.
.
}
Last edited by rknichols; 05-07-2012 at 08:40 AM.
Reason: added note
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.