消息对列的操作

来源:互联网 发布:linux 格式化硬盘 编辑:程序博客网 时间:2024/05/22 19:27

#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
struct msgbuf
{
    int type;
    char ptr[0];
};
int main(int argc,char *argv[])
{
    key_t key;
    key=ftok(argv[1],100);

    int msgid;
    msgid=msgget(key,IPC_CREAT|0600);

    pid_t pid;
    pid=fork();
    if(pid==0)    //send
    {
        while(1)
        {
            printf("pls input msg to send:");
            char buf[128];
            fgets(buf,128,stdin);
            struct msgbuf *ptr=malloc(sizeof(struct msgbuf)+strlen(buf)+1);
            ptr->type=2;    //send msg type=2
            memcpy(ptr->ptr,buf,strlen(buf)+1);
            msgsnd(msgid,ptr,strlen(buf)+1,0);
            free(ptr);
        }
    }
    else
    {
        struct msgbuf{
            int type;
            char ptr[1024];
        };
        while(1)
        {
            struct msgbuf mybuf;
            memset(&mybuf,'\0',sizeof(mybuf));
            msgrcv(msgid,&mybuf,1024,1,0);    //recv msg type=2
            printf("recv msg:%s\n",mybuf.ptr);
        }
    }
}

////////////////////////////////////////////////////////////////////////

#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
struct msgbuf
{
    int type;
    char ptr[0];
};

int main(int argc,char *argv[])
{
    key_t key;
    key = ftok(argv[1],100);
    int msgid;
    msgid = msgget(key, IPC_CREAT|0600);
    pid_t pid;
    pid=fork();

    if(pid==0)
    {
        while(1)
        {
            printf("pls input msg to send:");
            char buf[128];
            fgets(buf,128,stdin);
            struct msgbuf *ptr = malloc(sizeof(struct msgbuf)+strlen(buf)+1);
            ptr->type=1;
            memcpy(ptr->ptr, buf, strlen(buf)+1);
            msgsnd(msgid,ptr,strlen(buf)+1,0);
            free(ptr);
        }
    }
    else
    {
        struct msgbuf
        {
            int type;
            char ptr[1024];
        };

        while(1)
        {
            struct msgbuf mybuf;
            memset(&mybuf,'\0',sizeof(mybuf));
            msgrcv(msgid,&mybuf,1024,2,0);    
            printf("recv msg:%s\n",mybuf.ptr);
        }
    }
}


////////////////////////////////////////////////////////////////////////////////

两进程通过消息队列收发消息


    (1)发送消息队列程序

    msgsnd.c源代码如下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include  <time.h>
#define TEXT_SIZE  512
struct msgbuf
{
    long mtype ;
    int  status ;
    char time[20] ;
    char mtext[TEXT_SIZE] ;
};

char  *getxtsj()
{
    time_t  tv ;
    struct  tm   *tmp ;
    static  char  buf[20] ;
    tv = time( 0 ) ;
    tmp = localtime(&tv) ;
    sprintf(buf,"%02d:%02d:%02d",tmp->tm_hour , tmp->tm_min,tmp->tm_sec);
    return   buf ;
}

int main(int argc, char **argv)
{
    int msqid ;
    struct msqid_ds info ;
    struct msgbuf buf ;
    struct msgbuf buf1 ;
    int flag ;
    int sendlength, recvlength ;
    int key ;
    key = ftok("msg.tmp", 0x01 ) ;
    if ( key < 0 )
    {
        perror("ftok key error") ;
        return -1 ;
    }
    msqid = msgget( key, 0600|IPC_CREAT ) ;
    if ( msqid < 0 )
    {
        perror("create message queue error") ;
        return -1 ;
    }
    buf.mtype = 1 ;
    buf.status = 9 ;
    strcpy(buf.time, getxtsj()) ;
    strcpy(buf.mtext, "happy new year!") ;
    sendlength = sizeof(struct msgbuf) - sizeof(long) ;
    flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
    if ( flag < 0 )
    {
        perror("send message error") ;
        return -1 ;
    }
    buf.mtype = 3 ;
    buf.status = 9 ;
    strcpy(buf.time, getxtsj()) ;
    strcpy(buf.mtext, "good bye!") ;
    sendlength = sizeof(struct msgbuf) - sizeof(long) ;
    flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
    if ( flag < 0 )
    {
        perror("send message error") ;
        return -1 ;
    }
    system("ipcs -q") ;
    return 0 ;
}



(2)接收消息队列程序
    msgrcv.c源代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define TEXT_SIZE  512
struct msgbuf
{
    long mtype ;
    int  status ;
    char time[20] ;
    char mtext[TEXT_SIZE] ;
}  ;

int main(int argc, char **argv)
{
    int msqid ;
    struct msqid_ds info ;
    struct msgbuf buf1 ;
    int flag ;
    int  recvlength ;
    int key ;
    int mtype ;

    key = ftok("msg.tmp", 0x01 ) ;
    if ( key < 0 )
    {
        perror("ftok key error") ;
        return -1 ;
    }
    msqid = msgget( key, 0 ) ;
    if ( msqid < 0 )
    {
        perror("get ipc_id error") ;
        return -1 ;
    }

    recvlength = sizeof(struct msgbuf) - sizeof(long) ;
    memset(&buf1, 0x00, sizeof(struct msgbuf)) ;
    mtype = 1 ;
    flag = msgrcv( msqid, &buf1, recvlength ,mtype,0 ) ;
    if ( flag < 0 )
    {
        perror("recv message error\n") ;
        return -1 ;
    }
    printf("type=%d,time=%s, message=%s\n", buf1.mtype, buf1.time,  buf1.mtext) ;
    system("ipcs -q") ;
    return 0 ;
}
0 0