未加星标

exec family of functions in C

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

The exec family of functions replaces the current running process with a new process. It can be used to run a C program by using another C program. It comes under the header file unistd.h. There are many members in the exec family which are shown below with examples.

execvp : Using this command, the created child process does not have to run the same program as the parent process does. The exec type system calls allow a process to run any program files, which include a binary executable or a shell script . Syntax: int execvp (const char *file, char *const argv[]);

file:points to the file name associated with the file being executed.

argv:is a null terminated array of character pointers.

Let us see a small example to show how to use execvp() function in C. We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by calling execvp() function in execDemo.c .

//EXEC.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
printf("I am EXEC.c called by execvp() ");
printf("\n");
return 0;
}

Now,create an executable file of EXEC.c using command

gcc EXEC.c -o EXEC //execDemo.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);
/*All statements are ignored after execvp() call as this whole
process(execDemo.c) is replaced by another process (EXEC.c)
*/
printf("Ending-----");
return 0;
}

Now, create an executable file of execDemo.c using command

gcc execDemo.c -o execDemo

After running the executable file of execDemo.cby using command ./excDemo, we get the following output:

I AM EXEC.c called by execvp() When the file execDemo.c is compiled, as soon as the statement execvp(args[0],args) is executed, this very program is replaced by the program EXEC.c. “Ending― ” is not printed because because as soon as the execvp() function is called, this program is replaced by the program EXEC.c. execv : This is very similar to execvp() function in terms of syntax as well. The syntax of execv() is as shown below: Syntax: int execv(const char *path, char *const argv[]);

path:should point to the path of the file being executed.

argv[]:is a null terminated array of character pointers.

Let us see a small example to show how to use execv() function in C. This example is similar to the example shown above for execvp() . We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by calling execv() function in execDemo.c .

//EXEC.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
printf("I am EXEC.c called by execv() ");
printf("\n");
return 0;
}

Now,create an executable file of EXEC.c using command

gcc EXEC.c -o EXEC //execDemo.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);
/*All statements are ignored after execvp() call as this whole
process(execDemo.c) is replaced by another process (EXEC.c)
*/
printf("Ending-----");
return 0;
}

Now, create an executable file of execDemo.c using command

gcc execDemo.c -o execDemo

After running the executable file of execDemo.c by using command ./excDemo, we get the following output:

I AM EXEC.c called by execv() execlp and execl : These two also serve the same purpose but the syntax of of them are a bit different which is as shown below: Syntax: int execlp(const char *file, const char *arg,.../* (char *) NULL */);
int execl(const char *path, const char *arg,.../* (char *) NULL */);

file:file name associated with the file being executed

const char *arg and ellipses : describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.

The same C programs shown above can be executed with execlp() or execl() functions and they will perform the same task i.e. replacing the current process the with a new process.

execvpe and execle : These two also serve the same purpose but the syntax of them are a bit different from all the above members of exec family. The synatxes of both of them are shown below :
Syntax: int execvpe(const char *file, char *const argv[],char *const envp[]);
Syntax:
int execle(const char *path, const char *arg, .../*, (char *) NULL,
char * const envp[] */);

The syntaxes above shown has one different argument from all the above exec members, i.e.

char * const envp[]: allow the caller to specify the environment of the executed program via the argument envp.

envp:This argument is an array of pointers to null-terminated strings and must be terminated by a null pointer. The other functions take the environment for the new process image from the external variable environ in the calling process.

Reference: exec(3) man page

This article is contributed by MAZHAR IMAM KHAN . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

分页:12
转载请注明
本文标题:exec family of functions in C
本站链接:http://www.codesec.net/view/531376.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(9)