Linux shop type of process control

      Share knowledge on a word to see peace, encourage each other: learning cycle is divided into learning, thinking, practice, corrected four stages, the shorter the period, the higher the efficiency of learning.

       Linux is a process task_struct structure. This structure describes the basic information in the process of running. Next, I say something about the process control, which is the practice session.

    1. Process Creation

      First introduce the function: pid_t fork (void);    return value: the child returns 0 parent returns the child process id, create sub-process fails or -1       

        When a process calls fork, the same code will produce two binary code. And they all run to the same place. Now, to show you what:

     

       

     As a result, we have been validated: the parent process run independently before fork, after the fork, both father and son were executed execution flow.

    fork principle:       

          After the process calls fork, when control is transferred to the kernel, the kernel will do four things:

          1. Allocate a new block of memory to the child process and data structures

          2. The portion of the parent data structure copied to the child process

          3. Add the child process to system processes (PCB) lists

               4.fork return, start scheduling

                                   

      Copy-on-write technology: 

      

       In fact fork is to create a child process pcb, pcb the parent process data copied , this time a parent and child because the virtual address space page table exactly the same, so their code and data look the same.

       However, the process should be independent, modify any process data should not affect other processes, so the child process should also open up their own physical memory to store data.

       However, if the child does not modify the data, and then open up the space update the page table, it is a waste of resources, so the operating system does not start to open up a copy of the data memory, but wait for a process to modify the data and then give the child to open up new physical memory, copying data copy technology when writing ---

 

      Then a brief vfork --- parent and child share a virtual address space, vfork because there is a problem on many systems, it is not recommended.

      vfork child process is running, the parent process blocks until the child process ends.

 

      2. The process terminates

    Process exit time, will save and exit reasons, so they know the process the task is completed correctly.

    Process exit scenario:

          1. normal exit, correct result;

          2. The normal exit, the result of an error;

               3. quit unexpectedly, results can not be used as criteria for judging

      The process of exit strategy: 

          1.main中return;

          2. Call the exit function; --- 1 and 2 are the first flush the buffer to do other work release before launching

          3. Call _exit function; --- rude quit immediately release all resources

    

    3. The process waits

      Why wait for the process? Because you want to avoid zombies .

      Because the parent does not know when the child process exits, so after creating the child process has been waiting for child process exits .

      Function Description:

        

         Blocking: To initiate a call to complete the operation, if the current conditions do not have to complete, then waits until the operation is completed.

         Difference between blocking and non-blocking: whether to return immediately after initiating calls. 

        Acquiring sub-process status: 

          waitpid wait and has an output parameter that status, is filled by the operating system.

          status can not simply as an integer of view, but a bitmap, specific details are as follows:

                       

            Test validation code:

 

            coredump sign: core dump flag

            When the program exits abnormally, stack information stored procedures to facilitate the post-mortem debugging  (a core dump usually is off by default: safety hazards and space occupancy)

  

    4. The program replacing

    Earlier we know what code to run a process, depending on the real physical address mapping code area code segment virtual address space of.

      It means that if the code segment mapped virtual address to physical memory location codes replace the position of another program, then the process will run another program.

      

 

       What is: Code Replace code mapping position becomes the position of the other memory area codes, and re-initialized data section

       Why: In most cases, we create a child process is to allow the child process to run another program and do other tasks

       Implementation: The operating system provides a set of interfaces can achieve replacement program, referred to as exec family of functions     

#include<unistd.h>

int execl (const char * path, const char * arg, ...); // NULL terminated behind
int execlp(const char* file,const char* arg,...);
int execle(const char* path,const char* grg,...,char* const  envp[]);

int execv (const char * path, const char * argv); // argv array should be NULL terminated
int execvp(const char* file,const char* argv);
int execve (const char * path, const char * argv, char * const envp []); // envp end of the array are NULL

// In fact, only execve system call, other functions are called execve

      2. arguments 

      path: indicates that the corresponding file in which directory (I also said that the program to be carried out which one); 

      file: only fill in the file name, the system will find in the PATH; 

      envp []: indicate environment variables, program after substitute your own set 

      3. Command understanding 

      l (list): using a parameter indicative of a list 

      v (vector): parameter takes the form of an array 

      p (path): p-automatic search PATH environment variable 

      e (envp): said he set the (maintenance) environment variables (environment variables yourself what is set, the environment variable is what the latest procedure)

      Theory talk so much, it is better to write test code to see how to use it:

#include <unistd.h>
int main ()
{
 char *const argv[] = {"ps", "-ef", NULL};
 char *const envp[] = {"PATH=/bin:/usr/bin", "TERM=console", NULL};

execl ( "/ bin / PS", "PS", "-ef", NULL);
// belt p, you can use the environment variable PATH, without having to write the whole path execlp("ps", "ps", "-ef", NULL);
// e with the need to assemble their own environment variables execle("ps", "ps", "-ef", NULL, envp);
execv ( "/ bin / PS", argv);
// belt p, you can use the environment variable PATH, without having to write the whole path execvp("ps", argv);
// e with the need to assemble their own environment variables execve("/bin/ps", argv, envp); exit(0); }

  

Guess you like

Origin www.cnblogs.com/Duikerdd/p/11687591.html