UNIX環境高級編程學習之第十五章進程間通信 - 通過有名管道實現進程間通信
UNIX環境高級編程學習之第十五章進程間通信 - 通過命名管道實現進程間通信
/* User:Lixiujie * Date:20100820 * Desc:通過有名管道實現進程間通信 * File:writeFifo.c * gcc writeFifo.c -o writeFifo */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <time.h> int main(void){ int fd = open("fifo", O_WRONLY); if (fd < 0){ mode_t mode = 0666; if (mkfifo("fifo", mode) < 0){ perror("mkfifo failed!"); exit(1); } fd = open("fifo", O_WRONLY); if (fd < 0){ perror("open failed!"); exit(1); } } int i; time_t t; for (i = 0;i < 10;i++){ char szBuf[512] = { 0x00 }; time(&t); sprintf(szBuf, "writeFifo %d Send:%s", getpid(), ctime(&t)); if (write(fd, szBuf, strlen(szBuf) + 1) < 0){ perror("write failed!"); exit(1); } sleep(3); } close(fd); return 0; }
/* User:Lixiujie * Date:20100820 * Desc:通過有名管道實現進程間通信 * File:readFifo.c * gcc readFifo.c -o readFifo */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(void){ int fd = open("fifo", O_RDONLY); if (fd < 0){ perror("mkfifo failed!"); exit(1); } char szBuf[512] = { 0x00 }; while (read(fd, szBuf, sizeof(szBuf)) > 0){ printf(szBuf); fflush(stdout); memset(szBuf, 0x00, sizeof(szBuf)); } close(fd); return 0; }
注意:當最後一個引用FIFO的進程終止時,其名字仍保留在系統中,數據刪除。
UNIX環境高級編程學習之第十一章線程-線程的創建、退出、等待、取消、分離 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #i
UNIX環境高級編程學習之第五章標準IO-實現查看所有用戶 #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(i
UNIX環境高級編程學習之第六章系統數據文件和信息-修改第四章實現的Shell的“ls -l”功能, 加入顯示文件的用戶名和組名 // 只能查看目錄中的所有文件屬性 #include <stdio.h> #include <stdlib.
UNIX環境高級編程學習之第十章信號-信號集的操作,讓進程阻塞SIGQUIT信號 /* File: sigprocmask.c Desc: 信號集的操作,讓進程阻塞SIGQUIT信號 */ #include <stdio.h> #in
UNIX環境高級編程學習之第八章進程控制-用父子進程實現打開kate,返回kate關閉狀態 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unis
UNIX環境高級編程學習之第三章文件IO-文件讀操作 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include
UNIX環境高級編程學習之第十章信號-信號的基本操作(綁定/忽略/默認/發送) /* File: signal.c Desc: 信號基本操作 */ #include <stdio.h> #include <stdlib.h> #inc
到現在,我Unix,數據庫的掃盲基本完成了,就差網絡這一塊了。從今天開始網絡掃盲。上週五開始看HTTPD的實現,我們的httpd是AA(AA是我們SBE的元老之一)開發的,只要是AA開發的東西,學習一遍都很有收穫。因爲AA是一位非常優秀的