<< 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 is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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?

Ask