Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:54:13+00:00 2026-05-11T01:54:13+00:00

As I am currently doing this project in only C, I’ve up untill this

  • 0

As I am currently doing this project in only C, I’ve up untill this point only used my webserver as a single threaded application. However, I dont want that anymore! So I have the following code that handles my Work.

void BeginListen() {         CreateSocket();          BindSocket();          ListenOnSocket();          while ( 1 )         {             ProcessConnections();         } } 

Now I’ve added fork(); before the start of ProcessConnection(); which helpes me allowing multiple connections! However, when I add code for daemoning the application found in this answer. I’ve encounted a little problem, using fork() will create a copy of my whole running app, which is the purpose of fork(). So, I’d like to solve this problem.

My ProcessConnection() looks like this

void ProcessConnections() {         fork();          addr_size = sizeof(connector);          connecting_socket = accept(current_socket, (struct sockaddr *)&connector, &addr_size);          if ( connecting_socket < 0 )         {                 perror('Accepting sockets');                 exit(-1);         }          HandleCurrentConnection(connecting_socket);           DisposeCurrentConnection(); } 

How would I do to simply just add a couple of lines above or after connecting=socket = accept… in order to make it accept more than one connection at the time? Can i use fork(); but when it comes down to DisposeCurrentConnection(); I want to kill that process and just have the parent-thread running.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:54:13+00:00Added an answer on May 11, 2026 at 1:54 am

    I’m not a 100% sure what it is that you’re trying to do, buy off the top of my head, I’d prefer to do the fork after the accept, and simply exit() when you’re done. Keep in mind though, that you need to react to the SIGCHLD signal when the child process exits, otherwise you’ll have a ton of zombie-processes hanging around, waiting to deliver their exit-status to the parent process. C-pseudo-code:

    for (;;) {   connecting_socket = accept(server_socket);   if (connecting_socket < 0)     {       if (errno == EINTR)         continue;       else         {           // handle error           break;         }     }    if (! (child_pid = fork ()))     {        // child process, do work with connecting socket        exit (0);     }   else if (child_pid > 0)     {       // parent process, keep track of child_pid if necessary.     }   else     {       // fork failed, unable to service request, send 503 or equivalent.     } } 

    The child_pid is needed to (as already mentioned) to kill the child-process, but also if you wish to use waitpid to collect the exit status.

    Concerning the zombie-processes, if you’re not interested in what happened to the process, you could install a signal hander for SIGCHLD and just loop on waitpid with -1 until it there are no more child-processes, like this

    while (-1 != waitpid (-1, NULL, WNOHANG))   /* no loop body */ ; 

    The waitpid function will return the pid of the child that exited, so if you wish you can correlate this to some other information about the connection (if you did keep track of the pid). Keep in mind that accept will probably exit with errno set to EINTR, without a valid connection if a SIGCHLD is caught, so remember to check for this on accepts return.

    EDIT:
    Don’t forget to check for error conditions, i.e. fork returns -1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently doing something like this in some code I'm working on right now:
Currently I'm doing something like this in markup <input type=text ONKEYPRESS=InputNumeric(event); id=txtNumber /> But
How can I access Class object for Generics? Currently, I am doing it this
I'm currently doing some GUI testing on a ASP.net 2.0 application. The RDBMS is
I am currently doing a CRUD project for school and basically they want us
I am currently doing some Project Euler problems and the earlier ones often involve
I'm currently running a Django project on my school's webserver with FCGI. I did
I currently have a single solution that contains both the one application developed so
I'm currently working on a class project, and we're doing a basic implementation of
I'm currently doing the following to use typed datasets in vs2008: Right click on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.