黑马《linux系统编程》学习笔记(从61到65)

六十一. sigaction函数的使用

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

void myfunc(int sig)
{
    printf("hello signal: %d\n", sig);
    sleep(5);
    printf("wake up .....\n");
}

int main(int argc, const char* argv[])
{
    // 注册信号捕捉函数
    struct sigaction act;
    act.sa_flags = 0;
    act.sa_handler = myfunc;
    // 设置临时屏蔽的信号
    sigemptyset(&act.sa_mask);  // 0
    // ctrl + 反斜杠
    sigaddset(&act.sa_mask, SIGQUIT);

    sigaction(SIGINT, &act, NULL);

    while(1);

    return 0;
}

六十二. 知识点概述

六十三. 守护进程概念

六十四. 创建守护进程正规流程

 写一个setsid.c

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

int main(int argc, const char * argv[])
{
	//创建一个会话
	//将子进程,变为会长
	
	pid_t pid = fork();
	if(pid > 0)
	{	
		exit(1);
		//以下方式都可以的
		#if 0
		kill(getpid(),SIGKILL);
		raise(SIGKILL);
		abort();
		#endif
	}
	else if(pid ==0)
	{
		//变成会长
		//会长就是一个守护进程
		setsid();
		//让子进程一直活着
		while(1);
	}
	return 0;
}

注意,守护进程,不受登陆和注销的影响。(当然会受关机的影响233333)

 

 六十五. 守护进程练习

思路整理

 process_work.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>

void dowork(int no)
{
        time_t curtime;
        //得到系统时间
        time(&curtime);
        //格式化时间
        char * pt = ctime(&curtime);
        //将时间写入文件
        int fd = open("/home/temp++.txt", O_CREAT | O_WRONLY | O_APPEND, 0664);
        write(fd, pt,strlen(pt)+1);
        close(fd);

}

int main(int argc, const char * argv[])
{

        pid_t pid = fork();
        if(pid > 0)
        {
                //父进程退出
                exit(1);
        }
        else if(pid ==0)
        {
                //变成会长--脱离控制终端--守护进程
                setsid();
                //改变工作目录
                chdir("/home/");
                //重置文件掩码
                umask(0);

                //关闭文件描述符
                close(STDIN_FILENO);
                close(STDOUT_FILENO);
                close(STDERR_FILENO);

                //执行核心操作
                //注册信号捕捉
                //此处不建议signal(),而建议sigaction(),因为前者可能不同linux发行版上有不同
                struct sigaction act;
                act.sa_flags = 0;
                act.sa_handler = dowork;
                sigemptyset(&act.sa_mask);
                sigaction(SIGALRM,&act, NULL);


                //创建定时器
                struct itimerval val;
                //第一次触发定时器的时间
                val.it_value.tv_usec = 0;
                val.it_value.tv_sec = 2;
                //循环周期
                val.it_interval.tv_usec = 0;
                val.it_interval.tv_sec = 1;
                setitimer(ITIMER_REAL, &val, NULL);
                //保证子进程处于运行状态
                while(1);
        }
        return 0;
}

猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/85363647