Find the answer to your Linux question:
Results 1 to 8 of 8
2. Can you spawn independent processes from a C Program using Linux, or are you always going to have the parent/child dependency (using fork() )? I assume that when using ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8

    Understanding Linux Processes 2


    2. Can you spawn independent processes from a C Program using Linux, or are you always going to have the parent/child dependency (using fork() )? I assume that when using the fork() call, if you kill the parent process the child is also killed. Can you create and run 4 totally independent processes from, say a main function, without weeding through several if statements to get to the 4th process? It seems to me that it should be the job of the OS to handle the scheduling of processes without the programmer having to use the if statements.

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,195
    Quote Originally Posted by josephstein View Post
    2. Can you spawn independent processes from a C Program using Linux, or are you always going to have the parent/child dependency (using fork() )? I assume that when using the fork() call, if you kill the parent process the child is also killed. Can you create and run 4 totally independent processes from, say a main function, without weeding through several if statements to get to the 4th process? It seems to me that it should be the job of the OS to handle the scheduling of processes without the programmer having to use the if statements.
    Fork() creates a child with its own PID; I should call that independent. If you kill the parent by its PID, that wouldn't affect the child at all. I'm not sure what you mean by your second statement. Why would you want a sequence of if statements? Just use fork() four times.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,448
    Quote Originally Posted by hazel View Post
    Fork() creates a child with its own PID; I should call that independent. If you kill the parent by its PID, that wouldn't affect the child at all. I'm not sure what you mean by your second statement. Why would you want a sequence of if statements? Just use fork() four times.
    Actually, if you kill the parent process, unless the child has either masked SIGHUP or has been nohup'd, then the child will be killed also.

    As for the original question:
    2. Can you spawn independent processes from a C Program using Linux, or are you always going to have the parent/child dependency (using fork() )? I assume that when using the fork() call, if you kill the parent process the child is also killed. Can you create and run 4 totally independent processes from, say a main function, without weeding through several if statements to get to the 4th process? It seems to me that it should be the job of the OS to handle the scheduling of processes without the programmer having to use the if statements.
    All processes are independent. I think you are confusing functions with processes and threads. Yes, the OS does schedule processes and threads as independent entities. Functions or blocks of code within a process or thread are serially executed, conditional statements or branches (if/else/while/for/case) applied.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,195
    Quote Originally Posted by Rubberman View Post
    Actually, if you kill the parent process, unless the child has either masked SIGHUP or has been nohup'd, then the child will be killed also.
    That's not always true. For example, if you launch a graphical application from the shell in an xterm and then exit the shell, your app will continue to run. In any case, the child does not die automatically with the parent as the OP seemed to think; it dies (if it dies) because it received a hangup signal from somewhere else (from the kernel maybe?)
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  5. #5
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by hazel View Post
    Fork() creates a child with its own PID; I should call that independent. If you kill the parent by its PID, that wouldn't affect the child at all. I'm not sure what you mean by your second statement. Why would you want a sequence of if statements? Just use fork() four times.
    The books I have been reading execute the fork() call for in the following way:
    ret = fork();
    if(ret == 0) then execute child process
    else if (ret > 0) execute parent process

    If you wanted to spawn more than one child process you would have to nest the fork() calls within the child or parent process.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,448
    Quote Originally Posted by hazel View Post
    That's not always true. For example, if you launch a graphical application from the shell in an xterm and then exit the shell, your app will continue to run. In any case, the child does not die automatically with the parent as the OP seemed to think; it dies (if it dies) because it received a hangup signal from somewhere else (from the kernel maybe?)
    That's because X-Windows (graphical) applications handle or mask those signals. Usually that's done automatically when the GUI libraries initialize the environment. Also, when the windowing environment terminates, GUI shutdown events are sent to any open GUI applications since they can no longer be signaled with SIGHUP events.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,195
    Quote Originally Posted by josephstein View Post
    The books I have been reading execute the fork() call for in the following way:
    ret = fork();
    if(ret == 0) then execute child process
    else if (ret > 0) execute parent process

    If you wanted to spawn more than one child process you would have to nest the fork() calls within the child or parent process.
    When fork is run, it creates a process which is initially an exact duplicate of the existing one, except that it has a different PID. Fork returns 0 to the child process, and the child's PID to the parent. Therefore if the return value from fork is zero, you have the child. Usually you then use an exec function to switch execution to a different program. Meanwhile the parent, for which fork returned a positive value, is allowed to continue its current program.

    You can then call fork again. It isn't nested; it's a separate call. For example:
    Code:
    ret = fork();
    /* Child exits the program at this point */
    if (ret == 0) then (execute some other program);
    /* Parent continues and spawns another child. The first child never gets to see this code so you don't need an else. */
    ret = fork();
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  8. #8
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,195
    @Rubberman
    Coincidentally I found this while browsing the libc manual. It describes what happens when a program exits.

    "If the process is a session leader that has a controlling terminal, then a SIGHUP signal is sent to each process in the foreground job, and the controlling terminal is disassociated from that session. "

    This obviously applies to a shell controlled by a Linux virtual console (ttyn); I guess you are right that it must also apply to a shell on an xterm (pts/n)
    "I'm just a little old lady; don't try to dazzle me with jargon!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •