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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:33:34+00:00 2026-05-26T03:33:34+00:00

When a child is forked from a parent process, all the variables from the

  • 0

When a child is forked from a parent process, all the variables from the parent are copied to the new child process. Hence when we open a common file before the fork happens, and try to read the same file both from parent and child, both should print results independently. But when i try to implement this, the reading from file for parent and child seems to be interleaved. Need to know as to why is this happening. Even the file descriptor should be duplicated when a new process is forked from the parent. So whats going wrong ?

#include<stdio.h>
#include<fcntl.h>

main()
{
int fp;
char buff[11];
int pid;
fp = open("file1.txt", O_RDONLY);
pid = fork();
if(pid==0)
{
    printf("Child begins %d\n", getpid());
    read(fp, buff, 5);
    buff[10] = '\0';
    printf("Child read:");
    puts(buff);
    printf("Child exiting\n");
}
else
{
    read(fp, buff, 5);
    buff[10] = '\0';
    printf("Parent read:");
    puts(buff);
    printf("Parent exiting\n");
}
 }

Now suppose the file1.txt has the content “Hello world”, then both the parent and child processes should have printed “Hello”. But what i see is one among child/parent prints “Hello” and the other prints ” world”.

  • 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-05-26T03:33:35+00:00Added an answer on May 26, 2026 at 3:33 am

    The file descriptors are duplicated, but the duplicates point to the SAME entry in the system file table, so the two processes share the same file pointer. Kernel read()’s and writes()’s are atomic, so each process will see the file pointer after it has been moved by the other.

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

Sidebar

Related Questions

I'm creating child processes with fork() in C/C++. When the parent process ends (or
I'm trying to spawn a new process from my C++-project using fork-exec. I'm using
From Parent window I need to open a file and fill up different tables
Throwing an exception via croak in a forked child process seems to print the
We have a C++ task that will fork a new process. That process in
In PHP, is there a way to have a child process forked off with
I have multiple child forked by the same parent and I try to construct
So here's my dilemma: Why when I fork(); a child of a process and
I launch a child process in Java as follows: final String[] cmd = {<childProcessName>};
I am launching a child process with ProcessBuilder, and need the child process to

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.