px_ipc_name函数
略
#include"unpipc.h"
void client(int, int), server(int, int);
int main(int argc, char**argv){
int pipe1[2], pipe2[2];
pid_t childpid;
Pipe(pipe1);
Pipe(pipe2);
if((childpid = Fork()) == 0){ //child
Close(pipe1[1]);
Close(pipe2[0]);
server(pipe1[0], pipe2[1]);
exit(0);
}
Close(pipe1[0]);
Close(pipe2[1]);
client(pipe2[0], pipe1[1]);
Waitpid(childpid, NULL,0);
exit(0);
}
void client(int readfd, int writefd){
size_t len;
ssize_t n;
char buff[MAXLINE];
//从标准输入读取路径名
Fgets(buff, MAXLINE, stdin);
len = strlen(buff); //fgets guarantees null byte at end
if(buff[len-1] == '\n'){
len--; //delete newline frim fgets
}
Write(writefd, buff, len);
while((n=Read(readfd, buff, MAXLINE))>0)
Write(STDOUT_FILENO, buff, n);
}
void server(int readfd, int writefd){
int fd;
ssize_t n;
char buff[MAXLINE+1];
//read pathname from IPC channel
if((n=Read(readfd, buff, MAXLINE))==0)
err_quit("END OF FILE WHILR READING PATH");
buff[n] = '\0'; //null terminate pathname
if((fd =open(buff, O_RDONLY))<0){
snprintf(buff+n, sizeof(buff)-n, "cannot open, %s\n",
strerror(errno));
n = strlen(buff);
Write(writefd, buff, n);
}else{
while((n=Read(fd, buff, MAXLINE))>0)
Write(writefd, buff, n);
Close(fd);
}
}
#include"unpipc.h"
int main(int argc, char**argv){
int fd[2], n;
char c;
pid_t childpid;
Pipe(fd);
if((childpid = Fork()) ==0){
sleep(3);
if((n=Read(fd[0], &c, 1))!=1)
err_quit("child:read returned %d",n);
//%c是以字符形式输出,只输出一个字符;
printf("child read %c\n",c);
Write(fd[0],"c",1);
exit(0);
}
Write(fd[1], "p", 1);
if((n=Read(fd[1], &c, 1))!=1)
err_quit("child:read returned %d",n);
printf("parent read %c\n",c);
exit(0);
}
#include"unpipc.h"
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
void client(int, int), server(int, int);
int main(int argc, char**argv){
int readfd, writefd;
pid_t childpid;
if((mkfifo(FIFO1, FILE_MODE)<0) && (errno !=EEXIST))
err_sys("cannot create fifo");
if((mkfifo(FIFO2, FILE_MODE)<0) && (errno !=EEXIST)){
unlink(FIFO1);
err_sys("cannot create fifo");
}
if((childpid = Fork()==0)){
readfd = Open(FIFO1, O_RDONLY, 0);
writefd = Open(FIFO2, O_WRONLY, 0);
server(readfd, writefd);
exit(0);
}
writefd = Open(FIFO1, O_WRONLY, 0);
readfd = Open(FIFO2, O_RDONLY, 0);
client(readfd, writefd);
Waitpid(childpid, NULL, 0); //wait for child to terminate
Close(readfd);
Close(writefd);
Unlink(FIFO1);
Unlink(FIFO2);
exit(0);
}
void client(int readfd, int writefd){
size_t len;
ssize_t n;
char buff[MAXLINE];
//从标准输入读取路径名
Fgets(buff, MAXLINE, stdin);
len = strlen(buff); //fgets guarantees null byte at end
if(buff[len-1] == '\n'){
len--; //delete newline frim fgets
}
Write(writefd, buff, len);
while((n=Read(readfd, buff, MAXLINE))>0)
Write(STDOUT_FILENO, buff, n);
}
void server(int readfd, int writefd){
int fd;
ssize_t n;
char buff[MAXLINE+1];
//read pathname from IPC channel
if((n=Read(readfd, buff, MAXLINE))==0)
err_quit("END OF FILE WHILR READING PATH");
buff[n] = '\0'; //null terminate pathname
if((fd =open(buff, O_RDONLY))<0){
snprintf(buff+n, sizeof(buff)-n, "cannot open, %s\n",
strerror(errno));
n = strlen(buff);
Write(writefd, buff, n);
}else{
while((n=Read(fd, buff, MAXLINE))>0)
Write(writefd, buff, n);
Close(fd);
}
}