我对 execl 的学习
上例子 [作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]
[root@localhost test]# cat exem.c #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { pid_t mpid; mpid=fork(); if( mpid<0) { } else if (mpid==0) { //the child itself if(execl("/soft/test/gaochange.o","gaochange",NULL)<0) perror("execl error!"); for (;;) { fprintf(stderr,"after execl call\n"); sleep(5); } }else{ for (;;) { sleep(1); } } }
被子进程调用起来的代码:
[root@localhost test]# cat gaochange.c #include <stdio.h> int main(int argc, char *argv[]) { for (;;) { sleep(10); fprintf(stderr,"we are in gaochange\n"); } return 0; } [root@localhost test]#
运行的结果,是这样的:
[root@localhost test]# gcc -o gaochange.o gaochange.c [root@localhost test]# gcc -o exem.o exem.c [root@localhost test]# ./exem.o we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange we are in gaochange …. [root@localhost test]# ps -ef|grep exem root 3173 2957 0 09:10 pts/2 00:00:00 ./exem.o root 3176 2917 0 09:10 pts/1 00:00:00 grep exem [root@localhost test]# ps -ef|grep 3173 root 3173 2957 0 09:10 pts/2 00:00:00 ./exem.o root 3174 3173 0 09:10 pts/2 00:00:00 gaochange root 3179 2917 0 09:10 pts/1 00:00:00 grep 3173 [root@localhost test]#
结束 [作者:技术者高健@博客园 mail: luckyjackgao@gmail.com ]