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~
Since you are allowed to use a forum...you should also be allowed to use Google? Just try a coding website..
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.
well yeah but as you can see coders are everywhere! also finding this sort of information can be time consuming
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 */ }
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.
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
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)
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.
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.
It splits off at the fork call. Like a fork. But again, fork can very a lot depending on programming language or operating system.
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.
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