免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3132 | 回复: 9
打印 上一主题 下一主题

守护进程结束的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-06 21:03 |只看该作者 |倒序浏览
创建一个守护进程后,可不可以用代码将自己(守护进程)结束掉?

论坛徽章:
0
2 [报告]
发表于 2007-08-06 21:09 |只看该作者
exit ?

论坛徽章:
0
3 [报告]
发表于 2007-08-06 21:31 |只看该作者

回复 #2 cugb_cat 的帖子

我用 exit(0)不起作用。
以下是代码:

#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>


int main()
{

        pid_t pid1;
        pid_t pid2;
        if( (pid1 = fork()) > 0)
                exit(0);
        else if( pid1 == 0)
        {
                setsid();
                chdir("/");
                umask(0);
                /* In the first child process*/
                if( (pid2 = fork()) > 0)
                {
                        exit(0);
                }else if( pid2 == 0 )
                {
                        while(1)
                        {
                        usleep(1000000*30);
                        printf("In second child process\n");
                        }
                }
               
        }

return 0;
}

论坛徽章:
0
4 [报告]
发表于 2007-08-06 21:59 |只看该作者
你地2个子进程并没有退出啊

论坛徽章:
0
5 [报告]
发表于 2007-08-06 22:30 |只看该作者
谢谢两位,问题解决了。但是为什么要第2个子进程结束,我的 守护进程才能结束?
还是不知其所以然!

论坛徽章:
0
6 [报告]
发表于 2007-08-06 22:44 |只看该作者
老大 你有一个进程还活着呢 怎么可能结束

论坛徽章:
0
7 [报告]
发表于 2007-08-06 23:47 |只看该作者
僵尸了~~~?

论坛徽章:
0
8 [报告]
发表于 2007-08-07 09:04 |只看该作者
生成守护进程不就是父进程在子进程(守护进程)之前退出的吗?

难道这是守护进程和普通进程的区别?

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
9 [报告]
发表于 2007-08-07 09:18 |只看该作者
man daemon
DAEMON(3)                         Linux Programmer's Manual                         DAEMON(3)

NAME
       daemon - run in the background

SYNOPSIS
       #include <unistd.h>

       int daemon(int nochdir, int noclose);

DESCRIPTION
       The  daemon()  function is for programs wishing to detach themselves from the control-
       ling terminal and run in the background as system daemons.

       Unless the argument nochdir is non-zero, daemon() changes the current  working  direc-
       tory to the root ("/").

       Unless  the argument noclose is non-zero, daemon() will redirect standard input, stan-
       dard output and standard error to /dev/null.

RETURN VALUE
       (This function forks, and if the fork() succeeds, the parent does  _exit(0),  so  that
       further  errors are seen by the child only.)  On success zero will be returned.  If an
       error occurs, daemon() returns -1 and sets the global variable errno  to  any  of  the
       errors specified for the library functions fork(2) and setsid(2).

SEE ALSO
       fork(2), setsid(2)

NOTES
       The glibc implementation can also return -1 when /dev/null exists but is not a charac-
       ter device with the expected major and minor numbers. In this case errno need  not  be
       set.

HISTORY
       The daemon() function first appeared in 4.4BSD.

BSD MANPAGE                               1993-06-09                                DAEMON(3)

论坛徽章:
0
10 [报告]
发表于 2007-08-07 10:42 |只看该作者
原帖由 cwgk 于 2007-8-7 09:04 发表
生成守护进程不就是父进程在子进程(守护进程)之前退出的吗?

难道这是守护进程和普通进程的区别?

做精灵进程要有很多事情做(如果自己写的话),不过可以使用楼上说的daemon函数  使程序成为守护进程
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP