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:
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?