FreeBSD 7.0-RELEASE-p5 amd64

Programming C/C++.
The down Queues (msgid = msgget ....) after message reception (msgrcv)

# ipcs
Message Queues:
T           ID          KEY MODE        OWNER    GROUP
q       327680         1174 --rw-rw-rw-     root    wheel
.....

Example:
msgserv.c
-------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <sys/errno.h>
#include "msgtypes.h"

int main(int argc, char * argv[])
{
  struct msg_t message;
  int msgid;
  char * response = "Ok!";
  msgid = msgget(KEY, 0666 | IPC_CREAT);
  msgrcv(msgid, &message, sizeof(message), 2, 0);
/*
  ATTENTION!!!
  After "msgrcv(msgid, &message, sizeof(message), 2, 0);" -
Queues(msgid) DOWN

  Helps only to create anew "msgid = msgget(KEY, 0666 | IPC_CREAT);"
*/
  printf("Client (pid = %i) sent: %s", message.snd_pid, message.body);
  message.mtype = 1;
  message.snd_pid = getpid();
  strcpy(message.body, response);

  msgsnd(msgid, &message, sizeof(message), 0);
// errno = 22 (bad msgid)
  msgrcv(msgid, &message, sizeof(message), 2, 0);
  msgctl(msgid, IPC_RMID, 0);
  return EXIT_SUCCESS;
}
-------------------

msgtypes.h
-------------------
#ifndef MSG_TYPES
#define MSG_TYPES

#define KEY 1174
#define MAXLEN 512

struct msg_t
{
   long mtype;
   int snd_pid;
   char body[MAXLEN];
};

#endif
-------------------

msgcli.c
-------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <strings.h>
#include "msgtypes.h"

int main(int argc, char * argv[])
{
  int msgid;
  int i;
  struct msg_t message;
  char buf[MAXLEN];
  msgid = msgget(KEY, 0666);
  if (msgid == -1)
  {
     printf("Server is not running!\n", msgid);
     return EXIT_FAILURE;
  }
  i = 0;
  while ( (i < (MAXLEN - 1)) && ((message.body[i++] = getchar()) !=
'\n') );
  message.body[i] = '\0';
  message.mtype = 2;
  message.snd_pid = getpid ();
  msgsnd(msgid, &message, sizeof(message), 0);
  msgrcv(msgid, &message, sizeof(message), 1, 0);
  printf("Server (pid= %i) responded: %s\n", message.snd_pid,
message.body);
  message.mtype = 2;
  msgsnd(msgid, &message, sizeof(message), 0);
  return EXIT_SUCCESS;
}
-------------------


Tested on FreeBSD 6.2 i386 - All has passed remarkably, errors were not


_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to