How to start a new process in Linux

Source: Internet
Author: User
Tags exit error code printf

Sometimes we need to start another program (process) in our own program (process) to help us do some work, so how do we start other processes in our own process? There are a number of ways to do this in Linux, and here's how to make a difference between them.

One, system function call

The prototype of the system function is:

#include <stdlib.h>  
int system (const char *string);

The function is to run a command passed to it as a string parameter and wait for the command to complete. The execution of a command is like executing a command in a shell: sh-c string. If the shell cannot be started to run this command, the system function returns error code 127 and, if it is a different error, returns-1. Otherwise, the system function returns the exit code for the command.

Note: The system function call uses a shell to start the program you want to execute, so you can put the program in the background, where the system function call returns immediately.

You can start with the following example, the source file is NEW_PS_SYSTEM.C, the code is as follows:

#include <stdlib.h>  
#include <stdio.h>  
      
int main ()  
{  
    printf ("Running PS with system\n");  
    The PS process does not return until it is finished, so the following code  
    system ("PS au") can be continued;//1  
    printf ("PS done\n");  
    Exit (0);  
}

The program calls the PS program to print all the processes related to the user, and finally print PS done. The results of the operation are as follows:

If you change the statement in Comment 1 to: System (PS au &), the system function returns immediately and executes the following code without waiting for the PS process to end. So you see the output, PS done may not appear in the last line, but in the middle.

In general, using the system function is not the ideal way to start another process, because it must start a shell with a shell, that is, to start the program first, and the Shell's environment is heavily dependent, so the use of the system function is inefficient.

Replacing process images--using the Exec series functions

The Exec series functions consist of a set of related functions that differ in how the process starts and how the program parameters are expressed. But the exec series functions have a common way of replacing the current process with a new one, that is, you can use the EXEC function to switch the execution of the program from one program to another, and after the new program starts, the original program is no longer executed. The new process is specified by the path or the file parameter. The EXEC function is more efficient than the system function.

The types of EXEC series functions are:

#include <unistd.h>  
      
char **environ;  
      
int execl (const char *path, const char *arg0, ..., (char*) 0);  
int EXECLP (const char *file, const char *arg0, ..., (char*) 0);  
int execle (const char *path, const char *arg0, ..., (char*) 0, Char *const envp[]);  
      
int execv (const char *path, char *const argv[]);  
int EXECVP (cosnt char *file, char *const argv[]);  
int Execve (const char *path, char *const argv[], char *const envp[]);

Related Article

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.