Laplace M
Laplace M

Reputation: 25

Thread Program on Linux (Posix Thread)

I want to modify the multithread program on the Linux operating system using this Pthread API.

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

int sum;
void *runner(void *param);

int main(int argc, char *argv[]) {
    pthread_t tid
        pthread_attr_t attr;

    if (argc != 2) {
        fprintf(stderr, "usage: a.out <integer value>\n");
        return -1;
    }
    if (atoi(argv[1]) < 0) {
        fprintf(stderr, "%d must be >=0\n", atoi(argv[1]));
        return -1;
    }

    pthread_attr_init(&attr);
    pthread_create(&tid, &attr, runner, argv[1]);
    pthread_join(tid, NULL);

    printf("sum = %d\n", sum);
}

void *runner(void *param);
{
    int i, upper = atoi(param);
    sum = 0;

    for (i = 1; i <= upper; i++)
        sum += i;

    pthread exit(0);
}

I want to change that program into a program that has 2 threads that work together to add a number. But i don't know how to change it, Thanks again for any help that can be offered. I am sorry,because I'm not good at explaining.

Upvotes: 0

Views: 422

Answers (2)

Henley n
Henley n

Reputation: 3805

first there is 3 errors : the pthread tid declaration has no ";", then there is one at the end of your runner()* function declaration, and last but not least, a underscore is missing on the last line pthread_exit(0) beware ahah

ok for vars :

    pthread_t tid;
    pthread_t tid2;
    pthread_attr_t attr;
    pthread_attr_t attr2;

and in the code after the ifs, add this :

pthread_attr_init(&attr);
pthread_attr_init(&attr2);
pthread_create(&tid, &attr, runner, argv[1]);
pthread_create(&tid2, &attr2, runner, argv[2]); // not sure for argv[2]?

not sure for argv[2], it depends if it's 2 different numbers?

pthread_join are no use, they are here only for pausing threads, i think that if you want them to work in parallel, you need to only do "pthread_create" and they should work in parallel (but was i saw on my CS class on parallel programming 3 years ago, it will never be "real real" parallel because only the OS can control this and you need to be some kind of a super root to be able to really control the threads

I mean it won't be faster because it will not be real parallel prog

Upvotes: 1

francium
francium

Reputation: 2480

I'm not exactly sure what you want, but a really quick and dirty solution based on the existing code is below. I'm assuming you just want two thread to sum a single variable to the input.

An explanation of what's going on: I had to fix some minor syntax issues you have in your code, one big one being the semicolon at the end of the runner function definition. I added a mutex to define a critical section in the runner's for loop. It makes sure only 1 thread can update the sum. I'm assuming you want the sum to equal the input, so we just increment it by 1 and check before incrementing whether the value is still below. Like I said, it's quite quick and dirty, not really the ideal solution. To create two threads, we just call the thread create function twice in main.

See https://computing.llnl.gov/tutorials/pthreads/#Mutexes for more important about mutexes and the pthread library.

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

int sum = 0; // set it once globally
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
void *runner(void *param);

int main(int argc, char *argv[]) {
    pthread_t tid1, tid2;
    pthread_attr_t attr;

    if (argc != 2) {
        fprintf(stderr, "usage: a.out <integer value>\n");
        return -1;
    }
    if (atoi(argv[1]) < 0) {
        fprintf(stderr, "%d must be >=0\n", atoi(argv[1]));
        return -1;
    }

    pthread_attr_init(&attr);
    pthread_create(&tid1, &attr, runner, argv[1]);
    pthread_create(&tid2, &attr, runner, argv[1]);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    printf("sum = %d\n", sum);
}

void *runner(void *param) {
    int i, upper = atoi(param);
    // sum = 0;

    int t = pthread_self();

    for (i = 1; i <= upper; i++) {
        pthread_mutex_lock(&mtx);
        if (sum < upper) {
            printf("%d incrementing\n", t);
            sum += 1;
        }
        pthread_mutex_unlock(&mtx);
    }

    pthread_exit(0);
}

Compile with cc -o main main.c -pthread.

Upvotes: 0

Related Questions