Linux高级通信

代码示例展示了如何使用FIFO(有名管道)进行进程间通信,以及如何创建和使用消息队列发送字符串数据。在FIFO部分,程序创建了一个名为my_fifo的管道并写入数据。在消息队列部分,程序创建了一个标识符为0x1234的消息队列,发送了一系列包含书名的消息。
摘要由CSDN通过智能技术生成

第一关有问题

第二关

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <string.h>

void namepipe_commu(const char *buffer)
{
	/***********begin***********/
	int fd;
    int w_num;
	if((mkfifo("./my_fifo",0777)<0))
    {
        printf("cannot create fifo...\n");
        exit(1);
    }
    //以阻塞型只写方式打开fifo
    fd=open("./my_fifo",O_WRONLY);
    w_num=write(fd,buffer,strlen(buffer));
	/***********end***********/
}

第三关

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

struct msgbuf
{
    long mytype;
    char bookname[100];
};

void mq_commu (void)
{
	/*********Begin*********/
	//创建消息队列
    int msgid = msgget((key_t)0x1234, 0666 | IPC_CREAT);

    struct msgbuf data[4];
    int i;
    for(i=0;i<4;i++){
        data[i].mytype=66;
    }

    strcpy(data[0].bookname, "C");
    strcpy(data[1].bookname, "Linux");
    strcpy(data[2].bookname, "Makefile");
    strcpy(data[3].bookname, "End");

    for(i=0;i<4;i++){
        msgsnd(msgid, &data[i], sizeof(struct msgbuf)-sizeof(long), 0);
        if(strcmp(data[i].bookname,"End")==0){
            break;
        }
        sleep(1);
    }	
	/**********End**********/
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值