Linux process state

Keywords: Programming

R Running: Indicating that a process is either running or in a running queue does not mean that the process must be running.

S sleeping: This means that the process is waiting for the completion of the event (sleep here is sometimes called interruptible sleep).

D Disk Sleep: Sometimes called uninterruptible sleep, the process in this state usually waits for the end of IO.

T stop state: A process can be stopped by sending a SIGSTOP signal to the process. The suspended process can send SIGCNT signals.

Keep the process running.

X dead: This state is just a return state, and you won't see it in the task list.

 

Process state modification:

kill-l View the list of signals supported by the system

kill-SIGSTOP stops (T) processes by sending SIGSTOP signals to processes

kill-SIGCONT keeps the process running by sending SIGCNT signals.

 

Z(zombie) - Botnet process

1. The zombie state is a special state. Botnet processes occur when a process exits and the parent process does not read the return code of the child process exiting.

2. The zombie process will remain in the process table with termination status, and will wait for the parent process to read the exit status code.

3. So, as long as the child process exits, the parent process is still running, but the parent process does not read the state of the child process, and the child process goes into the zombie state.

Use code simulation to implement zombie process, orphan process scenario.

Using code simulation to implement botnet processes

Code implementation:

#include<stdio.h>     
#include<stdlib.h>
#include<unistd.h>

 int main()
{
    pid_t pid = fork();
    if(pid < 0)
    { 
        perror("fork");                                                         
        return 1;
    }
    else if(pid > 0)
    {
        printf("parent : %d\n", getpid());
        sleep(30);
    }
    else
    {
        printf("child : %d\n",getpid());
        sleep(5);
    }   
}

The hazards of the zombie process:

1. The exit state of a process must be maintained because it cares about its process (parent process). If the parent process does not read, the child process will remain in Z state.

2. Maintaining exit status itself is data maintenance and belongs to the basic information of the process, so it is stored in task_struct. In other words, if Z status does not exit all the time, PCB will need it.

Always maintained

3. The parent process creates many sub-processes, that is, it does not recycle, which will cause a waste of memory resources, because the data structure object itself needs to occupy memory.

4. Memory leak

 

orphan

When the parent process exits first, the child process is called an orphan process.

The orphan process is adopted by the init process No. 1 and, of course, recycled by the init process

Implementing orphan processes using code simulation

code implementation

#include<stdio.h>                                                                                                                                           
#include<stdlib.h>                                                                                                                                          
#include<unistd.h>                                                                                                                                                                                                                                                                                                                                                                                                                                                            int main()                                                                                                                                                  
{                                                                                                                                                           
   pid_t id = fork();                                                                                                                                        
   if(id<0)                                                                                                                                                     
   {                                                                                                                                                         
     perror("fork");                                                                                                                                         
     return 1;                                                                                                                                               
   }                                                                                                                                                         
   else if(id==0)                                                                                                                                            
   {                                                                                                                                                         
     printf("child:%d\n",getpid());                                                                                                                          
     sleep(10);                                                                                                                                              
   }                                                                                                                                                         
   else
   {                                                                                                                                                                                                        
     printf("parent:%d\n",getpid());                                                                                                                         
     sleep(3);                                                                                                                                               
     exit(0);                                                                                                                                                
   }                                                                                                                                                         
   return 0;
}                                                                                                                                           

 

 

 

 

 

Posted by vigour on Sat, 02 Feb 2019 18:30:15 -0800