forked from Kevin19950607/Socket_greek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
25-IO-Process.c
97 lines (83 loc) · 1.64 KB
/
25-IO-Process.c
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
#define MAX_LINE 4096
char rot13_char(char c)
{
if((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
{
return c + 13;
}
else if((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
{
return c - 13;
}
else
{
return c;
}
}
void child_run(int fd)
{
char outbuf[MAX_LINE + 1];
size_t outbuf_used = 0;
ssize_t result;
while(1)
{
char ch;
result = recv(fd, &ch, 1, 0);
if(0 == result)
{
break;
}
else if( -1 == result)
{
perror("read");
break;
}
if( outbuf_used < sizeof(outbuf))
{
outbuf[outbuf_used++] = rot13_char(ch);
}
if( ch == '\n')
{
send(fd, outbuf_used, 0);
outbuf_used = 0;
continue;
}
}
}
// WNOHANG 用来告诉内核,即使还有未终止的子进程也不要阻塞在 waitpid 上
// 函数 sigchld_handler,在一个循环体内调用了 waitpid 函数,以便回收所有已终止的子进程
void sigchld_handler(int sig)
{
while( waitpid(-1, 0, WNOHANG) >0);
return;
}
int main(int argc, char* argv[])
{
int listener_fd = tcp_server_listen(SERV_PORT);
// 注册一个 SIGCHOLD 函数
signal(SIGCHLD, sigchld_handler);
while(1)
{
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int fd = accept(listener_fd, (struct sockaddr *) &ss, &slen);
if(fd <0)
{
error(1, error, "accept failed");
exit(1);
}
// 子进程处理逻辑
if(0 == fork())
{
close(listener_fd); // 子进程不需要关心监听套接字,这里关闭掉监听套接字 listen_fd
child_run(fd);
exit(0);
}
// 父进程逻辑
else
{
close(fd); // 父进程不需要关心连接套接字,所以在这里关闭连接套接字
}
}
return 0;
}