Any one can tell me how this happen?


 
Thread Tools Search this Thread
Top Forums Programming Any one can tell me how this happen?
# 8  
Old 06-11-2003
Quote:
Originally posted by yogie
When You do fork() process' address space is being copied. If it happens before prinft()'s buffers are flushed, buffer's content is inherited by child process, and then printf()'ed by it, so You get the same output twice (from child and parent process).If flushing is delayed buffers can be inherited over and over (bysare scheduled by OS, running program many times can give different results (different order).

Good Luck!
thanks

but i still have some doubts!
that is where does the printf save it's buffer and how does the buffer being copied?
does the process save it's buffer in it's struct user?or in it's data area?

and how does the OS know it's line-buffered or block-buffered?
Is there a flag in it?
# 9  
Old 06-12-2003
I/O buffers

By defalt stdout is "line - buffered".
It mean that after printf("...\n") buffer was flushed if output to stdout.

Certainly the file I/O buffers isn't "line-buffered", and before fork(), buffers was not flushed. So its filled buffers was copied.

Smilie What happen if we write " for(int i = 0 ; i < 3; ++i) " ? Smilie

You can manage I/O buffering by using "setvbuf" function.

Last edited by serge; 06-12-2003 at 07:57 AM..
# 10  
Old 06-12-2003
Re: I/O buffers

Quote:
Originally posted by serge
By defalt stdout is "line - buffered".
It mean that after printf("...\n") buffer was flushed if output to stdout.

Certainly the file I/O buffers isn't "line-buffered", and before fork(), buffers was not flushed. So its filled buffers was copied.

Smilie What happen if we write " for(int i = 0 ; i < 3; ++i) " ? Smilie

You can manage I/O buffering by using "setvbuf" function.
Do you mean when OS invoke printf(),it will invoke setbuf()? Smilie

and does it mean that setbuf() will set a buffer flag to determine
if it is line-buffered or block-buffered?


i have traced in C++ code when it invoke setbuf(),but i still can't find where it set flag?

i wanna thank all the people who reply to my question!!!Smilie
# 11  
Old 06-13-2003
Re: Re: I/O buffers

Quote:
Originally posted by jiangyanna
Do you mean when OS invoke printf(),it will invoke setbuf()? Smilie

and does it mean that setbuf() will set a buffer flag to determine
if it is line-buffered or block-buffered?
No, it only mean that you can change default behaviour of I/O buffering.

After "printf" call, all data come in I/O buffer. In first case it is standard output (stdout) buffer, which is "line - buffered".
In another case it is file I/O buffer, which is "block - buffered".

I hope that answered your question.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

When downloading a webpage by curl, what happen?

Hello, when im downloading an webpage from command line (CLI) by curl or wget the target website is loaded like i load it from browser? meaning target server connect to database and render data from mysql? Or only static content is downloaded? (2 Replies)
Discussion started by: postcd
2 Replies

2. AIX

How does ITIL processing happen in AIX?

How does ITIL process is implemened in AIX? (6 Replies)
Discussion started by: AIXlearner
6 Replies

3. Programming

what would happen if a process wrote to its own stdin?

what would happen if a process wrote to its own stdin? #include<unistd.h> #include<fcntl.h> int main() { if((write(STDIN_FILENO,"arrgh!",6))==-1) { perror("error writing to file"); } } output: $ gcc temp.c $ ./a.out arrgh!$ (9 Replies)
Discussion started by: c_d
9 Replies

4. UNIX for Dummies Questions & Answers

whats happen when we create new user

hi frndz I wanna knw exatly what happen when we create new user... which directories are created ?? which files are modified ?? thanx.... (2 Replies)
Discussion started by: luckypower
2 Replies

5. Shell Programming and Scripting

how to extract files one by one from a directory and let some processing happen

how to extract files one by one from a directory and let some processing be done on the file I have a directory by name INTRN which has files like INTR.0003080248636814 INTR.0003080248636816 INTR.0003080248636818 . . . . and so on and in a script... (5 Replies)
Discussion started by: saniya
5 Replies

6. UNIX for Advanced & Expert Users

Unix ID deleted - What happen to process

I have an unix id (AIX system) which is used to run a couple of processes. They also write some log files into a file system (that is not in the home directory of the user id, but in different location). One bad day, the id was deleted accidentally. But the home directory, files and everything... (1 Reply)
Discussion started by: cmgreat
1 Replies

7. Programming

What happen in registers Internaly using as in csl() function

Dear i do not understand that function used for clear screen (given below) cls { union REGS i,o; i.h.ah=6; i.h.al=0; i.h.ch=0; i.h.cl=0; i.h.dh=24; i.h.dl=79; i.h.bh=7; int86(16,&i,&o); } In Above function Registers are used(i think) but why and internally what the do for clearing... (3 Replies)
Discussion started by: brain_full
3 Replies

8. UNIX for Dummies Questions & Answers

How Do I Set a Task to Happen in the Future?

Is it possible to set a task to happen in the future? Say I want to log-off only after 10 hours of being logged on with out doing any activity in between? (2 Replies)
Discussion started by: Slick
2 Replies

9. UNIX for Dummies Questions & Answers

What would happen if. . .

Hi, Could someone please tell me what would happen if the following were entered into the command line: rm -i /books/*.* rm /books/* Many thanks! (3 Replies)
Discussion started by: crispy
3 Replies

10. UNIX for Dummies Questions & Answers

what happen when changing Hostname?

I 'm using RH 7.2 Genome in the Network Configuration I change therer are two places one for static hostname for my machine and in DNS hostname I don't know what happen when restarting my PC when connecting using dialer I can't browse the Internet also I can't use sendmail .......Server timeout... (2 Replies)
Discussion started by: atiato
2 Replies
Login or Register to Ask a Question