一,進程之間通訊方式有:訊號,管道,FIFO(具名管道),訊號量,訊息佇列,共用記憶體
二,訊號作用:
1.訊號是進程與進程之間或者進程與核心之間最小的通訊單位
2.訊號以1-31之間的數標示
3.進程啟動並執行任何時刻都可能接受到其他進程或核心發給他的訊號,並且使用訊號處理函數處理該訊號。處理完訊號後,返回到進程,繼續執行
此刻訊號的也即是一個非強制中斷
訊號舉例:
int kill (pid_t pid, int sig);
功能:向進程號為pid 的進程發送訊號sig
/* * fkill.c - Send a signal using kill(2) */#include <sys/types.h>#include <wait.h>#include <unistd.h>#include <signal.h>#include <stdio.h>#include <stdlib.h>int main(void){pid_t child;int errret; if((child = fork()) < 0) {perror("fork");exit(EXIT_FAILURE);} else if(child == 0) {/* in the child process */sleep(30);} else {/* in the parent *//* send a signal that gets ignored */printf("sending SIGCHLD to %d\n", child);errret = kill(child, SIGCHLD);if(errret < 0)perror("kill:SIGCHLD");elseprintf("%d still alive\n", child);/* now murder the child */printf("killing %d\n", child);if((kill(child, SIGTERM)) < 0)perror("kill:SIGTERM");/* have to wait to read the status */waitpid(child, NULL, 0 );}exit(EXIT_SUCCESS);}