Different excel và execlp linux

The exec family of system calls replaces the program executed by a process. When a process calls exec, all code [text] and data in the process is lost and replaced with the executable of the new program. Although all data is replaced, all open file descriptors remains open after calling exec unless explicitly set to close-on-exec. In the below diagram a process is executing Program 1. The program calls exec to replace the program executed by the process to Program 2.

execlp

The

#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
1 system call duplicates the actions of the shell in searching for an executable file if the specified file name does not contain a slash [/] character. The search path is the path specified in the environment by the
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
2 variable. If this variable isn’t specified, the default path
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
3 is used.

The

#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
1 system call can be used when the number of arguments to the new program is known at compile time. If the number of arguments is not known at compile time, use .

#include 

int execlp[const char *file, const char *arg, ...];

fileName of the program to execute.Remaining argumentsThe
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
5 and subsequent ellipses can be thought of as
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
6. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer.

Example

In

#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
7 you find the following example program demonstrating how
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
8 can be used.

#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}

The program uses

#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
1 to search the PATH for an executable file named
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
0 and passing
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
1 as argument to the new program. The new program is the same program used by the shell command
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
0 to list files in a directory.

Use

-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
3 to compile:

Run the program.

You should see something similar to this in the terminal.

-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src

pathThe path to the new program executable.fileThe name of the program executable
path

execvp

The

-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
4 system call will duplicate the actions of the shell in searching for an executable file if the specified file name does not contain a slash [/] character. The search path is the path specified in the environment by the
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
2 variable. If this variable isn’t specified, the default path
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
3 is used. In addition, certain errors are treated specially.

#include 

int execvp[const char *file, char *const argv[]];

fileName of the program to execute.argvArgument vector. An array of pointers to null-terminated strings that represent the argument list available to the new program. The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by a NULL pointer.

Example

In

-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
7 you find the following example program demonstrating how
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
4 can be used.

#include  // execvp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  char *const cmd[] = {"ls", "-l", NULL};
  execvp[cmd[0], cmd];
  perror["Return from execvp[] not expected"];
  exit[EXIT_FAILURE];
}

The program uses

-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
4 to search the PATH for an executable file named
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
0 and passing
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
1 as argument to the new program. The new program is the same program used by the shell command
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
0 to list files in a directory. In comparison to using
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
8 we don’t have to provide the full path to
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
0 when using
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
4, only the name of the executable.

Use

-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
3 to compile:

Run the program.

You should see something similar to this in the terminal.

total 8
-rw-r--r--@ 1 abcd1234  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  5 abcd1234  staff  170 Jan 27 21:17 bin
drwxr-xr-x  2 abcd1234  staff   68 Jan 27 21:17 obj
drwxr-xr-x  5 abcd1234  staff  170 Jan 27 21:16 src

execv

In comparison to

-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
4 the
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
8 system call doesn’t search the PATH. Instead, the full path to the new executable must be specified. .

#include 

int execv[const char *path, char *const argv[]];

pathThe path to the new program executable.argvArgument vector. The
#include 

int execvp[const char *file, char *const argv[]];
9 argument is an array of character pointers to null-terminated strings. The last member of this array must be a null pointer. These strings constitute the argument list available to the new process image. The value in
#include  // execvp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  char *const cmd[] = {"ls", "-l", NULL};
  execvp[cmd[0], cmd];
  perror["Return from execvp[] not expected"];
  exit[EXIT_FAILURE];
}
0 should point to the filename of the executable for the new program.

Example

In

#include  // execvp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  char *const cmd[] = {"ls", "-l", NULL};
  execvp[cmd[0], cmd];
  perror["Return from execvp[] not expected"];
  exit[EXIT_FAILURE];
}
1 you find the following example program demonstrating how
#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
8 can be used.

#include  // execv[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[] {
  char *const argv[] = {"/bin/ls", "-l", NULL};
  execv[argv[0], argv];
  perror["Return from execv[] not expected"];
  exit[EXIT_FAILURE];
}

The program uses

#include  // execlp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  execlp["ls", "ls", "-l", NULL];
  perror["Return from execlp[] not expected"];
  exit[EXIT_FAILURE];
}
8 to replace itself with the
#include  // execvp[]
#include   // perror[]
#include  // EXIT_SUCCESS, EXIT_FAILURE

int main[void] {
  char *const cmd[] = {"ls", "-l", NULL};
  execvp[cmd[0], cmd];
  perror["Return from execvp[] not expected"];
  exit[EXIT_FAILURE];
}
4 program passing
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
1 as argument to the new program. The new program is the same program used by the shell command
-rw-r--r--@  1 karl  staff  410 Jan 27 21:16 Makefile
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 bin
drwxr-xr-x   3 karl  staff  102 Dec  1  2016 data
drwxr-xr-x   2 karl  staff   68 Jan 28 22:08 obj
drwxr-xr-x  17 karl  staff  578 Jan 28 22:08 src
0 to list files in a directory.

Chủ Đề