赞
踩
Linux作业:有P1,P2,P3三个进程,P1和P2负责从键盘接收字符串,均发送给P3,P3接收到字符串,根据发送方分别显示”P3 received *** from P1(或P2)" ;分别用管道通信,消息队列和共享存储三种通信方式实现。
管道
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<string.h>
-
- int main(void){
- int fd1[2],fd2[2];
-
- if(pipe(fd1) == -1){
- perror("pipe1");
- exit(EXIT_FAILURE);
- }
-
- if(pipe(fd2) == -1){
- perror("pipe2");
- exit(EXIT_FAILURE);
- }
-
- if(fork() == 0){
- close(fd1[0]);//关闭P1读端
- close(fd2[0]);//关闭P2读端
- close(fd2[1]);//关闭P2写端
-
- char buffer[100];
- //读取输入的内容
- printf("Input in P1:");
- scanf("%s",buffer);
- write(fd1[1],buffer,strlen(buffer)+1);
- exit(EXIT_SUCCESS);
- }
- wait(NULL);
- if(fork() == 0){
- close(fd1[0]);
- close(fd1[1]);
- close(fd2[0]);
-
- char buffer[100];
- printf("Input in P2:");
- scanf("%s",buffer);
- write(fd2[1],buffer,strlen(buffer)+1);
- exit(EXIT_SUCCESS);
- }
- wait(NULL);
- //关闭写通道
- close(fd1[1]);
- close(fd2[1]);
-
- char buffer[100];
- int read_bytes;
- while(1){
- if((read_bytes = read(fd1[0],buffer,100))>0){
- printf("P3 received %s from P1\n",buffer);
- }
- if((read_bytes = read(fd2[0],buffer,100))>0){
- printf("P3 received %s from P2\n",buffer);
- }
- }
-
- return 0;
- }
消息队列
P1:
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
-
- #define MSGKEY 1307
- #define text_size 1024
-
- struct msgform {
- long mtype;
- char mtext[text_size];
- };
-
- int main(void) {
- struct msgform msg;
- int i,msgqid;
- // 获取消息队列标识符
- if ((msgqid = msgget(MSGKEY, 0777)) == -1) {
- perror("msgget");
- exit(EXIT_FAILURE);
- }
- while(1){
- printf("Input in P1:");
- scanf("%s",msg.mtext);
- msg.mtype = 2;
- msgsnd(msgqid, &msg, strlen(msg.mtext) + 1, 0);
- }
- return 0;
- }
P2:
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
-
- #define MSGKEY 1307
- #define text_size 1024
-
- struct msgform {
- long mtype;
- char mtext[text_size];
- };
-
- int main(void) {
- struct msgform msg;
- int i,msgqid;
-
- // 获取消息队列标识符
- if ((msgqid = msgget(MSGKEY, 0777)) == -1) {
- perror("msgget");
- exit(EXIT_FAILURE);
- }
- while(1){
- printf("Input in P2:");
- scanf("%s",msg.mtext);
- msg.mtype = 2;
- msgsnd(msgqid, &msg, strlen(msg.mtext) + 1, 0);
- }
-
- return 0;
- }
P3:
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- #include <string.h>
-
- #define MSGKEY 1307
- #define text_size 1024
-
- struct msgform {
- long mtype;
- char mtext[text_size];
- };
-
- int msgqid;
-
- void cleanup() {
- msgctl(msgqid, IPC_RMID, 0);
- exit(0);
- }
-
- int main(void) {
- struct msgform msg;
- int i;
- pid_t pid3;
-
- for (i = 0; i < 23; i++) {
- signal(i, cleanup);
- }
- // 创建消息队列
- if ((msgqid = msgget(MSGKEY, 0777 | IPC_CREAT)) == -1) {
- perror("msgget");
- exit(EXIT_FAILURE);
- }
-
- if ((pid3 = fork()) == -1) {
- perror("pid3");
- exit(EXIT_FAILURE);
- } else if (pid3 == 0) {
- for (;;) {
- msgrcv(msgqid, &msg, text_size, 1, 0);
- printf("P3 received %s from P1\n", msg.mtext);
- }
- exit(EXIT_SUCCESS);
- } else {
- if ((pid3 = fork()) == -1) {
- perror("pid3");
- exit(EXIT_FAILURE);
- } else if (pid3 == 0) {
- for (;;) {
- msgrcv(msgqid, &msg, text_size, 2, 0);
- printf("P3 received %s from P2\n", msg.mtext);
- }
- exit(EXIT_SUCCESS);
- }
- // 父进程等待子进程执行完毕
- wait(NULL);
- return 0;
- }
- }
共享存储:
- #include<unistd.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/ipc.h>
- #include<sys/shm.h>
- #include<string.h>
-
- #define MAXSIZE 1024
-
- int main(void){
- pid_t pid1,pid2,pid3;
- int shmid_1,shmid_2;
- char *shmptr_1,*shmptr_2;
- char input[MAXSIZE];
-
- // 创建共享内存
- if((shmid_1 = shmget(IPC_PRIVATE,MAXSIZE,IPC_CREAT|0666))==-1){
- perror("shmget_1");
- exit(EXIT_FAILURE);
- }
- if((shmid_2 = shmget(IPC_PRIVATE,MAXSIZE,IPC_CREAT|0666))==-1){
- perror("shmget_2");
- exit(EXIT_FAILURE);
- }
-
- // 将共享内存段连接到当前进程的地址空间
- if((shmptr_1= (char*)shmat(shmid_1,0,0)) == (char*)-1){
- perror("shmat_1");
- exit(EXIT_FAILURE);
- }
- if((shmptr_2 = (char*)shmat(shmid_2,0,0)) == (char*)-1){
- perror("shmat_2");
- exit(EXIT_FAILURE);
- }
-
- // P1
- if((pid1 = fork()) ==-1 ){
- perror("pid1");
- exit(EXIT_FAILURE);
- }
- else if (pid1 == 0){
- printf("Input in P1:");
- scanf("%s",shmptr_1);
-
- exit(EXIT_SUCCESS);
- }//P2
- else{
- wait(NULL);
- if((pid2 = fork()) ==-1 ){
- perror("pid2");
- exit(EXIT_FAILURE);
- }
- else if (pid2 == 0){
- printf("Input in P2:");
- scanf("%s",shmptr_2);
-
- exit(EXIT_SUCCESS);
- }
- }
- // 等待子进程结束
- wait(NULL);
- wait(NULL);
-
- printf("P3 received %s from P1\n",shmptr_1);
- printf("P3 received %s from P2\n",shmptr_2);
-
- // 分离共享内存
- shmdt(shmptr_1);
- shmdt(shmptr_2);
-
- // 删除共享内存段
- shmctl(shmid_1, IPC_RMID, NULL);
- shmctl(shmid_2, IPC_RMID, NULL);
-
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。