Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the use of fork function in Linux

Shulou Source: shulou.com Published: 2022-06-01 15:36:40 09月18日 Update

In this article Xiaobian for you to introduce in detail the "Linux fork function use case analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "Linux fork function use case analysis" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

A process, including code, data, and resources assigned to the process. 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.

After a process calls the fork () function, the system first allocates resources to the new process, such as the space to store data and code. Then copy all the values of the original process to the new process, except for the few values that are different from those of the original process. It's the equivalent of cloning yourself.

Example: / * > File Name: fork_test.c > Author: > Mail: > Created Time: 09:29:05 on Monday, June 6, 2016 * * * / # include # include int main () {pid_t fpid / / fpid represents the value returned by the fork function int count=0; fpid=fork (); if (fpid printf ("error in fork!"); else if (fpid= = 0) {printf ("child process | process id=% d | fpid=% d | parentid =% d\ n", getpid (), fpid,getppid ()); count++ } else {printf ("parent process | process id=% d | fpid =% d | parentid =% d\ n", getpid (), fpid,getppid ()); count++;} printf ("Statistical result is:% d\ n", count); getchar (); return 0;} 123456789101112131415161718192021222324252627

There may be two reasons for fork errors: 1) the current number of processes has reached the limit specified by the system, and the value of errno is set to EAGAIN. 2) the system is out of memory, so the value of errno is set to ENOMEM.

Second, fork advanced knowledge

Example

# include # include int main (void) {int ibido; printf ("I son/pa ppid pid fpid\ n"); / / ppid refers to the parent process of the current process pid / / pid refers to the pid of the current process, and / / fpid refers to the value for returned by the fork to the current process. Iif (fpid==0) printf ("iForce% d | child ppid=%4d | pid=%4d | fpid=%4d\ n", iGramage getppid (), getpid (), fpid); else printf ("iQuest% d | parent ppid=%4d | pid=%4d | fpid=%4d\ n", iGramage getppid (), getpid (), fpid);} getchar (); return 0;} 1234567891011121314151617181920

Running result

View the process

Analysis.

Read this, the "Linux fork function use example analysis" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Tags: Process function instance analysis system instance analysis different article same two code content data knowledge result resource allocation proper upper limit that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi NVidia Huawei Redmi