这是我的第一个程序….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 here杀死(xxxx,sigusr1);
帮助我。。!!!!啊!

最佳答案

在这里工作。

#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;
}

关于linux - 未收到SIGUSR1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14039277/

10-11 18:31