In Linux, another program is launched inside a program to create a new process.1.This work can be done through the library function system.
#include <stdlib.h>
int system (const char *string);
The function of the system function is to run the command as a string parameter and wait for the command to complete. The execution of the command is like executing the following command in the shell:
$ sh-c String
If the shell cannot be started to run this command, the system function returns error code 127, and the other error returns-1.
System.c
#include <stdlib.h>#include<stdio.h>int main () { printf (" Running Upper with system\n"); System ("./upper"); printf ("done.\n"); Exit (0);}
Compiling GCC system.c-o system
At execution time./system
You will find that the upper program is executed.
2. Create the process bottom interface exec.
The EXEC series function consists of a set of related functions that differ in how the process is started and how the program parameters are expressed. The EXEC function can replace the current process with a new process. You can use the EXEC function to switch the execution of a program from one program to another.
The EXEC function is more efficient than the system function because the original program is no longer running after the new program starts.
#include <unistd.h>
int execl (XXX);
int EXECLP (XXX);
int execle (XXX);
int execv (XXX);
int EXECVP (XXX);
int execve (XXX);
Or take the upper program as an example:
Exe_up.c
#include <stdio.h> #include <unistd.h> #include <stdlib.h>int main () {printf ( " runing With execlp\n " ); EXECLP ( " ./upper " , " /upper " " down " 0
GCC Exe_up.c-o exe_up
./exe_up
Ps:
Upper.c
#include <stdio.h>#include<stdlib.h>#include<ctype.h>int main () { int ch; while (EOF! = (ch=getchar ()) ) { Putchar (toupper (CH)); } Exit (0); }
GCC Upper.c-o Upper
All of the above is from the fourth edition of Linux programming.
Linux Learning-1 process