What is fork()

Discussion in 'Tech Discussion' started by Zaart, Jan 1, 2018.

  1. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    yo guys, i was coding for school project and i was allowed to use fork(), can someone explain me what it do?
    i tried reading the man but it didn't realy explain whatit does, just how it work.
    thanks in advance~
     
  2. MangoGuy

    MangoGuy Rambling Mango

    Joined:
    Apr 15, 2016
    Messages:
    7,625
    Likes Received:
    8,697
    Reading List:
    Link
    Since you are allowed to use a forum...you should also be allowed to use Google? Just try a coding website..
     
  3. lnv

    lnv ✪ Well-Known Hypocrite

    Joined:
    Jan 24, 2017
    Messages:
    7,702
    Likes Received:
    9,044
    Reading List:
    Link
    It is a form of multithreading but instead of creating a subprocess, your duplicating the original process and running them in parallel.

    The way forks and threads operate depends on OS or programming language you use.
     
  4. Trane

    Trane Well-Known Member

    Joined:
    Feb 20, 2016
    Messages:
    1,062
    Likes Received:
    497
    Reading List:
    Link
    ()=paranthesis
    []= brackets

    fork=??
     
  5. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    well yeah but as you can see coders are everywhere!
    also finding this sort of information can be time consuming
     
    AliceShiki and MangoGuy like this.
  6. Shance

    Shance 『Trying to evade the wall in the front』『Failing』

    Joined:
    Dec 2, 2015
    Messages:
    2,757
    Likes Received:
    2,048
    Reading List:
    Link
    [​IMG]
     
    AliceShiki and SayMrrp like this.
  7. Handsonic

    Handsonic Active Member

    Joined:
    Feb 16, 2016
    Messages:
    5
    Likes Received:
    14
    Reading List:
    Link
    The best way to think of fork() is to compare it to creating a new tab in your web browser.

    Forking creates a "child" process (new tab) that inherits the memory stack from its "parent" process (the original tab).

    The only real difference is that the memory between your child process and the parent process is not automatically shared.

    Calling fork will usually return you a 0 (process id/int) if you are in the child process or the nonzero process id of the child in the parent process. Here is some sample code:

    int pid = fork();
    if (pid == 0) {
    /* do child process things */
    } else if (pid < 0) {
    /* uh-oh an error*/
    } else {
    /* pid > 0 */
    /* do parent process things */
    }
     
  8. static7s

    static7s [Lightning Origin Slime][Void Prince][Zappy Doom]

    Joined:
    Dec 12, 2016
    Messages:
    116
    Likes Received:
    441
    Reading List:
    Link
    Not really. It seems like you're just lazy...which is fine. Part of the problem is that its kinda difficult to debug multi-process programs using just gdb, as while it has the ability to do so, it's method of doing so isn't exactly user friendly *thinks of his own assignment from about 2 months ago* If you think it might help, i can give you the source code from said assignment though.
     
    Zaart likes this.
  9. Little Evil

    Little Evil Hello o/

    Joined:
    Jul 27, 2017
    Messages:
    572
    Likes Received:
    12,960
    Reading List:
    Link
    Never got child parent stuff...
    When we were supposed to learn it, I didn't pay attention and semestral project I made was made by cutting apart relevant parts of the codes we made during the semester and sewing it all together with bits of my own stuff...
    No idea how it worked, but it did.
    Done in 2h, but I learned nothing while making it :p
     
  10. jimjimgimp

    jimjimgimp Well-Known Member

    Joined:
    Jan 27, 2017
    Messages:
    108
    Likes Received:
    106
    Reading List:
    Link
  11. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    well what i fail to understand is what they mean by duplicating the processus, does it re-lauch the program or something like that?
    (its also possible that because of my headache i just fail to understand =P)
     
  12. criticalmind

    criticalmind Enter Chunni Name Here

    Joined:
    Oct 20, 2015
    Messages:
    945
    Likes Received:
    656
    Reading List:
    Link
    Sleep for a few hours and try again. Then check stackexchange.
     
    Zaart likes this.
  13. Handsonic

    Handsonic Active Member

    Joined:
    Feb 16, 2016
    Messages:
    5
    Likes Received:
    14
    Reading List:
    Link
    So at a more detailed level, when the original program (the parent process) calls fork, it **copies** all of the memory both statically and dynamically allocated memory and creates a new process. The new process will begin execution at the line after fork().

    int pid;
    pid fork();
    /* new process (child) starts execution here and parent will continue execution here*/
    if (pid == 0) { /* child */ }
    else { /* parent */ }
    ...
    /* both */

    It's important to note that the memory is COPIED and not shared. Also note that you can end execution early for either process by returning or exiting.

    EDIT: thanks @Amethyss shouldve made that more clear.
     
    Last edited: Jan 1, 2018
    Zaart, AliceShiki and static7s like this.
  14. static7s

    static7s [Lightning Origin Slime][Void Prince][Zappy Doom]

    Joined:
    Dec 12, 2016
    Messages:
    116
    Likes Received:
    441
    Reading List:
    Link
    In a way, yes.
    Think of it like this: it clones itself, and the clone is assigned all the code in block A, while the original(parent) does all the code in block B.
    Both the clone and the original have the same memories of everything up until the clone was created. Only after that point do they start to differ.
    Typically, the clone would tell the original when it's finished its task, maybe pass some information back to the original, and then disappear.
    The original would then execute some more code, and then it'd disappear too. Thus ending the program.
     
  15. lnv

    lnv ✪ Well-Known Hypocrite

    Joined:
    Jan 24, 2017
    Messages:
    7,702
    Likes Received:
    9,044
    Reading List:
    Link
    It splits off at the fork call. Like a fork.

    But again, fork can very a lot depending on programming language or operating system.
     
    Zaart likes this.
  16. SAimNE

    SAimNE Well-Known Member

    Joined:
    Dec 28, 2015
    Messages:
    775
    Likes Received:
    824
    Reading List:
    Link
    Fork() is made obsolete by spork()
     
    userunfriendly likes this.
  17. Saitama.sensei

    Saitama.sensei [[xiantian lifeform]]

    Joined:
    Jun 15, 2016
    Messages:
    2,600
    Likes Received:
    2,278
    Reading List:
    Link
    I’m guna be super lame here and say that a fork is a utensil used to consume food.
     
  18. Amethyss

    Amethyss Active Member

    Joined:
    Aug 12, 2016
    Messages:
    23
    Likes Received:
    18
    Reading List:
    Link
    when fork is call, from that point on, the parent and child will execute starting from the next line of the leftover code. From the example code Handsonic gave, after the fork, both the parent and the child will start at the if statement.
     
  19. Nicolas

    Nicolas Well-Known Member

    Joined:
    Nov 1, 2015
    Messages:
    412
    Likes Received:
    523
    Reading List:
    Link
    Process does things A B C then fork() and thing D. So in memory it has ABCD
    fork() created child that has only ABC in memory
     
  20. userunfriendly

    userunfriendly A Wild Userunfriendly Appears!

    Joined:
    Oct 23, 2017
    Messages:
    11,643
    Likes Received:
    9,869
    Reading List:
    Link
    It's where vampires sparkle...(LOL)