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

  • Home
  • SEARCH
  • 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 8431297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:49:01+00:00 2026-06-10T05:49:01+00:00

How can the following code while(1) { client_name_len = sizeof(struct sockaddr_in); newsockfd = accept(sockfd,

  • 0

How can the following code

while(1) {
  client_name_len = sizeof(struct sockaddr_in);
  newsockfd = accept(sockfd, (struct sockaddr *)&client_name, &client_name_len);
  if (newsockfd < 0) {
    perror("ERROR on accept");
    printf("Finished\n");
    close(sockfd);
    exit (EXIT_FAILURE);
  }
  printf("a:");
  pthread_t thread1;
  int *addr = (int*)malloc(sizeof(int));
  *addr = newsockfd;
  pthread_create( &thread1, NULL, &ProcessClient, (void*)addr);
}

produce the following output:

a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:ERROR on accept: Too many open files
a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:Finished

The problem is that perror("ERROR on accept"), printf("Finished\n") and printf("a:") are executed in the same thread, but the output is mixed.

ProcessClient does not output anything and does not create any threads.
sockfd is a standard listening tcp socket.

  • 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-10T05:49:02+00:00Added an answer on June 10, 2026 at 5:49 am

    The printf("a:"); and printf("Finished\n"); are buffered I/O going to stdout.

    perror("ERROR on accept"); is unbuffered (or immediately flushed) I/O going to stderr.

    So the many a:‘s that show up after the ERROR on accept: Too many open files were actually printf()‘ed before, but buffering makes them show up later on the console.

    See https://stackoverflow.com/a/3746795/12711 for details and methods to solve the problem.

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

Sidebar

Related Questions

I think the following code can be used to create manipulators. #include<iostream> ostream &
Let's consider the following scenario: a function which can generate code colors from white
How can the following code be modified to run my insert statements 100 at
Using dotnet 2.0. Can the following code be improved in style ? private object
If use MongoRepository, You can have following code: @Repository public interface UserRepo extends MongoRepository<User,
The following code can be found in the NHibernate.Id.GuidCombGenerator class. The algorithm creates sequential
With CLLocationManager I can use the following code to determine if I can access
Any suggestions on how I can cleanup the following code pattern that repeats multiple
I'm not sure if the following code can cause redundant calculations, or is it
The Current Setup: So, I can use the following code to save a file

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.