In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you the understanding of the sub-process creation function fork. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
On the description of process control, have a certain understanding of the fork function, do this blog, make a record.
Routine:
/ * *
* @ file fork_try.c
* @ brief
*
*
* @ author Ma Hao (Alex), mahao@hust.edu.cn
*
* @ blog http://blog.csdn.net/sanwu2010
*
* @ internal
* Created on November 30th, 2013
* Version: 0.2
* Compiler gcc/g++
* Company Renesas Lab of HUST
* Copyright Copyright (c) 2013, Ma Hao
*
* Trust me! The programmer changes the world! Thanks the mum of XiaoPang Ma!
* =
, /
# include
# include
# include
# include
Int main ()
{
Pid_t pid
Int x = 1
Pid = fork ()
If (pid = = 0)
{
Printf ("child: X% d\ n", + + x)
Exit (0)
}
Printf ("parent: X% d\ n",-- x)
Exit (0)
}
Execution result:
Fightingcards, smartcards, cards, nuts, bolts, bugs, nuts, bolts, bugs, nuts, nuts,
Parent: Xero0
Child: Xero2
Understand the main points:
(1) the fork function is called once and returned twice
The fork function is called once by the parent process, but returns twice, once in the parent process, the result is the pid value of the child process, and once to the child process, the return result is 0
(2) the child process and the parent process execute concurrently, and the kernel executes the instructions in their logical control flow alternately in any way. It is impossible to make any assumptions about the alternating execution of instructions in different processes.
(3) the child process and the parent process have the same address space, the same user stack, the same global variable value, and the same code segment, because the child process gets a copy of the same user-level virtual address space of the parent process, but it is indeed independent of each other and has its own private address space. Therefore, the value of x in the program is independent and unaffected to the child process and the parent process.
(4) the child process gets the same copy as any open file descriptor of the parent process, so in this case, the child process also gets the standard output file descriptor.
Deepen understanding:
The exit (0) that this routine places in the if causes the child process and the parent process to use the same code segment, but they execute different code segments, so they can not well reflect the inheritance effect of the child process to the parent process. We modify the routine to cancel the exit after the end of the if judgment, and this returns the PID of the child process. The modified procedure is as follows:
# include
# include
# include
# include
Int main ()
{
Pid_t pid
Int x = 1
Pid = fork ()
If (pid = = 0)
{
Printf ("child: X% d\ n", + + x)
/ * exit (0); * /
}
Printf ("parent: X% d\ n",-- x)
Printf ("childpid:child_pid=%d\ n", pid)
Exit (0)
}
The running results are as follows:
Fightingcards, smartcards, cards, nuts, bolts, bugs, nuts, bolts, bugs, nuts, nuts,
Parent: Xero0
Childpid:child_pid=6306
Child: Xero2
Parent: Xero1
Childpid:child_pid=0
This highlights the inheritance effect of the child process to the parent process, as well as the concurrent execution of the child process and the parent process. In this example, the logical control flow of the parent process is executed first, and then the logical control flow of the child process is executed, so the child process is executing
When printf ("childpid:child_pid=%d\ n", pid);, no update of the pid value is obtained.
The above is the editor's understanding of creating the function fork for the shared child process. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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.
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.