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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:50:55+00:00 2026-06-06T06:50:55+00:00

My code looks somehow like this: //INCLUDES FILE *file; void handlesocket(int socket); int main(int

  • 0

My code looks somehow like this:

//INCLUDES

FILE *file;

void handlesocket(int socket);
int main(int argc, char *argv[])
{
    openlog("daemon", LOG_PID, LOG_USER);
    syslog(LOG_INFO, "daemon started.");
    file = fopen("/var/log/daemon.log","a+");
    fprintf(file,"Opened log file...");

     while (1) {
         pid = fork();
         if (pid == 0)  {
             handlesocket(socket);
             exit(0);
         }
         else close(socket);
     }
}

void handlesocket(int socket)
{
//handle socket
}

Basically, it is waiting for a new connections and then forks itself. (I removed all the socket code so it’s easier to read.

My problem is that everytime a new connection comes in (and a new fork() is getting called) the fprintf seems to get called again and there is a new "Opened log file..." in my log.

Why does this happen?

  • 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-06T06:50:56+00:00Added an answer on June 6, 2026 at 6:50 am

    Fork duplicates the process completely – including the buffers used by fprintf. You need to flush the file handle right before the fork so that your forked process starts with io buffers clear:

    fprintf( file, "opening log file\n" );
    fflush( file );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The basic pseudo code looks like this: void myFunction() { int size = 10;
My code looks like this : Vector<String> My_Vector=new Vector<String>(); String My_Array[]=new String[100]; for (int
My code looks like this: NSIndexPath *ip = [NSIndexPath indexPathForRow:15 inSection:0]; [[self tableView] selectRowAtIndexPath:ip
My code looks something like: template <typename type> void deserialize_element(type* result) { //... if
My C# code looks like this: myNum = dt.Columns[0]; myNum is an integer and
I apologize if this code looks a bit like a mess (considering the length);
My code looks like this, and it works: if Target.find_by_shrunk(params[:shrunk]) @target = Target.find_by_shrunk(params[:shrunk]) else
Okay so my code looks kinda like this // Returns the note of a
Currently I have code that looks like this Status(Start Step 1); result = Step1();
The original code looks like this: module Acme class Address STREET_NAME = 123 acme

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.