c - MessageQueue 名称和消息队列大小

标签 c linux posix

我还有两个问题。 1. 如何将消息队列名称作为命令行参数传递?例如我写道:

char src[50] , dest[50];
strcpy(src , "/");
strcpy(dest , argv[1]); // argv[1] is the name of message queue
strcat(dest , src);
char *msgqueue;
strcpy(msgqueue , dest);

但是,它给出了段错误,当我更改为 char msgqueue[50] 时,出现无效错误。

  1. 如何达到消息队列中元素的数量?我尝试了 m_attr.mq_msgsize,但我发现它没有给出答案。

我需要你的帮助,这是我完成作业的最后一步!

谢谢大家!

最佳答案

char *msgqueue; //allocate memory and then copy
strcpy(msgqueue , dest);  

  msgqueue=strdup(dest);    

正如 Joachim Pileborg 所建议的那样

如果不需要更多修改,只需msgqueue = dest;

而你想复制argv[1]到消息队列,然后在末尾添加“/”。

char *msgqueue=malloc(strlen(argv[1])+3); //one for null byte and another one for / for safe use one extra byte

strcpy(msgqueue,argv[1]);
strcat(msgqueue,"/");  

关于c - MessageQueue 名称和消息队列大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19511035/

相关文章:

c++ - linux CFS schedualar下C/C++多线程场景出现意外结果

linux - 如何为GNU find适本地限定-or运算符?如何找到指向可执行文件的符号链接(symbolic link)?

c - 将 CPU 亲和性设置为进程 - C - Linux

linux - 如何将单个目录中的所有文件移动到多个目录?

python - 如何从终端运行 google Colab Notebook?

linux - 在 linux 环境中创建窗口除了 openGLUT 和 x-windows 之外还有什么选择?

c - 目标文件夹修改后的 POSIX seekdir() 和 telldir() 行为

c - 如何让更新线程和渲染线程一起顺利工作(在 Linux 上)?

c - openmp中的rsa算法

c - 在 GTK+ 中执行操作后刷新窗口