linux c Process Management: 1. Create process: system function: int system (const char*filename); establish independent processes with independent code space, memory space wait for the new process to finish executing before system returns. (blocked) system: Creates a new blocked process, and the system returns after the new process is finished Case: &Nbsp; calls a program using System. observe the process ID. observe blocking. Code:text.c #include <stdio.h> #include <unistd.h> void main () { printf ("%d\n", Getpid ()) //printing the current process id sleep (Ten); //Process sleep 10 sec} gcctext.c –o text system.c #include <stdio.h> #include < stdlib.h> #include <unistd.h> void main () { printf ("%d\n", Getpid ()); int r=system ("./text"); printf ("%d\n ", r);} gccsystem.c –o main conclusion: The return value of the new process is related to the system return value. return value for any process: do not exceed 255. A single byte. 8-15-bit return code in the returned value of the system (one byte holds the return code) To get a return code, you cannot directly use the return value of system to take the 8-15-bit content of the return value. linux provides a macro to get the value wexitstatus (status), included in the #include<sys/wait.h> code: text.c #include <stdio.h> #include <unistd.h> int main () { printf ("%d\n", Getpid ()) //printing the current process id sleep (ten); //process sleep 10 seconds return 99;} gcctext.c –o text system.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> void main () { printf ("%d\n", Getpid ()); &NBSp; int r=system ("./text"); printf ("%d\n", WEXITSTATUS (r));} gccsystem.c –o main popen function: # include<stdio.h> function prototype: file * popen ( constchar * command , const char * type );int pclose ( file * stream ) popen: Create a child process establish a pipeline between the parent and child processes command: is a point to NULL end shell A pointer to the command string. This line of command will be uploaded to bin/sh and use the -c flag, and the shell will execute this command. type: can only be read or write one, the resulting return value (standard i/o stream) also has and type corresponding read-only or write-only type. If the type is "R" then the file pointer is connected to the command standard output, if type is "W" then the file pointer is connected to the command Standard input for . Return value: If the call succeeds, it returns a pointer to a read or open file, if it fails, returns NULL, and the specific error is determined according to errno int pclose (file*stream) parameter description: Stream:popen returns the file pointer return value: If the call fails, return -1 case: Use Popen to call Ls -l and establish a pipeline read output #include <stdio.h> #include <unistd.h> void main () { char Buf[1024]; file *f=popen ("Ls - l", "R"); int fd=fileno (f); int r; while (r= Read (fd,buf,1024)) >0) { buf[r]=0; printf ("%s", buf); } close (FD); pclose (f);} execlexecle: instead of code data in the code space of the current process, the function itself does not create a new process. excel function: int execl (Const char * path,const char*arg,....); First parameter: The second parameter of the replaced program ...: Command line LifeMake line format: Command name option parameter command line end must be empty string end case: executes a program with exec. Experience: * Do you want to create a new process? No * the command line format for EXECL parameters * experience the difference between Execl and EXECLP (execl only the current path) (not the current path must be the absolute path) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXECLP using the system's search path * EXECL Replace the current process code code: text.c #include <stdio.h> #include < Unistd.h> void main () { printf ("%d\n", Getpid ()); //printing the current process Id sleep (Ten); //process sleep 10 seconds } gcctext.c –o text exec.c #include <stdio.h> #include <unistd.h> void main ( ) { printf ("main::%d\n", Getpid ()); int r=excel ("./text" , "text", null), //int r=excel ("/bin/ls", "ls", "-L", NULL); //int r=excelp ("ls", "ls", "-l", NULL); printf ("End:%d\n", r);} Summary: After the program runs, the two print process IDs are the same, and Excel and EXECLP do not create new processes. The final printed statement cannot be executed because Excel and Excelp are replacing the code of the new program with the code space of the program. The first parameter of the two function that must be a 0 or a null function is the path to the executable, and the second parameter is the command fork function: function prototype: pid_t fork (); //1. Creating a process //2. What is the code for the new process: Cloning the code of the parent process And the location of the execution is cloned. (The code copied from the parent process, the code before the fork is no longer executed in the child process, the child process only executes the fork code copied from the parent process) //3. Returns a value when the child process does not call fork =0 (Pid=0 as a child process) &NBSP;&NBSP;&NBSP;//4. Parent-Child process executing . example code: #include <stdio.h> #include <unistd.h> void main () { printf ("before creating process \ n"); int pid=fork (); printf ("%d\n after Genesis", PID);} Although the code for a child process is a clone of the parent process, we can also separate the code that the process is executing from the code that the parent process executes. Example: #include <stdio.h> #include <unistd.h> voidmain () { printf ("Before creating the process:\ n "); int pid=fork (); if (pid==0) { printf ( "Sub-process: \ n"); } else &NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRINTF ("Parent process: \ n"); }}