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

Question: (a) Write down short notes on the different versions of Windows server 2003 mentioned below: 1. Windows Server 2003, Web Edition 2. Windows Server 2003, Stand

Linked allocation With linked allocation, every file is a linked list of disk blocks; the disk blocks might be scattered anywhere on the disk. The directory has a pointer to th

Q. Several CPU-scheduling algorithms are parameterized for instance the RR algorithm requires a parameter to indicate the time slice. Multilevel response queues require parameters

What is the use of FIFOs in client-server applications

Explain Formal Language Grammar A formal language grammar is a set of formation rules which explain which strings formed from the alphabet of a formal language are syntacticall

Explain in detail about ipc in linux

How does an operating system make multiprogramming possible by using single processor?

Explain Swapping Swapping is a method of temporarily removing inactive programs from the memory of computer system.

What are the different accessing methods of a file? The different types of accessing a file are: Sequential access: Information in the file is accessed sequentially