linux 进程通信 消息队列

1)
2)
3)

 

4)发送

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.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);
    printf("%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.txt", 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;
    }

    
    while(1)
    {
	    buf.mtype = 3;
	    buf.status = 9; 
	    strcpy(buf.time, getxtsj());
	    strcpy(buf.mtext, "hahhahah ....!");
	    sendlength = sizeof(struct msgbuf) - sizeof(long);
	    flag = msgsnd( msqid, &buf, sendlength , IPC_NOWAIT);
	    if ( flag < 0 )
	    {
	        perror("send message error");
	        return -1;
	    }
	    else
	    {
	    	printf(" send msg 3-9 \n");
	    }

	    sleep(3);

  	}
    
    
   return 0;
}


5)接收
 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.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.txt", 0x01 );
    if ( key < 0 )
    {
        perror("ftok key error");
        return -1;
    }
 
    msqid = msgget( key, 0 );
    if ( msqid < 0 )
    {
        perror("get ipc_id error");
        return -1;
    }
 
    while(1)
    {
    	
    recvlength = sizeof(struct msgbuf) - sizeof(long);
    memset(&buf1, 0x00, sizeof(struct msgbuf));
    mtype = 3;
    flag = msgrcv( msqid, &buf1, recvlength ,mtype, IPC_NOWAIT);
    if ( flag < 0 )
    {
        printf("1 no data... \n");
        //perror("recv message error");
        //return -1;
    }
    else
    printf("type=%ld,time=%s, message=%s\n", buf1.mtype, buf1.time,  buf1.mtext);
    
    recvlength = sizeof(struct msgbuf) - sizeof(long);
    memset(&buf1, 0x00, sizeof(struct msgbuf));
    mtype = 1;
    flag = msgrcv( msqid, &buf1, recvlength ,mtype, IPC_NOWAIT);
    if ( flag < 0 )
    {
        printf("2 no data... \n");
        //perror("recv message error");
        //return -1;
    }
    else
    printf("type=%ld,time=%s, message=%s\n", buf1.mtype, buf1.time,  buf1.mtext);
    
    
    sleep(1);
    //system("ipcs -q");
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/poject/article/details/85102509
今日推荐