msqid=msgget(SVKEY,0777|IPC_CREAT);
时间: 2024-05-27 09:09:21 浏览: 90
这是一个创建或获取 System V 消息队列的代码段。其中,SVKEY 是一个键值,用于唯一标识消息队列;0777 表示创建的消息队列权限为读、写、执行都允许;IPC_CREAT 表示如果 SVKEY 对应的消息队列不存在,则创建它。创建或获取消息队列成功后,函数会返回一个消息队列标识符 msqid,用于后续操作该消息队列。
相关问题
编写C程序,使用Linux中的IPC机制,完成 “石头、剪子、布”的游戏,代码示例
抱歉,由于该问题涉及程序编写,我可以回答。以下是示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
struct msgbuf {
long mtype;
char mtext[10];
};
#define MSG_SIZE 4
void game(int msqid, int player_id) {
struct msgbuf msg;
msg.mtype = player_id;
char choices[3][MSG_SIZE] = {"石头", "剪子", "布"};
int computer_choice = rand() % 3;
printf("电脑出了 %s\n", choices[computer_choice]);
printf("请输入您的选择(石头/剪子/布):");
fgets(msg.mtext, MSG_SIZE, stdin);
// 删除末尾的换行符
msg.mtext[strlen(msg.mtext) - 1] = '\0';
if (strcmp(msg.mtext, "石头") == 0 || strcmp(msg.mtext, "剪子") == 0 || strcmp(msg.mtext, "布") == 0) {
msgsnd(msqid, &msg, MSG_SIZE, 0);
msgrcv(msqid, &msg, MSG_SIZE, 3 - player_id, 0);
if (strcmp(msg.mtext, "平局") == 0) {
printf("平局\n");
} else if ((strcmp(msg.mtext, "石头") == 0 && computer_choice == 1) ||
(strcmp(msg.mtext, "剪子") == 0 && computer_choice == 2) ||
(strcmp(msg.mtext, "布") == 0 && computer_choice == 0)) {
printf("您赢了\n");
} else {
printf("您输了\n");
}
} else {
printf("请输入正确的选择(石头/剪子/布)\n");
}
}
int main() {
int msqid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
int pid = fork();
if (pid == 0) { // 子进程
game(msqid, 1);
} else if (pid > 0) { // 父进程
game(msqid, 2);
} else {
printf("进程创建失败\n");
}
msgctl(msqid, IPC_RMID, NULL);
return 0;
}
C语言实现FIFO算法,将这个项目编程三个例程,“发送者”、“接收者”和“路由器”。
下面是C语言实现FIFO算法的示例代码,包括发送者、接收者和路由器三个例程。
发送者(Sender):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_MSG_SIZE 512
struct msgbuf {
long mtype;
char mtext[MAX_MSG_SIZE];
};
int main(int argc, char **argv)
{
key_t key;
int msqid;
struct msgbuf msg;
int count = 0;
if (argc != 2) {
fprintf(stderr, "Usage: %s key\n", argv[0]);
exit(EXIT_FAILURE);
}
key = atoi(argv[1]);
msqid = msgget(key, IPC_CREAT | 0666);
if (msqid < 0) {
perror("msgget");
exit(EXIT_FAILURE);
}
while (1) {
sprintf(msg.mtext, "Message %d from sender", count++);
msg.mtype = 1;
if (msgsnd(msqid, &msg, strlen(msg.mtext)+1, 0) < 0) {
perror("msgsnd");
break;
}
printf("Sent message: %s\n", msg.mtext);
sleep(1);
}
msgctl(msqid, IPC_RMID, NULL);
return 0;
}
```
接收者(Receiver):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_MSG_SIZE 512
struct msgbuf {
long mtype;
char mtext[MAX_MSG_SIZE];
};
int main(int argc, char **argv)
{
key_t key;
int msqid;
struct msgbuf msg;
if (argc != 2) {
fprintf(stderr, "Usage: %s key\n", argv[0]);
exit(EXIT_FAILURE);
}
key = atoi(argv[1]);
msqid = msgget(key, IPC_CREAT | 0666);
if (msqid < 0) {
perror("msgget");
exit(EXIT_FAILURE);
}
while (1) {
if (msgrcv(msqid, &msg, MAX_MSG_SIZE, 1, 0) < 0) {
perror("msgrcv");
break;
}
printf("Received message: %s\n", msg.mtext);
}
msgctl(msqid, IPC_RMID, NULL);
return 0;
}
```
路由器(Router):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_MSG_SIZE 512
struct msgbuf {
long mtype;
char mtext[MAX_MSG_SIZE];
};
int main(int argc, char **argv)
{
key_t key;
int msqid[2];
struct msgbuf msg;
if (argc != 3) {
fprintf(stderr, "Usage: %s key1 key2\n", argv[0]);
exit(EXIT_FAILURE);
}
key = atoi(argv[1]);
msqid[0] = msgget(key, IPC_CREAT | 0666);
if (msqid[0] < 0) {
perror("msgget");
exit(EXIT_FAILURE);
}
key = atoi(argv[2]);
msqid[1] = msgget(key, IPC_CREAT | 0666);
if (msqid[1] < 0) {
perror("msgget");
exit(EXIT_FAILURE);
}
while (1) {
if (msgrcv(msqid[0], &msg, MAX_MSG_SIZE, 1, 0) < 0) {
perror("msgrcv");
break;
}
printf("Received message: %s\n", msg.mtext);
msg.mtype = 1;
if (msgsnd(msqid[1], &msg, strlen(msg.mtext)+1, 0) < 0) {
perror("msgsnd");
break;
}
printf("Sent message: %s\n", msg.mtext);
}
msgctl(msqid[0], IPC_RMID, NULL);
msgctl(msqid[1], IPC_RMID, NULL);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)