1、有P1,P2,P3三个进程,P1和P2负责从键盘接收字符串,均发送给P3,P3接收到字符串,根据发送方分别显示”P3 received *** from P1(或P2)" ;在一个程序中用c语言共享存储通信方式实现。
时间: 2024-05-13 14:19:53 浏览: 19
以下是一个使用共享内存实现的示例程序:
P1进程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024
int main() {
int shmid;
char *shmaddr;
char input[SHM_SIZE];
// 创建共享内存
shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget error");
exit(EXIT_FAILURE);
}
// 连接共享内存
shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (char *)-1) {
perror("shmat error");
exit(EXIT_FAILURE);
}
while (1) {
printf("P1: ");
fgets(input, SHM_SIZE, stdin);
// 将输入的字符串写入共享内存
strcpy(shmaddr, input);
// 发送信号通知P3进程
kill(getpid() + 2, SIGUSR1);
}
return 0;
}
```
P2进程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SHM_SIZE 1024
int main() {
int shmid;
char *shmaddr;
char input[SHM_SIZE];
// 创建共享内存
shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget error");
exit(EXIT_FAILURE);
}
// 连接共享内存
shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (char *)-1) {
perror("shmat error");
exit(EXIT_FAILURE);
}
while (1) {
printf("P2: ");
fgets(input, SHM_SIZE, stdin);
// 将输入的字符串写入共享内存
strcpy(shmaddr, input);
// 发送信号通知P3进程
kill(getpid() + 2, SIGUSR1);
}
return 0;
}
```
P3进程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#define SHM_SIZE 1024
void sigusr1_handler(int sig) {
printf("P3 received: %s", shmat(*(int *)sig, NULL, 0));
printf(" from P%d\n", sig - getpid() - 2);
}
int main() {
int shmid;
char *shmaddr;
struct sigaction sa;
// 创建共享内存
shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget error");
exit(EXIT_FAILURE);
}
// 连接共享内存
shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (char *)-1) {
perror("shmat error");
exit(EXIT_FAILURE);
}
// 注册信号处理函数
sa.sa_handler = sigusr1_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGUSR1, &sa, NULL) == -1) {
perror("sigaction error");
exit(EXIT_FAILURE);
}
// 将共享内存id传递给P1和P2进程
kill(getpid() - 2, shmid);
kill(getpid() - 1, shmid);
while (1) {
pause();
}
// 断开共享内存连接
if (shmdt(shmaddr) == -1) {
perror("shmdt error");
exit(EXIT_FAILURE);
}
// 删除共享内存
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl error");
exit(EXIT_FAILURE);
}
return 0;
}
```
程序的实现流程如下:
1. P1和P2进程分别创建一个共享内存,并将共享内存id通过信号发送给P3进程。
2. P1和P2进程不断从键盘读入字符串,将字符串写入各自的共享内存中,并发送信号通知P3进程。
3. P3进程接收到信号后,根据信号携带的共享内存id连接到对应的共享内存,读取字符串并输出收到的字符串及发送方信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)