#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
typedef struct
{
long mtype;
char mtext[128];
} msgbuff;
int main(int argc, char const *argv[])
{
key_t key = ftok("./", 2);
if (key < 0)
{
perror("ftok");
return -1;
}
int msgid = msgget(key, IPC_CREAT | 0777);
if (msgid < 0)
{
perror("msgid");
return -1;
}
msgbuff buff;
buff.mtype = 1;
strcpy(buff.mtext, "123456");
if (fork()) // 翻转进程
{
while (1)
{
for (int i = 0; i < 6 / 2l; i++)
{
int temp = buff.mtext[i];
buff.mtext[i] = buff.mtext[5 - i];
buff.mtext[5 - i] = temp;
}
if (msgsnd(msgid, &buff, sizeof(buff.mtext), 0) < 0)
{
perror("msgsnd");
return -1;
}
sleep(1);
}
}
else // 打印进程
{
while (1)
{
if (msgrcv(msgid, &buff, sizeof(buff.mtext), 1, 0) < 0)
{
perror("msgrcv");
return -1;
}
printf("%s\n", buff.mtext);
}
}
return 0;
}
2022-11-6
最新推荐文章于 2024-08-02 17:56:54 发布