msg_port_full

#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <string.h>
#include <signal.h>
struct msg_buf
    {
        int mtype;
        char data[255];
    };
int main()
{
        key_t key;
        pid_t pid;
        int msgid;
        int ret;
        struct msg_buf msgbuf;
        struct msg_buf msgbuf1;
        struct msg_buf msgbuf2;
        struct msg_buf msgbuf3;
        int i = 0;
        key=ftok(".",'a');
        printf("key =[%x]\n",key);
        msgid=msgget(key,IPC_CREAT|0666); /*通过文件对应*/
        if(msgid==-1)
        {
                printf("create error\n");
                return -1;
        }
         if((pid = fork()) < 0)
         {
             perror("fork error");
             exit(0);
         }
         if( pid > 0)
       {
            msgbuf1.mtype = 1;
            strcpy(msgbuf1.data,"test haha1");
            msgbuf2.mtype = 2;
            strcpy(msgbuf2.data,"test haha2");
            msgbuf3.mtype = 3;
            strcpy(msgbuf3.data,"test haha3");
            ret=msgsnd(msgid,&msgbuf1,sizeof(msgbuf1.data),IPC_NOWAIT);
            if(ret==-1)
            {
                printf("send message err\n");
                return -1;
            }
            //sleep(3);
            ret=msgsnd(msgid,&msgbuf2,sizeof(msgbuf2.data),IPC_NOWAIT);
            if(ret==-1)
            {
                printf("send message err\n");
                return -1;
            }
           // sleep(3);
            ret=msgsnd(msgid,&msgbuf3,sizeof(msgbuf3.data),IPC_NOWAIT);
            if(ret==-1)
            {
                printf("send message err\n");
                return -1;
            }
           // sleep(3);
           // kill(pid,SIGSTOP);
            waitpid(pid,NULL,0);
            exit(0);
       }
         if( pid == 0)
         {
            while(i < 3)
            {
                i++;
                memset(&msgbuf,0,sizeof(msgbuf));
                ret=msgrcv(msgid,&msgbuf,sizeof(msgbuf.data),i,0);
                if(ret==-1)
                {
                    printf("recv message err\n");
                    return -1;
                }
                printf("recv msg =[%s]\n",msgbuf.data);
                sleep(1);
            }
               exit(0);
         }
     return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux内核驱动中,构建一个队列struct ring_buffer { int head; int tail; struct msg *data; int size; unsigned int capacity; };,其中存放的是定义如下的结构体struct msg { u16 module_id; u16 cmd_id; u16 cmd_subid; u16 complete; u8 data[128]; };,请你给出操作这个队列的功能函数,分别为:初始化,入队、出队、注销等;再写两个函数,函数一构建msg,除msg中的data数组外,其他成员赋值为常数,并将两个unsigned int 类型的值使用移位的方式放入data数组中,并向队列中放置msg,使用usleep_range()函数等待函数二将msg的complete成员设置为1后,再退出函数;函数二将msg从队列中取出来,解析msg中的module_id,如果该值不为0x1,则报错,否则使用switch函数解析cmd_id,并根据不同的cmd_id再解析cmd_subid,将msg内data数组中放入的两个unsigned int值还原,并将其作为两个参数用在下列函数前两个参数中,static unsigned int phytuart_msg_cmd_set_txim(unsigned int im, unsigned int txim, struct pokemon_uart_port *pup) { if (txim == 0) { im &= ~REG_IMSC_TXIM; pokemon_uart_write(im, pup, REG_IMSC); } else{ im |= REG_IMSC_TXIM; pokemon_uart_write(im, pup, REG_IMSC); } return im; }并将msg中的complete成员设置为1,函数一和函数二需要使用队列的操作函数,注意函数一中将msg放进队列后,需要调用函数二解析,请在驱动注册时注册队列,在驱动卸载时注销队列请给出详细代码
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值