Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the use of fork function in Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report