Commit 3518a74cd9d305f971ba416f9687d109d496f095
1 parent
92cb4f97
Changed EINTR demonstration example
Showing
2 changed files
with
15 additions
and
5 deletions
eintrc.c
| @@ -12,10 +12,12 @@ | @@ -12,10 +12,12 @@ | ||
| 12 | void | 12 | void |
| 13 | alarmhandler (int s) | 13 | alarmhandler (int s) |
| 14 | { | 14 | { |
| 15 | - signal (SIGALRM, alarmhandler); | 15 | + printf("signal\n"); |
| 16 | alarm (1); | 16 | alarm (1); |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | +char buffer[1024 * 1024]; | ||
| 20 | + | ||
| 19 | int | 21 | int |
| 20 | main () | 22 | main () |
| 21 | { | 23 | { |
| @@ -24,8 +26,13 @@ main () | @@ -24,8 +26,13 @@ main () | ||
| 24 | struct sockaddr_in address; | 26 | struct sockaddr_in address; |
| 25 | int result; | 27 | int result; |
| 26 | ssize_t count; | 28 | ssize_t count; |
| 27 | - char buffer[1024 * 1024]; | ||
| 28 | - signal (SIGALRM, alarmhandler); | 29 | + |
| 30 | + struct sigaction sig; | ||
| 31 | + sig.sa_handler = alarmhandler; | ||
| 32 | + sig.sa_flags = 0; | ||
| 33 | + sigemptyset (&sig.sa_mask); | ||
| 34 | + sigaction (SIGALRM, &sig, NULL); | ||
| 35 | + | ||
| 29 | alarm (1); | 36 | alarm (1); |
| 30 | 37 | ||
| 31 | /* Create a socket for the client. */ | 38 | /* Create a socket for the client. */ |
| @@ -53,7 +60,7 @@ main () | @@ -53,7 +60,7 @@ main () | ||
| 53 | while (1) | 60 | while (1) |
| 54 | { | 61 | { |
| 55 | 62 | ||
| 56 | - count = write (sockfd, buffer, 1024 * 1024); | 63 | + count = write (sockfd, buffer, sizeof(buffer)); |
| 57 | if (count == -1) | 64 | if (count == -1) |
| 58 | perror ("write"); | 65 | perror ("write"); |
| 59 | else | 66 | else |
eintrs.c
| @@ -35,9 +35,12 @@ main () | @@ -35,9 +35,12 @@ main () | ||
| 35 | client_sockfd = accept (server_sockfd, | 35 | client_sockfd = accept (server_sockfd, |
| 36 | (struct sockaddr *) &client_address, &client_len); | 36 | (struct sockaddr *) &client_address, &client_len); |
| 37 | 37 | ||
| 38 | - while (1) | 38 | + for(int i=0; i<1000; ++i) |
| 39 | { | 39 | { |
| 40 | count = read (client_sockfd, buffer, 1024 * 1024); | 40 | count = read (client_sockfd, buffer, 1024 * 1024); |
| 41 | printf ("Read %zd bytes\n", count); | 41 | printf ("Read %zd bytes\n", count); |
| 42 | } | 42 | } |
| 43 | + | ||
| 44 | + while(1) | ||
| 45 | + sleep(1); | ||
| 43 | } | 46 | } |