A question of fork() function

  • Thread starter bite me if you can...
  • Start date
B

bite me if you can...

The man page of fork() says that the file descriptor are shared by
parent process & childern processes, so any operation of file
descriptor will affect them both.
But why the behavior of FILE doesn't like file descriptors?

This is my program, the result of operation of file descriptor is
ideal, but result of operation of FILE is amazing...
int fd=-1, pid;
FILE *fin=(FILE *)NULL;

fd=open("mem.tmp", O_RDWR);
fin=fopen("mem1.tmp", "r");
pid=fork();
if(pid) {
printf("(parent)\n");
printf("(parent) lseek(): %d\n", lseek(fd, 10, SEEK_SET));
fseek(fin, 10, SEEK_SET);
printf("(parent) ftell(): %d\n", ftell(fin));
}
else {
printf("(child)\n");
printf("(child)\n");
printf("(child) ftell(): %d\n", ftell(fin));
}
 
A

Artie Gold

bite said:
The man page of fork() says that the file descriptor are shared by
parent process & childern processes, so any operation of file
[snip]

....where things like fork() are *on*-topic.

HTH,
--ag
 
S

SM Ryan

# The man page of fork() says that the file descriptor are shared by
# parent process & childern processes, so any operation of file
# descriptor will affect them both.
# But why the behavior of FILE doesn't like file descriptors?

FILEs aren't file descriptors. They have additional buffering
and other attributes. If you want to multitask, like fork or
threads, you need to check your system's documentation how these
interact with stdio.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linux: using "clone3" and "waitid" 0
regarding lseek and fread 2
A Question of Style 51
fork/exec question 6
fork() 5
'fork' query 3
fork() 27
How to use shared memory with fork() ? 5

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top