ccbbcc 2024-10-04 08:30 采纳率: 91.6%
浏览 3
已结题

linux c 程序为什么都失败

代码:


```c
#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|0x0666);
    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 information",sizeof("test information"));
    ret=msgsnd(msg_id,&msg_mbuf,sizeof("test information"),msg_sflags);
    if(-1==ret)
    {
    printf("send 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](https://img-mid.csdnimg.cn/release/static/image/mid/ask/3c6cdfd5fdca438fa00b1a55da8f9ecb.jpeg "#left")
运行结果


![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/7cc7cd2a179c4a229012337e46b562c9.jpeg "#left")
  • 写回答

1条回答 默认 最新

  • 檀越@新空间 2024-10-04 08:32
    关注

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

    以下是一些可能的原因和解决方案:

    1. 权限问题:你需要有创建和删除文件的权限才能修改文件或目录的属性。你可以使用chmod命令来增加你的权限。
    2. 文件名冲突:如果你的文件名与正在使用的其他文件或目录名称相同,你可能会遇到这个问题。在这种情况下,你需要确保文件名是唯一的。
    3. 递归调用问题:如果你的程序中有递归调用,那么当函数返回时,它会继续调用自己。这可能会导致无限递归,最终导致程序崩溃。为了避免这种情况,你应该尽量避免使用递归。
    4. 处理错误的方法:如果消息显示错误信息,你可能需要检查你的程序是否正确地读取和处理这些消息。例如,你可能需要检查消息是否包含正确的参数。
    5. 错误处理:对于所有的异常情况,你都应该有一个清晰的错误处理机制。例如,你应该在可能的情况下捕获和记录异常,并提供有用的错误信息。

    由于这个示例代码没有给出任何明显的错误信息,我无法为你提供具体的解决方案。但是,你可以通过逐步分析上述问题,尝试不同的解决方案来找到最有效的解决方案。

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

报告相同问题?

问题事件

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