Linux process operation (creation process)

In Linux, the only way to create a new process that calls fork or vfork function by an existing process, a new process is created called the child process (child process), an existing process is called the parent (father process ).

1. fork function basis

function is essentially a fork system call, it is to create a new process, when a process calls it, two nearly identical process occurs after the completion of the new process which is called a fork to create a child process, the original process called parent process. Child process is a copy of the parent process, i.e., the child gets a copy of the data and stack segments from the parent process, which needs to allocate new memory; read-only code segment for, usually used to access shared memory mode.

Users typically use the fork function when the following requirements:

  • A process you want to copy itself, so that parent and child can execute code in different sections at the same time, in general, such applications involve network services: the parent process to wait for a request or response remote, when receiving this request or answer calls fork create a child process to complete the deal, while he continues to wait for the remote request or response.
  • The process wants to execute another program, such as the calling application user generated in the Shell.

Standard fork function call format:

#include <unistd.h>
pid_t fork(void);

fork function has no parameters, it is called once but returns twice:

  • For the parent process: the return value of the function is the process identifier of the child, because the child process a process can be more than one, so there is no function to make a process can obtain the process identifier of all its child processes, which must be ways to collect.
  • For the child: the return value is 0, a process has only one parent, the child process can always call getppid get the process identifier of the parent process, so there is no need to return the parent process identifier.
  • If an error occurs: the return value is "-1."

By the return value of the fork function to distinguish between the parent and child processes.

2. The child process and the parent shared data space

When the fork function returns, the child and parent are calling the next statement from the fork function begins execution, but the parent or child process, which is the first implementation of random, depending on the specific scheduling algorithm to determine if you need to make one of the first run, sleep and other functions so that you can use one of the "dormant" for some time, but this length of time is uncertain.

Generally speaking, the child process created by fork will be copied from the parent process data space of the parent process, heap and stack, and the parent process share the text segment together, the child is the only copy of a copy, and the parent process corresponding portions are completely independent.

3. The child and parent process of sharing files

After a call to fork child process will replicate the function of the corresponding parent process memory space, the parent process all open file descriptors will be copied to the child, in which case each open file descriptors parent and child processes will share the same a file entry.

Schematic file's parent and child processes share.

Here Insert Picture Description
Fork child process created and the parent process share the same file offset, at this time if the parent and child processes simultaneously on the same file for writing and without any form of synchronous operation, there will be chaos write file.

4. Create multiple sub-processes

You can create multiple processes using the fork function on Linux, the following paragraph use a while loop calls the fork function to create unlimited sub-process the code.

#include <unistd.h>
int main(int agrc,char *argv[])
{
    while(1)
    {
        fork();
    }
}

If you run the above code corresponding executable file, because the process leading to the creation of unlimited hardware resources are allocated light, Linux will soon enter the "crash state."

Published 71 original articles · won praise 131 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43239560/article/details/102647995