msgctl

Conor P. Cahill cpcahil at virtech.uucp
Sat Oct 6 09:53:23 AEST 1990


In article <3708 at wb3ffv.ampr.org> wmark at 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 



More information about the Comp.unix.questions mailing list