IPC-管道
问题描述:a进程把字符串发给b进程,b进程把字符串转换成大写后回发给a进程
server.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define SERVER_FIFO "./server.fifo"
struct MSG
{
int clientpid;
char data[512];
};
int readn(int fd, char *buf, size_t count)
{
int sum = 0;
int ret = -1;
while(ret != 0)
{
ret = read(fd, buf+sum, count-sum);
if(ret == -1)
{
perror("read");
exit(EXIT_FAILURE);
}
sum += ret;
}
return sum;
}
int writen(int fd, char *buf, size_t count)
{
int sum = 0;
int ret = 0;
while(sum != count)
{
ret = write(fd, buf+sum, count-sum);
if(ret == -1)
{
perror("write");
exit(EXIT_FAILURE);
}
sum += ret;
}
return sum;
}
int mkOpenFifo()
{
if(-1 == mkfifo(SERVER_FIFO, 0666))
{
perror("mkfifo");
exit(EXIT_FAILURE);
}
printf("mkfifo finish!\n");
int serverfd = open(SERVER_FIFO, O_RDONLY);
if(-1 == serverfd)
{
perror("open");
exit(EXIT_FAILURE);
}
printf("mkOpenFifo finish!\n");
return serverfd;
}
void convertUpper(char *buf)
{
while(*buf)
{
if(*buf >= 'a' && *buf <= 'z')
*buf -= 32;
++buf;
}
}
int openClientFifo(char *clientFifo, int flags)
{
int clientfd = open(clientFifo, flags);
if(-1 == clientfd)
{
perror("open");
exit(EXIT_FAILURE);
}
return clientfd;
}
int main(void)
{
int serverfd = mkOpenFifo();
struct MSG msg;
while(1)
{
printf("begin------------------------------\n");
memset(&msg, 0, sizeof(struct MSG));
readn(serverfd, (char*)&msg, sizeof(struct MSG));
printf("Receive from %d: %s\n", msg.clientpid, msg.data);
convertUpper(msg.data);
char clientFifo[32];
sprintf(clientFifo, "client%d.fifo", msg.clientpid);
int clientfd = openClientFifo(clientFifo, O_WRONLY);
printf("send to %d: %s\n", msg.clientpid, msg.data);
writen(clientfd, msg.data, strlen(msg.data)+1);
close(clientfd);
putchar(10);
}
close(serverfd);
return 0;
}
client.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#define SERVER_FIFO "./server.fifo"
struct MSG
{
int clientpid;
char data[512];
};
int readn(int fd, char *buf, size_t count)
{
int sum = 0;
int ret = -1;
while(ret != 0)
{
ret = read(fd, buf+sum, count-sum);
if(ret == -1)
{
perror("read");
exit(EXIT_FAILURE);
}
sum += ret;
}
return sum;
}
int writen(int fd, char *buf, size_t count)
{
int sum = 0;
int ret = 0;
while(sum != count)
{
ret = write(fd, buf+sum, count-sum);
if(ret == -1)
{
perror("write");
exit(EXIT_FAILURE);
}
sum += ret;
}
return sum;
}
void mkClientFifo(char *clientFifo)
{
if(-1 == mkfifo(clientFifo, 0666))
{
perror("mkfifo");
exit(EXIT_FAILURE);
}
printf("mkfifo finish!\n");
}
int openFifo(char *fifoPath, int flags)
{
int fd = open(fifoPath, flags);
if(-1 == fd)
{
perror("open");
exit(EXIT_FAILURE);
}
printf("openFifo finish!\n");
return fd;
}
int main(void)
{
int serverfd = openFifo(SERVER_FIFO, O_WRONLY);
char clientFifo[32];
memset(clientFifo, 0, sizeof(clientFifo));
sprintf(clientFifo, "client%d.fifo", getpid());
mkClientFifo(clientFifo);
struct MSG msg;
msg.clientpid = getpid();
char upperStr[512];
while(1)
{
printf("begin---------------------------------\n");
memset(&msg.data, 0, sizeof(msg.data));
printf("please input: ");
fgets(msg.data, sizeof(msg.data), stdin);
// int n = readn(STDIN_FILENO, msg.data, sizeof(msg.data)-1);
printf("send to server: pid: %d\tmsg: %s\n", getpid(), msg.data);
writen(serverfd, (char*)&msg, sizeof(struct MSG));
int clientfd = openFifo(clientFifo, O_RDONLY);
memset(upperStr, 0, sizeof(upperStr));
readn(clientfd, upperStr, sizeof(upperStr));
printf("receive from server: %s\n", upperStr);
close(clientfd);
putchar(10);
}
close(serverfd);
return 0;
}