目录介绍:
如何创建守护进程fork函数的运用
编写守护进程只要遵循一个特定的流程,就很方便写出自己的守护进程。
第一步、创建子进程,父进程退出;
pid = fork()
if (pid 0)
{
exit(0); //父进程退出
}
第二步、在子进程中创建新会话;
第三步、改变当前目录为根目录;
第四步、重设文件权限掩码;
第五步、关闭文件描述符;
这样就基本创建了一个守护进程。
下面看一个实例:作用是让该守护进程每隔10秒向日志文件/tmp/daemon.log中写入一句话。
[cpp] view plaincopy
/*
* daemon.c
*
* Created on: 2012-7-13
* Author: liwei.cai
*/
#include stdlib.h
#include stdio.h
#include string.h
#include fcntl.h
#include sys/types.h
#include unistd.h
#include sys/wait.h
int main()
{
pid_t pid;
int i, fd;
char *buf ="This is a Daemon\n";
pid = fork(); //第一步 创建子进程,父进程退出
if (pid 0)
{
printf("Error fork\n");
exit(1);
}
else if (pid 0)
{
exit(0);// 父进程退出
}
setsid();//第二步 在子进程中创建新会话
chdir("/");//第三步 改变当前目录为根目录
umask(0);//第四步 重设文件权限掩码
for(i = 0; i getdtablesize(); i++) //第五步 关闭文件描述符
{
close(i);
}
//这时候已经创建完成守护进程,以下开始正式进入守护进程工作
while(1)
{
if((fd = open("/tmp/daemon.log",
O_CREAT|O_WRONLY|O_APPEND,0600)) 0)
{
printf("Open file error\n");
exit(1);
}
write(fd, buf, strlen(buf)+1);
close(fd);
sleep(10);
}
exit(0);
}
用createprocess函数创建进程的意义是什么?
是windows调用的createprocess函数,并不是没有用createprocess函数。
《windows程序设计》摘抄:
操作系统事实上并不是真的调用main函数,而是去调用C/C++运行期启动函数,此函数初始化C/C++运行期库。因此,在应用程序中可以调用malloc和free之类的函数。它也会保证在用户的代码执行之前所有的全局的或静态的C++对象能够被正确的创建,即执行这些对象构造函数中的代码。
组成Win32进程的两个部分:
(1)进程内核对象。操作系统使用此内核对象来管理进程,也是操作系统存放进程统计信息的地方。
(2)私有的虚拟地址空间。此地址空间包含了所有可执行的或者是DLL模块的代码和数据,它也是程序动态申请内存的地方,比如说线程堆栈和进程堆。
在控制台应用程序中,C/C++运行期启动函数会调用程序入口函数main,所以如果程序中没有main函数的实现代码的话,连接器将返回“unresolved external symbol”错误。Win32应用程序的启动过程就是进程的创建过程,操作系统是通过调用CreateProcess函数来创建新的进程的。当一个线程调用CreateProcess函数的时候,系统会创建一个进程内核对象,其使用计数初始化为1.此进程内核对象不是进程本身,仅仅是一个系统用来管理这个进程的一个小的数据结构(PCB,Process Control Block)。系统然后会为新的进程创建一个虚拟地址空间,加载应用程序运行时所需的代码和数据。
系统接着会为新进程创建一个主线程,这个主线程通过执行C/C++运行期启动代码开始运行,C/C++运行期启动代码又会调用main函数。如果系统能够成功创建新的进程和进程的主线程,CreateProcess函数返回TRUE,否则返回FALSE。
popen函数怎么创建进程的
popen使用FIFO管道执行外部程序。
#include stdio.h
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。
下面看一个例子:
/*******************************************************************************************
** Name:popen.c
** This program is used to show the usage of popen() .
** Author:zieckey,(zieckey@yahoo.com.cn)
** Date:2007/9/30 11:47
** All rights reserved!
*******************************************************************************************/
#include sys/types.h
#include unistd.h
#include stdlib.h
#include stdio.h
#include string.h
int main( void )
{
FILE *stream;
FILE *wstream;
char buf[1024];
memset( buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
wstream = fopen( "test_popen.txt", "w+"); //新建一个可写的文件
fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE *wstream对应的流中,也是写到文件中
pclose( stream );
fclose( wstream );
return 0;
}
[root@localhost src]# gcc popen.c
[root@localhost src]# ./a.out
[root@localhost src]# cat test_popen.txt
总计 128
-rwxr-xr-x 1 root root 5558 09-30 11:51 a.out
-rwxr-xr-x 1 root root 542 09-30 00:00 child_fork.c
-rwxr-xr-x 1 root root 480 09-30 00:13 execve.c
-rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c
-rwxr-xr-x 1 root root 162 09-29 18:54 getpid.c
-rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c
-rwxr-xr-x 1 root root 443 09-30 00:55 system.c
-rwxr-xr-x 1 root root 0 09-30 11:51 test_popen.txt
-rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt
本文来自: () 详细出处参考:
网友评论
最新评论
-xr-x 1 root root 1105 09-30 11:49 popen.c-rwxr-xr-x 1 root root 443 09-30 00:55 system.c-r
符;这样就基本创建了一个守护进程。下面看一个实例:作用是让该守护进程每隔10秒向日志文件/tmp/daemon.log中写入一句话。[cpp] view plaincopy/* * daemon.c * * Created
ain() { pid_t pid; int i, fd; char *buf ="This is a Daemon\n"; pid = fork(); //第一步 创建子进程,父进程退出 if (pid 0)
-xr-x 1 root root 1105 09-30 11:49 popen.c-rwxr-xr-x 1 root root 443 09-30 00:55 system.c-rwxr-xr-x 1 root root
buf), stream); //将刚刚FILE* stream的数据流读取到buf中 fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE *wstream