Brief description of Linux process status
After understanding the basic concepts of the process and the concept of process creation, we need to understand the state of the process. So what is the process state? Simply speaking, the process state is to divide a process into different states at different times. It describes what a process does at what time.
Under normal circumstances, a process has three states, ready, running, and blocked.
State name | description |
---|---|
Ready | The process has the running conditions, but the CPU has not been allocated, and it can run when it gets the time slice |
carried out | The process occupies the CPU and runs on the CPU |
block | The process is temporarily unable to run due to waiting for something to happen (such as: I/O request) |
The conversion relationship of the three states is as follows:
Process status under Linux
State name | description |
---|---|
run | Running or getting the time slice to be able to run does not mean that the process must be running, it indicates that the process is either running or in the run queue |
Interruptible sleep | Means that the process is waiting for the event to complete |
Uninterruptible sleep | Processes in this state usually wait for the end of I/O |
stop | You can stop (T) the process by sending the SIGSTOP signal to the process. The suspended process can continue to run by sending the SIGCONT signal |
State of death | This status is just a return status, you will not see this status in the task list |
Create a child process through the system call fork()
Create a child process by fork(), and create a child process by copying the process. The code is shared, but the data is unique.
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int ret = fork();
printf("hello proc : %d!, ret: %d\n", getpid(), ret);
sleep(1);
return 0;
}
Fork() function, the pid of the child process is greater than zero for the parent process, and the return value is 0 for the child process; the parent and child processes can be distinguished by the return value; the return value of f is less than 0, indicating that the creation failed, and the return value is 0. 0 is the parent process, and 0 is the child process.
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int ret = fork();
if(ret < 0){
perror("fork");
return 1;
}
else if(ret == 0){
//child
printf("I am child : %d!, ret: %d\n", getpid(), ret);
}else{
//father
printf("I am father : %d!, ret: %d\n", getpid(), ret);
}
sleep(1);
return 0;
}
Zombie process
Zombie state: is a special state that refers to a state where a process has exited but the resources have not been completely released.
Zombie process: refers to the use of fork to create a child process, if the child process exits, and the parent process does not call wait or waitpid to obtain the child The status information of the process, then the process descriptor of the child process is still stored in the system. That is, a process in a zombie state.
example:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
int main()
{
pid_t pid = fork();
if (pid < 0)
{
perror("fork");
return 1;
}
else if (pid == 0)
{
//创建子进程成功
printf ("我是子进程,我即将退出\n");
exit(0);
}
else
{
printf("我是父进程,我将睡眠30秒\n");
sleep(30);
}
printf(" 父进程退出\n");
return 0;
}
Use the psps aux | grep testJiangshi command to view the process status
The harm of zombie processes
Because the child process needs to tell the parent process how the task assigned by the parent process to the child process is completed, the exit status of the process must be maintained. But the parent process has not read, the child process will always be in a zombie state. The maintenance of the exit status itself is to maintain with data, which is also the basic information of the process, so it is saved in task_struct (PCB). Because the Z (zombie) status does not exit, the PCB always needs to be maintained. If a child process creates many The child process, without recycling, will cause a waste of memory resources. (Just like the zombies in the movie, they are dead, but the corpses still exist. If there are more and more "zombies" and cannot be destroyed, the limited space will be completely filled).
Orphan process
Orphan process: Contrary to the process produced by the zombie process, the orphan process means that the parent process exits before the child process. The child process becomes an orphan process and runs in the background, and the parent process becomes the No. 1 process (the orphan process exits will not become a zombie process because it Will be directly processed by process 1)
Code:
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <stdlib.h>
int main()
{
pid_t pid = fork();
if(pid < 0)
{
// 创建失败
perror("fork");
}
else if(0 == pid)
{
//子进程
printf("我是子进程!\n");
printf("pid: %d\tppid:%d\n",getpid(),getppid());
printf("我将睡眠10秒");//睡眠10秒,保证父进程先退出
sleep(10);
printf("pid: %d\tppid:%d\n",getpid(),getppid());
printf("子进程退出!\n");
}
else
{
printf("我是父进程!\n");
sleep(1);
printf("父进程退出");
}
return 0;
}
The child process becomes an orphan process, and the parent process becomes the number 1 process