0% found this document useful (0 votes)
3 views5 pages

PES1UG24CS838_VIKASKS

The document describes a programming exercise that involves creating a multithreaded program to calculate the sum of an array of integers using mutex locks to prevent race conditions. It includes a code implementation in C that initializes an array, creates multiple threads to compute partial sums, and safely updates a global sum variable. The program demonstrates thread synchronization through mutex usage and outputs the final sum of the array elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

PES1UG24CS838_VIKASKS

The document describes a programming exercise that involves creating a multithreaded program to calculate the sum of an array of integers using mutex locks to prevent race conditions. It includes a code implementation in C that initializes an array, creates multiple threads to compute partial sums, and safely updates a global sum variable. The program demonstrates thread synchronization through mutex usage and outputs the final sum of the array elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Operating System

Programming Exercise-2

NAME VIKAS K S
SRN PES1UG24CS838
SECTION J
3) Using mutex locks, write a program that creates multiple threads
and performs a parallel sum of an array of integers. Each thread
should work on a portion of the array and update a global variable
that stores the sum. The program should use mutex locks to protect
the access to the global variable and avoid race conditions.

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

#define ARRAY_SIZE 100


#define NUM_THREADS 4

int arr[ARRAY_SIZE]; // Array of integers


int global_sum = 0; // Global sum
pthread_mutex_t sum_mutex; // Mutex lock for
protecting global_sum

// Structure to pass data to threads


typedef struct {
int start_index;
int end_index;
} ThreadData;
// Function that each thread will execute
void* thread_sum(void* arg) {
ThreadData* data = (ThreadData*) arg;
int local_sum = 0;

// Calculate the sum of the portion of the array


assigned to this thread
for (int i = data->start_index; i < data->end_index;
i++) {
local_sum += arr[i];
}

// Lock the mutex before updating the global sum


pthread_mutex_lock(&sum_mutex);
global_sum += local_sum;
pthread_mutex_unlock(&sum_mutex);

pthread_exit(NULL);
}

int main() {
// Initialize the array with values (e.g., 1 to
ARRAY_SIZE)
for (int i = 0; i < ARRAY_SIZE; i++) {
arr[i] = i + 1;
}

pthread_t threads[NUM_THREADS];
ThreadData thread_data[NUM_THREADS];
int segment_size = ARRAY_SIZE / NUM_THREADS;

// Initialize the mutex


pthread_mutex_init(&sum_mutex, NULL);

// Create threads and assign each thread a portion


of the array
for (int i = 0; i < NUM_THREADS; i++) {
thread_data[i].start_index = i * segment_size;
thread_data[i].end_index = (i == NUM_THREADS -
1) ? ARRAY_SIZE : (i + 1) * segment_size;
pthread_create(&threads[i], NULL, thread_sum,
&thread_data[i]);
}

// Wait for all threads to complete


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

// Destroy the mutex


pthread_mutex_destroy(&sum_mutex);

// Print the final result


printf("Sum of array elements: %d\n", global_sum);

return 0;
}
Output :

You might also like