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 8476951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:19:13+00:00 2026-06-10T18:19:13+00:00

Here a piece of my fork-server code: signal (SIGINT, ( void *)sig_handler); while(1){ memset(&cli_addr,

  • 0

Here a piece of my fork-server code:

    signal (SIGINT, ( void *)sig_handler);
while(1){
    memset(&cli_addr, 0, sizeof(cli_addr));

    if((newsockd = accept(sockd, (struct sockaddr *) &cli_addr, (socklen_t *) &socket_len)) < 0){
        perror("Errore nella connessione\n");
        onexit(newsockd, sockd, 0, 2);
    }
    fprintf(stdout, "Ricevuta richiesta di connessione dall' indirizzo %s\n", inet_ntoa(cli_addr.sin_addr));

    child_pid = fork();
    if(child_pid < 0){
        perror("Fork error");
        onexit(newsockd, sockd, 0, 2);
    }
    if(child_pid == 0){
        do_child(newsockd);
    }
    else{
        attended_pid = waitpid(child_pid, NULL, 0);
        if(attended_pid != child_pid){
            printf("Child error");
        }
    }
}

My question is: have i to close the main sock (sockd) before executing the client?
I think no but:

  • i’ve added a “sigint controller” and if i press CTRL-C on the server (with commented sockd -> //close(sockd);) the sigint will be executed 3 times with 2 concurrent connection (why??)
  • if i have this code close(sockd) the sigint will be executed only 1 time but i got an :bad file descriptor on the accept instruction.

So, what have i to do?
Thanks!

PS: my sigint code:

void sig_handler(const int signo, const int sockd, const int newsockd){
  if (signo == SIGINT){
    printf("Ricevuto SIGINT, esco...\n");
    if(newsockd) close(newsockd);
    if(sockd) close(sockd);
    exit(EXIT_SUCCESS);
  }
}
  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-06-10T18:19:14+00:00Added an answer on June 10, 2026 at 6:19 pm

    From the fork(2) manual page:

    The child inherits copies of the parent’s set of open file descriptors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).

    This tells me that you should not close the listening socket in the child process. You shouldn’t close the accepted socket in the parent process either, but let it be closed by the child process when it’s done.

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

Sidebar

Related Questions

Here's a piece of code I'm using: public void Start() { Dns.BeginGetHostEntry(www.google.com, new AsyncCallback(Stop),
Here's the piece of my HTML code: <li> <a id=section1 href=# onclick=return false>&raquo; Section
Here is my piece of code that creates buttons programmatically: NSArray *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@Cover_0.png],
Here a small piece of code for testing and explain the problem. I have
Here I have piece of code, showing example of how I want to update
Here is a piece of my code $file=Files::model()->findByPk($id); if($file == null) { throw new
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
Here's a piece of code from the xss_clean method of the Input_Core class of
Here's a piece of code that takes most time in my program, according to
Here's a piece of code, currently driving me nuts. It's a UIView, with an

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.