Detailed introduction of unix programming process control

  • 2021-10-25 08:05:04
  • OfStack

unix programming process control:

fork function

fork to create a child process, child process is a copy of the parent process, will get a copy of the parent process data space, heap, stack.

Then the file sharing is also more complicated. The parent process and the child process each have a file descriptor table, but the file representation is common (while the file table is unique to each of the two processes), that is to say, the offset of the file is 1. 1 file is opened in the parent process, and it will be opened once in the child process. Therefore, if this file is not used in the child process, close should be used first.

vfork function

And fork functions have the following differences:

1: The vfork child process executes first, and the child process calls the exec function
2: The vfork child process will not have the address space of the copy parent process, that is, it will be common.

exit function

The exit function closes the I/O flow

wait waitpid function

If all child processes are still running, block
If a child process has terminated, the parent process immediately gets its terminated status and returns.
If there are no child processes, an error returns.

waitpid function

1. pid==-1 waits for any 1 child process
2. pid > 0 Wait for pid to be specified
3. pid==0 The wait group ID is equal to any one process that calls the process group ID
4. Wait for any one child process whose group ID is equal to the absolute value of pid

Of course, these two functions can also be set to not block

exec function

After the fork process, the child process often calls the exec function, the exec function does not create the process, and the process ID remains unchanged. exec simply replaces the body segment, data segment, heap segment, and stack segment of the current process with a new program on disk.

execl, execv, execle, execve, execlp, execvp, fexecve

Among these functions,

l stands for list, each command is a separate parameter, and the last parameter needs to be written as (char *) 0

char*pathname ,char *arg0,...char *argn, (char *)0

p stands for filename as a parameter to look for an executable file from PATH.
v stands for vector and is an array from which the parameters are obtained.
e stands for environment, and one environment variable can be specified for child processes.

One more point, preceded by the FD_CLOEXEC flag, means that the descriptor is closed when exec is executed. Because the fork child process opens the descriptor opened in the parent process by default.

Change user ID and group ID

This needs to be noted that whether the process has permission to read and write each file depends mainly on the effective user ID of the process.

Pay special attention to 1 point, when the program file is set to set the user ID bit. When the exec function executes this program, it sets the uid valid user ID according to this program file. If

Without the user ID bit set, the exec function does not change the valid user ID, maintaining the existing value.

Parser file

#! pathname Parameters
This is the interpreter file encountered in the file
When executing the execl function, this command will be executed first.

Function system

In fact, it is similar to playing commands in shell, which is actually fork1 processes and then executing exec

Process scheduling

You can use the nice function to set the priority of the process

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: