#include<stdio.h>

#include<sys/types.h>

#include<sys/wait.h>

#include<unistd.h>

#include<signal.h>





int main( void )

{

    daemon_init();



    fprintf(stderr, "main进程[%d]\n", getpid() );

    while( 1 )

    {

    }



    exit(0);

}





int daemon_init()

{

    pid_t pid;



    if( getppid() == 1 )

    {

umask( 0 );

return 0;

    }





    if( (pid = fork()) < 0 )

    {

        fprintf(stderr, "fork err!\n");

        return -1;

    }

    else if( pid > 0 )

    {

        fprintf( stderr, "结束进程[%d]\n", getpid() );

        //exit( 0 );

return 0;

    }



    fprintf( stderr, "孤儿进程[%d]\n", getpid() );

    signal( SIGHUP, SIG_IGN );

    signal( SIGQUIT, SIG_IGN );

    signal( SIGCHLD, SIG_IGN );



    setsid();

    umask( 0 );



    return 0;

}

30行假设为exit( 0 ),执行结果:

孤儿进程[9777]

main进程[9777]

结束进程[9776]

假设将30行的exit( 0 )替换为return 0,执行结果:

孤儿进程[9777]

main进程[9777]

结束进程[9776]

main进程[9776]

这就体现了return和exit的差别之中的一个。return是逐层退出,而exit是马上结束进程。

return和exit的差别的更多相关文章

  1. linux之return和exit引发的大问题(vfork和fork)

    在coolshell.cn上看到的一个问题.为此拿来研究一下. 首先 看看return和exit的差别 在linux上分别跑一下这个代码 int main() { return 0; //exit(0 ...

  2. return和exit函数的区别

    在上Linux课的时候,老师提到一句,调用vfork产生的子进程就是为了使用exec族函数来执行其他的代码逻辑. 在子进程退出的时候有两种方式,exit和exec族函数,不能使用return,为什么不 ...

  3. 关于return和exit

    关于return和exit 在子进程退出的时候有两种方式,exit和exec族函数,不能使用return,为什么不能用return呢,exit改成return 会出现父子进程又各自重复开始进行. 1. ...

  4. 理解 break, continue, return 和 exit

    你们知道 “break”, “continue”, “return” 和 “exit”的作用吗? 它们是功能强大的语言结构体.下面通过一个测试函数来说明它们之间的不同. 1 2 3 4 5 6 7 8 ...

  5. Linux编程return与exit区别

    Linux编程return与exit区别 exit  是用来结束一个程序的执行的,而return只是用来从一个函数中返回. return return 表示从被调函数返回到主调函数继续执行,返回时可附 ...

  6. DevC++ return 1 exit status

    当使用DevC++时编译运行程序出现 return 1 exit status 有可能是因为有在运行的命令窗口未关闭.

  7. Windows下return,exit和ExitProcess的区别和分析

    通常,我们为了使自己的程序结束,会在主函数中使用return或调用exit().在windows下还有ExitProcess()和TerminateProcess()等函数. 本文的目的是比较以上几种 ...

  8. C++中的return和exit区别

    在main函数中,return和exit经常混用,两者的一个区别:return会执行statck unwinding,而exit不会.如果触发了信号,exit也同样不会做stack unwinding ...

  9. oracle 存储过程循环体中的return和exit区别:

    oracle 存储过程循环体中的return和exit区别:   (1) return 跳出整个循环,终止该循环, 后面的不再执行.     相当于 Java 中的break;   (2)  exit ...

随机推荐

  1. 【MFC】MFC中使对话框变成圆角矩形的代码(转)

    原文转自 http://blog.csdn.net/cracent/article/details/48274469 BOOL CLoginDlg::OnInitDialog() { CDialog: ...

  2. git超详细教程【转】

    转自:http://blog.csdn.net/liuwengai/article/details/52072344 GitHub操作总结 : 总结看不明白就看下面的详细讲解.   GitHub操作流 ...

  3. AC日记——线段树练习5 codevs 4927

    4927 线段树练习5  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有n个数和5种操作 add a b ...

  4. HDU 4587 TWO NODES 枚举+割点

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 TWO NODES Time Limit: 24000/12000 MS (Java/Other ...

  5. AtCoder - 3913 XOR Tree

    Problem Statement You are given a tree with N vertices. The vertices are numbered 0 through N−1, and ...

  6. CountDownLatch、CyclicBarrier、Samephore浅谈三大机制

    CountDownLatch.CyclieBarrier与SamePhore都可用来控制线程的执行,那么他们之间有什么区别呢 CountDownLatch CountDowenlatch可以看成一个线 ...

  7. 查询和设置mysql事务隔离级别

    1.查看当前会话隔离级别 select @@tx_isolation; 2.查看系统当前隔离级别 select @@global.tx_isolation; 3.设置当前会话隔离级别 set sess ...

  8. Microsoft SQL Server 2005技术内幕:存储引擎笔记

    http://www.cnblogs.com/lyhabc/articles/3942053.html

  9. 文件夹浏览(SHBrowseForFolder)

    from http://www.cnblogs.com/Clingingboy/archive/2011/04/16/2018284.html 一.首先要为SHBrowseForFolder准备一个结 ...

  10. 谈Objective-C block的实现(转)

    前言 这里有关于block的5道测试题,建议你阅读本文之前先做一下测试. 先介绍一下什么是闭包.在wikipedia上,闭包的定义)是: In programming languages, a clo ...