(1)client1,基于SIGIO的写法:

 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h> #include <stdio.h>
#include <stdlib.h>
#include <errno.h> #define BUFSIZE 128 static int fdr;
static int fdw; static void handler(int unuse)
{
int ret;
char buf[BUFSIZE]; ret = read(fdr, buf, BUFSIZE);
if (ret == -) {
return;
}
if (ret == ) {
exit();
}
write(, "\033[31m", );
write(, buf, ret);
write(, "\033[0m", );
} int main(void)
{
int ret;
char buf[BUFSIZE];
long flags; signal(SIGIO, handler); fdr = open("f1", O_RDONLY);
if (fdr == -) {
perror("f1");
goto open_fdr_err;
} fdw = open("f2", O_WRONLY);
if (fdw == -) {
perror("f2");
goto open_fdw_err;
} flags = fcntl(fdr, F_GETFL);
flags |= O_ASYNC;
fcntl(fdr, F_SETFL, flags); fcntl(fdr, F_SETOWN, getpid()); while () {
ret = read(, buf, BUFSIZE);
if (ret == -) {
if (errno == EINTR) {
continue;
}
perror("read()");
break;
}
if (ret == ) {
break;
}
write(fdw, buf, ret);
} close(fdw);
close(fdr); return ; close(fdw);
open_fdw_err:
close(fdr);
open_fdr_err:
return ;
}

client1,基于自定义信号的写法:

 #define _GNU_SOURCE

 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h> #include <stdio.h>
#include <stdlib.h>
#include <errno.h> #define BUFSIZE 128 static int fdr;
static int fdw; static void handler(int unuse)
{
int ret;
char buf[BUFSIZE]; ret = read(fdr, buf, BUFSIZE);
if (ret == -) {
return;
}
if (ret == ) {
exit();
}
write(, "\033[31m", );
write(, buf, ret);
write(, "\033[0m", );
} int main(void)
{
int ret;
char buf[BUFSIZE];
long flags; signal(SIGUSR1, handler); fdr = open("f1", O_RDONLY);
if (fdr == -) {
perror("f1");
goto open_fdr_err;
} fdw = open("f2", O_WRONLY);
if (fdw == -) {
perror("f2");
goto open_fdw_err;
} flags = fcntl(fdr, F_GETFL);
flags |= O_ASYNC;
fcntl(fdr, F_SETFL, flags); fcntl(fdr, F_SETOWN, getpid());
fcntl(fdr, F_SETSIG, SIGUSR1); while () {
ret = read(, buf, BUFSIZE);
if (ret == -) {
if (errno == EINTR) {
continue;
}
perror("read()");
break;
}
if (ret == ) {
break;
}
write(fdw, buf, ret);
} close(fdw);
close(fdr); return ; close(fdw);
open_fdw_err:
close(fdr);
open_fdr_err:
return ;
}

(2)client2:

 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h> #include <stdio.h>
#include <stdlib.h>
#include <errno.h> #define BUFSIZE 128 static int fdr;
static int fdw; static void handler(int unuse)
{
int ret;
char buf[BUFSIZE]; ret = read(fdr, buf, BUFSIZE);
if (ret == -) {
return;
}
if (ret == ) {
exit();
}
write(, "\033[31m", );
write(, buf, ret);
write(, "\033[0m", );
} int main(void)
{
int ret;
char buf[BUFSIZE];
long flags; signal(SIGIO, handler); fdw = open("f1", O_WRONLY);
if (fdw == -) {
perror("f1");
goto open_fdw_err;
} fdr = open("f2", O_RDONLY);
if (fdr == -) {
perror("f2");
goto open_fdr_err;
} flags = fcntl(fdr, F_GETFL);
flags |= O_ASYNC;
fcntl(fdr, F_SETFL, flags); fcntl(fdr, F_SETOWN, getpid()); while () {
ret = read(, buf, BUFSIZE);
if (ret == -) {
if (errno == EINTR) {
continue;
}
perror("read()");
break;
}
if (ret == ) {
break;
}
write(fdw, buf, ret);
} close(fdr);
close(fdw); return ; close(fdr);
open_fdr_err:
close(fdw);
open_fdw_err:
return ;
}
04-27 23:03