Usually, when I need to use fork in C, I do something like this:
pid_t p = fork(); if(p == 0) { /* do child stuff */ } else { /* do parent stuff and pray there wasn't an error */ }
It occurred to me that I can remove the extra variable and use:
if(fork() == 0) { /* child */ } else { /* parent/pray */ }
Incorrect error handling aside (why) does it work / not work?
What you offer will certainly work. However, error handling is not required in any well-organized application. The following implementation pattern is similarly concise and also handles errors. In addition, it stores the value of fork () return in the pid variable if you want to use it later in the parent to, say, wait for the child.
switch (pid = fork()) { case -1: /* Failure */ /* ... */ case 0: /* Child */ /* ... */ default: /* Parent */ /* ... */ }
, , . , , , , ( , , PID , PID , ). PID, , .
, -1 , forking, , .
. , . .
int p; if((p = fork()) == 0) { /* child */ } else { /* parent/pray */ }
C, , fork - . , , - . , PID, , , waitpid ..
Source: https://habr.com/ru/post/1697521/More articles:How to execute a dynamic SQL query against MS Access 2003 through VBA? - access-vbaIs there a .Net replacement for GetAsyncKeyState? - apiChange column length - sql-serverpositioning of three divs with css - htmlPHP log / graph runtime - phpHow to find out if the maximum file size was reached when using a stream? - c ++Best way to handle multilingual support? - c #How to search archive files using Perl - searchFinalizer is running while its object is still in use - garbage-collectionDifferences between Ruby Virtual Machines - ruby | fooobar.comAll Articles