Does the CPU freeze the parent and start executing the child process right away when this gets called? Or does it just create a child process image, then resume the parent process where the child is ready for execution? Thanks
Does the CPU freeze the parent and start executing the child process right away when this gets called? Or does it just create a child process image, then resume the parent process where the child is ready for execution? Thanks
That is more or less implementation defined. The scheduler may first put either the parent or the child process on the cpu.
We have no idea what happens. The fork system call requests the operating system to make another process and execution continues from there. In my opinion, the way fork should be called is:
Code:int pid = fork(); if(pid == -1) { // Some kind of error handling for fork() failure } // In the child process, the value of pid == 0. So pretend // like we're running the same code in a parallel universe // in which pid == 0 else if(pid == 0) { // Stuff to do in child process. Maybe something like execvp()? } // In the parent process, the value of pid is the process ID of // the child. else { // Business as usual }
> Does the CPU freeze the parent and start executing the child process right away when this gets called?
What happens if your machine has only one CPU?
What happens if your machine has more than one CPU?
The latter could be running both at the same time.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
What if we're talking just 1 CPU where only a single process can be running at a time?
For my assignment we are writing a unix variant operating system based on something like this where the child appears to only get created then execution resumes inside the parent. The only way child gets executed after fork is if we explicitly context switch to it by calling a switch.
I'm not sure if this is what is actually done on Red Had or Ubuntu linux flavors? Or this only happens because it's a single CPU 1 running proc at a time environment and for a multiple running process environment, both would be running even if that environment has only 1 CPU?
Read the manual pages for your system.
In the absence of any positive statement like "child always runs first", then you should assume anything can happen.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.