Start a new process (fork and exec functions)

Source: Internet
Author: User

I. copying process images

1,ForkFunction Introduction

This system call mainly copies the current process and creates a new table item in the Table. Many attributes of the new table item are the same as those of the current process. The new process is almost identical to the original process.CodeBut the new process has its own data space, environment, and file descriptor.

2TYPICAL USEForkCode snippet: 

 Pid_t PID; PID = Fork (); Switch  (PID ){  Case - 1 : //  Error occur Perror ( "  Fork failed  "  ); Exit (  1  );  Case   0 : //  Child     Break  ;  Default : //  Parent      Break  ;} 

 3, Example

Sample Code: 

View code

# Include <sys/types. h> # Include <Unistd. h> # Include <Stdio. h> # Include <Stdlib. h> Int  Main () {pid_t PID;  Char * Message;  Int  N; printf (  "  Fork program starting \ n  "  ); PID = Fork ();  Switch  (PID ){  Case -1  : Perror (  "  Fork failed  "  ); Exit (  1  );  Case   0  : Message = "  This is the child  "  ; N = 5 ;  Break  ;  Default  : Message = "  This is the parent  "  ; N = 3  ;  Break  ;}  For (; N> 0 ; N --) {Puts (Message); sleep (  1  );} Exit (  0  );} 

 The running effect is as follows:

2. Replacing process images

1,ExecSeries Functions 

ExecA series of functions can replace the current process with a new process. The list of functions is as follows: 

 Int Execl ( Const   Char * Path, Const   Char * Arg ,...);  Int Execlp ( Const   Char * File, Const   Char * Arg ,...);  Int Execle ( Const   Char * Path,Const   Char * Arg ,..., Char * Const  Envp []);  Int Execv ( Const   Char * Path, Char * Const  Argv []);  Int Execvp ( Const   Char * File, Char * Const  Argv []);  Int Execvpe ( Const   Char * File, Char * Const Argv [], Char * Const Envp []);

2Header file:

# Include <unistd. h>

3, Example

Sample Code:

 # include 
   
   #include  
    
    #include  
     
      int  
      main () {execlp ( 
      " 
      ls  
     " , 
     "  
      ls  
     " , 
      " 
     -L  
     " , 
      0  
     ); printf ( 
     "  
      OK \ n  
     "  
     ); exit ( 
      0  
     ) ;} 
    
   
  

Running effect:

3. Start a new process 

1,SystemFunction implementation 

1.1Description 

SystemThe function can beProgramTo create a new process.

 
# Include <stdlib. h>IntSystem (Const Char*String);

SystemThe function is used to run the command passed to it as a string parameter and wait for the completion of the command. Command Execution is likeShellRun the following command: 

$ Sh-C string 

1.2Example 

Sample Code:

# Include <stdlib. h># Include<Stdio. h>IntMain () {system ("Ls-l"); Printf ("OK \ n"); Exit (0);}

Running effect:

2 , fork and exec Series function implementation

generally, System functions are not ideal for starting other processes, because it must use a shell to start the required program. You need to start a shell and shell the installation status and environment used depend heavily, so use System inefficient functions. In this case, you can consider using fork subscribe the process, and then use exec the generation function replaces the current sub-process to meet this requirement.

Sample Code (Wait1.c):

# Include <sys/types. h> # Include <Sys/Wait. H> # Include <Unistd. h> # Include <Stdio. h> # Include <Stdlib. h> Int  Main () {pid_t PID;  Char * Message;  Int Exit_code; printf (  "  Fork program starting \ n  "  ); PID = Fork ();  Switch  (PID ){  Case - 1  : Perror (  "  Fork failed  "  ); Exit ( 1  );  Case   0  : Message = "  This is the child  "  ; Sleep (  3 ); //  Sleep Execlp ( "  Ls  " ,"  Ls  " , "  -L  " , 0  ); Exit_code = 37  ;  Break  ;  Default  : Message = " This is the parent  "  ; Exit_code = 0  ;  Break  ;}  If (PID! = 0 ) //  Wait child  {  Int  Stat_val; pid_t child_pid; child_pid = Wait (&Stat_val); printf (  "  Child has finished: pid = % d \ n  "  , Child_pid );  If  (Wifexited (stat_val) printf (  "  Child exited with code % d \ n  "  , Wexitstatus (stat_val ));  Else  Printf (  " Child terminated abnormally \ n  "  );} Exit (exit_code );} 

Running effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.