UNIX環境高級編程學習之第十章信號-用信號實現父子進程同步
/* FileName:ParentAndChildProcessSynchronous.c Date: 20100309 Description: 用信號實現父子進程同步。 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <signal.h> static volatile sig_atomic_t sigflag; static sigset_t newmask, oldmask, zeromask; static void sig_usr(int signo) { sigflag = 1; } void TELL_WAIT(void) { if (signal(SIGUSR1, sig_usr) == SIG_ERR) { printf("signal(SIGUSR1) ERROR /n"); exit(-1); } if (signal(SIGUSR2, sig_usr) == SIG_ERR) { printf("signal(SIGUSR2) ERROR /n"); exit(-1); } sigemptyset(&zeromask); sigemptyset(&newmask); sigaddset(&newmask, SIGUSR1); sigaddset(&newmask, SIGUSR2); if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) { printf("SIB_BLOCK ERROR /n"); exit(-1); } } void TELL_PARENT(pid_t pid) { kill(pid, SIGUSR2); } void WAIT_PARENT(void) { while (sigflag == 0) { sigsuspend(&zeromask); } sigflag = 0; if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) { printf("SIG_SETMASK ERROR/n"); exit(-1); } } void TELL_CHILD(pid_t pid) { kill(pid, SIGUSR1); } void WAIT_CHILD(void) { while (sigflag == 0) { sigsuspend(&zeromask); } sigflag = 0; if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) { printf("SIG_SETMASK ERROR/n"); exit(-1); } } static void charatatime(char* ); int main(int argc, char* argv[]) { pid_t pid; TELL_WAIT(); if ((pid = fork()) < 0) { printf("fork error/n"); exit(-1); }else if (pid == 0){ WAIT_PARENT(); charatatime("output from child/n"); }else{ charatatime("output from parent/n"); TELL_CHILD(pid); } return 0; } static void charatatime(char* str) { char* ptr; int c; setbuf(stdout, NULL); for (ptr = str; (c = *ptr++) != 0;) // 無緩衝的標準輸出 { putc(c, stdout); } }
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是一位非常優秀的