Re: Question about multi-processing, multi-threading...
Re: Question about multi-processing, multi-threading...
- Subject: Re: Question about multi-processing, multi-threading...
- From: Shawn Erickson <email@hidden>
- Date: Wed, 21 Jan 2004 10:42:16 -0800
On Jan 21, 2004, at 9:33 AM, Thierry Bucco wrote:
On Jan 21, 2004, at 8:38 AM, Thierry Bucco wrote:
Hi,
I have written a little C program who execute an infinite loop.
When I launch process viewer, I can see that my 2 processors are
busy.
(60%, 40%).
I was thinking that if an application was not multi threaded (like my
little exemple), the task was rans on only 1 procesor, it seems that
it's wrong.
But If I change my source code, to execute 2 infinite loops in a
forked
process, my processors are very busy (100%, 100%).
Is there a way to force a thread to run on the second processor ?
What are you trying to insure by attempting this?
It's just a question...
Could you give me some informations about scheduling on OS X ?
In the first case you have single process that is single threaded in
that it is only doing processing in a single thread (secondary
threads may exist in support of your application). The scheduler if
free to run your processes thread on any available CPU when it is
ready to run. So it may ping-pong between the CPUs and hence you see
the load somewhat shared by the CPUs (note it adds to near 100% on
average, 100% of a single CPU).
I have the feeling that my single threaded application is running on
both processors, is it possible ?
Because the load of processors increases considerably.
Is it possible that in my loop, scheduler gives some work by turns, or
is my loop only executed on one processor ?
In fact I would like to know if my loop is running on one processor or
on both at the same time.
As I tried to say in the above... your loop is run by only a single
processor at a time but that processor can change over time. The
scheduler will give your thread, the one running the loop, to any
available processor but to only one at a given time.
By definition a thread is a stream (aka "thread") of executable code
and supporting data that can be run by a processor. It is the work
primitive used in Mac OS X for scheduling (and most other modern OSes).
A given thread cannot be run on multiple processors at the same time
because the instructions in the thread and the data they manipulate are
interdependent (again by the definition of a thread).
In the second case you have two separate processes with two separate
threads running your loop. Again the CPU is free to schedule the
threads as appropriate. In this case you have two so each CPU can be
given one to run at the same time.
I don't understand why the load is 100% for both processors, and not
when I don't use thread.
In the second case you have two processes that each contain their own
thread. Each thread is coded to execute your loop and each thread can
be run independently of each other (your not attempting to synchronize
the two threads in any fashion). Your threads are also non-blocking
(always wanting to run your loop) so the schedular will given them as
much time as it can and since you have two processors it can have both
threads running at the same time. So the threads will consume as many
free cycles as available and hence your CPUs will be 100% utilized (at
least from the POV of always having a thread running).
In the first case you have a single thread and that thread can only run
on a single processor at a time (by definition of a thread). So it only
has the ability to consume 100% of a single CPU at a time however that
load could be share across CPUs because the schedular may give the
thread to different CPUs at different times. Just remember that while
one CPU is running your thread the others cannot be running it.
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.