IPC通信-命名管道

server.c

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<signal.h>
#include<time.h>
#include<errno.h>

#define FIFO_PIPE "fifo_pipe"
int count=0;
int fd;
typedef struct{
    int x;
    char y[128];
    double z;
}msg_buf;
msg_buf send_data;
char *msg[5]={"Hello","How are U","Fine Thank you","Good morning","Hmm..."};
double msgdb[5]={1.1111, 2.2222, 3.3333, 4.4444, 5.5555};
struct timespec t;

void handler(int in){
    close(fd);
    if(0==unlink(FIFO_PIPE))
        perror("server unlink");
    exit(0);
}

void handler2(int in){//when client terminate,server would not be terminated by PIPE error.
    perror("SIGPIPE");
}

int main(){
    signal(SIGINT,handler);
    signal(SIGPIPE,handler2);
    if(mkfifo(FIFO_PIPE,0666)==-1){
        perror("mkfifo(FIFO_PIPE)");
        exit(EXIT_FAILURE);
    }
    //if run server before, process would be blocked at open() until client run to open(0_RDONLY).
open:
    if((fd=open(FIFO_PIPE,O_WRONLY))==-1){
        perror("open(FIFO_PIPE)");
        sleep(1);
        goto open;
    }
//if server is faster than client, pipe is increasing, eventhough terminate server
//client would contiously read until read()=0;
    t.tv_nsec=5e8;
    t.tv_sec=0;

    while(1){
        send_data.x=count;
        strcpy(send_data.y, msg[rand()%5]);
        send_data.z=msgdb[rand()%5];
        if(-1==write(fd,&send_data,sizeof(send_data))){
            perror("write failure");
        }
        else{
            printf("Server: x=%d, y=%s, z=%f\n",send_data.x,send_data.y,send_data.z);
            count++;
        }
            
        nanosleep(&t,NULL);
    }

    return 0;
}

client.c

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

#define FIFO_PIPE "fifo_pipe"
int fd;
typedef struct{
    int x;
    char y[128];
    double z;
}msg_buf;
msg_buf receive_data;
struct timespec t;

void handler(int in){
    exit(0);
}

int main(){
    signal(SIGINT,handler);
open:
    if((fd=open(FIFO_PIPE,O_RDONLY))==-1){
        perror("open(FIFO_PIPE)");
        sleep(1);
        goto open;
    }
    //It is not necessary to nanosleep in client its speed is restricted by server write.
    //t.tv_nsec=5e7;
    //t.tv_sec=0;
    int ret;
    while(1){
        ret=read(fd,&receive_data,sizeof(receive_data));
        if(-1==ret)
            perror("read failure");
        else if(0==ret){//if pipe unlinked by server the read return 0
            perror("read failure0");
            goto open;
        }
        else
            printf("Client: x=%d, y=%s, z=%f\n",receive_data.x, receive_data.y, receive_data.z);
        //nanosleep(&t,NULL);
    }
    return 0;
}

makefile

# 定义编译器和链接器
CXX := g++
CC := gcc
AS := as
LD_GOLD := gold

# 定义编译选项
CXXFLAGS := -Wall -O3
CFLAGS := -Wall -O3
ASFLAGS :=

# 定义链接选项
LDFLAGS :=

# 自动寻找所有的源文件
CPP_SRCS := $(wildcard *.cpp)
C_SRCS := $(wildcard *.c)
S_SRCS := $(wildcard *.s)

# 生成目标文件名
CPP_ELFS := $(patsubst %.cpp,%.elf,$(CPP_SRCS))
C_ELFS := $(patsubst %.c,%.elf,$(C_SRCS))
S_ELFS := $(patsubst %.s,%.elf,$(S_SRCS))

# 默认目标
all: $(CPP_ELFS) $(C_ELFS) $(S_ELFS)

# C++ 文件的编译和链接规则
%.elf: %.cpp
	-$(CXX) $(CXXFLAGS) $< -c -o $(patsubst %.elf,%.o,$@)
	-$(CXX) $(patsubst %.elf,%.o,$@) -o $@

# C 文件的编译和链接规则
%.elf: %.c
	-$(CC) $(CFLAGS) $< -c -o $(patsubst %.elf,%.o,$@)
	-$(CC) $(patsubst %.elf,%.o,$@) -o $@

# 汇编文件的编译和链接规则,使用gold
%.elf: %.s
	-$(AS) $(ASFLAGS) $< -o $(patsubst %.elf,%.o,$@)
	-$(LD_GOLD) $(patsubst %.elf,%.o,$@) -o $@

# 清理构建产物
clean:
	rm -f *.o *.elf

.PHONY: all clean

watch -n 1 stat <pipe_name>  //Check pipe status.

if 1 server, 2 clients, there is resource capture conflict... 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值