root/sysv-ipc/messqsrv.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. printrequest
  2. fetchrequest
  3. main

#include<sys/ipc.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>

#define RAND(i) (random()%(i))
#define NELS(i) (sizeof(i)/sizeof(*i))

#define APPPATH "/etc/passwd"
#define LANDING 10L
#define TAKINGOFF 20L

struct Request {
        long type;
        int no;
        char name[30];
        char city[30];
        char model[20];
};

void printrequest(struct Request *r) {
        printf("Airline: %s, No: %d, Model: %s\n",
                r->name,r->no,r->model);
        if (r->type==LANDING) 
                printf("Coming from %s",r->city);
        else 
                printf("Departing to %s",r->city);
        printf(" is given the lane\n\n");
}

int fetchrequest(int key,struct Request *r) {
        int tmp;

        /*
        tmp=msgrcv(key,r,sizeof(struct Request),-LANDING,IPC_NOWAIT);
        if (tmp<0) {
                tmp=msgrcv(key,r,sizeof(struct Request),0,0);
                if (tmp<0)
                        return 0;
        }
        */
        tmp=msgrcv(key,r,sizeof(struct Request),-TAKINGOFF,0);
        return 1;
}

int main() {
        struct Request req;
        long queueid,key;
        queueid=ftok(APPPATH,0);
        printf("opening/creating %x\n",queueid);
        if ((key=msgget(queueid,0664|IPC_CREAT))<=0) {
                perror("opening/creating queue");
                exit(1);
        }
        while (1) {
                if (fetchrequest(key,&req))
                        printrequest(&req);
                sleep(RAND(10));
        }
}

/* [<][>][^][v][top][bottom][index][help] */