SoFunction
Updated on 2025-04-06

Detailed introduction to unix programming process control

unix programming process control:

fork function

Fork creates a child process. The child process is a copy of the parent process and will get a copy of the parent process's data space, heap, and stack.

Then the sharing of files is also more complicated. The parent process and the child process each have file descriptor tables, but the file represents common (and generally two processes, the file table is unique to each process), which means that the offset of the file is the same. A file is opened in the parent process and will also be opened in the child process. Therefore, if this file is not used in the child process, you must first close the file.

vfork function

There is a difference between the fork function as follows:

1: The vfork child process is executed first, and the child process calls the exec function
2: The vfork child process will not copy the address space of the parent process, which means it will be public.

exit function

The exit function will close the I/O stream

wait waitpid function

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

waitpid function

==-1 Wait for any child process
2. pid > 0 Wait for the specified pid
==0 Wait for any process whose group ID is equal to the calling process group ID
4. Wait for any child process whose group ID is equal to the absolute value of the 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 just replaces the main segment, data segment, heap segment, and stack segment of the current process with a new program on the disk.

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

Among these functions

l represents list, each command is an independent parameter, the last parameter needs to be written as (char *)0

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

p represents filename as parameter, looking for executable files from PATH.
v stands for vector, is an array, and the parameters are obtained from the array.
e represents the environment, and you can specify an environment variable to the child process.

Another point is that the FD_CLOEXEC flag was previously used, which means that the descriptor is closed if 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 requires attention. Whether the process has permission to read and write each file depends mainly on the valid user ID of the process.

Pay special attention to the setting user ID bit when the program file is set. When exec function executes this program, it will set the valid user ID based on the uid of this program file. if

Without setting the user ID bit, the exec function will not change the valid user ID and maintain the existing value.

Parser file

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

Function system

In fact, it is similar to typing a command in the shell. In fact, it is fork a process and then executes exec.

Process Scheduling

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

Thank you for reading, I hope it can help you. Thank you for your support for this site!