Inter-process communication between embedded Linux (II.)--exec family functions

Source: Internet
Author: User

Inter-process communication between embedded Linux (II.)--exec family function

exec The function family does this by locating the executable file according to the specified file name and substituting it for the contents of the calling process, in other words, executing an executable file inside the calling process. The executable file here can be either a binary file or a script file that can be executed under any Linux .

The exec family functions include the following functions:

#include <unistd.h>

extern char **environ;

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[]);

The difference between execl and execv is that the parameters are passed differently,execl The parameters in a list,execv The parameters are stored in a string array. EXECLP and execvp Add the path to the file lookup, first find the path parameter of the file, the environment variable is not found PATH to find. execle adds an array of strings to the executable program passing environment variables.

Examples of function use:

Execl ("/bin/ls", "ls", "-a", "-l", NULL);

Char *const arg[] = {"ls", "-L", "-a", NULL};

EXECV ("/bin/ls", Arg);

EXECVP ("LS", ARG);

Execle usage Examples:

EXEC.C: Compile build exec

#include <stdio.h>

#include <unistd.h>

int main (int argc, char **argv)

{

Char *const envp[] = {"Name=scorpio", "host=192.168.6.200", NULL};

Execle ("./hello", "Hello", NULL, ENVP);

return 0;

}

HELLO.C: Compile build Hello

#include <stdio.h>

#include <unistd.h>

extern char **environ;

int main (int argc, char **argv)

{

printf ("hello\n");

int i = 0;

for (i = 0; Environ[i]! = NULL; i++)

{

printf ("%s\n", Environ[i]);

}

return 0;

}

Run ./exec:

Hello

Name=scorpio

host=192.168.6.200

The environment variables defined in the EXEC program are passed to the calling Hello program.

Common errors:

A, The file or path cannot be found, at which point errno is set to Enoent

b, array and ENVP forget to use null errno is set to efault

C, there is no permission to run the file to execute , at which point errno is set to eacces


This article from "Endless life, Struggle not only" blog, reprint please contact the author!

Inter-process communication between embedded Linux (II.)--exec family functions

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.