-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfork.c
More file actions
152 lines (127 loc) · 3.69 KB
/
fork.c
File metadata and controls
152 lines (127 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* A simple preforking echo server in C.
*
* Building:
*
* $ gcc -Wall -o echo echo.c
*
* Usage:
*
* $ ./echo
*
* ~ then in another terminal ... ~
*
* $ echo 'Hello, world!' | nc localhost 4242
*
*/
#include <unistd.h> /* fork, close */
#include <stdlib.h> /* exit */
#include <string.h> /* strlen */
#include <stdio.h> /* perror, fdopen, fgets */
#include <sys/socket.h>
#include <sys/wait.h> /* waitpid */
#include <netdb.h> /* getaddrinfo */
#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define PORT "4242"
#define NUM_CHILDREN 3
#define MAXLEN 1024
int readline(int fd, char *buf, int maxlen); // forward declaration
int
main(int argc, char** argv)
{
int i, n, sockfd, clientfd;
int yes = 1; // used in setsockopt(2)
struct addrinfo *ai;
struct sockaddr_in *client;
socklen_t client_t;
pid_t cpid; // child pid
char line[MAXLEN];
char cpid_s[32];
char welcome[32];
/* Create a socket and get its file descriptor -- socket(2) */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
die("Couldn't create a socket");
}
/* Prevents those dreaded "Address already in use" errors */
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&yes, sizeof(int)) == -1) {
die("Couldn't setsockopt");
}
/* Fill the address info struct (host + port) -- getaddrinfo(3) */
if (getaddrinfo(NULL, PORT, NULL, &ai) != 0) {
die("Couldn't get address");
}
/* Assign address to this socket's fd */
if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) != 0) {
die("Couldn't bind socket to address");
}
/* Free the memory used by our address info struct */
freeaddrinfo(ai);
/* Mark this socket as able to accept incoming connections */
if (listen(sockfd, 10) == -1) {
die("Couldn't make socket listen");
}
/* Fork you some child processes. */
for (i = 0; i < NUM_CHILDREN; i++) {
cpid = fork();
if (cpid == -1) {
die("Couldn't fork");
}
if (cpid == 0) { // We're in the child ...
for (;;) { // Run forever ...
/* Necessary initialization for accept(2) */
client_t = sizeof client;
/* Blocks! */
clientfd = accept(sockfd, (struct sockaddr *)&client, &client_t);
if (clientfd == -1) {
die("Couldn't accept a connection");
}
/* Send a welcome message/prompt */
bzero(cpid_s, 32);
bzero(welcome, 32);
sprintf(cpid_s, "%d", getpid());
sprintf(welcome, "Child %s echo> ", cpid_s);
send(clientfd, welcome, strlen(welcome), 0);
/* Read a line from the client socket ... */
n = readline(clientfd, line, MAXLEN);
if (n == -1) {
die("Couldn't read line from connection");
}
/* ... and echo it back */
send(clientfd, line, n, 0);
/* Clean up the client socket */
close(clientfd);
}
}
}
/* Sit back and wait for all child processes to exit */
while (waitpid(-1, NULL, 0) > 0);
/* Close up our socket */
close(sockfd);
return 0;
}
/**
* Simple utility function that reads a line from a file descriptor fd,
* up to maxlen bytes -- ripped from Unix Network Programming, Stevens.
*/
int
readline(int fd, char *buf, int maxlen)
{
int n, rc;
char c;
for (n = 1; n < maxlen; n++) {
if ((rc = read(fd, &c, 1)) == 1) {
*buf++ = c;
if (c == '\n')
break;
} else if (rc == 0) {
if (n == 1)
return 0; // EOF, no data read
else
break; // EOF, read some data
} else
return -1; // error
}
*buf = '\0'; // null-terminate
return n;
}