exec函数族

 

1、execl函数

	if(fork() == 0){
		//in child1
		printf("fork1 is OK;execl\n");
		
		if(execl("/bin/ls","ls","-a",NULL) == -1){
			perror("execl error");
			exit(1);
		}
	}

2、execv函数

char *arg[] = {"ls","-a",NULL};
	
    if(fork() == 0){
		//in child2
		printf("fork2 is OK;execv\n");
		
		if(execv("/bin/ls",arg) == -1){
			perror("execv error");
			exit(1);
		}
	}

3、execlp函数

	if(fork() == 0){
		//in child3
		printf("fork3 is OK;execlp\n");
		
		if(execlp("ls","ls","-a",NULL) == -1){
			perror("execlp error");
			exit(1);
		}
	}
	

4、execvp函数

	char *arg[] = {"ls","-a",NULL};

   	if(fork() == 0)
    {
		//in child4
		printf("fork4 is OK;execvp\n");
		
		if(execvp("ls",arg) == -1){
			perror("execvp error");
			exit(1);
		}
	}
	

5、execle函数

char *arg[] = {"ls","-a",NULL};
	
if(fork() == 0){
		//in child5
		printf("fork5 is OK;execle\n");
		
		if(execle("/bin/ls","ls","-a",NULL,NULL) == -1){
			perror("execle error");
			exit(1);
		}
	}
	

6、execve函数

    char *arg[] = {"ls","-a",NULL};

	if(fork() == 0){
		//in child6
		printf("fork6 is OK;execve\n");
		
		if(execve("/bin/ls",arg,NULL) == -1){
			perror("execve error");
			exit(1);
		}
	}

7、测试文件

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
	char *arg[] = {"ls","-a",NULL};
	
	if(fork() == 0){
		//in child1
		printf("fork1 is OK;execl\n");
		
		if(execl("/bin/ls","ls","-a",NULL) == -1){
			perror("execl error");
			exit(1);
		}
	}

	usleep(20000);
	if(fork() == 0){
		//in child2
		printf("fork2 is OK;execv\n");
		
		if(execv("/bin/ls",arg) == -1){
			perror("execv error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child3
		printf("fork3 is OK;execlp\n");
		
		if(execlp("ls","ls","-a",NULL) == -1){
			perror("execlp error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child4
		printf("fork4 is OK;execvp\n");
		
		if(execvp("ls",arg) == -1){
			perror("execvp error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child5
		printf("fork5 is OK;execle\n");
		
		if(execle("/bin/ls","ls","-a",NULL,NULL) == -1){
			perror("execle error");
			exit(1);
		}
	}
	
	usleep(20000);
	if(fork() == 0){
		//in child6
		printf("fork6 is OK;execve\n");
		
		if(execve("/bin/ls",arg,NULL) == -1){
			perror("execve error");
			exit(1);
		}
	}
	//加入小延时可以避免发生混乱的情况
	usleep(20000);
	return 0;
}

8、测试结果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值