求助:用信号量机制实现程序的重新启动

myxs 发布于 2013/05/13 14:32
阅读 92
收藏 0

思路如下 捕获信号量SIGINT ,再用execl重新在进程中载入自身 问题 前几次还好,但是后面就对ctrol + C 没反应了 不知道是啥问题

运行结果如下

program start ....
./rsr
the 0 ...
^Csignal captured...
program start ....
./rsr
the 0 ...
^Csignal captured...
program start ....
./rsr
the 0 ...
^Cthe 1 ...
^C^Cthe 2 ...
^C^Cthe 3 ...
the 4 ...
the 5 ...
the 6 ...
the 7 ...
the 8 ...
the 9 ...



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

void func(int sig);

int main (int argc, char* argv[])
{
    int i = 0;
    struct sigaction act,oldact;
    act.sa_handler=func;

    printf("program start ....\n");
    printf("%s\n",path);

    sigaction(SIGINT,&act,&oldact);

    for(;i<10;i++)
    {
        printf("the %d ...\n",i);

        sleep(1);

    }

    return 0;
}

void func(int i)
{
    printf("signal captured...\n");
        execl("/home/myx/yxsys/restart/rsr","./rsr",NULL);
}

加载中
0
剑尖血凝紫
剑尖血凝紫
写个脚本调用ps检查进程在不在运行不就行了?
myxs
myxs
谢谢啊
myxs
myxs
呃,只是想用C来实现
0
剑尖血凝紫
剑尖血凝紫

void func(int i)
{
    printf("signal captured...\n");
    if (execl("/test/test","./a.out",NULL) == -1)
    {

    	perror("a.out");
    }
}
solaris

luj@ux-v480[112]> uname -a
SunOS ux-v480 5.8 Generic_108528-22 sun4u sparc SUNW,Sun-Fire-480R

luj@ux-v480[135]>  CC sig_test.cpp

luj@ux-v480[136]> ./a.out
program start ....
the 0 ...
the 1 ...
^Csignal captured...
a.out: Permission denied
the 2 ...
the 3 ...
the 4 ...
the 5 ...
^Csignal captured...
a.out: Permission denied
the 6 ...
the 7 ...
the 8 ...

the 9 ...


ubuntu12

[root@bogon test]# uname -a
Linux bogon 2.6.32-131.0.15.el6.i686 #1 SMP Tue May 10 15:42:28 EDT 2011 i686 i686 i386 GNU/Linux
[root@bogon test]# ./a.out
program start ....
the 0 ...
the 1 ...
the 2 ...
^Csignal captured...
a.out: Permission denied
the 3 ...
^Csignal captured...
a.out: Permission denied
the 4 ...
^Csignal captured...
a.out: Permission denied
the 5 ...
the 6 ...
the 7 ...
the 8 ...
the 9 ...



返回顶部
顶部