关于调用linux下的daemon函数出现的线程退出问题。

根根小盆友 2015-08-03 07:01:13
程序在前台跑正常,一切到后台,线程就退出了。不知道什么原因,求大神指导。。。

#include <stdio.h>
#include <pthread.h>

pthread_t tid;

void *fun()
{
FILE* pf = fopen("test", "ab");
while(1)
{
fprintf(pf, "aaaa\n");
sleep(5);
fflush(pf);
}
}

int aa()
{
pthread_create(&tid, NULL, fun, NULL);
}

int main()
{
aa();

//daemon注释打开线程就自动的退出了
//daemon(0,0);

pthread_join(tid, NULL);
}
...全文
219 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
根根小盆友 2015-08-06
  • 打赏
  • 举报
回复
引用 1 楼 zxh707wk 的回复:
http://blog.csdn.net/hunhunzi/article/details/6094719
谢谢了
根根小盆友 2015-08-06
  • 打赏
  • 举报
回复
引用 2 楼 lgbxyz 的回复:

int main(void)
{
   	if(daemon(1,0) < 0)	
	{
		perror("daemon:");
		return -1;
	}
	
	aa();
    pthread_join(tid, NULL);
    
	return 0;
 }

谢谢,明白了此中的奥秘
LubinLew 2015-08-04
  • 打赏
  • 举报
回复

int main(void)
{
   	if(daemon(1,0) < 0)	
	{
		perror("daemon:");
		return -1;
	}
	
	aa();
    pthread_join(tid, NULL);
    
	return 0;
 }

707wk 2015-08-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/hunhunzi/article/details/6094719

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧