ccbbcc 2024-10-04 09:45 采纳率: 91.6%
浏览 1
已结题

发送消息接收消息失败

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<unistd.h>
#include<sys/ipc.h>
void msg_show_attr(int msg_id,struct msqid_ds msg_info)
{
    int ret=-1;
    sleep(1);
    ret=msgctl(msg_id,IPC_STAT,&msg_info);
    if(-1==ret)
    {
        printf("obtain news information failed!\n");
        return ;
    }
    printf("\n");
    printf("now queue byte num:%d\n",msg_info.msg_cbytes);
    printf("queue news num:%d\n",msg_info.msg_qnum);
    printf("queue max byte num:%d\n",msg_info.msg_qbytes);
    printf("finally send information jincheng pid:%d\n",msg_info.msg_lspid);
    printf("finally recv information jincheng pid:%d\n",msg_info.msg_lrpid);

    printf("finally send information time:%s\n",ctime(&(msg_info.msg_stime)));
    printf("finally recv information time:%s\n",ctime(&(msg_info.msg_rtime)));
    printf("finally change time: %s\n",ctime(&(msg_info.msg_ctime)));
    printf("information UID is: %d\n",msg_info.msg_perm.uid);
    printf("informatiomn GID is:%d\n",msg_info.msg_perm.gid);
}
int main(void)
{
    int ret=-1;
    int msg_flags,msg_id;
    key_t key;
    struct msgmbuf
    {
        int mtype;
        char mtext[10];
    };
    struct msqid_ds msg_info;
    struct msgmbuf msg_mbuf;

    int msg_sflags,msg_rflags;
    char *msgpath="ipc/msg/";
    key=ftok(msgpath,'b');
    if(key!=-1)
    {
        printf("succeed build KEY\n");
    }
    else
    {
        printf("build KEY failed\n");
    }
    msg_flags=IPC_CREAT|IPC_EXCL;
    msg_id=msgget(key,msg_flags|0x777);
    if(-1==msg_id)
    {
        printf("information build failed\n");
        return 0;
    }
    msg_show_attr(msg_id,msg_info);
    msg_sflags=IPC_NOWAIT;
    msg_mbuf.mtype=10;
    memcpy(msg_mbuf.mtext,"test news",sizeof("test news"));
    ret=msgsnd(msg_id,&msg_mbuf,sizeof("test news"),msg_sflags);
    if(-1==ret)
    {
    printf("send information failed\n");
    }
    msg_rflags=IPC_NOWAIT|MSG_NOERROR;
    ret=msgrcv(msg_id,&msg_mbuf,10,10,msg_rflag);
    if(-1==ret)
    {
        printf("recv information failed\n");
    }    
    else
    {
        printf("recv information succeed,length,%d\n",ret);
    }
    msg_show_attr(msg_id,msg_info);
    msg_info.msg_perm.uid=8;
    msg_info.msg_perm.gid=8;
    msg_info.msg_qbytes=12345;
    ret=msgctl(msg_id,IPC_SET,&msg_info);
    if(-1==ret)
    {
        printf("set information attribute failed!");
        return 0;
    }
    msg_show_attr(msg_id,msg_info);
    ret=msgctl(msg_id,IPC_RMID,NULL);
    if(-1==ret)
    {
        printf("delete information failed\n");
        return 0;
    }
    return 0;
}


运行结果:

img


请问专家,我把文件属性设为777,为什么发送消息接收消息都失败?谢谢!

  • 写回答

1条回答 默认 最新

  • 檀越@新空间 2024-10-04 09:46
    关注

    上午好☀️☀️☀️️
    本答案参考ChatGPT-3.5

    这是因为在没有创建权限的目录中尝试写入文件时抛出了Segmentation fault错误(即“stack smashing detected”)。 要解决这个问题,您需要首先确保具有足够权限访问该目录:

    sudo chmod 777 /path/to/directory
    

    然后您可以尝试重新运行您的脚本:

    sudo ./ccb
    

    这应该会成功运行,并且消息应该能够正常被发送和接收。如果问题仍然存在,那么可能需要检查您的系统是否有其他相关的错误或陷阱。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 创建了问题 10月4日