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

Define cpu scheduling, Define CPU scheduling. CPU scheduling is the met...

Define CPU scheduling. CPU scheduling is the method of switching the CPU among several processes. CPU scheduling is the basis of multiprogrammed operating systems. By switching

How can application use memory via the win32 api, Q. Explain some of the wa...

Q. Explain some of the ways an application can use memory via the Win32 API. Answer: (1) Virtual memory offers several functions that allow an application to reserve and rele

Is disk scheduling except fcfs scheduling, Q. Is disk scheduling except FC...

Q. Is disk scheduling except FCFS scheduling useful in a single-user environment? describe your answer. Answer: In a single-user environment the I/O queue typically is empty.

What is a relocatable program, What is a Relocatable program? The Reloc...

What is a Relocatable program? The Relocatable programs can be loaded almost anywhere in memory.

Requirements to satisfied by the critical section problem, What are the req...

What are the requirements to be satisfied by the critical section problem? Following are the necessities to be satisfied by the critical section problem: a)      Mutual exc

Load sharing , Load Sharing : Processes are not given to a particular proc...

Load Sharing : Processes are not given to a particular processor. A global queue of threads is used to maintain. Each processor, when idle, picks a thread from this queue. Note th

Determine scheduling policy for time-shared operating system, Determine a s...

Determine a scheduling policy that is most suitable for a time-shared operating system   Round-Robin is a scheduling policy that is most suitable for a time-shared operating s

Discuss distributed file system caching, Problem: (a) Discuss Distribut...

Problem: (a) Discuss Distributed File System Caching. (Your discussion should include the goal and architecture of distributed file system caching and the various possible c

Virtualization, What factors are involved in choosing the host operating sy...

What factors are involved in choosing the host operating system?

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