作业1:
要求实现AB进程对话
A进程先发送一句话给B进程,B进程接收后打印
B进程再回复一句话给A进程,A进程接收后打印
重复1.2步骤,当收到quit后,要结束AB进程
提示:两根管道
提示:用一个消息队列,两种类型即可
当对方输入quit后,退出AB进程删除消息队列;
进程A:
// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
long mytype; // 消息类型
char mtext[128]; // 消息内容
};
int main()
{
// 创建消息队列
key_t key = ftok("/home/ubuntu/", 1);
if (key < 0)
{
perror("ftok");
return -1;
}
// 通过key值找到对应的消息队列
int msqid = msgget(key, IPC_CREAT | 0664);
if (msqid < 0)
{
perror("msgget");
return -1;
}
struct msgbuf rcv; // 接收
struct msgbuf send; // 发送
printf("请注意:本程序通讯类型为2\n");
while (1)
{ // 接收阻塞
if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 2, 0) < 0)
{
perror("msgrcv");
return -1;
}
printf("收到:%s\n", rcv.mtext);
if (strcmp(rcv.mtext, "quit") == 0)
break;
// 发送
printf("请输入消息类型>>> ");
scanf("%ld", &send.mytype);
getchar();
printf("请输入消息内容>>> ");
scanf("%s", send.mtext);
getchar();
// 发送数据到消息队列中
if (msgsnd(msqid, &send, sizeof(send.mtext), 0) < 0)
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
if (strcmp(send.mtext, "quit")==0) // 如果发送quit,跳出循环
break;
}
return 0;
}
进程B:
// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct msgbuf
{
long mytype; // 消息类型
char mtext[128]; // 消息内容
};
int main()
{
// 创建消息队列
key_t key = ftok("/home/ubuntu/", 1);
if (key < 0)
{
perror("ftok");
return -1;
}
// 通过key值找到对应的消息队列
int msqid = msgget(key, IPC_CREAT | 0664);
if (msqid < 0)
{
perror("msgget");
return -1;
}
struct msgbuf rcv; // 接收
struct msgbuf send; // 发送
printf("请注意:本程序通讯类型为1\n");
while (1)
{
/***发送***/
printf("请输入消息类型:\t ");
scanf("%ld", &send.mytype);
getchar();
printf("请输入消息内容:\t ");
scanf("%s", send.mtext);
getchar();
if (msgsnd(msqid, &send, sizeof(send.mtext), 0) < 0) // 发送数据到消息队列中
{
perror("msgsnd");
return -1;
}
printf("发送成功\n");
if (strcmp(send.mtext, "quit") == 0) // 如果发送qUit,跳出循环
break;
/***接收阻塞***/
if (msgrcv(msqid, &rcv, sizeof(rcv.mtext), 1, 0) < 0)
{
perror("msgrcv");
return -1;
}
if (strcmp(rcv.mtext, "quit") == 0) // 如果接收到quit,跳出循环
break;
printf("收到:%s\n", rcv.mtext);
}
return 0;
}
作业2:
一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储:flag+字符串
逆置程序:
// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
/*
程序要求:一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储:flag+字符串
*/
int main()
{
// key_t ftok(const char *pathname, int proj_id);
key_t key = ftok("/home/ubuntu/", 'a'); // 创建
if (key < 0)
{
perror("ftok");
return -1;
}
// int shmget(key_t key, size_t size, int shmflg);
int shmid = shmget(key, 32, IPC_CREAT | 0664);
if (shmid < 0)
{
perror("shmget");
return -1;
}
// 将共享内存映射到用户空间中
void *addr = shmat(shmid, NULL, 0);
if ((void *)-1 == addr)
{
perror("shmat");
return -1;
}
/*倒置程序段*/
int *flag = (int *)addr;
*flag = 0;
char *str = (char *)addr + 4;
strcpy(str, "hello world");
while (1)
{
if (*flag == 1)
{
int i = 0, j = strlen(str) - 1;
while (i < j)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
* flag = 0;
}
}
return 0;
}
输出程序:
// C语言专用模板
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
/*
程序要求:一个进程对共享内存中的数据打印,另一个进程对共享内存中的数据倒置。
提示:共享内存中存储:flag+字符串
*/
int main()
{
// key_t ftok(const char *pathname, int proj_id);
key_t key = ftok("/home/ubuntu/", 'a'); // 创建
if (key < 0)
{
perror("ftok");
return -1;
}
// int shmget(key_t key, size_t size, int shmflg);
int shmid = shmget(key, 32, IPC_CREAT | 0664);
if (shmid < 0)
{
perror("shmget");
return -1;
}
// 将共享内存映射到用户空间中
void *addr = shmat(shmid, NULL, 0);
if ((void *)-1 == addr)
{
perror("shmat");
return -1;
}
/*输出段*/
int *flag = (int *)addr;
while (1)
{
if (*flag == 0)
{
printf("%s\n", (char *)addr + 4);
*flag = 1;
}
}
return 0;
}