UC進階編程--利用訊號實現處理序間通訊

來源:互聯網
上載者:User

標籤:blog   http   檔案   資料   os   2014   

/********************************************************************** *Copyright (c) 2014,TianYuan *All rights reserved. * * 檔案名稱: sigin.c * 檔案標識:無 * 內容摘要:利用訊號實現處理序間通訊,測試代碼. 把要發送的資料存放到檔案中,並發送訊號.* 其它說明:無 * 目前的版本: V1.0 * 作    者: wuyq * 完成日期: 20140711 * *修改記錄1:   //修改記錄,包括修改日期、版本號碼、修改人及修改內容等 *修改日期        版本號碼              修改人         修改內容 * -------------------------------------------------------------------------------------------------- * 20140711         V1.0                wuyq            建立 **********************************************************************/ #include <signal.h>#include <fcntl.h>#include <string.h>#include <stdio.h>int main(){pid_t who;char buf[100];int fd;printf("qing shu ru dui fang jin cheng id:");scanf("%d", &who);while(1){printf("input send data:");scanf(" %[^\n]", buf);while(1){fd = open("data", O_WRONLY|O_CREAT|O_EXCL, 0666);if(fd >= 0){break;}puts("檔案已經存在");sleep(1);}write(fd, buf, strlen(buf));close(fd);kill(who, SIGUSR1);//給進程發送訊號}return 0;}

/********************************************************************** *Copyright (c) 2014,TianYuan *All rights reserved. * * 檔案名稱: sigout.c * 檔案標識:無 * 內容摘要:利用訊號實現處理序間通訊,測試代碼.收到訊號後,從檔案讀取內容.* 其它說明:無 * 目前的版本: V1.0 * 作    者: wuyq * 完成日期: 20140711 * *修改記錄1:   //修改記錄,包括修改日期、版本號碼、修改人及修改內容等 *修改日期        版本號碼              修改人         修改內容 * -------------------------------------------------------------------------------------------------- * 20140711         V1.0                wuyq            建立 **********************************************************************/    #include <unistd.h>#include <signal.h>#include <fcntl.h>#include <string.h>#include <stdlib.h>#include <stdio.h>void func(int sig)//訊號處理函數{char buf[100];int fd = open("data", O_RDONLY);if(fd < 0) {return;}int len = read(fd, buf, sizeof(buf));if(len > 0){buf[len]='\0';puts(buf);//輸出檔案中讀取到的內容}close(fd);unlink("data");//刪除檔案}int main(){signal(SIGUSR1, func);printf("pid = %d\n", getpid());while(1){sleep(1);}return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.