| Aaron Walker 2004-01-23, 5:01 pm |
| As stated in other previous posts, I am attempting to write a http
server. I was testing it out, and opened up gnome system monitor to
look at the mem usage and noticed that each time I request a page, the
child processes' memory usage gradually grows. They all start out at
around 180k. After serving about 30 files or so (basically hitting
reload in the browser that many times), all the children were using
about 1.5MB each. Am I leaking memory?
Here is child_main() that each child process runs after the initial
forking at the start of the program.
static void child_main(int i, int listenfd, int addrlen)
{
char *url;
int clientfd;
socklen_t client_addrlen;
struct sockaddr_in client_addr;
if(verbose)
printf("Starting child process %d [%ld]\n", i + 1,
(long)getpid());
for(;;) {
client_addrlen = addrlen;
if((clientfd = accept(listenfd,
(struct sockaddr *)&client_addr,
&client_addrlen)) == -1)
{
if(errno == EINTR)
continue;
else {
fprintf(stderr, "accept: %s\n",
strerror(errno));
exit(1);
}
url = dissect_request(clientfd,
inet_ntoa(client_addr.sin_addr));
if(url) {
process_request(clientfd, url);
free(url);
}
}
close(clientfd);
}
}
I can't see how it could be leaking memory. dissect_request() calls
malloc() for url and then I free() it after processing the client's
request. process_request calls malloc() once for a temporary char
string, but then free()'s it before returning to child_main(). Once the
main server loop starts in each child process, it stays in that loop
until until the parent process recieves a signal, so I know if there was
a leak, it would have to be something inside that main loop.
any suggestions?
I appreciate all the help everyone has given me so far. I plan on
sticking around comp.unix.programmer for a while, and hopefully once I
am more knowledgable I can help others like I have been helped so far.
Once again, thanks.
Aaron
|