hi
i had some kind of an argument today in the company and thought i might get some advise from you guys:
i was looking at a c program today where several child processes were created by means of the fork() system call. because these child processes did all the same kind of thing (only with different parameters), i wondered why the programmer did not simply do all the work sequentially and rather used the fork()
the answer i was given was: to speed things up! were talking here about maybe 5 different child processes that all do the same kind of work (fetching data from a database, processing it in some way and inserting it all in their respective output files), and i thought, how would this possibly speed up things, given that you're adding the context switch overhead AND theyre all doing pretty much the same thing (i.e. they'll be competing for the same resources more ore less at the same time), so i said no way is this going to make the program run faster than if you had everyhing run in sequence...what do you guys think??
fork() to speed things up
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
-
- Member
- Posts: 368
- Joined: Sun Sep 23, 2007 4:52 am
-
- Member
- Posts: 199
- Joined: Fri Jul 13, 2007 6:37 am
- Location: Stuttgart/Germany
- Contact:
right
but even if its cpu bound and you HAD several processors in place: how do you know its going to be sped up? the other cpu might very well be busy running a different job at the same time..also, even if its cpu bound, chances are you cant hold your working set entirely in registers, so you'll be accessing memory so youll be competing for the bus constantly...
is this really an advisable technique in a very general case?
but even if its cpu bound and you HAD several processors in place: how do you know its going to be sped up? the other cpu might very well be busy running a different job at the same time..also, even if its cpu bound, chances are you cant hold your working set entirely in registers, so you'll be accessing memory so youll be competing for the bus constantly...
is this really an advisable technique in a very general case?
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Technically, there are too many variables to be certain about this.sancho1980 wrote:but even if its cpu bound and you HAD several processors in place: how do you know its going to be sped up?
which frees the current CPUs of performing that taskthe other cpu might very well be busy running a different job at the same time..
Why do you think the cache was invented?also, even if its cpu bound, chances are you cant hold your working set entirely in registers, so you'll be accessing memory so youll be competing for the bus constantly...
The conclusion is: multithreading for performance only works on multiprocessor systems with cpu-bound tasks.is this really an advisable technique in a very general case?
-
- Posts: 7
- Joined: Sun Dec 16, 2007 9:47 am
- Location: The (Other) Counterweight Continent
Re: fork() to speed things up
I think this sounds like the work of im/premature optimisation.sancho1980 wrote:hi
i had some kind of an argument today in the company and thought i might get some advise from you guys:
i was looking at a c program today where several child processes were created by means of the fork() system call. because these child processes did all the same kind of thing (only with different parameters), i wondered why the programmer did not simply do all the work sequentially and rather used the fork()
the answer i was given was: to speed things up! were talking here about maybe 5 different child processes that all do the same kind of work (fetching data from a database, processing it in some way and inserting it all in their respective output files), and i thought, how would this possibly speed up things, given that you're adding the context switch overhead AND theyre all doing pretty much the same thing (i.e. they'll be competing for the same resources more ore less at the same time), so i said no way is this going to make the program run faster than if you had everyhing run in sequence...what do you guys think??
In situations like these, I find it's best to actually measure the performance of both types. It's no use trying to convince people whose understanding come from a set of principles by arguing with another set of principles. Hard data shuts people up.
"Pissing people off since 1986."
"Edible: n, As in a worm to a toad, a toad to a snake, a snake to a pig, a pig to a man, and a man to a worm."
"Edible: n, As in a worm to a toad, a toad to a snake, a snake to a pig, a pig to a man, and a man to a worm."
Hard to say without seeing the actual layout of the system, i.e. what kind of database, if the database is running on the same machine etc. etc.
Even on a single-CPU machine, splitting a job into two threads can result in the job being done faster - as many systems limit the amount of CPU / I/O a single process can get, so splitting up into two processes can result in more ressources being available overall.
But GuiltySpark is right. Generally speaking, there is only one way to be sure: Measure, optimize, measure. The system I am working on is running on a 16-CPU server. We had a job split up in 12 processes, thinking we'd make good use of available ressources. We actually became faster by reducing to 8 processes - but still faster when we used 20 processes... that's computers for you.
Even on a single-CPU machine, splitting a job into two threads can result in the job being done faster - as many systems limit the amount of CPU / I/O a single process can get, so splitting up into two processes can result in more ressources being available overall.
But GuiltySpark is right. Generally speaking, there is only one way to be sure: Measure, optimize, measure. The system I am working on is running on a 16-CPU server. We had a job split up in 12 processes, thinking we'd make good use of available ressources. We actually became faster by reducing to 8 processes - but still faster when we used 20 processes... that's computers for you.
Every good solution is obvious once you've found it.
Re: fork() to speed things up
This reminds me of "make -j x" switch. The "-j x" allows you to run more than one job simultaneously. Even on a single processor computer, you get a performance boost (depending on what you set it at, it normally twice the speed then normal). This is because compiling is IO bound.
From what you describe, each child process is IO bound ('inserting it all in their respective output file' is IO bound, it's hard to say weather the database is IO bound, but it's more than likely to be IO bound) and therefore you will get a preformance boost.
From what you describe, each child process is IO bound ('inserting it all in their respective output file' is IO bound, it's hard to say weather the database is IO bound, but it's more than likely to be IO bound) and therefore you will get a preformance boost.
Microsoft: "let everyone run after us. We'll just INNOV~1"
-
- Member
- Posts: 368
- Joined: Sun Sep 23, 2007 4:52 am
That could be a can of worms. I agree with the others in that the answer is not trivial, even in a general sense. You could check out things like Amdahl's law or the definition of speed up. Once you can quantify these then you have a starting point.sancho1980 wrote: is this really an advisable technique in a very general case?
It's probably obvious to you, but making a program parallel can cause other "boundings" to arise that may not exist in the non parallel case.
Thanks for all the fish.