Differences between processes, processes, and threads
Link: http://www.orlion.ga/1015/
I. Process
Each process has a process control block (PCB) in the kernel to maintain process-related information. The process control block of the Linux kernel is the task_struct structure, which includes:
Process id. Each process in the system has a unique id, which is represented by the pid_t type in C language.
The status of a process, including running, suspended, stopped, and botnets.
Some CPU registers that need to be saved and restored during process switching
Description of the virtual address space
Describe the control terminal information
Current working directory
Umask mask
The file descriptor table contains many pointers to the file struct.
Signal-related information
User ID and group
Control terminals, sessions, and process groups.
Maximum number of resources that a process can use
Fork is used to copy a new process (child process) based on an existing process (parent process), and multiple processes are running simultaneously in the system, these processes are copied one by one from the beginning. You can run a program by running commands in Shell, the reason is that the Shell process will call fork to copy a new Shell process after reading the command entered by the user, and then the new Shell process will call exec to execute a new program.
A program can be loaded to the memory multiple times to become multiple processes that run simultaneously. For example, you can run/bin/bash on multiple terminals at the same time. A process can also execute two different programs before and after exec is called. For example, if you enter the ls command in Shell, fork creates a sub-process, at this time, the sub-process is still executing the/bin/bash program, and then the sub-process calls exec to execute the new program/bin/ls. As shown in:
The child process PCB is copied Based on the parent process, so the umask mask is the same as that of the parent process. Similarly, the current working directory of the sub-process is the same as that of the parent process. Therefore, we use cd to change the working directory of the Shell process, and then ls to list the files under that directory, the ls process is actually in the current directory of its own, rather than the current directory of the Shell process, but the directory of the ls process is the same as that of the Shell. However, the process id in the child process PCB is different from that in the parent process.
II. Environment Variables
When the exec system calls and executes a new program, it will pass the command line parameters and environment variable table to the main function. Their locations in the whole process address space are as follows:
Similar to the command line parameter argv, the environment variable table is also a set of strings, as shown in:
The global variable environ defined by libc points to the environment variable table. environ is not included in the header file. Therefore, extern should be used for declaration. Example:
#include <stdio.h>int main(void){ extern char **environ; int i; for (i=0; environ[i] != NULL; i++) printf("%s\n", environ[i]); return 0;}
The execution result is:
When the parent process calls fork to create a child process, it also copies its environment variable table to the child process. Therefore, the printed environment variables are the same as those printed by the Shell process. Environment Variables define the running environment of processes.
You can use char * getenv (const char * name) to obtain the value of name in the environment variable table.
You can use int setenv (const char * name, const char * value, int rewrite); To set environment variables.
You can use void unsetenv (const char * name); to delete the definition of name.
Modifying environment variables in a child process does not change the environment variables of the parent process.