Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing stdout among multiple threads/processes

I have a linux program(the language doesn't matter) which prints it's log onto stdout. The log IS needed for monitoring of the process.

Now I'm going to parallelize it by fork'ing or using threads.

The problem: resulting stdout will contain unreadable mix of unrelated lines...

And finally The question: How would you re-construct the output logic for parallel processes ?

like image 872
LiMar Avatar asked Jun 07 '12 08:06

LiMar


People also ask

Is stdout shared between processes?

Yes they share the same location.

Can a thread be shared by multiple processes?

A thread is generated and owned by a process. It cannot be shared. There are a whole lot of security considerations that make doing so a bit of a nightmare. Best to save thread state somewhere that can be accessed by another process.

How can I share data between two threads?

All static and controlled data is shared between threads. All other data can also be shared through arguments/parameters and through based references, as long as the data is allocated and is not freed until all of the threads have finished using the data.

Is it easier to share data between threads or share data between processes?

Sharing objects between threads is easier, as they share the same memory space. To achieve the same between process, we have to use some kind of IPC (inter-process communication) model, typically provided by the OS.


3 Answers

Sorry for answering myself...

The definite solution was to use the GNU parallel utility.

It came to replace the well known xargs utility, but runs the commands in parallel, separating the output into groups.

So I just left my simple one-process, one-thread utility as-is and piped its call through the parallel like that:

generate-argument-list | parallel < options > my-utility

This, depending on parallel's options can produce nicely grouped outputs for multiple calls of my-utility

like image 159
LiMar Avatar answered Nov 15 '22 08:11

LiMar


If you're in C++ I'd consider using Pantheios or derivative version Boost::Log or using look at Logging In C++ : Part 2 or

If you're in another language then file locking around the IO operations is probably the way to go see File Locks, you can achieve the same results using semaphonres or any other process control system but for me file locks are the easiest.

You could also consider using syslog if this monitoring is considered as system wide.

like image 34
Richard Harrison Avatar answered Nov 15 '22 08:11

Richard Harrison


Another approach, which we use, is to delegate a thread, logger thread, for logging. All other threads wishing to log will send it to logger thread. This method gives you flexibility as formatting of logs can be done is single place, which can also be configurable. If you don't want to worry about locks can use sockets for message passing.

like image 39
tuxuday Avatar answered Nov 15 '22 10:11

tuxuday