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

Main advantage of the layered approach to system design, What is the main a...

What is the main advantage of the layered approach to system design? As in all cases of modular design, designing an operating system in a modular way has several benefits. Th

Explain internal fragmentation, Explain internal fragmentation The Inter...

Explain internal fragmentation The Internal fragmentation signifies to the result of reserving a piece of space without ever intending to use it. This space is wasted that this

Recovery regarding the file system in windows 2000, Write short note on rec...

Write short note on recovery regarding the file system in Windows 2000. In many file systems a power breakdown at the wrong time can damage the file system data structures so s

Explain processing predefined application protocols, Explain Processing Pre...

Explain Processing Predefined Application Protocols Implementing and swift processing of predefined application protocols is sure to improve performance of server - side applic

Rule a magnetic hard disk, Q. If magnetic hard disks eventually have the s...

Q. If magnetic hard disks eventually have the same cost per gigabyte as do tapes will tapes turn into obsolete or will they still be needed? Describe your answer. Answer: Tap

Explain next fit algorithm, NEXT FIT ALGORITHM Here scanning starts fro...

NEXT FIT ALGORITHM Here scanning starts from the first fit position and then it finds the next position which is large sufficient to hold the process. Thus the name next fit.

Determine what a locality of reference entails, Locality of reference entai...

Locality of reference entails that the page reference being made by a process  Locality of reference entails that the page reference being made by a process is similarly to be

Explain internal file structure, Internal file structure All disk I/O i...

Internal file structure All disk I/O is performed in units of single block, and all blocks are the similar size. It is unlikely that the physical record size will exactly match

What is the use of system calls, Q. What is the use of system calls? A...

Q. What is the use of system calls? Answer: System calls permit user-level processes to request services of the operating system.

Explain activities of an os in regard to file management, What are the five...

What are the five major activities of an operating system in regard to file management? The creation and deletion of files The creation and deletion of directories The s

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