1

I'm trying to understand how to create fork trees,is there any simple way to understand that?

Exemple:

    include<stdio.h>
include<unistd.h>
void main(){

fork();
if fork();
if fork();
fork();
sleep(10);

}
Laamiri Oussema
  • 527
  • 2
  • 8
  • 25
  • 1
    This isn't clear - what specifically don't you understand? – Oliver Charlesworth May 15 '17 at 08:43
  • Here you go, buddy... [GeeksForGeeks](http://www.geeksforgeeks.org/fork-and-binary-tree/) – Gaurav Pathak May 15 '17 at 08:45
  • @GauravPathak I'm realy new, have you a basic exemples ? – Laamiri Oussema May 15 '17 at 08:59
  • Please answer this [question](http://stackoverflow.com/questions/43974927/creation-of-process-with-fork?noredirect=1#comment74979497_43974927). – Gaurav Pathak May 15 '17 at 09:03
  • It's not something I do often, (well, ever, actually), and so, if I may ask, what are fork trees like this used for? – ThingyWotsit May 15 '17 at 09:09
  • 2
    Not sure what language that is, but it's not C. – pat May 15 '17 at 09:21
  • On Unix (as in the tag), [`main()` always returns `int`](http://stackoverflow.com/questions/204476/). You need `#include`, not `include`; you need `if (fork()) …`. It isn't clear what you want to happen after the conditional forks. All-in-all, this is not valid C on numerous grounds. Please remember to post an MCVE ([MCVE]). It'd probably be worth including some sort of output in the code, if only so you know about the processes running. Multiple uses of `printf("%d\n", (int)getpid());` would be one possibility, for example; including the return value from `getppid()` might be useful too. – Jonathan Leffler May 15 '17 at 14:16
  • @ThingyWotsit: this sort of process tree is used to confuse students. In practice, you don't create process trees like this except coincidentally as part of other operations. For example, a shell might end up with a a process structure like this, because it runs scripts or programs that lead to that structure, but (except for academic teaching purposes) you don't go out of your way to do this. Don't get me wrong: as a pedagogic device, such exercises are useful to teach/check what `fork()` does (if the code is sensibly instrumented). But they're teaching exercises, not 'real world'. – Jonathan Leffler May 15 '17 at 14:57
  • @JonathanLeffler OK, I guessed it was probably a homework dump, but could not be sure:( – ThingyWotsit May 15 '17 at 16:17

2 Answers2

0

From linux manual:

fork() creates a new process by duplicating the calling process.

Basically it creates a new process, referred to as the child, which is an exact duplicate, with same code, of the calling process, referred to as the parent, except for few things (take a look at man fork). It returns the child process ID if you're the parent, 0 if you're the child or -1 (and sets errno) to the parent on failure. Here's a code example of a fork tree:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

/*
 * I'm going to create a fork tree
 * 
 */


int main(){
    pid_t pid; /*Use it for fork() calls*/
    pid = fork(); /*Generating the first child*/
    if(pid == 0){ /*I'm the child*/
        pid_t pid_child = fork();
        if(pid_child == 0){ /*I'm the grandchild*/
            printf("I'M THE GRANDCHILD\n");
            return 0; /*Terminates the new process*/
        }else if(pid_child > 0){ /* I'm the child*/ 
            waitpid(pid_child,NULL,0);
            printf("I'M THE CHILD\n");
            return 0; /*Terminates the new process*/
        }
    }else if(pid > 0){ /*I'm the parent*/
        waitpid(pid,NULL,0); /*Waiting for the child*/
        printf("I'M THE PARENT\n");
    }
    return 0;
}
simo-r
  • 733
  • 1
  • 9
  • 13
0

Every time you are calling fork() you are creating a Child that has the exact code the father has until this moment, but its own memory map.

Then you have to 2 processes with the same code. If you want to make them do something different you have to use fork()'s return. Fork returns the pid of the child and ''assigns'' it at Father's memory. Through that mechanism Father can refer to the child using its pid (process ID) which is only known to him. If child tries to see the exact pid created for it through fork(), it simply can't and would be zero (because fork return PID to a process for other child processes).

Example code of the above is the bellow:

void  main(void)
{
    char sth[20]="something";
    pid_t  pid;

    pid = fork(); // Create a child
    // At this line (so this specific comment if you may like) has 2 processes with the above code
    printf("I am process with ID<%ld> and i will print sth var <%s>", getpid(),sth);
    // The above printf would be printed by both processes because you haven't issued yet a way to make each process run a different code.
    // To do that you have to create the following if statement and check PID according to what said above.
    if (pid == 0) // If PID == 0, child will run the code
        printf("Hello from child process with pid <%ld>",getpid());
        printf(", created by process with id <%ld>\n",getppid());
    else          // Else the father would run the code
        printf("Hello from father process with pid <%ld>",getpid());
}

I tried to be as naive as i could. Hope it helps somehow.

Sarriman
  • 382
  • 5
  • 22
  • On Unix, `void main(void)` is unconditionally wrong. The [return type for `main()`](http://stackoverflow.com/questions/204476/) on Unix is `int`. (There's a get-out clause on Windows, but the question is tagged Unix so that get-out clause doesn't apply.) – Jonathan Leffler May 15 '17 at 14:20