管道server-client
Posted 吾乃世间奇才
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了管道server-client相关的知识,希望对你有一定的参考价值。
server.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#define LOG_W(fmt, args...) printf("\\033[33m[SERVER][%s@%d]:\\033[0m" fmt , __func__, __LINE__, ## args)
#define FIFO_PRO_NUM 4
double col_num_avr(double *arr, int num)
int i=0;
double tmp = 0.0;
for(i=0; i < num; i++)
printf("%lf ", arr[i]);
tmp += arr[i];
printf("\\n");
return tmp/num;
struct tras_data
double num[10];
double aver;
;
struct fifo_data
int rfd;
int wfd;
char rfilename[8];
char wfilename[8];
struct tras_data data;
;
struct fifo_data fifo_list[4] =
.rfilename = "r1", .wfilename = "w1",
.rfilename = "r2", .wfilename = "w2",
.rfilename = "r3", .wfilename = "w3",
.rfilename = "r4", .wfilename = "w4"
;
void *deal_num_data(void *arg)
int len;
struct fifo_data *datap = (struct fifo_data *)arg;
struct tras_data snd_data;
snd_data.aver = col_num_avr(datap->data.num, 10);
len = write(datap->wfd, &snd_data, sizeof(struct tras_data));
return NULL;
void close_fun(int signo)
int i=0;
if(SIGINT == signo)
for(i=0; i < FIFO_PRO_NUM; i++)
unlink(fifo_list[i].rfilename);
unlink(fifo_list[i].wfilename);
close(fifo_list[i].wfd);
close(fifo_list[i].rfd);
exit(0);
int main()
int fd = 0;
int len = 0;
int ret;
int i;
pthread_t pid;
signal(SIGINT, close_fun);
for(i=0; i < FIFO_PRO_NUM; i++)
mkfifo(fifo_list[i].rfilename,0777);
mkfifo(fifo_list[i].wfilename,0777);
fifo_list[i].rfd = open(fifo_list[i].rfilename, O_RDONLY);
fifo_list[i].wfd = open(fifo_list[i].wfilename, O_WRONLY);
len = read(fifo_list[i].rfd, &fifo_list[i].data.num, sizeof(fifo_list[i].data.num));
pthread_create(&pid, NULL, deal_num_data, &fifo_list[i]);
while(1);
return 0;
client.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>
struct tras_data
double num[10];
double aver;
;
void col_num_avr(double *arr, int num)
int i=0;
double tmp = 0.0;
for(i=0; i < num; i++)
printf("%lf ", arr[i]);
tmp += arr[i];
printf("\\n");
int main(int argc, char *argv[])
int fd = 0;
int wfd;
int len = 0;
int i;
struct tras_data data;
char write_fifoname[8];
char read_fifoname[8];
sprintf(write_fifoname, "r%s", argv[1]);
sprintf(read_fifoname, "w%s", argv[1]);
fd = open(write_fifoname, O_WRONLY);
wfd = open(read_fifoname, O_RDONLY);
if (fd < 0)
perror("open error");
exit(1);
srand(atoi(argv[1]) + time(NULL));
for(i=0; i < 10; i++)
data.num[i] = 0+1.0*(rand()%RAND_MAX)/RAND_MAX *(1-0);
//向FIFO写入数据
write(fd, &data, sizeof(data));
memset(&data, 0, sizeof(data));
len = read(wfd, &data, sizeof(data));
printf("the , fd is %d, argageis %lf\\n", len, data.aver);
if(len < 0)
printf("%s\\n", strerror(errno));
close(fd);
return 0;
两个程序,客户端生成随机10个float数,然后服务端接受,并返回给client平均值,
执行
./server
seq 1 4 | xargs -P 0 -n 1 ./client
以上是关于管道server-client的主要内容,如果未能解决你的问题,请参考以下文章