| 1 |
/* sktfork.c: this program illustrates use of fork under Unix SysV with a |
| 2 |
* simple server. |
| 3 |
*/ |
| 4 |
#include <stdio.h> |
| 5 |
#include <sys/types.h> |
| 6 |
#include <sys/wait.h> |
| 7 |
#include <signal.h> |
| 8 |
#include "xtdio.h" |
| 9 |
#include "sockets.h" |
| 10 |
|
| 11 |
/* ------------------------------------------------------------------------ |
| 12 |
* Definitions: |
| 13 |
*/ |
| 14 |
#define BUFSIZE 256 |
| 15 |
|
| 16 |
/* ------------------------------------------------------------------------ |
| 17 |
* Globals: |
| 18 |
*/ |
| 19 |
int childstat= 0; |
| 20 |
|
| 21 |
/* ------------------------------------------------------------------------ |
| 22 |
* Prototypes: |
| 23 |
*/ |
| 24 |
int main( int, char **); /* sktfork.c */ |
| 25 |
void sigchld_handler(void); /* sktfork.c */ |
| 26 |
|
| 27 |
/* ------------------------------------------------------------------------ |
| 28 |
* Source Code: |
| 29 |
*/ |
| 30 |
|
| 31 |
/* main: */ |
| 32 |
int main( |
| 33 |
int argc, |
| 34 |
char **argv) |
| 35 |
{ |
| 36 |
int sktcnt = 0; |
| 37 |
int istat; |
| 38 |
int ret; |
| 39 |
Socket *srvr = NULL; |
| 40 |
Socket *nskt = NULL; |
| 41 |
|
| 42 |
if (argc <= 1) { |
| 43 |
fprintf(stderr,"***usage*** sktfork srvrname\n"); |
| 44 |
exit(1); |
| 45 |
} |
| 46 |
|
| 47 |
srvr = Sopen(argv[1],"s"); |
| 48 |
if (!srvr) { |
| 49 |
fprintf(stderr, "***error*** unable to open <%s> server\n", argv[1]); |
| 50 |
exit(1); |
| 51 |
} |
| 52 |
|
| 53 |
/* install a SIGCHLD handler */ |
| 54 |
sigset(SIGCHLD, sigchld_handler); |
| 55 |
|
| 56 |
while(1) { |
| 57 |
printf("Wait for new socket to be accepted\n"); |
| 58 |
nskt = Saccept(srvr); |
| 59 |
if (!nskt) { /* presumably I just got a SIGCHLD */ |
| 60 |
printf("WEXITSTATUS(childstat=%d)=%d\n", childstat, |
| 61 |
WEXITSTATUS(childstat)); |
| 62 |
if (WEXITSTATUS(childstat) == 1) break; |
| 63 |
continue; |
| 64 |
} else { |
| 65 |
++sktcnt; |
| 66 |
printf("normal new skt: sktcnt=%d\n", sktcnt); |
| 67 |
} |
| 68 |
ret = fork(); |
| 69 |
|
| 70 |
if (ret == 0) { |
| 71 |
/* child process */ |
| 72 |
char buf[BUFSIZE]; |
| 73 |
Socket *skt; |
| 74 |
|
| 75 |
printf("child() skt<%s>", Sprtskt(nskt)); |
| 76 |
skt = nskt; |
| 77 |
nskt = NULL; |
| 78 |
|
| 79 |
if (!Sgets(buf, BUFSIZE, skt)) { |
| 80 |
fprintf(stderr, "***error*** unable to Sgets\n"); |
| 81 |
_exit(0); |
| 82 |
} |
| 83 |
Sputs("goodbye!", skt); |
| 84 |
printf("got <%s>, goodbye!\n", buf); |
| 85 |
Sclose(skt); |
| 86 |
skt= NULL; |
| 87 |
|
| 88 |
if (!strcmp(buf, "Q")) { |
| 89 |
printf("return: child 1\n"); |
| 90 |
_exit(1); |
| 91 |
} |
| 92 |
printf("return: child 0\n"); |
| 93 |
_exit(0); |
| 94 |
} else if (ret == -1) { |
| 95 |
fprintf(stderr, "***error*** unable to fork\n"); |
| 96 |
if (nskt) Sclose(nskt); |
| 97 |
if (srvr) Sclose(srvr); |
| 98 |
srvr = nskt = NULL; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
printf("closing down server: sktcnt=%d\n", sktcnt); |
| 104 |
if (srvr) Sclose(srvr); |
| 105 |
srvr = NULL; |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
/* --------------------------------------------------------------------- */ |
| 111 |
|
| 112 |
/* sigchld_handler: this function */ |
| 113 |
void sigchld_handler() |
| 114 |
{ |
| 115 |
wait(&childstat); |
| 116 |
} |
| 117 |
|
| 118 |
/* --------------------------------------------------------------------- */ |