有P1,P2,P3三个进程,P1和P2负责从键盘接收字符串,均发送给P3,P3接收到字符串,根据发送方分别显示”P3 received *** from P1(或P2)" ;分别用管道通信,消息队列和共享存储三种通信方式实现。
时间: 2023-06-24 22:05:20 浏览: 173
管道通信方式实现:
P1和P2创建两个无名管道,分别用于向P3发送数据。P3创建两个无名管道分别用于接收来自P1和P2的数据。
P1和P2创建进程后,循环读取键盘输入字符串,将字符串写入到对应的管道中。P3在循环中不断从两个管道中读取数据,并根据发送方的不同显示不同的信息。
P1代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUF_SIZE 256
int main(void) {
int fd[2];
char buf[BUF_SIZE];
if (pipe(fd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter a string:");
fgets(buf, BUF_SIZE, stdin);
write(fd[1], buf, strlen(buf) + 1);
}
close(fd[1]);
return 0;
}
```
P2代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUF_SIZE 256
int main(void) {
int fd[2];
char buf[BUF_SIZE];
if (pipe(fd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter a string:");
fgets(buf, BUF_SIZE, stdin);
write(fd[1], buf, strlen(buf) + 1);
}
close(fd[1]);
return 0;
}
```
P3代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUF_SIZE 256
int main(void) {
int fd1[2], fd2[2];
char buf[BUF_SIZE];
int n;
if (pipe(fd1) == -1 || pipe(fd2) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
while (1) {
n = read(fd1[0], buf, BUF_SIZE);
if (n > 0) {
printf("P3 received %s from P1\n", buf);
}
n = read(fd2[0], buf, BUF_SIZE);
if (n > 0) {
printf("P3 received %s from P2\n", buf);
}
}
close(fd1[0]);
close(fd2[0]);
return 0;
}
```
消息队列通信方式实现:
P1和P2创建消息队列,并循环读取键盘输入的字符串,将字符串发送到消息队列中。P3在循环中不断从消息队列中接收消息,并根据发送方的不同显示不同的信息。
P1代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>
#define BUF_SIZE 256
struct message {
long type;
char text[BUF_SIZE];
};
int main(void) {
int msgid;
struct message msg;
msgid = msgget(IPC_PRIVATE, 0644);
if (msgid == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter a string:");
fgets(msg.text, BUF_SIZE, stdin);
msg.type = 1;
if (msgsnd(msgid, &msg, sizeof(msg.text), 0) == -1) {
perror("msgsnd");
exit(EXIT_FAILURE);
}
}
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(EXIT_FAILURE);
}
return 0;
}
```
P2代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>
#define BUF_SIZE 256
struct message {
long type;
char text[BUF_SIZE];
};
int main(void) {
int msgid;
struct message msg;
msgid = msgget(IPC_PRIVATE, 0644);
if (msgid == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter a string:");
fgets(msg.text, BUF_SIZE, stdin);
msg.type = 2;
if (msgsnd(msgid, &msg, sizeof(msg.text), 0) == -1) {
perror("msgsnd");
exit(EXIT_FAILURE);
}
}
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(EXIT_FAILURE);
}
return 0;
}
```
P3代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>
#define BUF_SIZE 256
struct message {
long type;
char text[BUF_SIZE];
};
int main(void) {
int msgid;
struct message msg;
int n;
msgid = msgget(IPC_PRIVATE, 0644);
if (msgid == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}
while (1) {
n = msgrcv(msgid, &msg, sizeof(msg.text), 0, 0);
if (n == -1) {
perror("msgrcv");
exit(EXIT_FAILURE);
}
if (msg.type == 1) {
printf("P3 received %s from P1\n", msg.text);
} else if (msg.type == 2) {
printf("P3 received %s from P2\n", msg.text);
}
}
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(EXIT_FAILURE);
}
return 0;
}
```
共享存储通信方式实现:
P1和P2创建共享内存,并循环读取键盘输入的字符串,将字符串写入到共享内存中。P3在循环中不断从共享内存中读取数据,并根据发送方的不同显示不同的信息。
P1代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#define BUF_SIZE 256
int main(void) {
int fd;
char *ptr;
struct stat buf;
fd = shm_open("/shm", O_CREAT | O_RDWR, 0664);
if (fd == -1) {
perror("shm_open");
exit(EXIT_FAILURE);
}
if (fstat(fd, &buf) == -1) {
perror("fstat");
exit(EXIT_FAILURE);
}
if (buf.st_size == 0) {
if (ftruncate(fd, BUF_SIZE) == -1) {
perror("ftruncate");
exit(EXIT_FAILURE);
}
}
ptr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter a string:");
fgets(ptr, BUF_SIZE, stdin);
}
if (munmap(ptr, BUF_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
if (shm_unlink("/shm") == -1) {
perror("shm_unlink");
exit(EXIT_FAILURE);
}
return 0;
}
```
P2代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#define BUF_SIZE 256
int main(void) {
int fd;
char *ptr;
struct stat buf;
fd = shm_open("/shm", O_CREAT | O_RDWR, 0664);
if (fd == -1) {
perror("shm_open");
exit(EXIT_FAILURE);
}
if (fstat(fd, &buf) == -1) {
perror("fstat");
exit(EXIT_FAILURE);
}
if (buf.st_size == 0) {
if (ftruncate(fd, BUF_SIZE) == -1) {
perror("ftruncate");
exit(EXIT_FAILURE);
}
}
ptr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter a string:");
fgets(ptr, BUF_SIZE, stdin);
}
if (munmap(ptr, BUF_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
if (shm_unlink("/shm") == -1) {
perror("shm_unlink");
exit(EXIT_FAILURE);
}
return 0;
}
```
P3代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#define BUF_SIZE 256
int main(void) {
int fd;
char *ptr;
int n;
fd = shm_open("/shm", O_RDWR, 0664);
if (fd == -1) {
perror("shm_open");
exit(EXIT_FAILURE);
}
ptr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
while (1) {
n = read(STDIN_FILENO, ptr, BUF_SIZE);
if (n > 0) {
printf("P3 received %s from %s\n", ptr, (ptr[0] == 'P') ? "P1" : "P2");
}
}
if (munmap(ptr, BUF_SIZE) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
return 0;
}
```
阅读全文