Click here to Skip to main content
15,846,009 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
our task is to write a multi threaded program called factorize that is able to factorize natural numbers concurrently. The program reads the numbers to be factorized from the standard input and writes the result to the standard output. Multiple numbers can be factorized concurrently using the thread pool pattern. The -t command line argument controls is shown below:

$ cat << _end_ | .factorize -t 5
> a 12
> b 234
> _end_
a 12 = 2 2 3
b 234 = 2 3 3 13

Note that every number is prefixed with a tag that is used to match results to requests. The content of the tag is arbitrary but it is safe to assume it does not contain white space or control characters. Here is an example where the same number is factorised several times concurrently and the results appear in a different order on the standard output:

$ cat <<_end_. |. ./factorise -t 5

> c 18446744 = 2 2 2 349 6607
> a 18446744 = 2 2 2 349 6607
> b 18446744 = 2 2 2 349 6607
Detailed requirements:
The program has to factorize number in the unsigned 64-bit integer number space correctly.
The program must use the thread pool pattern where a fixed number of threads are created to perform the factorization. The numbers are read from the standard input by the main thread and placed into a queue to be picked up by the worker threads . Factorisation results are passed back to the main thread and they are written by the main thread to the standard output.
The program should exit once the end of the standard input has been reached and there are no busy threads anymore.
The program must follow good programming practices . In particular it needs to handle all runtime error situations in an appropriate manner.

What I have tried:

i have no idea how to solve it, given in unix so , should i use C or c++. and how is the multi-threading program writtten .
Updated 26-Jun-22 10:17am
Richard MacCutchan 26-Jun-22 10:59am    
You solve it by first working out the algorithm necessary to solve the mathematical part. You then translate the steps of your algorithm into code (c, C++ or whatever your preferred language). Then it is just a matter of buid, test, build, test ...

But if you really have no idea then I suggest you study a) your programming language, and b) Linux threads.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer
i have no idea how to solve it

It looks like each thread will factorize an integer, so start by writing a single thread program that factorize an integer, then you will see how to convert to multithreading. Note you will have to respect some rules to be threading complatible.
given in unix so , should i use C or c++.

Choose the language you prefer.
and how is the multi-threading program written .

the fear you have to talk to your teacher, there is no way we can teach you multithreading programming in the scope of this little textbox.
Share this answer
To implement this, you need a work queue (of numbers to be factored). The original thread starts by creating a pool of worker threads. It then reads inputs to create work items that it puts on the work queue, and the worker threads remove them. You also need a results queue. After a worker thread factors a number, it puts the result on that queue, and the original thread removes results from that queue and reports them.

The challenge with multi-threading is that threads run interleaved. A thread will randomly stop running anywhere so that another thread can run, and n threads can even run simultaneously if you have n cores. This means that any data which is used by more than one thread must be protected by a critical region when it is being accessed. The critical region prevents one thread from trying to read data that another thread is in the middle of modifying (and which is therefore in an unstable state).

Given a choice between C and C++, I would use C++ for this because it has <thread>[^], <queue>[^], and <mutex>[^], which should allow you to do this in a platform-independent way. Each queue should have its own mutex. The mutex must be acquired before accessing the queue and must be released immediately after accessing it. This prevents two threads from trying to handle the same work item, for example.

There are many ways to implement the function that worker threads execute. I don't know what has been covered in your course, so I will suggest a simple way. Other ways are arguably a bit better but more complicated. A worker thread basically has to do this:
   acquire the work queue mutex;

   if(the work queue is empty)
      release the work queue mutex;
      sleep for 100 milliseconds;
      pop an item off the work queue;
      release the work queue mutex;
      factor the number;
      acquire the results queue mutex;
      push the result onto the results queue;
      release the results queue mutex;
After the original thread creates the worker threads, it will do much the same as this, except that it pushes items onto the work queue and pops them off the results queue. When it has read all the input and both queues are empty, it exits.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900