0

I'm trying to calculate prime numbers from a given interval from CLA. The example input should be: ./program interval_min interval_max number_process number_thread

And here is the expected process and thread schema: enter image description here

I coded a function for finding primes that:

int *print_prime(int min, int max)
{   
    if(min == 0){
        min += 2;
    }
    else if (min == 1)
    {
        min += 1;
    }
    
    int *list;
    list = new int;
    int listNumber = 0;
    for (size_t i = min; i <= max; i++){
        int flag = 0;

        for (size_t j = min + 1; j < i; j++){
            if(i % j == 0){
                flag++;
                continue;
            }
        }
        if (flag == 0){
            list[listNumber] = i;
            listNumber++;
        }                
    }
    return list;
}

And I handled the creating the processes like that:

int main(int argc, char **argv)
{   
    if(argc < 5){
        std::cout << "Invalid entry. Please enter 4 arguments." << std::endl;
        return EXIT_FAILURE;
    }

    std::cout << "Master: Started." << std::endl; /* The master process executed */

    pid_t pid;

    int interval_min = std::stoi(argv[1]);
    int interval_max = std::stoi(argv[2]);
    int np = std::stoi(argv[3]);
    int nt = std::stoi(argv[4]);

    for (int i = 0; i < np; i++){
        pid = fork();

        if(pid > 0){
        std::cout << "slave " << i << std::endl; 
        }
         if(pid == 0){
        std::cout << "This is a child Process" << i << std::endl;
        break;
        }
    } 

But I stuck at this point. How can I add threads under processes?

Expected output should be like this: enter image description here

  • Look into prime number sieves. You'll probably speed things up so much that threads will become unnecessary. – user4581301 Dec 05 '20 at 00:27
  • 1
    As for threads, have you created a simple multithreaded program without involving fork? If not, do that first. What you need to do should be obvious once you've gotten a few simpler examples under your belt. – user4581301 Dec 05 '20 at 00:31
  • Thank you very much so I need to handle threads before processes right? – Levent Kaya Dec 05 '20 at 00:32
  • No, but if you've never written a multithreaded program before, just play with threading for a while and make sure you have it figured out before you move to threads and processes. – user4581301 Dec 05 '20 at 00:35
  • This is homework or something like it, yes? You are required to use processes and threads for this task? – user4581301 Dec 05 '20 at 00:37
  • yes it is. Yes i need to use process and threads both. – Levent Kaya Dec 05 '20 at 00:38
  • Take it step by step Figure out one and then figure out the other and finally put the two together. You will find passing data back and forth between threads is easy. Unfortunately it is too easy, making it easy for one thread to blast the snot out of the other thread by mistake. Processes don't share information nearly as easily. This means it's really hard for pone process to damage another, but collating the results will be trickier. If you try to learn and do both at the same time odds are very high you'll spend more time than splitting the job up and learning both separately. – user4581301 Dec 05 '20 at 00:50
  • If we strip out the background context, does your question boil down to [how to create a thread](https://stackoverflow.com/search?q=%5Bc%2B%2B%5D+how+to+create+a+thread)? Perhaps you are looking for a [simple example of threading](https://stackoverflow.com/questions/266168)? – JaMiT Dec 05 '20 at 01:42

0 Answers0