Listing 3 (sender.c)
/********************************************************* *** *** *** Program to illustrate message passing under QNX. *** Written and tested under QNX version 2.15C atp *** Compiler used: Quantum's C compiler *** *** This program is used to send a message to the *** holder.c program (<a href="/showArticle.jhtml?documentID=cuj9109busigin&pgno=3">listing 2</a>). It can request that *** holder.c provide one of the following services: *** *** 1) Store a text string that is sent to it by this *** task. *** 2) Reply to this task with a previously stored *** string. *** 3) Have holder.c commit suicide. *** **********************************************************/ #include <stdio.h> #include "message.h" main(argc, argv) int argc; char **argv; { unsigned rtid; /* Variable to hold receiving task id */ struct message buff; /* Message buffers */ /* If an incorrect number of arguments have been passed to this program print a command usage message and exit. */ if (argc != 2) { printf("\nUsage: sender <arg>"); printf("\n\nWhere: <arg> = t=\"text string\""); printf("\n (stores string)"); printf("\n <arg> = -query"); printf("\n (get stored string)"); printf("\n <arg> = -kill"); printf("\n (cause holder to die)\n"); exit(-1); } /* Find the task id of holder.c which will receive the messages from this task. */ if(!(rtid = name_locate(HOLDER_NAME, 0, sizeof(buff)))) { printf("\nname_locate() failed\n"); exit(-1); } /* Build message to send to holder. */ switch ( (*arg v[1] << 8) | *(argv[1] +1) ) { case ('t' << 8) | '=' : strcpy(buff.text, (argv[1] + 2) ); buff.mssg_type = STORE; break; case ('-' << 8) | 'q' : buff.mssg_type = RETRIEVE; break; case ('-' << 8) | 'k' : buff.mssg_type = KILL; break; default: printf("\nUnidentified argument used.\n"); exit(-1); } /* Send holder the message and print the reply if there there is one. */ send(rtid, &buff, &buff, sizeof(buff) ); switch (buff.mssg_type) { case STORED: printf("\nMessage stored\n"); break; case RETRIEVED: printf("\nRetrieved message [%s]\n",buff.text); break; case KILL: printf("\nSuicide request sent to holder\n"); break; default: printf("\nReceived unknown message [%d].\n", buff.mssg_type); break; } } /* End of File */