首页 > 学院 > 操作系统 > 正文

信号驱动的IO

2024-06-28 13:21:23
字体:
来源:转载
供稿:网友
信号驱动的IO

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

 1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <unistd.h> 5 #include <signal.h> 6  7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <errno.h>10 11 #define BUFSIZE 12812 13 static int fdr;14 static int fdw;15 16 static void handler(int unuse)17 {18     int ret;19     char buf[BUFSIZE];20 21     ret = read(fdr, buf, BUFSIZE);22     if (ret == -1) {23         return;24     }25     if (ret == 0) {26         exit(0);27     }28     write(1, "/033[31m", 5);29     write(1, buf, ret);30     write(1, "/033[0m", 4);31 }32 33 int main(void)34 {35     int ret;36     char buf[BUFSIZE];37     long flags;38 39     signal(SIGIO, handler);40 41     fdr = open("f1", O_RDONLY);42     if (fdr == -1) {43         perror("f1");44         goto open_fdr_err;45     }46 47     fdw = open("f2", O_WRONLY);48     if (fdw == -1) {49         perror("f2");50                 goto open_fdw_err;51     }52 53     flags = fcntl(fdr, F_GETFL);54     flags |= O_ASYNC;55     fcntl(fdr, F_SETFL, flags);56 57     fcntl(fdr, F_SETOWN, getpid());58 59     while (1) {60         ret = read(0, buf, BUFSIZE);61         if (ret == -1) {62             if (errno == EINTR) {63                 continue;64             }65             perror("read()");66             break;67         }68         if (ret == 0) {69             break;70         }71         write(fdw, buf, ret);72     }73 74     close(fdw);75     close(fdr);76 77     return 0;78 79 80     close(fdw);81 open_fdw_err:82     close(fdr);83 open_fdr_err:84     return 1;85 }
View Code

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

 1 #define _GNU_SOURCE 2  3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 #include <unistd.h> 7 #include <signal.h> 8  9 #include <stdio.h>10 #include <stdlib.h>11 #include <errno.h>12 13 #define BUFSIZE 12814 15 static int fdr;16 static int fdw;17 18 static void handler(int unuse)19 {20     int ret;21     char buf[BUFSIZE];22 23     ret = read(fdr, buf, BUFSIZE);24     if (ret == -1) {25         return;26     }27     if (ret == 0) {28         exit(0);29     }30     write(1, "/033[31m", 5);31     write(1, buf, ret);32     write(1, "/033[0m", 4);33 }34 35 int main(void)36 {37     int ret;38     char buf[BUFSIZE];39     long flags;40 41     signal(SIGUSR1, handler);42 43     fdr = open("f1", O_RDONLY);44     if (fdr == -1) {45         perror("f1");46         goto open_fdr_err;47     }48 49     fdw = open("f2", O_WRONLY);50     if (fdw == -1) {51         perror("f2");52                 goto open_fdw_err;53     }54 55     flags = fcntl(fdr, F_GETFL);56     flags |= O_ASYNC;57     fcntl(fdr, F_SETFL, flags);58 59     fcntl(fdr, F_SETOWN, getpid());60     fcntl(fdr, F_SETSIG, SIGUSR1);61 62     while (1) {63         ret = read(0, buf, BUFSIZE);64         if (ret == -1) {65             if (errno == EINTR) {66                 continue;67             }68             perror("read()");69             break;70         }71         if (ret == 0) {72             break;73         }74         write(fdw, buf, ret);75     }76 77     close(fdw);78     close(fdr);79 80     return 0;81 82 83     close(fdw);84 open_fdw_err:85     close(fdr);86 open_fdr_err:87     return 1;88 }
View Code

(2)client2:

 1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <signal.h> 5 #include <unistd.h> 6  7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <errno.h>10 11 #define BUFSIZE 12812 13 static int fdr;14 static int fdw;15 16 static void handler(int unuse)17 {18     int ret;19     char buf[BUFSIZE];20 21     ret = read(fdr, buf, BUFSIZE);22     if (ret == -1) {23         return;24     }25     if (ret == 0) {26         exit(0);27     }28     write(1, "/033[31m", 5);29     write(1, buf, ret);30     write(1, "/033[0m", 4);31 }32 33 int main(void)34 {35     int ret;36     char buf[BUFSIZE];37     long flags;38 39     signal(SIGIO, handler);40 41     fdw = open("f1", O_WRONLY);42     if (fdw == -1) {43         perror("f1");44                 goto open_fdw_err;45     }46 47     fdr = open("f2", O_RDONLY);48     if (fdr == -1) {49         perror("f2");50         goto open_fdr_err;51     }52 53     flags = fcntl(fdr, F_GETFL);54     flags |= O_ASYNC;55     fcntl(fdr, F_SETFL, flags);56 57     fcntl(fdr, F_SETOWN, getpid());58 59     while (1) {60         ret = read(0, buf, BUFSIZE);61         if (ret == -1) {62             if (errno == EINTR) {63                 continue;64             }65             perror("read()");66             break;67         }68         if (ret == 0) {69             break;70         }71         write(fdw, buf, ret);72     }73 74     close(fdr);75     close(fdw);76 77     return 0;78 79 80     close(fdr);81 open_fdr_err:82     close(fdw);83 open_fdw_err:84     return 1;85 }
View Code


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表