Bug 17572 - The file position was affected by child process
The file position was affected by child process
Status: CLOSED NOTABUG
Product: Red Hat Linux
Classification: Retired
Component: glibc (Show other bugs)
6.2
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2000-09-17 05:57 EDT by Fumio Nakayama
Modified: 2008-05-01 11:37 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2002-12-14 20:52:29 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Fumio Nakayama 2000-09-17 05:57:28 EDT
The file position was affected by child process.
The following sample does not work fine.
But this code works fine on Solaris.

[CODE]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define     READ_SIZE     256
#define     ERR          -1

main(int argc, char *argv[], char *envp[]) {
    int     pid;
    int     rc;
    int     status;
    FILE    *fd;
    char    data_rec[READ_SIZE + 1];

        if(argc != 2){
                fprintf(stderr, "Usage: %s file_name\n", argv[0]);
                exit(-1);
        }

        printf("Main_First\n");
    if((fd = fopen(argv[1], "r")) == 0) {
      perror(argv[0]);
      fprintf(stderr, "%s: fopen() for \"%s\" failed.\n", argv[0],
argv[1]);
      exit(ERR);
    }

    pid = fork();
    if (pid == -1) {
                perror(argv[0]);
        fprintf(stderr, "%s: fork error.\n", argv[0]);
        exit(errno);
    } else if (pid == 0) {
                printf("Child_First\n");

        if(fgets(data_rec, READ_SIZE, fd) != NULL) {
          fprintf(stdout, "Read data: %s\n", data_rec);
        } else {
          perror("fgets()");
          fprintf(stderr,
                  "%s: c-fgets() for \"%s\" failed.\n%d\n",
                  argv[0], argv[1], errno);
          exit(errno);
        }

        exit(0);
    } else {                          
        wait(&status);
                printf("Main_End\n");

        if(fgets(data_rec, READ_SIZE, fd) != NULL) {
          fprintf(stdout, "Read data: %s\n", data_rec);
        } else {
          perror("fgets()");
          fprintf(stderr,
                  "%s: p-fgets() for \"%s\" failed.\n%d\n",
                  argv[0], argv[1], errno);
          exit(errno);
        }

        exit(0);
    }
}

[HOW TO REPRODUCE]
1. make a sample data file.
   the following is a sample data file "data".

test data line #1
test data line #2
test data line #3
test data line #4
test data line #5

2. compile the source code and execute it.
$ cc test.c.

$ ./a.out data 
Main_First
Child_First
Read data: test data line #1

Main_End
fgets(): Success
./a.out: p-fgets() for "data" failed.
0

Note You need to log in before you can comment on or make changes to this bug.