web123456

4. Parent and child processes

Parent and child processes           

                     The child process is a copy of the parent process

Unix/linuxThe process creation in the system is like this, and it is a bit confusing to understand.

The parent process executes firstfork()System call, the result of this call is that there is an additional process in the system that is exactly the same as the parent process.,This new process is called a child process, of course, of coursePCBThe parent process pointer points to the first process.

The first and last two processes have their own address space.It's a bit like copying a copy of a file. Although the resources are also independent of each other, the data generated by the parent process's execution process when copying is also copied, and the child process has also copied it. Simply put it simplyA program that has been executed halfway suddenly has a twin brother in the system. Everything is the same as yourself, but you have to call yourself dad.

Of course, such a simple copy itself is of no use. To make it work,Need to be executed again     exec(B )System call, this call allows the current process to execute another executable code (a new program).Simply put, the process was originally executingAOnce the program has been executed, it starts executing.Bprogram.

At this point,The process of father and son becomes different.But no matter what code they execute, their father-son relationship will not change.Processes that can use child processes in parent processesID(In executionfork()(gets the return value when the child process is executed) to abort the execution of the child process.Of course, the child process can also terminate the execution because its own execution program ends

 

 

The issue of the parent and child processes executing one after another,That's it, inforkafter,The parent process executes first, and then the child process executes again after a time slice arrives.

 

Each child process has a parent process. When the process terminates or ends, a parent process will be sent to it.SIGCHLDThe system defaults to the parent process ignores this signal. If the parent process wants to be informed of this state change of its child process, it should capture this signal. The capture function is generallywaitFunction to get child processesIDand child process status.

 

For the keyboardCtrl+The operation of the key is usually to generate a signal, and the process then captures the signal. In addition, of course we can use these signals directly,killcommand to send signals to other processes.

 

waitThe function is the parent process waiting for the child process to end, which means that when the child process ends, a signal will be sent to the parent process.SIGCHID, at this time the parent process passeswaitWhen the function receives this signal, the parent process knows that the child process has ended. This is just used inshellIn the writing of the parser,shellThe parser is the parent process, and the command line command is the child process. When the child process ends, it will tell the parent process. At this time, the parent process can prompt for the next command.

 

A process can only set process groups for itself or its child processesID, in his child processexecAfter the function, the process group of the child process cannot be changed.IDNow.

 

 

LinuxProcess management is commonly used software for players using computers, and then I will study and conduct in-depth research.LinuxProcess management, let's discuss it with you hereLinuxI hope it will be useful to everyone. Programs are software designed to accomplish a certain task, such asOpenOfficeIt's a program.

What is a process?A process is a running program. A running program may have multiple processes.for example UsedWWWThe server isapacheWhen the administrator starts the service, many people may visit the server, which means that many users will request it at the same time.httpdServe,apacheThe server will be created with multiplehttpdprocess to serve it.