(在 UNIX 中执行内置程序 (C)
(Executing a built in program in UNIX (C)
#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 在不同的目录中,但我不是 sure.When 我执行了这个从终端它给了我子进程和父进程的 ID 但没有通知我关于程序 a.
对我有用。我不得不将呼叫更改为等待:
int retStat;
wait(&retStat);
因为 wait 真的想要 return 一个值,没有它程序就崩溃了。您是否检查过您的程序是否在您的路径中,或者您是否在 exec 调用中包含了该路径?
#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 在不同的目录中,但我不是 sure.When 我执行了这个从终端它给了我子进程和父进程的 ID 但没有通知我关于程序 a.
对我有用。我不得不将呼叫更改为等待:
int retStat;
wait(&retStat);
因为 wait 真的想要 return 一个值,没有它程序就崩溃了。您是否检查过您的程序是否在您的路径中,或者您是否在 exec 调用中包含了该路径?