The following introduces the two returns of fork. How can a function call return two values? It should be emphasized here that instead of returning two values, it returns two times and one value at a time, so it is still In line with the characteristics of the return value of the function-only one value can be returned.
Generally, when the PCB of a process is created, this process also exists, then there are already two processes, one is the parent process, and the other is a newly created child process. Afterwards it returns 1. Which is a non-zero return, which takes us to the first printf. After the printf we call jmpback.
Which is the actual hack. This function makes it so that it appears that saveesp returns a second time. It does this by pushing the saved return address down the stack and doing a ret. The ret will pop the address from the stack and jump to it. The return code is set to zero this time around. So when we 'reach' back to our C routine it appears we've just came back from saveesp with zero return value.
Thus the second printf is reached. If you're interested in this sort of hacks you should read a bit more about setjmp and longjmp from the C standard that are used to implement exception handling. Have a look here at lines and it's pretty much the same C code as above. And then have a look at the assembly code here at line is the savecpu function that returns the first time on suspend and at line is where we return the second time around when we come back on resume.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. We are modelling non-deterministic behavior and need the program to fork if there is more In my case, I OpenBlas, the issue is described here and here. After a fork, which has succeeded, errno is set at 0 in the father's process as expected , Does anyone have a clue to explain that undesired behavior?
I need to implement my shell that handles multiple pipe commands. For example I need to be able to handle this: ls grep -i cs sort uniq The parent process gets the child's process ID returned to it, the child gets 0 - an invalid process ID, so the code can tell it is the child.
The child is a new process, running the same code and is at the same place in the code as the parent that spawned it. If you read, build, and run the following program you should get a better idea of what is going on. The reason that the fflush stdout is needed is that since the process is duplicated by fork that means that the buffering done for stdout by stdio is duplicated as well.
The key insight here is to think about the fact that after a fork you have really two copies of your program. These are two processes, running the same exact copy of your code, and the execution pointer is exactly at the same line of code i.
The OS arranges for the fork to return in the parent process with the pid of the child, and for it to return in the child process with zero if things go well. Right after a fork system call you simply have two independent processes executing the same code, and the returned pid from fork is the only way to distinguish which process are you in - the parent or the child.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
How is it possible for fork to return two values?
0コメント