【发布时间】:2023-04-07 16:29:01
【问题描述】:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc,char *argv[],char *envp[])
{
int pid;
int id;
pid=fork();
if(pid<0)
{
printf("\n Error ");
exit(1);
}
else if(pid==0) //Child process
{
execve("a",argv,envp); //Problem is in here
printf("\n Pid of child process is %d ",getpid()); //Finds the id of the child process
exit(0);
}
else //Parent process
{
wait(3);
printf("\n Pid of parent process is %d ",getpid());
exit(1);
}
}
我正在尝试在 UNIX 中执行一个名为 a 的程序,但它不起作用可能是因为我使用了错误的 exec 命令或程序 a 在不同的目录中,但我不确定。当我从终端执行它时,它给出我是子进程和父进程的 ID,但没有通知我有关程序的信息。
【问题讨论】:
-
您是否阅读过您调用的任何函数的手册页?
-
如果你运行这段代码会发生什么,会返回错误或者只是不执行预期的结果?
-
如果 printf 调用正在执行,则某些内容是 FUed。你检查过errno的值吗?打过 strerror 电话了吗?
-
它给了我子进程调用和父进程调用的 id。我知道我的程序在当前状态下没有返回整数,但在最新版本的 Ubuntu 中可能不需要它。这没有任何意义,但我找不到更好的解释。退出语句可能是这种情况
标签: c linux unix operating-system exec