Re: [PLUG] Weird Output of a C program in Linux
On 7/18/07, yogesh tillu [EMAIL PROTECTED] wrote: Aditya Godbole [EMAIL PROTECTED] wrote: On 7/17/07, Pranav Peshwe wrote: IMHO, it does send the data to the buffer. Due to the fork, the new task(child) gets a copy of the buffer (which already contains the 'Hello World!' string). This(fork()) is where the duplication occurs. Further printfs in the two tasks lead to flushing of their respective buffers to the console. With a '\n' after 'Hello World!', the child would get an empty buffer to begin with. Yeah, thats right. The buffer is filled but not flushed. After the fork, the entire address space (and hence the buffer) gets replicated * But I think currently copy on write approach is used for the *implementation of the fork .So entire address space will not get *replicated {previously clone approach was used , so in that *whole address space gets replicated } * and is flushed after the second the printf in both processes. -aditya cheers, yogesh Yes.Thats right.But isnt the buffer in the user area.Which is copied as is, except for the pointer to the proc structure. - 5, 50, 500, 5000. Store N number of mails in your inbox. Click here. -- __ Pune GNU/Linux Users Group Mailing List: (plug-mail@plug.org.in) List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions. -- Regards, Ashutosh Adkar -- __ Pune GNU/Linux Users Group Mailing List: (plug-mail@plug.org.in) List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.
Re: [PLUG] Weird Output of a C program in Linux
On 7/18/07, yogesh tillu [EMAIL PROTECTED] wrote: Yeah, thats right. The buffer is filled but not flushed. After the fork, the entire address space (and hence the buffer) gets replicated * But I think currently copy on write approach is used for the *implementation of the fork .So entire address space will not get *replicated {previously clone approach was used , so in that *whole address space gets replicated } * Copy-on-write (COW) is a method of replication of address spaces. Its a bit hard to explain on mail, but the basic concept is this - It is not necessary to actually copy the pages in memory until the data in the two processes becomes different. Till then the OS maintains page tables such that the same page is used until one of the processes modifies the contents of that page. When that happens, the page is first copied and then written. Obviously, it is no longer shared after that. So it doesnt matter whether whether the OS uses COW or not. When seen from userland, the address space is replicated. Hope that answers your question. For details, refer to some book on OS theory. -aditya -- __ Pune GNU/Linux Users Group Mailing List: (plug-mail@plug.org.in) List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.
Re: [PLUG] Weird Output of a C program in Linux
Hi, On 7/17/07, Ashutosh Adkar [EMAIL PROTECTED] wrote: int main () { printf (Hello World!); if (fork == 0) printf (I'm the child!\n); else printf (I'm the parent!\n); } The output of the above program is : Hello World!I'm the child! Hello World!I'm the parent! How did you get the output ? I don't think that the above program will compile at all, with fork being undeclared. If its calling fork() - then your guess is right, when you append the newline character, the buffer is flushed and Hello World! is printed before the fork call. If you want to see this -You can experiment with fflush() or use gdb. Eg devendra:$ gcc test.c -g devendra:$ gdb a.out GNU gdb 6.5 (gdb) b main Breakpoint 1 at 0x80483f0: file test.c, line 5. (gdb) r Starting program: /home/devendra/a.out Breakpoint 1, main () at test.c:5 5 printf (Hello World!\n); (gdb) n Hello World!-- The buffer is cleared 6 if (fork() == 0) (gdb) c -Devendra Laulkar. -- __ Pune GNU/Linux Users Group Mailing List: (plug-mail@plug.org.in) List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.
Re: [PLUG] Weird Output of a C program in Linux
On 7/17/07, Pramod Sonar [EMAIL PROTECTED] wrote: Hi Ashu you are right ! Hello world should be print only ones! but it's printing twice as when printf executes it does not send data to output buffer. IMHO, it does send the data to the buffer. Due to the fork, the new task(child) gets a copy of the buffer (which already contains the 'Hello World!' string). This(fork()) is where the duplication occurs. Further printfs in the two tasks lead to flushing of their respective buffers to the console. With a '\n' after 'Hello World!', the child would get an empty buffer to begin with. CMIIW. Regards, Pranav -- __ Pune GNU/Linux Users Group Mailing List: (plug-mail@plug.org.in) List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.
Re: [PLUG] Weird Output of a C program in Linux
On 7/17/07, Pranav Peshwe [EMAIL PROTECTED] wrote: IMHO, it does send the data to the buffer. Due to the fork, the new task(child) gets a copy of the buffer (which already contains the 'Hello World!' string). This(fork()) is where the duplication occurs. Further printfs in the two tasks lead to flushing of their respective buffers to the console. With a '\n' after 'Hello World!', the child would get an empty buffer to begin with. Yeah, thats right. The buffer is filled but not flushed. After the fork, the entire address space (and hence the buffer) gets replicated and is flushed after the second the printf in both processes. -aditya -- __ Pune GNU/Linux Users Group Mailing List: (plug-mail@plug.org.in) List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail Send 'help' to [EMAIL PROTECTED] for mailing instructions.