Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
5
Добавлен:
26.05.2014
Размер:
1.33 Кб
Скачать

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_SEND_SIZE 80

struct mymsgbuf {
  long mtype;
  char mtext[MAX_SEND_SIZE];
};

void send_message(int qid, struct mymsgbuf *qbuf,  char *text);
void read_message(int qid, struct mymsgbuf *qbuf );
void remove_queue(int qid);

int main(void)
{

  key_t key;
  int   msgqueue_id;
  struct mymsgbuf qbuf;
key = ftok(".", 'm');
  if ((msgqueue_id = msgget(key, IPC_CREAT|0660)) == -1) {
     perror("msgget");
     exit(1);
  }
send_message(msgqueue_id, (struct mymsgbuf *)&qbuf,"test string to send");
read_message(msgqueue_id, &qbuf);
remove_queue(msgqueue_id);
return(0);
}

void send_message(int qid, struct mymsgbuf *qbuf,char *text)
{
  /* Send a message */
  printf("Sending a message ...\n");
  qbuf->mtype = 1;
  strcpy(qbuf->mtext, text);
  if ((msgsnd(qid, (struct msgbuf *)qbuf, strlen(qbuf->mtext)+1, 0)) ==-1)
  {
     perror("msgsnd");
     exit(1);
  }
}

void read_message(int qid, struct mymsgbuf *qbuf)
{
  /* Read a message */
  printf("Reading a message ...\n");
  qbuf->mtype = 1;
  msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE,1, 0);
  printf("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
}

void remove_queue(int qid)
{
  /* Remove */
  msgctl(qid, IPC_RMID, 0);
}
Соседние файлы в папке mes
  • #
    26.05.201412.01 Кб5a.out
  • #
    26.05.20141.38 Кб5l4.c
  • #
    26.05.201462 б5Makefile
  • #
    26.05.201412.01 Кб5start.me
  • #
    26.05.20141.33 Кб5tst.c