gpt4 book ai didi

c - 消息队列。消息发送 : Invalid argument

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:47 26 4
gpt4 key购买 nike

任务是:查找指定区间内的所有质数,这些数被分成几个范围。每个范围都在生成的过程中处理。好吧,我真的努力做到了。但是出了点问题。无法理解我的错误。代码:

#include <stdio.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <stdbool.h>

#define MAX_SEND_SIZE 50

#define PROCESS_NUMBER 5

struct mymsgbuf {
long mtype;
int marray[MAX_SEND_SIZE];
};
typedef struct mymsgbuf type_mymsgbuf;

int msgqid, rc;

void send_message(int qid, type_mymsgbuf* qbuf, long type, int* text, int numbers_per_process) {
int i;
int length = sizeof(struct mymsgbuf) - sizeof(long);
qbuf->mtype = type;
for (i = 0; i < numbers_per_process; i++) {
qbuf->marray[i] = text[i];
}
if ((msgsnd(qid, (struct msgbuf*)qbuf, (size_t) length, 0)) == -1) {
perror("msgsnd");
exit(1);
}
}

void read_message(int qid, type_mymsgbuf* qbuf, long type, int numbers_per_process) {
int length = sizeof(struct mymsgbuf) - sizeof(long);
qbuf->mtype = type;
msgrcv(qid, (struct msgbuf*)qbuf, (size_t)length, type, 0);
for (int i = 0; i < numbers_per_process && qbuf->marray[i] != 0; i++) {
printf("%d\n", qbuf->marray[i]);
}
}

bool isprime (int a) {
if(a == 1) {
return false;
}
for (int i = 2; i*i < a; ++i) {
if ((a % i) == 0) {
return false;
}
}
return true;
}

int* find_primes_part (int a, int b) {
int j = 0;
int* primes = (int*)malloc(sizeof(int)*(b-a));
for (int k = 0; k < (b-a); ++k) {
primes[k] = 0;
}
for (int i = a; i < b; ++i) {
if(isprime(i)) {
primes[j] = i;
printf("%d\n TEST", primes[j]);
j++;
}
}
return primes;
}

int find_primes(int a, int b) {
int status, stat;
pid_t pid[PROCESS_NUMBER];
key_t key;
int qtype = 1;
type_mymsgbuf qbuf;

int numbers_per_process = (b-a)/PROCESS_NUMBER;
if ((key = ftok(".", 'm')) < 0) {
perror("ftok");
exit(1);
}
if((msgqid = msgget(key, IPC_CREAT|0660)) == -1) {
perror("msgget");
exit(1);
}

for (int i = 0; i < PROCESS_NUMBER; i++) {
pid[i] = fork();

if (-1 == pid[i]) {
perror("fork");
exit(1);
}
else if (0 == pid[i]) {
int* part_primes = find_primes_part(a + numbers_per_process*i, a + numbers_per_process*(i+1));
send_message(msgqid, (type_mymsgbuf*)&qbuf, qtype, part_primes, numbers_per_process);
}
}
for (int j = 0; j < PROCESS_NUMBER; ++j) {
status = waitpid(pid[j], &stat, 0);
if (pid[j] == status) {
if (WEXITSTATUS(stat) != 0) {
perror("process failed: ");
exit(1);
}
}
}
for (int i = 0; i < PROCESS_NUMBER; ++i) {
read_message(msgqid, &qbuf, qtype, numbers_per_process);
}

if ((rc = msgctl(msgqid, IPC_RMID, NULL)) < 0) {
perror("msgctl");
return 1;
}
return 0;
}

int main() {
find_primes(10, 200);
return 0;
}

输出非常错误,而且每次都不同。它是这样的:

181
191
193
197
199
163
167
169msgsnd: Invalid argument

173
179
181
191
193
197
msgctl: Invalid argument

主要问题是这2个错误

msgsnd: Invalid argument
msgctl: Invalid argument

如何消除?

最佳答案

我怀疑消息队列被过早移除了。

问题可能在于每个进程(包括子进程)都执行清理代码。只有 parent 应该运行清理代码。

我认为您缺少一个 return;exit(0);在客户端特定代码中,在调用 send_message() 之后.

关于c - 消息队列。消息发送 : Invalid argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com