In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you how to use Linux fork to create a sub-process of the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.
The fork () function creates a process that is almost identical to the original process through a system call, that is, two processes can do exactly the same thing, but if the initial parameters or the variables passed in are different, the two processes can also do different things.
1. Pid_t fork (void);
Function: create a parent-child process
Parameter: none
Return value: success: in parent process: return value is PID of child process
In the child process: return value 0 failed:-1 Note:
1) the fork function is used to create processes. Two processes are generated after fork, and each process will have a return value, so the process number of the child process (> 0) is returned in the parent process, and 0 is returned in the child process.
2) the child process copies almost all the contents of the parent process. Including code, data, PC values in system data segments, data in the stack, files opened in the parent process, etc.; but their PID and PPID are different.
3) the parent and child processes have independent address space and do not affect each other; when changing global variables and static variables in the corresponding process, they do not affect each other.
4) if the parent process ends first, the child process becomes an orphan and is adopted by the init process (at this time, the father of the child process is init), and the child process becomes a background process. (init process number is 1)
5) if the child process ends first, if the parent process is not reclaimed in time, the child process becomes a zombie process (to avoid zombie process generation)
2. Pid_t getpid (void)
Function: get the process number of the process that is calling this API
Return value: get the PID number
3. Pid_t getppid (void)
Function: get the parent process number of the process that is calling this API
Return value: get PID
4. Pid_t wait (int * status) header file
Function: blocking waits for the end of any child process. The recycled resource status is an integer pointer to an object used to save the state of the child process when it exits. If status is empty, it means that the state of omitting the child process when exiting. If status is not empty, it means saving the state of the child process when it exits. In addition, the end state of the child process can be determined by some specific macros in the Linux.
5. Pid_t waitpid (pid_t pid, int * status, int options)
Function: wait for the end of the child process and recover resources
Parameters:
(1) pid: pid > 0: only wait for the child process whose ID is equal to pid. No matter there are other child processes that have finished running and exited, waitpid will wait forever as long as the specified child process is not finished. Pid=-1: waits for any child process to exit, which is the same as wait.
(2) status: same as wait
(3) options:WNOHANG: do not block, return 0 (not received), pid number (received successfully) 0: same as wait, block the parent process and wait for the child process to exit. Failed-1, pid number returned: normal: the process number of the child process that ended, using the option WNOHANG and no child process ends: 0 error:-1 equivalent: wait (NULL) = = waitpid (- 1, NULL, 0) WEXITSTATUS (status)
6. Void exit (int status); function: end the calling process and clean up the cache before the program finishes
7. _ exit:void _ exit (int status); function: end the calling process, do not clean the cache before the end of the program Note: status is an integer parameter, you can use this parameter to transfer the state of the process at the end of the process. Usually 0 indicates a normal end; other values indicate that an error has occurred and the process ends abnormally. In actual programming, you can use the wait system call to receive the return value of the child process and deal with it accordingly. Exit is used to end the entire running program, it returns parameters to OS and gives control to the operating system, while return exits the current function, returns the value of the function, and gives control to the calling function.
Example of fork creation child process:
# include # include int main (int argc, const char * argv []) {pid_t pid; pid = fork (); if (pid = =-1) / / failed to return-1 {perror ("fork fail:"); exit (1);} else if (pid = = 0) / / in the child process: the return value is 0 {printf ("child\ n") Printf ("child getpid ()% d\ n", getpid ()); printf ("child getppid ()% d\ n", getppid ()); exit (1);} else / / in the parent process: return the PID {int s; wait (& s) of the child process; printf ("fork\ n"); printf ("father pid% d\ n", pid) / / Child process pid printf ("father getpid ()% d\ n", getpid ()); / / parent process pid printf ("father getppid ()% d\ n", getppid ()); / / pid printf of parent process ("father% d\ n", WEXITSTATUS (s)); perror ((char*) & s);} return 0;} Test:
The child process copies the entire contents of the parent process, showing the pid number and ppid number, but the child process program executes the
These are all the contents of the article "how to create a child process with Linux fork". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.