📜  门| GATE-CS-2006 |问题23(1)

📅  最后修改于: 2023-12-03 15:42:16.469000             🧑  作者: Mango

GATE-CS-2006 Problem 23

This problem involves implementing a concurrent program to increment an integer counter. The program must ensure that the counter is incremented exactly n times, where n is a positive integer specified by the user.

Solution

One possible solution is to use a shared variable to store the current value of the counter, and a mutex to protect access to it. Each thread will lock the mutex, increment the counter, and unlock the mutex. The program will create n threads, each of which will perform the increment operation.

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

void *increment(void *param) {
    pthread_mutex_lock(&mutex);
    counter++;
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s n\n", argv[0]);
        return 1;
    }

    int n = atoi(argv[1]);
    pthread_t threads[n];

    for (int i = 0; i < n; i++) {
        if (pthread_create(&threads[i], NULL, increment, NULL)) {
            fprintf(stderr, "Error creating thread\n");
            return 1;
        }
    }

    for (int i = 0; i < n; i++) {
        pthread_join(threads[i], NULL);
    }

    printf("Counter value: %d\n", counter);
    return 0;
}

In this solution, we create n threads using pthread_create, passing the increment function as the start routine. The increment function locks the mutex using pthread_mutex_lock, increments the counter, and unlocks the mutex using pthread_mutex_unlock. We wait for all the threads to complete using pthread_join, and then print the final value of the counter.

Explanation

The pthread_mutex_lock and pthread_mutex_unlock calls ensure that only one thread at a time can access the shared variable. This prevents race conditions, where two threads try to increment the counter at the same time, causing unpredictable results. The use of a mutex ensures that the critical section of code (i.e., accessing the shared variable) is protected.

The pthread_create call creates a thread and starts it executing the specified start routine (i.e., the increment function in our case). The second argument is a pointer to an attributes object, which we set to NULL to use the default attributes. The last argument is a pointer to void, which we set to NULL because we do not need to pass any arguments to the thread.

The pthread_join call waits for the specified thread to complete before continuing execution. The second argument is a pointer to a location in which to store the exit status of the thread, which we set to NULL because we do not care about the exit status in this case.

Conclusion

In summary, we have implemented a concurrent program to increment an integer counter exactly n times, using a shared variable and a mutex to protect access to it. The program creates n threads, each of which performs the increment operation. The use of a mutex ensures that the critical section of the code is protected, preventing race conditions.