Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.unix.questions Subject: Re: msgctl Message-ID: <1990Oct05.235323.7998@virtech.uucp> Date: 5 Oct 90 23:53:23 GMT References: <3708@wb3ffv.ampr.org> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 74 In article <3708@wb3ffv.ampr.org> wmark@wb3ffv.ampr.org (Mark Winsor) writes: >Does anyone have a fragment of code to show the use of msgctl(), msgget(), and >msgop(). I'm in need of these facilities but i'm just not clicking with the >manual descriptions. Snippet that allocates the message queue: key = (key_t) 0; if( (env=getenv("TALK_MSGQ_KEY")) != (char *) 0) { key = (key_t) atoi(env); } if( key <= 0 ) { key = (key_t) TALK_MSGQ_KEY; } talkmsgqid = msgget(key,MODE|IPC_CREAT); if( talkmsgqid == -1 ) { fatal("Unable to allocate talker message queue."); } Snippet that sends a message out that queue: /* * recieved acks and naks are processed by the talker. */ optr.type = TY_TALK_ACK; optr.data[0] = pkt->type; optr.pknum = pkt->data[0]; if( msgsnd(talkmsgqid,&optr,sizeof(optr),0) == -1 ) { sprintf(buffer, "Failed %s, errno=%d", "to send message to talker", errno); logit(buffer); } Snippet that receives a message from the same queue: alarm(alarmtime); rtn = msgrcv(talkmsgqid, &optr, sizeof(optr), 0,0); alarm(0); if( rtn == -1 ) { if( ! timeout ) { sprintf(outdata, "%s, errno = %d, %s", "Msgrcv() FAILED", errno, "- restarting..."); logit(outdata); continue; } else { timeout = FALSE; continue; } } And control snippet that removes the message queue: fprintf(stderr,"Shutting down..."); msgctl(talkmsgqid,IPC_RMID); -- Conor P. Cahill (703)430-9247 Virtual Technologies, Inc., uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160 Sterling, VA 22170