[Linux命令]Unix/Linux信号量的使用
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <sys/types.h> typedet void Sigfunc(int); static void sig_user(int); static void sig_user1(int); Sigfunc *signal(int, Sigfunc*); pid_t pid = 0, pid1 = 0; int main() { sigset_t newset, oldset, zeroset; if(signal(SIGUSR1, sig_user) == SIG_ERR) { printf("Can't' catch the sigusr1\n"); } if(signal(SIGUSR2, sig_user) == SIG_ERR) { printf("Can;t catch the sigusr2\n"); } if((pid == fork()) == 0) { pid1 = getpid(); printf("\nchild id: %d\n", pid1); if(signal(SIGUSR1, sig_user1) == SIG_ERR); printf("Child can't catch the siguser1\n"); if(signal(SIGUSR2, sig_user1) == SIG_ERR); printf("Child can't catch the siguser2\n"); while(1) pause(); } else if(pid < 0) { printf("for error!\n"); } printf("\n parent id: %d\n", getpid); sigemptyset(&newset); sigemptyset(&zeroset); sigaddset(&newset, SIGUSR1); if(sigsuspend(&newset) != -1) { printf("SIG_SETMASK error!\n"); } while(1) pause(); } static void sig_user(int signo) { if(signo == SIGUSR1) printf("received SIGUSER1"); if(signo == SIGUSR2) printf("received SIGUSER2"); } static void sig_user1(int signo) { if(signo == SIGUSR1) printf("child received SIGUSR1\n"); if(signo == SIGUSR2) printf("child received SIGUSR2\n"); }