【问题标题】:SIGUSR1 not received未收到 SIGUSR1
【发布时间】:2012-12-26 10:29:09
【问题描述】:

这是我的第一个程序....ctrlcsignal.c

enter code here
#include<stdio.h>
#include<unistd.h>
#include<signal.h>



void signal_handler(int sigNo)
{
    //if Ctrl+c signal
if(sigNo==SIGINT){
    printf("value of SIGINT:-%d\t",SIGINT);
    printf("received SIGINT\n");
}

// if some other signal , but this part wont get executed
 // as the signal_handler function is registered with SIGINT only
else
{   
    printf("Some other signal found");
    printf("value of other signal:-%d",sigNo);
}

}

int main(void)
{
//registering the signal handler function with a signal
kill(19574,SIGUSR1);
if(signal(SIGINT,signal_handler)==SIG_ERR)
{   
    printf("\n can't catch SIGINT\n");
}

while(1)          //infinite loop
    sleep(1); // 1s ,so that the CPU is not busy 

    return 0;
 }

这是我的第二个程序....userdefinedsignals.c

enter code here
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void signal_handler(int sigNo)
{


printf("function entered...");
// check for userdefined Signal SIGUSR1
 if (sigNo == SIGUSR1)
{
    printf("received SIGUSR1 with value :- %d",SIGUSR1);
}
//checking for KILL Signal
else if (sigNo == SIGKILL)
{
    printf("received SIGKILL with value :- %d",SIGKILL);
}
//checking for STOP Signal
else if (sigNo == SIGSTOP)
{
    printf("received SIGSTOP with value :- %d",SIGSTOP);
}
// if some other signal , but this part wont get executed
// as the signal_handler function is registered with SIGINT only
else
{
    printf("Some other signal found");
    printf("value of other signal:-%d",sigNo);
}

}


int main(void)
{

int pid_t;
printf("process id is %d",getpid());

//registering the signal handler function with a signal

if(signal(SIGUSR1,signal_handler) == SIG_ERR)
{
    printf("\n can't catch SIGSIGUSR1\n");
} 
if(signal(SIGKILL,signal_handler)==SIG_ERR)
{
    printf("\n can't catch SIGKILL\n");
}
 if(signal(SIGSTOP,signal_handler)==SIG_ERR)
{
    printf("\n can't catch SIGSTOP\n");
}

 while(1)          //infinite loop
    sleep(1); // 1s ,so that the CPU is not busy

return 0;
}

我得到了第二个进程的 pid ...假设 xxxx 然后我使用以下命令... enter code here 杀死 -USR1 xxxx

但它什么也没显示.... 然后我也尝试通过在第一个程序中调用以下函数......但没有用...... enter code herekill(xxxx,SIGUSR1);

帮帮我..!!!!

【问题讨论】:

  • 1) 不应在信号处理程序中使用 printf(); printf 是不可重入的。 2)您的 main() 无声退出,尝试添加无限循环或 sleep() 或 pause()。 3)也许,你会想要添加一个 waitXX()
  • @wildplasser 这个评论有很多很好的建议。您为什么不将其发布为答案?
  • 因为都是小事。通常的嫌疑人......一个小的搜索,甚至可能在 SO 上也会发现完全相同的问题。 (甚至更多)
  • 我也试过不使用 printf(),有一个 sleep(1);添加无限循环的语句;..

标签: linux signals


【解决方案1】:

在这里工作。

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

#include <stdarg.h> /* vsnprintf() */
#include <signal.h> /* signal */

void myprintf(FILE *fp, char *fmt, ...)
{
char buff[512];
int rc,fd;
va_list argh;
va_start (argh, fmt);

rc = vsnprintf(buff, sizeof buff, fmt, argh);
if (rc < 0  || rc >= sizeof buff) {
        rc = sprintf(buff, "Argh!: %d:\n", rc);
        }

if (!fp) fp = stderr;
fd = fileno(fp);
if (fd < 0) return;
if (rc > 0)  write(fd, buff, rc);
return;
}

void signal_handler(int sigNo)
{
switch (sigNo ) {
case SIGUSR1:
    myprintf(NULL, "received SIGUSR1 with value :- %d\n", SIGUSR1);
    break;
case SIGKILL:
    myprintf(NULL, "received SIGKILL with value :- %d\n", SIGKILL);
    break;
case SIGSTOP:
    myprintf(NULL, "received SIGSTOP with value :- %d\n", SIGSTOP);
    break;
default:
    myprintf(NULL, "Some other signal occured: %d\n", sigNo);
    break;
        }
return;
}

int main(void)
{
pid_t mypid;
mypid = getpid();
printf("process id is %d\n",  (int) mypid);

if(signal(SIGUSR1,signal_handler) == SIG_ERR)
        { printf("\n can't catch SIGSIGUSR1\n"); }
if(signal(SIGKILL,signal_handler)==SIG_ERR)
        { printf("\n can't catch SIGKILL\n"); }
if(signal(SIGSTOP,signal_handler)==SIG_ERR)
        { printf("\n can't catch SIGSTOP\n"); }
if(signal(SIGCONT,signal_handler)==SIG_ERR)
        { printf("\n can't catch SIGCONT\n"); }

while(1)  {
    sleep(1);
    }

return 0;
}

【讨论】:

    【解决方案2】:

    您可以正常捕获信号,但没有看到消息,因为您没有正确终止行,并且系统上的标准输出流是行缓冲的(假设您的程序在终端中运行)。

    标准 C 为输出流定义了三个级别的缓冲:

    • 无缓冲,输出立即传输
    • 行缓冲,当遇到换行符时传输输出
    • 完全缓冲,当内部缓冲区填满时传输输出

    (这是一种简化 - 请参阅 C 参考或标准了解详细信息)。

    考虑:

    #include <stdio.h>
    #include <unistd.h>
    
    int main(void)
    {
        printf("Hello");
        pause();
    }
    

    这不会在终端中产生任何输出。通过终止该行来修复它:

    printf("Hello\n");
    

    这将在终端中产生预期的输出。

    如果标准输出没有连接到终端——例如,你重定向到一个文件——那么流就变成了完全缓冲的。这个:

    ./a.out > foo
    

    Ctrl-C

    cat foo
    

    不产生任何输出,即使添加了换行符。在这里,您需要显式刷新以在缓冲区满之前传输输出。

    #include <stdio.h>
    #include <unistd.h>
    
    int main(void)
    {
        printf("Hello\n");
        fflush(stdout);
        pause();
    }
    

    即使重定向到文件也会产生输出。

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 2021-11-06
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      相关资源
      最近更新 更多