Most people don’t quite understand the knowledge points of this article on whether linux can create multiple processes, so the editor summarizes the following content for you. The content is detailed, the steps are clear, and it has certain reference value. I hope you can read it. This article can be rewarding, let's take a look at this article on whether linux can create multiple processes.
Linux can create multiple processes. Linux supports multiple processes and can handle multiple tasks at the same time to maximize the use of system resources. Communication methods between linux processes: 1. Use unnamed pipes; 2. Use well-known pipes (FIFO); 3. Use signal single; 4. Use shared memory; 5. Use message queues; 6. Use semaphores.
Linux can create multiple processes.
Linux supports multiple processes. One of the benefits of a multi-process system is that it can handle multiple tasks at the same time to maximize the use of system resources.
1.1 Overview
In linux, a running program is called a process.
Program: static concept, it is a compiled binary file
Process: dynamic concept, when the program is running, the system will Automatically run a corresponding process
The process includes three parts: process control block (PCB), code segment, and data segment
Process control block: a structure is used in linux To represent, record the status information of the process
Zombie process: the parent process exits before the child process
If you create a child process, but the child process is not recycled in the parent process resources of the process, then the child process will become a zombie process, and the zombie process will eventually be recycled by a process called INIT in the system.
The init process (process No. 1) is the first process that runs when the system starts, and is the ancestor process of all processes.
top View dynamic process information
ps -ef View process details
pstree Display process information in a tree format
bg Put the suspended process Run in the background
1.2 Process running status
Execution state (RUNNING): The process is occupying the CPU.
Ready state (RUNNING): The process is in the waiting queue waiting for scheduling.
Light sleep (INTERRUPTABLE): At this time, the process is waiting for the occurrence of an event or some kind of system resource, and can respond to the signal.
Deep sleep (UNINTERRUPTABLE): At this time, the process is waiting for an event to occur or some kind of system resource, and cannot respond to the signal.
Stop state (STOPPED): The process is suspended at this time.
Zombie state (ZOMBIE): At this time, the process cannot be scheduled, but the PCB has not been released.
DEAD: This is a terminated process and the PCB will be released
Kernel state: also called kernel space, which is the area where kernel processes/threads are located. Mainly responsible for operating system and hardware interaction.
User Mode: Also called User Space, it is the area where user processes/threads are located. Mainly used to execute user programs.
1. Difference
Kernel mode: The running code is not subject to any restrictions, and the CPU can execute any instruction.
User mode: CPU cannot be scheduled, and hardware cannot be accessed directly. The running code needs to be checked by the CPU a lot, and cannot directly access kernel data and programs, that is, it cannot access any valid address like a kernel-mode thread.
When the operating system executes user programs, it mainly works in the user state, and only switches to the kernel state when it performs tasks that it does not have permission to complete.
2. Distinguish between user mode and kernel mode reasons
Protection mechanism to prevent user processes from misoperation or malicious damage to the system< /p>
Ensure the centralized management of resources and reduce resource conflicts.
3. Switch from user mode to kernel mode
(1) System call (active)
The system call (system call) is the interface provided by the operating system to the user process to request the operating system to perform some privileged operations, that is, the window that provides services for the user process. Under Linux, you can use the man syscalls command to view all system call API interfaces provided by Linux.
Because the user mode cannot complete certain tasks, the user mode will request to switch to the kernel mode, and the kernel mode completes the switch through interrupts specially opened for users.
(2) Peripheral device interrupt (passive)
The peripheral device sends out an interrupt signal. When the interrupt occurs, the currently running process is suspended, and the operating system kernel For interrupt process processing, if the CPU executes the user mode program before the interrupt, it is equivalent to switching from the user mode to the kernel mode.
Interrupts are used to ensure that CPU control is handed over to the operating system, so that the operating system can perform certain operations.
(3) Abnormal (passive)
Some unknown exceptions occur when executing user programs, which will switch from user programs to the kernel The program that handles the exception is switched to the kernel state.
1.3 Process interface function
1, fork(), vfork()< br/> (1) The newly created process is called a child process, which copies all the resources of the parent process (only once at the time of creation, the value of the global variable will be different in the future), the parent and child process who Who comes first is uncertain.
#include
(2)**vfork()**The child process shares all the resources of the parent process, It must be the child process first run, and then the parent process runs (even if you add sleep() to artificially interfere, it is useless)
(3) Note
The use of exit() in the child process is completely different from the result of not using it Not the same
Whether sleep() is used in the parent-child process to give up the cpu time slice is also different
Whether wait() is used in the parent process, the result of waitpid() is also different
< p>
(4) Process switching execution
1 , exit(), _exit()
#include
1. wait()
#include
2, waitpid()
pid_t waitpid(pid_t pid, int *stat_loc, int options); recycle child process/process group
Parameters: pid ---- "You specify the id of the child process to be recycled
back
==0 Wait for a child process in this process group to exit
> 0 Waiting for the process whose PID is pid
stat_loc-----"Store the exit status of the child process (can be NULL)
options ----"Generally set to 0
WNOHANG returns immediately when there are no child processes
WUNTRACED returns immediately when a child process is suspended
WCONTINUED returns immediately when a child process receives SIGCONT
Return value: -1 Execution failed
> 0 success The return value is the PID of the recycled process
0 WNOHANG is specified, and there is no exited child process
( 1) Get your own id getpid()
#include
Whether it is communication between processes or communication between threads. It is nothing more than to solve a problem: the allocation of shared resources (coordinating the access of different processes/threads to shared resources)
2.1 Communication methods between processes
1. Traditional interprocess communication method
Unnamed pipe
Famous pipe
Signal
2, System V IPC object
Shared memory
Message queue
Semaphore
3, BSD< /p>
Network socket (socket)
1. Features : The most primitive way of communication between processes
It can only communicate between processes with affinity (parent-child process, sibling process);
It has no name (it exists );
Can be created in the sharing between linux and windows (there is no pipeline file generated at all), but the well-known pipe cannot be created (generated pipeline file);
Half-duplex communication.
2. Use of unnamed pipes
(1) Create pipe()
#include
(2) send and receive pipe information
myid = fork(); //Create child process
if(myid == 0)
{
write(fd[1],"dad,thanks!",20); //The child process sends a message to the parent process
close(fd[1]);
close(fd[0]);
exit(0);
}
else if(myid > 0)
{
read(fd[0],buf,20); //The parent process is blocked to receive messages from the child process
printf("buf is:%s\n",buf);
close(fd[1]);
close(fd[0]);
}
1. Features: between any two processes
Cannot be created in the share between linux and windows;
Guarantee the atomicity of writing (atomicity: either do not do it, do it once The tone is finished without external interference);
The well-known pipelinecannot be overridden and created (general generationUse the access() function in the code to determine whether it exists, if there is already a pipeline with the same name, it cannot be created again);
Remember to close it after use;
When the pipe is opened as read-only, it will block until another process opens the pipe as write-only, then It is not blocked; if it is opened in a readable and writable manner, it will not be blocked.
Full-duplex communication, half-duplex.
2. Use of famous pipes
(1) Create mkfifo()
#include
(2) FIFO process information sending and receiving
fifo_read.c :------- ---》
#define FIFO1 "myfifo1"
#define FIFO2 "myfifo2"
int main(void) {
int my_fd,fd1,fd2;
char r_buff[30];
char w_buff[30];
bzero(r_buff,30);
if(access(FIFO1,F_OK)==-1) {
my_fd = mkfifo(FIFO1,0664); //create pipeline 1
if(my_fd == -1) {
perror("failed!\n");
return -1;
}
}
if(access(FIFO2,F_OK)==-1) {
my_fd = mkfifo(FIFO2,0664); //create pipeline 2
if(my_fd == -1) {
perror("failed!\n");
return -1;
}
}
fd1 = open(FIFO1,O_RDONLY); //Open pipe 1 for read-only, get the pipe file descriptor
if(fd1==-1) {
the pricentf("open fifo1 file failed!\n");
exit(0);
}
fd2 = open(FIFO2,O_WRONLY); //Open pipe 2 for writing only, get the pipe file descriptor
if(fd2==-1) {
printf("open fifo2 file failed!\n");
exit(0);
}
while(1) {
bzero(r_buff,30);
read(fd1,r_buff,sizeof(r_buff)); //Read the message of pipeline 1
printf("client receive message is: %s\n",r_buff);
printf("client please input a message!\n");
fgets(w_buff,30,stdin);
write(fd2,w_buff,30); //Send information to pipeline 2
}
close(fd2);
close(fd1);
return 0;
}
fifo_write.c :-----------》
#define FIFO1 "myfifo1"
#define FIFO2 "myfifo2"
int main(void)
{
int my_fd,fd1,fd2;
char w_buff[30];
char r_buff[30];
bzero(w_buff,30);
if(access(FIFO1,F_OK)==-1) {
my_fd = mkfifo(FIFO1,0664);
if(my_fd == -1) {
perror("failed!\n");
return -1;
}
}
if(access(FIFO2,F_OK)==-1) {
my_fd = mkfifo(FIFO2,0664);
if(my_fd == -1) {
perror("failed!\n");
return -1;
}
}
fd1 = open(FIFO1,O_WRONLY);
if(fd1==-1) {
printf("open fifo1 file failed!\n");
exit(0);
}
fd2 = open(FIFO2,O_RDONLY);if(fd2==-1) {
printf("open fifo2 file failed!\n");
exit(0);
}
while(1) {
bzero(w_buff,30);
printf("server please input a message!\n");
fgets(w_buff,30,stdin);
write(fd1,w_buff,strlen(w_buff)); //write message to pipeline 1 file
read(fd2,r_buff,30); //Read information from pipeline 2
printf("server receive message is:%s\n",r_buff);
}
close(fd1);
close(fd2);
return 0;
}
When the program (process) is running, the outside world will send a signal to the program from time to time. The program is faced with two choices:
Ignore it (blocking/ignoring)
Blocking: refers to suspending the signal and waiting until the program is finished running before responding
Ignoring: discarding the signal
/> Respond to it
1. What are the signals in linux: kill -l view
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39)SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
(1) Signals 1 to 31 are called non-real-time signals: queues are not supported (if multiple signals come at the same time, the response Irregular)
(2) User-defined signals 10) SIGUSR1 12) SIGUSR2
(3) Signals 34 to 64 are called real-time signals: support queues, which are added later in the Linux system
Signals are similar to interrupts: hardware and software
There are two special signals above: SIGKILL, SIGSTOP cannot be ignored, nor can they be blocked
2 , Signal-related operation functions
(1) Send signal kill()
#include
(2) Signal capture signal()
#include
(3) Waiting for signal pause()
#include
(4) Signal blocking
Each process has its own signal mask (that is, the process is running The signals that will be blocked are called signal masks).
A series of functions about signal mask operation:
#include
(5) Configure the signal mask sigprocmask()—block or unblock the signal
< p>
#include
(6) Capture the specified signal and obtain the signal carrying information sigaction()
#include
Copyright Description:No reproduction without permission。
Knowledge sharing community for developers。
Let more developers benefit from it。
Help developers share knowledge through the Internet。
Follow us
2023-03-20
2023-02-06
2023-02-04