第一题: 使用消息队列实现2个终端之间的互相聊天功能
#include <mqueue.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #define MSG_QUEUE_NAME "/chat_queue" #define BUFFER_SIZE 100 int main(int argc, char *argv[]) { mqd_t mq = mq_open(MSG_QUEUE_NAME, O_RDWR); if (mq == (mqd_t)-1) { perror("mq_open error"); exit(EXIT_FAILURE); } char buffer[BUFFER_SIZE]; ssize_t n; while (1) { printf("Enter your message: "); fgets(buffer, BUFFER_SIZE, stdin); mq_send(mq, buffer, strlen(buffer), 0); printf("Message sent\n"); n = mq_receive(mq, buffer, BUFFER_SIZE, NULL); if (n > 0) printf("Received message: %.*s\n", (int)n, buffer); else if (n == -1) { if (errno == EAGAIN) { printf("No messages in the queue\n"); } else { perror("mq_receive error"); exit(EXIT_FAILURE); } } } mq_close(mq); return 0; }
第二题: 创建一对父子进程,父进程循环输出 A,子进程循环输出a 使用信号灯集同步这对父子进程,实现效果 A_a_A_a ..... _表示休眠1秒
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <unistd.h> #include <stdio.h> union semun { int val; struct semid_ds *buf; unsigned short *array; }; void set_value(int semid, int index, int value) { union semun sem_union; sem_union.val = value; semctl(semid, index, SETVAL, sem_union); } int main() { int semid = semget(IPC_PRIVATE, 2, IPC_CREAT | 0666); set_value(semid, 0, 0); // 父进程信号量初始化为0 set_value(semid, 1, 1); // 子进程信号量初始化为1 int pid = fork(); if (pid == 0) { // 子进程 while (1) { semop(semid, &(struct sembuf){1, 0, SEM_UNDO}), 1); // 等待父进程信号 printf("a"); semop(semid, &(struct sembuf){0, 1, SEM_UNDO}), 1); // 通知父进程 sleep(1); } } else { // 父进程 while (1) { semop(semid, &(struct sembuf){1, -1, SEM_UNDO}), 1); // 等待子进程信号 printf("A"); semop(semid, &(struct sembuf){0, 1, SEM_UNDO}), 1); // 通知子进程 sleep(1); } } return 0; }
第三题: 使用共享内存 + 信号灯集实现2个终端之间互相聊天
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #define SHM_KEY 1234 #define SEM_KEY 5678 #define BUF_SIZE 100 union semun { int val; struct semid_ds *buf; unsigned short *array; }; void set_value(int semid, int index, int value) { union semun sem_union; sem_union.val = value; semctl(semid, index, SETVAL, sem_union); } int main(int argc, char *argv[]) { int shm_id, sem_id; char *shm; struct sembuf op; shm_id = shmget(SHM_KEY, BUF_SIZE, 0666 | IPC_CREAT); if (shm_id == -1) { perror("shmget error"); exit(EXIT_FAILURE); } shm = shmat(shm_id, NULL, 0); if (shm == (char *)(-1)) { perror("shmat error"); exit(EXIT_FAILURE); } sem_id = semget(SEM_KEY, 1, 0666 | IPC_CREAT); set_value(sem_id, 0, 1); while (1) { op.sem_num = 0; op.sem_op = -1; op.sem_flg = SEM_UNDO; semop(sem_id, &op, 1); // 等待信号 printf("Enter your message: "); fgets(shm, BUF_SIZE, stdin); printf("Message sent\n"); op.sem_op = 1; semop(sem_id, &op, 1); // 发送信号 sleep(1); } shmdt(shm); return 0; }
07-24
20
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-24
24
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-24
22
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-19
289
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-12