[问题]一个C的进程问题

C、C++和Java语言
回复
luchen
帖子: 52
注册时间: 2007-07-16 18:40
来自: 浙江
送出感谢: 0
接收感谢: 0

[问题]一个C的进程问题

#1

帖子 luchen » 2007-12-09 13:37

下面是小弟初学进程编程中的一些疑问,望高手解答(疑问处以用红字标出和给出我的问题)
其运行结果是
root@person:/home/luchen/mytext# ./p6_3
Child process PID: 6103.
Process6103: calling exec .
/bin/sh: Can't open arg2
Parent process PID:6102.
Process6102: The parent has fork process 6103.
Process6102: The child has called exec or has exited.

源程序如下:
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main(void){
pid_t pid;
if ((pid=vfork())<0){
printf("fork error!\n");
exit(1);
}
else if (pid==0){
printf("Child process PID: %d.\n",getpid());
setenv("PS1","CHILD\\$",1);
printf("Process%4d: calling exec .\n",getpid());
if(execl("/bin/sh","/bin/sh","arg2",NULL)<0){
printf("Process%4d: execl error!\n",getpid());
exit(0);
}
printf("Process%4d: You should never see this because the child is alread gone. \n",getpid());
printf("Process%4d: The child process is exiting.",getpid());
}
else{
printf("Parent process PID:%4d.\n",getpid());
printf("Process%4d: The parent has fork process %d.\n",getpid(),pid);
printf("Process%4d: The child has called exec or has exited.\n",getpid());
}
return 0;
}

代码: 全选


想问setenv("PS1","CHILD\\$",1);是何意思?
execl("/bin/sh","/bin/sh","arg2",NULL)<0是何意思?
wumingxiaobei
帖子: 8
注册时间: 2006-11-26 22:42
送出感谢: 0
接收感谢: 0

#2

帖子 wumingxiaobei » 2007-12-10 15:34

期待中
头像
奇诺
帖子: 3
注册时间: 2007-11-27 16:14
送出感谢: 0
接收感谢: 0

#3

帖子 奇诺 » 2007-12-10 16:13

没学过,但是可以查手册-,-
man setenv

代码: 全选

int setenv(const char *name, const char *value, int overwrite);
设置环境变量,参数分别为变量名,变量值,是否覆盖

man execl

代码: 全选

execl("/bin/sh","/bin/sh","arg2",NULL)
就是在子进程中开一个shell,然后执行arg2,似乎程序写的有点问题
回复

回到 “C/C++/Java”