waitpid.c:17: warning: implicit declaration of function `sleep'
waitpid.c:18: warning: implicit declaration of function `execlp'
waitpid.c: In function `main':
waitpid.c:90: warning: implicit declaration of function `fork'
其中,implicit declaration of function ""指函数“”声明不明确,也就是缺少声明。然后就可以利用man <N> function来查寻需要的头文件。一般地,N=3就可以查到。比如,
clean:
rm -rf $(OBJS)
运行:
[armlinux@lqm waitpid]$ ./waitpid 1 1
In child2: execute 'date'
二 11月 7 17:03:16 CST 2006
Get child2 12779.
/home/armlinux/program/my_apue_practice/waitpid
Get child1 12778.
总用量 40
-rw-r--r-- 1 armlinux armlinux 125 11月 3 16:43 Makefile
-rwxrwxr-x 1 armlinux armlinux 32038 11月 7 16:57 waitpid
-rw-rw-r-- 1 armlinux armlinux 1705 11月 7 16:57 waitpid.c
[armlinux@lqm waitpid]$ ./waitpid 0 1
In child1 process:
The child2 process has not
In child2: execute 'date'
In child1 process:
The child2 process has not
In child1 process:
The child2 process has not
In child1 process:
The child2 process has not
In child1 process:
The child2 process has not
二 11月 7 17:03:36 CST 2006
Get child2 12782.
/home/armlinux/program/my_apue_practice/waitpid
Get child1 12781.
总用量 40
-rw-r--r-- 1 armlinux armlinux 125 11月 3 16:43 Makefile
-rwxrwxr-x 1 armlinux armlinux 32038 11月 7 16:57 waitpid
-rw-rw-r-- 1 armlinux armlinux 1705 11月 7 16:57 waitpid.c
将Makefile中的CC改为arm-linux-gcc,经过交叉编译,下载到target board上面,运行良好。
3、注意问题
1)exec族函数执行完成自动退出整个子程序,因此sleep(5)语句要放在其前面。另外,exec函数容易出错,必须要加入出错处理,只要perror就可以了。perror函数在输出字符串后自动加一个冒号,然后输出出错原因。
2)程序框架比较简单,还可以加入许多处理,进行扩充。