2012-12-26 107 views
1

這是我的第一個程序.... ctrlcsignal.cSIGUSR1沒有收到

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

,但它顯示沒什麼.... 也然後我試圖通過調用下面的函數int第一個節目......但沒有用.. enter code here殺(XXXX,SIGUSR1);

HELP ME .. !!!!

+1

1)你不應該在signalhandlers中使用printf() printf是不可重入的。2)你的main()會靜靜地退出,嘗試添加一個無限循環或sleep()或暫停()。 3),也許,你會想要添加一個waitXX() – wildplasser

+0

@wildplasser這個評論是很好的建議。你爲什麼不把它作爲答案發布? –

+0

因爲這都是微不足道的東西。美國的嫌疑人...一個小搜索,甚至可能在SO上揭示完全相同的問題。 (也許更多) – wildplasser

回答

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

你捕捉信號的所有權利,但沒有看到該消息,因爲你不終止線路正常,並在系統上的標準輸出流線緩衝(假設你的程序在終端運行)。

標準C定義了三種級別的緩衝的對於輸出流:

  • 無緩衝,其中輸出被立即傳送
  • 行緩衝,其中當遇到一個新行字符
  • 充分緩衝的輸出被髮送,當內部緩衝區填充時傳輸輸出

(這是一個簡化 - 詳見C參考或標準)。

考慮:

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

int main(void) 
{ 
    printf("Hello"); 
    pause(); 
} 

這產生沒有輸出中的終端。通過終止線修復:

printf("Hello\n"); 

這將產生在終端預期的輸出。

如果stdout未連接到一個終端 - 例如,你重定向到文件 - 然後流變成完全緩衝。此:

./a.out > foo 

Ctrl-C鍵

cat foo 

不產生輸出,即使有換行符加入。這裏你需要一個明確的清空來在緩衝區滿之前傳輸輸出。

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

int main(void) 
{ 
    printf("Hello\n"); 
    fflush(stdout); 
    pause(); 
} 

即使重定向到文件,也會產生輸出。