Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

exec 函数示例

$
0
0

execv.c

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

 

extern char **environ;

 

void main(int argc, char* argv[]){

         printf("In execv func.\n");

 

         execv("linuxdba",argv);            // 跳转去执行linuxdba程序

 

         printf("Should do not execute.\n");          // execv执行成功,此句不会被执行

 

}

 

linuxdba.c

#include <stdio.h>

#include <unistd.h>

 

void main(){

         sleep(3);

         printf("This is in linuxdba func.\n");

 

}

 

 

execlp.c

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

 

extern char **environ;

 

void main(int argc, char* argv[]){

         execlp("/bin/cat", "cat", argv[1], (char *)NULL);

}

 

gcc –o execlp execlp.c

./execlp  /etc/passwd

 

execlp() 执行文件,并将第二个参数作为此执行文件的参数文件,参数必须以字符型空指针来结尾,以结束。

作者:liuy5277 发表于2013-11-20 18:20:05 原文链接
阅读:39 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>