linux 進程通訊之訊號

來源:互聯網
上載者:User

一,進程之間通訊方式有:訊號,管道,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);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.