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...