美文网首页
信号实现任务管理

信号实现任务管理

作者: 一路向后 | 来源:发表于2021-03-11 22:47 被阅读0次

1.源码实现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <syslog.h>
#include <signal.h>

static short skip = 0;

void skipwork(int dunno)
{
    skip = 1;
}

void cwork(int dunno)
{
    skip = 0;
}

int sub_work1(short skip)
{
    time_t now = 0;
    time(&now);

    if(!skip)
    {
        syslog(LOG_USER|LOG_INFO,"do work 1: \t%s\t\t\n",ctime(&now));
        sleep(1);
        return 0;
    }

    syslog(LOG_USER|LOG_INFO,"skip work 1: \t%s\t\t\n",ctime(&now));

    return -1;
}

int sub_work2(short skip)
{
    time_t now = 0;
    time(&now);

    if(!skip)
    {
        syslog(LOG_USER|LOG_INFO,"do work 2: \t%s\t\t\n",ctime(&now));
        sleep(1);
        return 0;
    }

    syslog(LOG_USER|LOG_INFO,"skip work 2: \t%s\t\t\n",ctime(&now));

    return -1;
}

int sub_work3(short skip)
{
    time_t now = 0;
    time(&now);

    if(!skip)
    {
        syslog(LOG_USER|LOG_INFO,"do work 3: \t%s\t\t\n",ctime(&now));
        sleep(1);
        return 0;
    }

    syslog(LOG_USER|LOG_INFO,"skip work 3: \t%s\t\t\n",ctime(&now));

    return -1;
}

int sub_work4(short skip)
{
    time_t now = 0;
    time(&now);

    if(!skip)
    {
        syslog(LOG_USER|LOG_INFO,"do work 4: \t%s\t\t\n",ctime(&now));
        sleep(1);
        return 0;
    }

    syslog(LOG_USER|LOG_INFO,"skip work 4: \t%s\t\t\n",ctime(&now));

    return -1;
}

int sub_work5(short skip)
{
    time_t now = 0;
    time(&now);

    if(!skip)
    {
        syslog(LOG_USER|LOG_INFO,"do work 5: \t%s\t\t\n",ctime(&now));
        sleep(1);
        return 0;
    }

    syslog(LOG_USER|LOG_INFO,"skip work 5: \t%s\t\t\n",ctime(&now));

    return -1;
}

int work()
{
    sub_work1(skip);
    sub_work2(skip);
    sub_work3(skip);
    sub_work4(skip);
    sub_work5(skip);

    return 0;
}

int main()
{
    time_t now = 0;

    daemon(1, 0);

    signal(SIGCONT, cwork);
    signal(SIGTTOU, skipwork);

    while(1)
    { 
        sleep(1);
        work();
    }

    return 0;
}

2.编译源码

$ gcc -o work work.c

3.运行程序

$ ./work

4.查看日志/var/log/message

Mar  9 05:00:52 bogon work: do work 1: #011Tue Mar  9 05:00:52 2021#012#011#011
Mar  9 05:00:53 bogon work: do work 2: #011Tue Mar  9 05:00:53 2021#012#011#011
Mar  9 05:00:54 bogon work: do work 3: #011Tue Mar  9 05:00:54 2021#012#011#011
Mar  9 05:00:55 bogon work: do work 4: #011Tue Mar  9 05:00:55 2021#012#011#011
Mar  9 05:00:56 bogon work: do work 5: #011Tue Mar  9 05:00:56 2021#012#011#011
Mar  9 05:00:58 bogon work: do work 1: #011Tue Mar  9 05:00:58 2021#012#011#011
Mar  9 05:00:59 bogon work: do work 2: #011Tue Mar  9 05:00:59 2021#012#011#011
Mar  9 05:01:00 bogon work: do work 3: #011Tue Mar  9 05:01:00 2021#012#011#011
Mar  9 05:01:01 bogon work: do work 4: #011Tue Mar  9 05:01:01 2021#012#011#011
Mar  9 05:01:02 bogon work: do work 5: #011Tue Mar  9 05:01:02 2021#012#011#011

5.跳过任务

$ kill -SIGTTOU 2331

6.查看日志

Mar  9 05:01:39 bogon work: skip work 1: #011Tue Mar  9 05:01:39 2021#012#011#011
Mar  9 05:01:39 bogon work: skip work 2: #011Tue Mar  9 05:01:39 2021#012#011#011
Mar  9 05:01:39 bogon work: skip work 3: #011Tue Mar  9 05:01:39 2021#012#011#011
Mar  9 05:01:39 bogon work: skip work 4: #011Tue Mar  9 05:01:39 2021#012#011#011
Mar  9 05:01:39 bogon work: skip work 5: #011Tue Mar  9 05:01:39 2021#012#011#011
Mar  9 05:01:40 bogon work: skip work 1: #011Tue Mar  9 05:01:40 2021#012#011#011
Mar  9 05:01:40 bogon work: skip work 2: #011Tue Mar  9 05:01:40 2021#012#011#011
Mar  9 05:01:40 bogon work: skip work 3: #011Tue Mar  9 05:01:40 2021#012#011#011
Mar  9 05:01:40 bogon work: skip work 4: #011Tue Mar  9 05:01:40 2021#012#011#011
Mar  9 05:01:40 bogon work: skip work 5: #011Tue Mar  9 05:01:40 2021#012#011#011

7.继续任务

$ kill -SIGCONT 2331

相关文章

网友评论

      本文标题:信号实现任务管理

      本文链接:https://www.haomeiwen.com/subject/defaqltx.html