Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program for Ubuntu to find the Amicable unsigned number pairs between 10 and some user provided limit. In order to improve performance,

Write a C program for Ubuntu to find the Amicable unsigned number pairs between 10 and some user provided limit. In order to improve performance, the program should use threads to perform some of the computations in parallel. The program should read the thread count option and number limit from the command.

Each thread should obtain a number to check for a possible amicable number pair. If the sum of divsiors is less than or equal to the number, it will have already been checked and thus can be skipped. If an amicable number pair is found, it should be counted and placed in a two-dimensional array. The count, array, and several other variables will be globally defined.

The reference, POSIX thread (pthread) libraries, may be useful along with the C Thread Example (last page) (link below) Linux Tutorial: POSIX Threads (cmu.edu)

https://www.cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html

Command Line Arguments:

The program should read the number of threads and and use the limit from the command line in the following format; ./amNums -t -l . For example: .

/amNums -t 4 -l 10000

If either option or value is not correct, an applicable error message should be displayed. The allowed range for the thread count is 1 to 64 (inclusive). The minimum acceptable limit value is 100. If no arguments are entered, a usage message should be displayed. Refer to the sample executions for examples of the appropriate error messages. Refer to the C get options example for command line argument handing.

Note, the command line handling must be in a dedicated function (not done in the main).

Locking When changing global variables, a mutex must be used. For this usage, the mutex variables are declared globally.

// global declaration pthread_mutex_t myLock; // mutex variable

// initialize myLock mutex. pthread_mutex_init(&myLock, NULL);

// code section pthread_mutex_lock(&myLock);

// critical section code

pthread_mutex_unlock(&myLock);

The program should use two mutexes; one mutex for the global counter and one for the counters.

Example Execution: The following are some example executions, including the required error checking. Note, there are five (5) spaces before each number in the list using a %7lu format:

user-vm% time ./amNums -t 1 -l 100000 Amicable Numbers Program

Hardware Cores: 12 Thread Count: 1 Numbers Limit: 100000

Please wait. Running...

Amicable Numbers 220 284 1184 1210 2620 2924 5020 5564 6232 6368 10744 10856 12285 14595 17296 18416 63020 76084 66928 66992 67095 71145 69615 87633 79750 88730

Count of amicable number pairs from 1 to 100000 is 13

real 0m21.848s user 0m21.847s sys 0m0.000s

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago