1. 尝试处理普通信号
#include "test.h"#define MAXSIZE 128void handler(int signo)
{if (SIGINT == signo){printf("用户按下了 ctrl + c 键\n");}
}int main(int argc, char const *argv[])
{if (signal(SIGINT, SIG_IGN) == SIG_ERR){perror("signal error");return -1;}if (signal(SIGINT, SIG_DFL) == SIG_ERR){perror("signal error");return -1;}if (signal(SIGINT, handler) == SIG_ERR){perror("signal error");return -1;}while (1){printf("我真的还想再活五百年\n");sleep(1);}return 0;
}
2. 尝试捕获或忽略SIGKILL信号
#include "test.h"#define MAXSIZE 128void handler(int signo)
{if (SIGINT == signo){printf("用户按下了 ctrl + c 键\n");}if (SIGKILL == signo){printf("捕获了 SIGKILL 信号\n");}
}int main(int argc, char const *argv[])
{if (signal(SIGKILL, SIG_IGN) == SIG_ERR){perror("signal error");return -1;}if (signal(SIGKILL, handler) == SIG_ERR){perror("signal error");return -1;}if (signal(SIGKILL, SIG_DFL) == SIG_ERR){perror("signal error");return -1;}while (1){printf("我真的还想再活五百年\n");sleep(1);}return 0;
}
3. 使用SIGCHLD信号回收僵尸进程
#include "test.h"#define MAXSIZE 128void handler(int signo)
{if (SIGCHLD == signo){while (waitpid(-1, NULL, WNOHANG) > 0);}
}int main(int argc, char const *argv[])
{if (signal(SIGCHLD, handler) == SIG_ERR){perror("signal error");return -1;}for (int i = 0; i < 10; i++){if (fork() == 0){sleep(1);exit(EXIT_SUCCESS);}}while (1);return 0;
}
4. 模拟出牌案例
#include "test.h"#define MAXSIZE 128void handler(int signo)
{if (SIGALRM == signo){printf("系统为你随机出了一张牌\n");alarm(5);}
}int main(int argc, char const *argv[])
{if (signal(SIGALRM, handler) == SIG_ERR){perror("signal error");return -1;}alarm(5);char ch = 0;while (1){scanf("%c", &ch);getchar();printf("您出的牌为:%c\n", ch);alarm(5);}return 0;
}
5. 验证发送信号函数
#include "test.h"#define MAXSIZE 128void handler(int signo)
{if (SIGUSR1 == signo){printf("造化弄人呀\n");raise(SIGKILL);}
}int main(int argc, char const *argv[])
{if (signal(SIGUSR1, handler) == SIG_ERR){perror("signal error");return -1;}pid_t pid = fork();if (pid > 0){while (1){printf("我真的还想再活五百年\n");sleep(1);}}else if (pid == 0){sleep(5);printf("我是子进程,我要独立啦!\n");kill(getpid(), SIGUSR1);while (1){printf("富贵险中求\n");sleep(1);}}return 0;
}
6. 消息队列
#include "test.h"struct msgbuf
{long mtype;char mtext[1024];
};#define MSGSIZE sizeof(struct msgbuf) - sizeof(long)int main(int argc, char const *argv[])
{key_t key = 0;if ((key = ftok("/", 'k')) == -1){perror("ftok error");return -1;}printf("ftok success key = %#x\n", key);int msgid = -1;if ((msgid = msgget(key, IPC_CREAT|0664)) == -1){perror("msgget error");return -1;}printf("msgget success msgid = %d\n", msgid);struct msgbuf sbuf;while (1){bzero(sbuf.mtext, sizeof(sbuf.mtext));printf("请输入当前消息的类型:");scanf("%ld", &sbuf.mtype);getchar();printf("请输入消息正文:");fgets(sbuf.mtext, sizeof(sbuf.mtext), stdin);sbuf.mtext[strlen(sbuf.mtext) - 1] = 0;msgsnd(msgid, &sbuf, MSGSIZE, 0);printf("发送成功\n");if (strcmp(sbuf.mtext, "quit") == 0){break;}}return 0;
}
#include "test.h"struct msgbuf
{long mtype;char mtext[1024];
};#define MSGSIZE sizeof(struct msgbuf) - sizeof(long)int main(int argc, char const *argv[])
{key_t key = 0;if ((key = ftok("/", 'k')) == -1){perror("ftok error");return -1;}printf("ftok success key = %#x\n", key);int msgid = -1;if ((msgid = msgget(key, IPC_CREAT|0664)) == -1){perror("msgget error");return -1;}printf("msgget success msgid = %d\n", msgid);struct msgbuf rbuf;while (1){bzero(rbuf.mtext, sizeof(rbuf.mtext));msgrcv(msgid, &rbuf, MSGSIZE, 1, 0);printf("收到的消息为:%s\n", rbuf.mtext);if (strcmp(rbuf.mtext, "quit") == 0){break;}}return 0;
}
7. 使用消息队列完成两个进程间通信
#include "test.h"struct msgbuf
{long mtype;char mtext[1024];
};#define MSGSIZE sizeof(struct msgbuf) - sizeof(long)void send(int msgid, long msgtype)
{struct msgbuf sbuf;while (1){bzero(sbuf.mtext, sizeof(sbuf.mtext));printf("请输入消息正文:");fgets(sbuf.mtext, sizeof(sbuf.mtext), stdin);sbuf.mtext[strlen(sbuf.mtext) - 1] = 0;sbuf.mtype = msgtype;msgsnd(msgid, &sbuf, MSGSIZE, 0);printf("发送成功\n");if (strcmp(sbuf.mtext, "quit") == 0){break;}}
}void receive(int msgid, long msgtype)
{struct msgbuf rbuf;while (1){bzero(rbuf.mtext, sizeof(rbuf.mtext));printf("\r");msgrcv(msgid, &rbuf, MSGSIZE, msgtype, 0);printf("收到的消息为:%s\n", rbuf.mtext);printf("请输入消息正文:");fflush(stdout);if (strcmp(rbuf.mtext, "quit") == 0){break;}}
}int main(int argc, char const *argv[])
{key_t key = 0;if ((key = ftok("/", 'k')) == -1){perror("ftok error");return -1;}printf("ftok success key = %#x\n", key);int msgid = -1;if ((msgid = msgget(key, IPC_CREAT|0664)) == -1){perror("msgget error");return -1;}printf("msgget success msgid = %d\n", msgid);int flag;puts("请选择:");puts("1. 主发子收");puts("2. 主收子发");printf("请输入:");scanf("%d", &flag);getchar();pid_t pid = fork();if (pid > 0){if (flag == 1){send(msgid, 1);}else if (flag == 2){receive(msgid, 1);}}else if (pid == 0){if (flag == 1){receive(msgid, 2);}else if (flag == 2){send(msgid, 2);}}return 0;
}