LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 936|回复: 3

exit()问题

[复制链接]
发表于 2005-11-4 13:37:11 | 显示全部楼层 |阅读模式
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
        pid_t pid1,pid2;
        if ((pid1=fork())<0)
        {
                printf("fork error!\n");
                exit(0);
        }
        else if (pid1==0)
        {
                if ((pid2=fork())<0)
                {
                        printf("fork error!\n");
                        exit(0);
                }
                else if (pid2>0)
                        exit(0);
                else
                {
                        sleep(2);
                        printf("this is grandson,parent pid is :%d\n",getpid());
                        exit(0);
                }
        }   
        else
        {
                printf("this is the grandpa process,pid is %d\n",getpid());
                exit(0);
        }
        if (waitpid(pid1,NULL,0)!=pid1)
                exit(0);
        printf("this is grandson,parent pid is :%d\n",getpid());
        exit(0);
        return 0;

}
---------------------------------------------------------------------------------------
[root@localhost cpro]# gcc wait.c
wait.c: 在函数 ‘main’ 中:
wait.c:11: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:18: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:21: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:32: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:35: 警告:内建函数 ‘exit’ 不兼容的隐式声明


在编译的时候出现上面的提示,请问这个是什么原因,是不是exit 函数出了问题?
但是我在编译另外的程序上也用了exit,编译没有问题阿 。。。
发表于 2005-11-4 13:46:21 | 显示全部楼层
加#include <stdlib.h>
回复 支持 反对

使用道具 举报

发表于 2005-11-4 21:18:10 | 显示全部楼层
最后那个return 0;执行不到吧,前面都exit(0)了。

Post by cqulzg
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
        pid_t pid1,pid2;
        if ((pid1=fork())<0)
        {
                printf("fork error!\n");
                exit(0);
        }
        else if (pid1==0)
        {
                if ((pid2=fork())<0)
                {
                        printf("fork error!\n");
                        exit(0);
                }
                else if (pid2>0)
                        exit(0);
                else
                {
                        sleep(2);
                        printf("this is grandson,parent pid is :%d\n",getpid());
                        exit(0);
                }
        }   
        else
        {
                printf("this is the grandpa process,pid is %d\n",getpid());
                exit(0);
        }
        if (waitpid(pid1,NULL,0)!=pid1)
                exit(0);
        printf("this is grandson,parent pid is :%d\n",getpid());
        exit(0);
        return 0;

}
---------------------------------------------------------------------------------------
[root@localhost cpro]# gcc wait.c
wait.c: 在函数 ‘main’ 中:
wait.c:11: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:18: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:21: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:32: 警告:内建函数 ‘exit’ 不兼容的隐式声明
wait.c:35: 警告:内建函数 ‘exit’ 不兼容的隐式声明


在编译的时候出现上面的提示,请问这个是什么原因,是不是exit 函数出了问题?
但是我在编译另外的程序上也用了exit,编译没有问题阿 。。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-11-5 22:06:56 | 显示全部楼层
Post by haohao_h
加#include <stdlib.h>

谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表