compiles code but does not work, just successfully finishes

Asked

Viewed 79 times

0

Well I am new to the C language and I decided to venture into the world of multithreads, I thought of making a program that would find primes in a range from 1 to N, and for that I would divide the task into X threads. For this the user would need to pass via commandline the N and the number of threads, so I wrote the code below but it compiles without errors but does not work as expected, even if I put a prinf("test") at the beginning of the main() function it does not appear in the console, what’s going on and where I went wrong?

I’m using a C compiler online and passing 100 and 32 to N and number of threads respectively

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *calculaPrimo(void *limitValue) {
    int primo, j;
    int num = 1;
    int maxValue = limitValue;

    while (num <= maxValue) {
        primo = 1;

        for (j = 2; j < num && primo; j++) {
            if (num % j == 0) {
                primo = 0;
            }
        }
        if (primo == 1) {
            printf("%d ", num);
        }
        num++;
    }
    printf("\n");
}

void criaThreads(char *args[]) {
    int maxValue = atoi(args[1]);
    int maxThreads = atoi(args[2]);
    printf("%d , %d", maxValue, maxThreads);
    
    long t;
    
    pthread_t threads[maxThreads];
    for (t = 0; t < maxThreads; t++) {
        pthread_create(&threads[t], NULL,(void * ) calculaPrimo, &maxValue);
    }
    for (t = 0; t < maxThreads; t++) {
        pthread_join(threads[t],NULL);
    }

}


int main(int argc, int argv[]) {
    char *args[] = {argv[0], argv[1], argv[2]};
    criaThreads(args);
    pthread_exit(NULL);
    return 0;
}
  • How are you compiling? Which compiler? What arguments are you passing to start the application?

  • I edited the question with this information

  • One mistake I see is char *args[] = {argv[1], argv[2]}, here you take the second and third argument, if you pass only two, the third is null. In this context the correct one would be char *args[] = {argv[0], argv[1]}. Ensure your parameters are correct in argv.

  • I fixed it however I keep getting a warning: initialization makes Pointer from integer without a cast

  • @Brunod. Actually the ideal is to check the argc to see if the amount of arguments is what you need (if calling the program without arguments, it is no use trying to catch the argv[1], argv[2], etc - in C, this may not give error, but generates undefined behavior). And in the main, sure is char *argv[] and not int *argv[]. Anyway, here’s the tip I already gave below: as you’re starting out, first study the basics of the language and only then go for more complicated things (like threads and pointers)

1 answer

0

Dear, I made some changes to your code. They are in tThread. c. For ease, I left your original in tThread_orig. c, and a comparison between the two versions (alias diff) in tThread.c.diff

I followed the ANSI C standard, which is best known and popular. There are other versions of C, which are still not widely documented.

Your code is interesting and has some good ideas. Congratulations !

Congratulations also for trying to enter the world of concurrent programming, which is even more challenging than conventional programming.

However, your code has a fundamental problem: as you comment, your idea is to divide the task of locating primes among the various threads. However, its function calculaPrimo() does exactly the same job for everyone threads.

It would be necessary to divide the total band for calculation between the various threads.

Naturally, this would bring the need for communication between the threads. But that’s problem for another question.

Still a relevant point is that if you want to increase the performance of a process, you should try to limit the number of threads the number of processors available in your architecture. The value you said you placed, 32 threads, would only make sense in an architecture with at least 32 processors.

Good luck and keep working with courage.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.