exec函数族

exec函数族(重点俩个,以及个dup2的配合使用)

dup2是文件的重定向(第二个参数重定向到第一个参数)
		文件描述符1代表标准输出
		文件描述符2代表标准输入
execlp:借助PATH环境变量 (执行系统命令或者函数用这个)
execl:路径加程序名         (用户自定义的可执行程序)
int execl(const char *path, const char *arg, ...)
int execv(const char *path, char *const argv[])
int execle(const char *path, const char *arg, ..., char *const envp[])
int execve(const char *path, char *const argv[], char *const envp[])
int execlp(const char *file, const char *arg, ...)
int execvp(const char *file, char *const argv[])

***execlp 和execv的简单使用

 #include<stdio.h>
  2 #include <stdlib.h>
  3 #include<unistd.h>
  4 
  5 int main()
  6 {
  7         pid_t pid;
  8         pid =  fork();
  9         if(pid == -1)
 10         {
 11                 perror("fork error");
 12                 exit(1);
 13         }
 14         else if( pid > 0 )
 15         {
 16                 sleep(1);
 17                 printf("I am partent\n");
 18         }
 19         else
 20                 //execlp("ls","ls","-l",NULL);
 21                 //execl("/home/s/main","main",NULL);
 22                 execlp("ps","ps","aux",NULL);
 23         return 0;

代码结果
在这里插入图片描述
dup2与exec配合使用

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>

int main()
{

        int fd = open("open.out",O_CREAT | O_WRONLY,0777);
        if(fd == -1)
        {
                perror("open error");
                exit(1);
        }

        dup2(fd,STDOUT_FILENO);

        execlp("ps","ps","aux",NULL);


        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YanWenCheng_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值