<< Chapter < Page Chapter >> Page >

Operating system-supported multithreading

When the operating system supports multiple threads per process, you can begin to use these threads to do simultaneous computational activity. There is still no requirement that these applications be executed on a multiprocessor system. When an application that uses four operating system threads is executed on a single processor machine, the threads execute in a time-shared fashion. If there is no other load on the system, each thread gets 1/4 of the processor. While there are good reasons to have more threads than processors for noncompute applications, it’s not a good idea to have more active threads than processors for compute-intensive applications because of thread-switching overhead. (For more detail on the effect of too many threads, see Appendix D, How FORTRAN Manages Threads at Runtime.

If you are using the POSIX threads library, it is a simple modification to request that your threads be created as operating-system rather rather than user threads, as the following code shows:

#define _REENTRANT /* basic 3-lines for threads */ #include<stdio.h>#include<pthread.h>#define THREAD_COUNT 2 void *SpinFunc(void *);int globvar; /* A global variable */ int index[THREAD_COUNT]; /* Local zero-based thread index */ pthread_t thread_id[THREAD_COUNT]; /* POSIX Thread IDs */ pthread_attr_t attr; /* Thread attributes NULL=use default */main() {int i,retval; pthread_t tid;globvar = 0;pthread_attr_init(&attr); /* Initialize attr with defaults */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); printf("Main - globvar=%d\n",globvar);for(i=0;i<THREAD_COUNT;i++) { index[i]= i; retval = pthread_create(&tid,&attr,SpinFunc,(void *) index[i]);printf("Main - creating i=%d tid=%d retval=%d\n",i,tid,retval); thread_id[i]= tid; }printf("Main thread - threads started globvar=%d\n",globvar);for(i=0;i<THREAD_COUNT;i++) { printf("Main - waiting for join %d\n",thread_id[i]); retval = pthread_join( thread_id[i], NULL ) ; printf("Main - back from join %d retval=%d\n",i,retval);} printf("Main thread - threads completed globvar=%d\n",globvar);}

The code executed by the master thread is modified slightly. We create an “attribute” data structure and set the PTHREAD_SCOPE_SYSTEM attribute to indicate that we would like our new threads to be created and scheduled by the operating system. We use the attribute information on the call to pthread_create( ) . None of the other code has been changed. The following is the execution output of this new program:

recs % create3 Main - globvar=0Main - creating i=0 tid=4 retval=0 SpinFunc me=0 - sleeping 1 seconds ...Main - creating i=1 tid=5 retval=0 Main thread - threads started globvar=0Main - waiting for join 4 SpinFunc me=1 - sleeping 2 seconds ...SpinFunc me=0 - wake globvar=0... SpinFunc me=0 - spinning globvar=1...SpinFunc me=1 - wake globvar=1... SpinFunc me=1 - spinning globvar=2...SpinFunc me=1 - done globvar=2... SpinFunc me=0 - done globvar=2...Main - back from join 0 retval=0 Main - waiting for join 5Main - back from join 1 retval=0 Main thread - threads completed globvar=2recs %

Now the program executes properly. When the first thread starts spinning, the operating system is context switching between all three threads. As the threads come out of their sleep( ) , they increment their shared variable, and when the final thread increments the shared variable, the other two threads instantly notice the new value (because of the cache coherency protocol) and finish the loop. If there are fewer than three CPUs, a thread may have to wait for a time-sharing context switch to occur before it notices the updated global variable.

With operating-system threads and multiple processors, a program can realistically break up a large computation between several independent threads and compute the solution more quickly. Of course this presupposes that the computation could be done in parallel in the first place.

Questions & Answers

What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, High performance computing. OpenStax CNX. Aug 25, 2010 Download for free at http://cnx.org/content/col11136/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'High performance computing' conversation and receive update notifications?