Linux basic tutorial: 8. Linux process management (1)

Like the window system, linux also has this powerful process management program that is the same as the window task manager. We only need to open the doc window and enter ps -u to see the currently running process:

ps -u

 Whether you add - or not, the meaning of u together is to print the current running process statically, why is it static and what is static, because it does not move, and we can see it with the naked eye. However, we can easily find from this data that they are a table with various process parameters on the header;

So, what is a process? We all know that if we want to play a game or use some programs, we need to download them from the Internet and then execute them on the computer. The things stored on our computer hard disk are called programs. When we start them When running, they need to start a process, and the process is in memory;

1. We can enter top to view the process dynamically:

top

 

Here top directly prints all process information, and it is still changing. You can see many columns, including process number (that is, PID, which is equivalent to the descriptor of the file), and user (user, generally used To filter), the use of cup, time and name, etc., we can easily analyze it by looking at the data, we should pay attention to one place is S (the state of the process):

D=uninterruptible sleep R=running S=sleep T=trace/stop Z=zombie process

Here you can see that vicious is both s and T. Generally, if you do server maintenance, you should pay attention to cleaning up this zombie process;

When we enter the top command, there are actually many options:

top M(按照内存排序)/P(CPU使用排序)/T(时间排序)/K(可以根据pid杀死线程)

It means that we need to enter the pid of the thread to be killed; but in addition to this way of killing the thread, we can also use the kill command to kill the thread;

kill -9 线程PID

I will not do a demonstration here; if you want to exit first, just ctrl+c, remember the thread number, and then enter the above command to kill the corresponding thread;

2. Process control

The process has four states in total: ready state, running state, sleep state, and terminated state; the ready state here is the state waiting for the CPU to execute after the resources of the program are allocated in place, the running state is the state in which the cup is executing, and the sleep state is blocked Suspended state and suspended state means that a process is blocked due to some reasons that it cannot be executed, and the state that the process that has only temporarily exited but has not been terminated can be woken up at any time is suspended;

In linux, we can see that task switching seems to be seamless, but from a microscopic point of view, switching processes actually require events, but there are fewer events, which means that processes in linux are actually concurrent, not Parallel; it’s just that we can’t feel the constant switching between each process;

3. fork function

This function can create a new thread, and the content of this thread is the same as the parent process. Let’s find out what is the child process and what is the parent process. On the window side, if we use WeChat to open a small program, is it small? The program depends on the WeChat environment, so we can call them parent-child threads;

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
​
        pid_t pid;
        pid=fork();
        if(pid==-1)
        {
                perror("fork error");
                exit(1);
        }
        else if(pid>0)
        {
                printf("parent process , pid=%d,ppid=%d\n",getpid(),getppid());
        }
        else if(pid==0)
        {
                
                printf("parent process , pid=%d,ppid=%d\n",getpid(),getppid());
        }
        printf("finish!\n");
        return 0;
}

 

In fact, here are a few sentences printed by the parent-child thread. The ppid (that is, the parent process id) of the second sentence here is the pin (this thread Id) of the first sentence, so the first thread is the parent process, and the first thread is the parent process. The second is the child process. We can get the thread id and the parent thread id respectively through getpid and getppid, but we can’t see it through ps now, because the program enters the termination state after execution;

The pid_t here should be the macro definition in the header file, which is equivalent to int; when we execute the fork function, a sub-thread exactly the same as this thread will be created, and it will be executed directly, so we should pay attention to the use of this function , fork also has a return value, this return value can cause 0, 1, -1, which respectively represent the failure of the child thread, parent thread, and creation thread; if you still don’t understand, you can use the man command;

Guess you like

Origin blog.csdn.net/aiwanchengxu/article/details/127807092