信号の役割は何ですか?
各信号はキャッチがあなたのプロセスを終了または発生する例がたくさんで、すなわち何かを行いますしない限り、というデフォルトのアクションすなわちdisposition
を持っていた、あなたは十分な権限を持つシステム上の何かがあなたに信号を送信したことを知らせるためにコアダンプなど
いいえ、OSは信号をプロセスに送信します。それはどのように正確に行われますか?
OSは、それが望んでいるものは何でも行うことができます。私はあなたのプログラム内で任意の関数を呼び出すことができます。シグナルの場合はシグナルハンドラがセットされているかどうかをチェックし、そうであればシグナル番号でコールします。そうでなければ、そのシステムのデフォルトが何であるかを決める。
シグナルはどのように処理されたことがわかりますか?
到着時を知らせるハンドラを設定できます。ハンドラを設定しないと、デフォルトのアクションが発生します。次のコードで
はそれをコンパイルし、それを終了したり、様々なシグナル番号を、それを送信するためにキルを使用する
CTRL-c
を使用し、それを実行します。これはシグナルを捕まえることを示しています。
CAVEAT。以下のコードはMacとLinux上で実行されており、signal_handler関数を設定するためにsignalを使う方法の例としてここでは確かに移植可能でない場合、整数値について多くの仮定をしています。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
extern char *taunts[];
int taunt();
void sig_handler(int signo) {
switch(signo) {
case SIGHUP: printf("\t%s %d\n", taunts[taunt()], SIGHUP);break;
case SIGINT: printf("\t%s %d\n", taunts[taunt()], SIGINT);break;
case SIGQUIT: printf("\t%s %d\n", taunts[taunt()], SIGQUIT);break;
case SIGILL: printf("\t%s %d\n", taunts[taunt()], SIGILL);break;
case SIGTRAP: printf("\t%s %d\n", taunts[taunt()], SIGTRAP);break;
case SIGABRT: printf("\t%s %d\n", taunts[taunt()], SIGABRT);break;
case SIGFPE: printf("\t%s %d\n", taunts[taunt()], SIGFPE);break;
case SIGKILL: printf("\t%s %d\n", taunts[taunt()], SIGKILL);break;
case SIGBUS: printf("\t%s %d\n", taunts[taunt()], SIGBUS);break;
case SIGSEGV: printf("\t%s %d\n", taunts[taunt()], SIGSEGV);break;
case SIGSYS: printf("\t%s %d\n", taunts[taunt()], SIGSYS);break;
case SIGPIPE: printf("\t%s %d\n", taunts[taunt()], SIGPIPE);break;
case SIGALRM: printf("\t%s %d\n", taunts[taunt()], SIGALRM);break;
case SIGTERM: printf("\t%s %d\n", taunts[taunt()], SIGTERM);break;
default: printf("\tMerde\n");
}
#ifdef __linux__
if (signal(signo, sig_handler) == SIG_ERR) {
printf("Merde: %d\n", signo);
}
#endif
}
int main(void) {
srand(time(NULL));
int i = 0;
for(i = 0; i < 15; i ++) {
if (signal(i, sig_handler) == SIG_ERR) {
printf("Merde: %d\n", i);
}
}
while(1) {
sleep(10);
}
return 0;
}
int taunt() {
return rand() % 12;
}
char *taunts[13] = {
"\n No, now go away or I shall taunt you a second time! With youri\n rubbish signal... ",
"\n You don't frighten us, English pig dogs with your daft signal... ",
"\n Go and boil your bottoms, you sons of a silly person and your\n \
nincompoop signal... ",
"\n I blow my nose at you, so-called \"Arthur King,\" you and all\n \
your silly English K-nig-hts. With your silly signal... ",
"\n I'm French. Why do you think I have this outrageous accent, you\n \
silly king? With your silly signal... ",
"\n Mind your own business With your ludicrous signal... ",
"\n You don't frighten us with your silly knees-bent running around\n \
advancing behavior! With your silly signal... ",
"\n How you English say, I one more time-a unclog my nose in your\n"
" direction, sons of a window-dresser! With your weak signal... ",
"\n No chance, English bedwetting types. With your outrageous signal... ",
"\n Yes, depart a lot at this time and cut the approaching any more\n \
or we fire arrows at the tops of your heads and make castanets\n \
out of your testiclesi already! With your stupido signal.."
"\n And now remain gone illegitimate faced buggerfolk! and your farty\n signal... ",
"\n And, if you think you got nasty taunting this time, you ain't\n heard nothing yet! I signal in your general direction... ",
"\n Daffy English kniggets! Thpppt!... ",
};
は、Linuxのスケジューラがの – red0ct
が重複する可能性がどのように機能するかについて読むことが有用であろう[どのようにLinux上で実行された非同期シグナルハンドラです?](http://stackoverflow.com/questions/6949025/how-are-asynchronous -signal-handler-executed-linux) – shodanex