Problem of unbalanced loads, Operating System

Present your own fully documented and tested programming example illustrating the problem of unbalanced loads. Describe the use of OpenMP's scheduler as a means of mitigating this problem.

The below example shows a number of tasks that all update a global counter. Since threads share the same memory space, they indeed see and update the same memory location. The code returns a false result because updating the variable isĀ  much quicker than creating the thread as on a multicore processor the chance of errors will greatly increase. If we artificially increase the time for the update, we will no longer get the right result. All threads read out the value of sum, wait a while (presumably calculating something) and then update.

#include

#include

#include "pthread.h"

int sum=0;

void adder() {

int sum = 0;

int t = sum; sleep(1); sum = t+1;

return;

}

#define NTHREADS 50

int main() {

int i;

pthread_t threads[NTHREADS];

printf("forking\n");

for (i=0; i

if (pthread_create(threads+i,NULL,&adder,NULL)!=0) return i+1;

printf("joining\n");

for (i=0; i

{

if (pthread_join(threads[i],NULL)!=0) return NTHREADS+i+1;

printf("Sum computed: %d\n",sum);

}

return 0;

}

The use of OpenMP is the parallel loop. Here, all iterations can be executed independently and in any order. The pragma CPP directive then conveys this fact to the compiler. A sequential code can be easily parallelized this way.

#include 
#include 
#include "pthread.h"
int sum=0;
void adder() {
int sum = 0;
int t = sum; sleep(1); sum = t+1;
return;
}
#define NTHREADS 50
int main() {
int i;
pthread_t threads[NTHREADS];
printf("forking\n");
#pragma omp for
for (i=0; i
if (pthread_create(threads+i,NULL,&adder,NULL)!=0) return i+1;
}
printf("joining\n");
for (i=0; i
{
if (pthread_join(threads[i],NULL)!=0) return NTHREADS+i+1;
printf("Sum computed: %d\n",sum);
}
return 0;
}
Posted Date: 6/29/2012 2:17:42 AM | Location : United States







Related Discussions:- Problem of unbalanced loads, Assignment Help, Ask Question on Problem of unbalanced loads, Get Answer, Expert's Help, Problem of unbalanced loads Discussions

Write discussion on Problem of unbalanced loads
Your posts are moderated
Related Questions
How can the deadlock be prevented-Safe State Safe state : A state is safe if the system is able to allocate resources to each process in some order and still avoid deadlock.

What is reference string We evaluate an algorithm by running it on a definite string of memory reference and computing the number of page faults. The string of memory reference

Define a program in execution is known as A program in execution is known as a process

Explain segmentation hardware? We define an completion to map two-dimensional user-defined addresses into one-dimensional physical addresses. This mapping is affected by means

What is parsing Parsing is the procedure of analyzing a text, made of a sequence of tokens, to define its grammatical structure regarding to a given formal grammar. Parsing is

What is sector sparing is proper definition

Paging full question

Question: (a) Distinguish between local and active directory Group Policy in Windows Server 2008? (b) Describe the three areas of account Policies that can be managed thro

Research the phenomenon of data races. Give an example of how an unprotected data race can give rise to data inconsistency. How do OpenMP and Cilk resolve this problem? Data

Q. Remapping of bad blocks by sector sparing or else sector slipping could influence performance. Presume that the drive in Subsequent Exercise has a total of 100 bad sectors at r