Problem of unbalanced loads, Operating System

Assignment Help:

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;
}

Related Discussions:- Problem of unbalanced loads

Correct the code for visitor and vehicle portion, Gopher Gallery consists o...

Gopher Gallery consists of a shopping mall and a cart ride that covers the 150 acre habitat. There are m visitors and n single-person vehicles. Visitors stroll around the mall at t

Naming scheme for servers in an enterprise, Problem: Within a corporate...

Problem: Within a corporate environment, there are usually many servers located across various departments. Naming of servers is often a tedious task. In many circumstances, w

Explain optimal page replacement, Optimal page replacement An optimal ...

Optimal page replacement An optimal page replacement algorithm has the least page fault rate of all algorithms. The algorithm states that a 'replace the page that will not be

What are the deadlock prevention methodologies?, What are the deadlock p r...

What are the deadlock p revention methodologies? a.       Necessitate that processes request all resources before starting - if cannot be granted don't run. b.      Process

Define unix device driver, Define UNIX device driver The UNIX device dr...

Define UNIX device driver The UNIX device driver is structured into two halves called top half and bottom half

Describe virtual memory, What is virtual memory? Virtual memory is a me...

What is virtual memory? Virtual memory is a method that allows the execution of processes that might not be completely in memory. It is the separation of user logical memory fr

Synchronization, What is the different between basic synchronization and hi...

What is the different between basic synchronization and high level synchronization

Describe the possible rmi invocation semantics, Question: (a) Explain t...

Question: (a) Explain the similarities and differences between two different threads running in the same process and two independent processes. When would you want to use two t

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd