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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:56:45+00:00 2026-06-14T02:56:45+00:00

#include<stdio.h> #include<sys/types.h> #include<unistd.h> void main() { int i = 1; pid_t child_pid; printf(The main

  • 0
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
void main()
{
  int i = 1;
  pid_t child_pid;
  printf("The main program process ID is %d", (int) getpid());
  printf("%d", i);
  child_pid = fork();
  if (child_pid != 0) {
    i++;
    printf("%d", i);
    printf("This is the parent process, with ID %d \n", (int) getpid());
    printf("The child process is %d ", (int) child_pid);
  } else {
    printf("%d", i);
    printf("This is the child process, with ID %d \n", (int) getpid());
  }
}

I’m running this program using the C language, using the fork() function. As I understand it, when a process calls fork(), a duplicate process, called a child process, is created.
The parent process continues executing from the point that fork() was called, and the child process, too, executes the same program from the same place.

So when I run my program, I expect the output be like the following text:

The main program process ID is 181411This is the child process, with ID 1815
The main program process ID is 18142This is the parent process,with ID 1814
The child process is 1815

But I actually see this output:

The main program process ID is 181411This is the child process, with ID 1815
The main program process ID is 181412This is the parent process,with ID 1814
The child process is 1815

It means that the child executes the program first!!!
When I put \n at the end of each printf statement the output is correct!!!

I’ve tried it on the Fedora v12 and rhel 5 distributions.
Is there any logical relation between the \n and the fork() operation? How I can solve this problem?

  • 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-14T02:56:47+00:00Added an answer on June 14, 2026 at 2:56 am

    The problem is that output is line-buffered and it isn’t really flushed to your screen until you either output \n or explicitly call fflush on stdout. (That said, there’s no guarantee about which process is going to be faster at outputting stuff).

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

Sidebar

Related Questions

Consider the following piece of code: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void)
Given this code : #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { int
This is my chat server : #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h>
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> void error(char *msg) {
#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main() { uint32_t ip =
stattest.c: // compile: gcc -o stattest stattest.c #include <stdio.h> #include <sys/stat.h> int main(int argc,
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
Client code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h>
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include structs.h #include server.h
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include

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.