gpt4 book ai didi

C 系统调用 msgsnd() : invalid argument error on certain compiler versions

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:06 25 4
gpt4 key购买 nike

好吧,我正在做一个大学项目,很抱歉提出这个愚蠢的问题。

我的任务是通过消息队列推送一个字符串,派生一个子进程并接收消息,然后通过相同的消息队列将响应发送回父进程,循环执行所有这些操作。

我在家里编写了代码,它工作得很好,但部分任务是让它在大学服务器上工作,这是我遇到的问题。

这是我的代码的相关部分:

父进程:

void fel2()
{
int messq, status;
key_t key = ftok("key", 1);
messq = msgget( key, 0600 | IPC_CREAT );

OpBike * ob_first = NULL;
ob_first = ob_read_in(ob_first);
char confirm_string[64];

while (exists_busy_bike(ob_first))
{
sleep(5);
ob_first = NULL;
ob_first = ob_read_in(ob_first);

int busy_bikes = count_busy_bikes(ob_first);
ob_first = NULL;

srand(time(NULL));
int bret = (rand() % busy_bikes);

ob_first = ob_read_in(ob_first);
ob_first = ob_read_in(ob_first);

int ret_bike_id = get_bikeid_by_jumps(ob_first, bret);

char ret_bike_id_char[64];
snprintf(ret_bike_id_char, 64, "%d", ret_bike_id);

send(messq, ret_bike_id_char);
fel2_child(messq, key);
ob_first = NULL;

wait(NULL);

strcpy(confirm_string, recv(messq));

ob_first = NULL;
ob_first = ob_read_in(ob_first);
wait(NULL);
}


status = msgctl( messq, IPC_RMID, NULL );
wait(NULL);
return;


}

子进程:

int fel2_child(int messq, key_t key)
{
pid_t child = fork();

if (child > 0) // parent process does nothing here
{
}
else { // child
char rbikeid[64];
strcpy(rbikeid, recv(messq));

OpBike * ob_first = NULL;
ob_first = ob_read_in(ob_first);
OpRent * or_first = NULL;
or_first = or_read_in(or_first);

OpBike * bike_ret = get_bike_by_bikeid(ob_first, rbikeid);
char check[64];
strcpy(check, bike_ret->bike_id);

ob_first = NULL;
ob_first = ob_read_in(ob_first);

or_add_return(or_first, ob_first, rbikeid);

ob_first = NULL;
ob_first = ob_read_in(ob_first);

OpBike * bike_check = get_bike_by_bikeid(ob_first, rbikeid);
strcpy(check, bike_check->bike_status);


send(messq, check);
exit(0);

}

return 0;

}

发送方和接收方函数:

int send( int messq, char * text ) 
{
struct message me = { 119, "asd" };
int status;

strcpy(me.mtext, text);

// THIS IS WHERE EVERYTHING GOES WRONG
status = msgsnd( messq, &me, strlen ( me.mtext ) + 1 , IPC_NOWAIT );

if ( status < 0 )
perror("msgsnd");
return 0;
}

char * recv( int messq)
{
struct message me;
int status;
status = msgrcv(messq, &me, 1024, 119, 0 );

if ( status < 0 )
perror("msgsnd");

char * ret;
ret = malloc(sizeof( char ) * strlen (me.mtext));
strcpy(ret, me.mtext);

return ret;
}

在我家的电脑上运行上面的代码完美无缺。运行 Ubuntu 14.10,我的代码是用 gcc 4.9.1 编译的。但是,它不适用于运行 SLES 11 SP 1 的学校服务器,已使用 gcc 4.3.4 编译了代码。

问题是我尝试发送的字符串没有正确发送。它到达函数,被复制到消息结构中,然后在过程中的某个地方消失,唯一读回的是未定义的数据。

我现在有点没主意了。我已经尝试将消息队列的声明放在其他地方(正如我的教授所建议的),但并没有解决问题。我无法在我学校的服务器上更新编译器,所以这正是我必须处理的。你们中有没有人经历过不同编译器版本的系统调用的奇怪行为?有人知道如何解决这个问题吗?

提前感谢您的回复!

编辑 2: struct message 的定义。

struct message { 
long mtype;
char mtext [ 1024 ];
};

最佳答案

好吧,事实证明我的 gcc 版本(或 linux 内核或我使用的任何软件组件)会自动在 ftok 上创建 key 文件(请注意,“ key ”在我的系统上不存在)。然而,大学服务器没有。所以

key_t key = ftok("argv[0]", 1);

key_t key = ftok("filename", 1);

成功了。

关于C 系统调用 msgsnd() : invalid argument error on certain compiler versions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27281930/

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