Bug 17572 - The file position was affected by child process
Summary: The file position was affected by child process
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: glibc
Version: 6.2
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2000-09-17 09:57 UTC by Fumio Nakayama
Modified: 2008-05-01 15:37 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2002-12-15 01:52:29 UTC
Embargoed:


Attachments (Terms of Use)

Description Fumio Nakayama 2000-09-17 09:57:28 UTC
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.