LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2021, 06:49 AM   #16
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,204

Rep: Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824

printf is buffered, that means that may be printed later, when the buffer is flushed. And it is only occurred when it is full. Or explicitly flushed.
 
Old 11-16-2021, 12:00 PM   #17
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 726

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by Sxing View Post
I am a rookie in multi-threading programming. please help me.
Consider writing an actual multi-threaded program. Look up pthread_create (), pthread_join (), pthread_mutex_lock (), pthread_mutex_unlock (), etc.

In a multithreaded program, all threads share the same address space. This makes it easy to assign work to threads and to keep the results in a desired order.
Ed
 
Old 11-16-2021, 01:10 PM   #18
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,188

Rep: Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752
Sorry, this meme is obligatory:

https://www.reddit.com/r/aww/comment..._and_practice/
 
1 members found this post helpful.
Old 11-16-2021, 01:29 PM   #19
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 726

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
I have written a lot of multithreaded programs. Multithreaded programming is not particularly hard as long as the locks are done properly. The reputation comes from locking problems being very hard to debug.
Ed
 
Old 11-17-2021, 05:58 AM   #20
Sxing
LQ Newbie
 
Registered: Nov 2021
Location: Hangzhou, China
Distribution: ArchLinux, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by EdGr View Post
I have written a lot of multithreaded programs. Multithreaded programming is not particularly hard as long as the locks are done properly. The reputation comes from locking problems being very hard to debug.
Ed
Haha, that's impressive. I'm just an undergraduate student who is worried about his assignments 😂. I will definitely consider your suggestions in the future. Thanks!
 
Old 11-18-2021, 07:01 AM   #21
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,202
Blog Entries: 1

Rep: Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268Reputation: 4268
Quote:
Originally Posted by dugan View Post
Not seen that one before. Love it.
Thanks for sharing.
 
Old 11-18-2021, 08:52 PM   #22
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,434
Blog Entries: 4

Rep: Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376
Your basic confusion comes from "what is happening behind the scenes." As each of the independently-running "child processes" consume their STDINs and generate their STDOUTs and STDERRs, they of course do so independently. But what you might not initially realize is that there is another(!) process out there which is gathering those various STDOUT/STDERR streams together from the various children, and delivering them "to one place." Whether that place is a disk file or your terminal window.

Each of the individual "child" streams are of course independent, but the gathering ("parent ...") process is not. This process, which you do not control, is ultimately responsible for the final output that you see. This process has several independent input-streams (one for each child process ...) to read from simultaneously, and it reads from them in no predictable order.

Last edited by sundialsvcs; 11-18-2021 at 08:55 PM.
 
Old 11-19-2021, 10:36 AM   #23
Sxing
LQ Newbie
 
Registered: Nov 2021
Location: Hangzhou, China
Distribution: ArchLinux, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
Whether that place is a disk file or your terminal window.
That's exactly what I'm confused about. Whatever is going on behind the scenes, the difference between the disk file and my terminal window should only be in order, but not in content. (Again, output in a file has two more lines than one in terminal)

The main difference between a disk file and a terminal is the way they are buffered. But that still doesn't explain the question above.
 
Old 11-19-2021, 10:55 AM   #24
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,204

Rep: Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824
I have that explained already.
the printf("init") will put the text into a buffer. The fork process(es) will duplicate the process (3 fork will make it 3 times) and the buffer is duplicated too.
The flush command will be automatically executed (when that buffer is full) - after that fork, so all the three children will flush their own instance of that buffer. You can simply check it by flushing it before fork.
 
1 members found this post helpful.
Old 11-19-2021, 11:02 AM   #25
Sxing
LQ Newbie
 
Registered: Nov 2021
Location: Hangzhou, China
Distribution: ArchLinux, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I have that explained already.
the printf("init") will put the text into a buffer. The fork process(es) will duplicate the process (3 fork will make it 3 times) and the buffer is duplicated too.
The flush command will be automatically executed (when that buffer is full) - after that fork, so all the three children will flush their own instance of that buffer. You can simply check it by flushing it before fork.
But it still can't explain how different buffer types affect the behavior. Your descreption should be a **Commonality**.
 
Old 11-19-2021, 11:06 AM   #26
Sxing
LQ Newbie
 
Registered: Nov 2021
Location: Hangzhou, China
Distribution: ArchLinux, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: Disabled
Btw, the Windows icon sucks. Transparent background could be better.

Especially whem i'm using "Dark Reader"
https://raw.githubusercontent.com/fr...1120000849.png

Last edited by Sxing; 11-19-2021 at 11:09 AM.
 
Old 11-19-2021, 01:13 PM   #27
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,620

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
Quote:
Originally Posted by Sxing View Post
But it still can't explain how different buffer types affect the behavior. Your descreption should be a **Commonality**.
I skimmed the thread quicky, so this may have been mentioned already, but the difference is that for terminals the buffer is automatically flushed whenever you print a newline (by default). Whereas for files, it's every 4096(?) bytes or so.

https://manned.org/setvbuf.3
Quote:
Normally all files are block buffered. If a stream refers to a terminal
(as stdout normally does), it is line buffered. The standard error
stream stderr is always unbuffered by default.
 
Old 11-19-2021, 01:44 PM   #28
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,204

Rep: Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824Reputation: 5824
Quote:
Originally Posted by ntubski View Post
I skimmed the thread quicky, so this may have been mentioned already, but the difference is that for terminals the buffer is automatically flushed whenever you print a newline (by default). Whereas for files, it's every 4096(?) bytes or so.
That is what I really hate, the "implied" "intelligence". But anyway, you are right, the c library (of printf) detects the type of the output and may act accordingly.
 
Old 11-20-2021, 12:21 AM   #29
Sxing
LQ Newbie
 
Registered: Nov 2021
Location: Hangzhou, China
Distribution: ArchLinux, Ubuntu
Posts: 13

Original Poster
Rep: Reputation: Disabled
Sorry, this is my fault. #24 is the correct answer for me.

The key point is the inheritance feature of fork(), which includes buffers.
In the case of using block buffers, when fork() is called, there is some content that has not yet been flushed to its destination. Each child gets a copy to its own buffer (now there are N+1 buffers).
These copies are the extra lines in the output.
In the case of line-buffering, the buffer is refreshed for each line. So when fork() is called, the buffers are empty.

As a little experiment, you can remove the "\n" after "init". Then you can get the expected behavior.
 
1 members found this post helpful.
Old 11-21-2021, 07:24 PM   #30
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,434
Blog Entries: 4

Rep: Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376
Yeah, fork() – in both Unix® and Linux® – is a lot more expansive than the users of many other systems expect it to be, if they're accustomed to a mere "CreateProcess()" system function!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can I set the cups output order to always print in reverse order Thane Ubuntu 3 07-09-2018 10:49 PM
Logi Sales Manager on Ncurses (invoice, invoicing, orders, order, sale order, sales order...)? Xeratul Linux - Software 0 03-25-2017 03:45 PM
Why am I not able to redirect using htaccess. It says too many redirect rules. swathi.akkineni Linux - Newbie 1 07-31-2015 04:20 PM
How to make output redirect to a file and at the same time display on consloe? chinabenjamin66 General 1 05-01-2013 11:33 PM
Why the output files (trace file e.g. out.tr) are not same for the same tcl script ? askerwhat Ubuntu 1 06-03-2011 11:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration