/*************************************************************************
> File Name: test.c
> Author: ndj
> Mail: 172305913@qq.com
> Created Time: 2017年07月22日 星期六 18时42分58秒
************************************************************************/
//exec函数示例
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/wait.h>
char *env_init[] = {"USER=unkown", "PATH=/home/ndj/Unix/test8_16/echoall", NULL};//环境变量
int main(void)
{
pid_t pid;
if((pid=fork()) < 0)
{
fprintf(stderr, "fork failed\n");
exit(0);
}
else if(pid == 0)//子进程
{
if(execle("/home/ndj/Unix/test8_16/echoall", "echoall", "myarg1", "MY ARG2", (char*)0, env_init) < 0)
{
fprintf(stderr, "execle failed\n");
exit(0);
}
}
if((waitpid(pid, NULL, 0)) < 0)
{
fprintf(stderr, "waitpid failed\n");
exit(0);
}
setenv("PATH", "/home/ndj/Unix/test8_16", 1);
if((pid = fork()) < 0)
{
fprintf(stderr, "fork2 failed\n");
exit(0);
}
else if(pid == 0)
{
if(execlp("echoall", "echoall", "only 1 arg", (char*)0) < 0)
{
fprintf(stderr, "execlp failed\n");
exit(0);
}
}
exit(0);
}
Linux中环境变量的作用
最新推荐文章于 2024-06-25 11:09:24 发布