如何在Linux下用c语言创建守护进程并监控系统运行期间的所有进程

如何在Linux下用c语言创建守护进程并监控系统运行期间的所有进程,第1张

可以分三步来做:

做两个简单的守护进程,并能正常运行

监控进程是否在运行

启动进程

综合起来就可以了,代码如下:

被监控进程thisisatest.c(来自):

#include<unistd.h>

#include<signal.h>

#include<stdio.h>

#include<stdlib.h>

#include<sys/param.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<time.h>

void init_daemon()

{

int pid

int i

pid=fork()

if(pid<0)    

    exit(1) //创建错误,退出

else if(pid>0) //父进程退出

    exit(0)

    

setsid()//使子进程成为组长

pid=fork()

if(pid>0)

    exit(0)//再次退出,使进程不是组长,这样进程就不会打开控制终端

else if(pid<0)    

    exit(1)

//关闭进程打开的文件句柄

for(i=0i<NOFILEi++)

    close(i)

chdir("/root/test") //改变目录

umask(0)//重设文件创建的掩码

return

}

void main()

{

    FILE *fp

    time_t t

    init_daemon()

    while(1)

    {

        sleep(60)//等待一分钟再写入

        fp=fopen("testfork2.log","a")

        if(fp>=0)

        {

            time(&t)

            fprintf(fp,"current time is:%s\n",asctime(localtime(&t))) //转换为本地时间输出

            fclose(fp)

        }

    }

    return

}

监控进程monitor.c:

#include<unistd.h>

#include<signal.h>

#include<stdio.h>

#include<stdlib.h>

#include<sys/param.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<time.h>

#include<sys/wait.h>

#include<fcntl.h>

#include<limits.h>

#define BUFSZ 150

void init_daemon()

{

int pid

int i

pid=fork()

if(pid<0)

    exit(1) //创建错误,退出

else if(pid>0) //父进程退出

    exit(0)

setsid()//使子进程成为组长

pid=fork()

if(pid>0)

    exit(0)//再次退出,使进程不是组长,这样进程就不会打开控制终端

else if(pid<0)

    exit(1)

//关闭进程打开的文件句柄

for(i=0i<NOFILEi++)

    close(i)

chdir("/root/test") //改变目录

umask(0)//重设文件创建的掩码

return

}

void err_quit(char *msg)

{

perror(msg)

exit(EXIT_FAILURE)

}

// 判断程序是否在运行

int does_service_work()

{

FILE* fp

int count

char buf[BUFSZ]

char command[150]

sprintf(command, "ps -ef | grep thisisatest | grep -v grep | wc -l" )

if((fp = popen(command,"r")) == NULL)

err_quit("popen")

if( (fgets(buf,BUFSZ,fp))!= NULL )

{

count = atoi(buf)

}

pclose(fp)

    return count

// exit(EXIT_SUCCESS)

}

void main()

{

    FILE *fp

    time_t t

    int count

    init_daemon()

    while(1)

    {

        sleep(10)//等待一分钟再写入

        fp=fopen("testfork3.log","a")

        if(fp>=0)

        {

            count = does_service_work()

            time(&t)

            if(count>0)

                fprintf(fp,"current time is:%s and the process exists, the count is %d\n",asctime(localtime(&t)), count) //转换为本地时间输出

            else

            {

                fprintf(fp,"current time is:%s and the process does not exist, restart it!\n",asctime(localtime(&t))) //转换为本地时间输出

                system("/home/user/daemon/thisisatest")//启动服务

            }

            fclose(fp)

        }

    }

    return

}

具体CMD命令:

cc thisisatest.c -o thisisatest

./thisisatest

cc monitor.c -o monitor

./monitor

tail -f testfork3.log   -- 查看日志

在Linux下,我们使用ulimit -n 命令可以看到单个进程能够打开的最大文件句柄数量(socket连接也算在里面)。系统默认值1024。

对于一般的应用来说(象Apache、系统进程)1024完全足够使用。但是如何象squid、mysql、java等单进程处理大量请求的应用来说就有 点捉襟见肘了。如果单个进程打开的文件句柄数量超过了系统定义的值,就会提到“too many files open”的错误提示。如何知道当前进程打开了多少个文件句柄呢?下面一段小脚本可以帮你查看:

lsof -n |awk '{print $2}'|sort|uniq -c |sort -nr|more

在系统访问高峰时间以root用户执行上面的脚本,可能出现的结果如下:

# lsof -n|awk '{print $2}'|sort|uniq -c |sort -nr|more

131 24204

57 24244

57 24231

56 24264

其中第一行是打开的文件句柄数量,第二行是进程号。得到进程号后,我们可以通过ps命令得到进程的详细内容。

ps -aef |grep 24204

mysql24204 24162 99 16:15 ?00:24:25 /usr/sbin/mysqld

哦,原来是mysql进程打开最多文件句柄数量。但是他目前只打开了131个文件句柄数量,远远底于系统默认值1024。

但是如果系统并发特别大,尤其是squid服务器,很有可能会超过1024。这时候就必须要调整系统参数,以适应应用变化。Linux有硬性限制和软性限制。可以通过ulimit来设定这两个参数。方法如下,以root用户运行以下命令:

ulimit -HSn 4096

以上命令中,H指定了硬性大小,S指定了软性大小,n表示设定单个进程最大的打开文件句柄数量。个人觉得最好不要超过4096,毕竟打开的文件句柄 数越多响应时间肯定会越慢。设定句柄数量后,系统重启后,又会恢复默认值。如果想永久保存下来,可以修改.bash_profile文件,可以修改 /etc/profile 把上面命令加到最后。

ulimit:

1,查看进程允许打开的最大文件句柄数

ulimit -n

2,设置进程能打开的最大文件句柄数

ulimit -n xxx

-H 指定资源的硬限制

-S 指定资源的软限制

hard 代表当前硬限制

soft 代表当前软件限制

unlimited 代表不限制.

3, 文件限制配置文件

/etc/security/limits.conf

4,文件句柄最大数据配置

配置文件:/proc/sys/fs/file-max

这个参数的默认值和内存大小有关系,可以使用公式:file-max 内存大小/ 10k.

4.1 建议将整个系统的文件句柄值至少设置为 65536

4.2 echo "65536" >/proc/sys/fs/file-max

4.3 sysctl -w fs.file-max=65536

4.4 echo "fs.file-max=65536" >>/etc/sysctl.conf

5,文件句柄使用情况配置文件:/proc/sys/fs/file-nr

这三个值分别指:系统已经分配出去的句柄数、已经分配但是还没有使用的句柄数以及系统最大的句柄数(和file-max一样)。

6,查看进程打开的文件句柄数

lsof:列出当前系统打开文件的工具。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7521791.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-06
下一篇2023-04-06

发表评论

登录后才能评论

评论列表(0条)

    保存