|
我得代码是这样的:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char buf[100]={'\0'};
int pid;
static int aa;
for ( ; ; ) {
memset(buf,'\0',100);
if(strcmp(buf,"quit\n")==0)
break;
switch ( pid=fork() ) {
case -1:
_exit(0);
case 0:{
fgets(buf,100,stdin);
aa = getpid();
printf("this is child pid is:%d static address:%d\n",aa,&aa);
execl("/bin/sh","sh","-c",buf,(char *) 0);
_exit(0);
}
default:
wait(&pid);//break;
printf("main process ID is: %d child process ID is:%d static address:%d\n",getpid(),aa,&aa);
}
}
exit(EXIT_SUCCESS);
}
运行后输出为:
[fenstein@pmsvr01 code]$ gcc -o debug/edit Edit1.c
[fenstein@pmsvr01 code]$ ./debug/edit
ls
this is child pid is:10977 static address:134519028
debug Edit1.c.bak tcpclient.c tcpsever.c test02.c test.c texecl.c
Edit1.c tcp.c tcpserv.c test01.c test1.c testfile.c UDPsvr.c
main process ID is: 10976 child process ID is:0 static address:134519028
为什么在两个进程中aa的输出是不一样的呢?!!
如何才能获得一个进程的孙子进程的id?!! |
|