c++ - "Invalid argument" with message queue operations -


i'm trying send 1kb string on message queue between parent process , forked child. unfortunately, calls msgsnd, msgrcv, etc. returning -1 , causing einval error.

i found error (in case of msgsnd, example) occurs when msqid invalid, message type argument set @ <1, or msgsz out of range. upon testing, far can tell, msgget returning valid id number , type set fine. there must problem buffer ranges, thought set them correctly. in code, have added comments explain (sorry of frantically-added #includes):

#include <errno.h>   #include <stdio.h> #include <iostream>  #include <stdlib.h> #include <string.h> #include <string>  #include <cstring>  #include <sys/msg.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <sys/time.h> #include <sstream>   #define perms (s_irusr | s_iwusr)  #define numbytes 1024 //number of chars (bytes) sent   using namespace std;   //special structure messages typedef struct {   long mtype;    char mtext[numbytes];   } mymsg_t;   int main(){    //construct generic test message of specified size   char message[numbytes];   for(int = 0; < numbytes; i++)     message[i] = 'a';      //create message queue (accessed both parent   //and child processes)   int msqid;   int len;     if(msqid = msgget(ipc_private, perms) == -1)     perror("failed create new message queue!\n");       if(fork() == 0){  //child process...does sending      mymsg_t* mbuf;     len = sizeof(mymsg_t) + strlen(message); //doesn't work " + sizeof(message)" either       void* space;      if((space = malloc(len)) == null) //this works fine; no error output        perror("failed allocate buffer message queue.\n");      mbuf = (mymsg_t*)space;        strcpy(mbuf->mtext, message);      mbuf->mtype = 1; //a default     //some error checks tried...  //cout<<"msqid " << msqid << endl; //cout << "mbuf ptr size " << sizeof(mbuf) << ". , non-ptr: "<<sizeof(*mbuf)<<". ,  //len: "<<len<<endl;       if(msgsnd(msqid, mbuf, len+1, 0) == -1)       perror("failed send message.\n");  //this error occurs every time!       free(mbuf);     }        else{ //parent process...does receiving       usleep(10000); //let message come           mymsg_t mymsg; //buffer hold message     int size;      if((size = msgrcv(msqid, &mymsg, len+1, 0, 0)) == -1) //error every time       perror("failed read message queue.\n");        //checking made      //cout << "hopefully printing now? : " << endl;      //if(write(stdout_fileno, mymsg.mtext, size) == -1)     //  perror("failed write standard output!\n");      }    ostringstream oss;   oss << "ipcrm -q " << msqid;    string command = oss.str();     if(system(command.c_str()) != 0)  //also errors every time, not main focus here      perror("failed clean message queue!");  } 

what going on here? thought had buffer procedure working fine , sufficient space..


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -