LAB-4: THREAD PROGRAMMING
NAME: PATTAN SAAD FAHEEM KHAN
REG. NO.: 22BPS1216
1. ADDING 2 VALUES USING THREADS:
CODE:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
// Function to add the values
void* addValues(void* arg) {
int* values = (int*)arg;
int value1 = values[0];
int value2 = values[1];
int result = value1 + value2;
int* result_ptr = malloc(sizeof(int));
*result_ptr = result;
pthread_exit(result_ptr);
}
int main() {
// Initialize values
int values[2] = {5, 10};
// Create thread and add values
pthread_t thread;
pthread_create(&thread, NULL, addValues, (void*)values);
// Wait for the thread to finish
int* result;
pthread_join(thread, (void**)&result);
// Print the result
printf("The sum of %d and %d is %d\n", values[0], values[1], *result);
// Free the allocated memory
free(result);
return 0;
}
OUTPUT:
2. FINDING THE SUM OF AN ARRAY USING THREADS:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define ARRAY_SIZE 10
#define NUM_THREADS 2
// Global variables
int array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;
pthread_mutex_t mutex;
// Thread function to calculate the sum of a portion of the array
void* calculateSum(void* arg) {
int thread_id = *((int*)arg);
int start = thread_id * (ARRAY_SIZE / NUM_THREADS);
int end = start + (ARRAY_SIZE / NUM_THREADS);
int thread_sum = 0;
for (int i = start; i < end; i++) {
thread_sum += array[i];
}
// Lock the mutex before updating the sum
pthread_mutex_lock(&mutex);
sum += thread_sum;
// Unlock the mutex after updating the sum
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main() {
// Initialize the mutex
pthread_mutex_init(&mutex, NULL);
// Create an array of thread IDs
pthread_t threads[NUM_THREADS];
// Create the threads
for (int i = 0; i < NUM_THREADS; i++) {
int* thread_id = malloc(sizeof(int));
*thread_id = i;
pthread_create(&threads[i], NULL, calculateSum, (void*)thread_id);
}
// Wait for the threads to finish
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Destroy the mutex
pthread_mutex_destroy(&mutex);
// Print the sum
printf("The sum of the array is: %d\n", sum);
return 0;
}
OUTPUT:
3. PRINTING ODD AND EVEN VALUES USING THREADS:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_NUMBER 10
// Global variables
pthread_mutex_t mutex;
pthread_cond_t cond;
int current_number = 1;
// Thread function to print odd numbers
void* printOdd(void* arg) {
while (1) {
// Lock the mutex before accessing the shared variable
pthread_mutex_lock(&mutex);
// Check if it's the odd number's turn to print
if (current_number % 2 != 0) {
printf("Odd: %d\n", current_number++);
}
// Signal the even thread to continue
pthread_cond_signal(&cond);
// Wait for the even thread to signal
pthread_cond_wait(&cond, &mutex);
// Unlock the mutex after accessing the shared variable
pthread_mutex_unlock(&mutex);
// Exit the loop when reaching the maximum number
if (current_number > MAX_NUMBER) {
break;
}
}
pthread_exit(NULL);
}
// Thread function to print even numbers
void* printEven(void* arg) {
while (1) {
// Lock the mutex before accessing the shared variable
pthread_mutex_lock(&mutex);
// Check if it's the even number's turn to print
if (current_number % 2 == 0) {
printf("Even: %d\n", current_number++);
}
// Signal the odd thread to continue
pthread_cond_signal(&cond);
// Wait for the odd thread to signal
pthread_cond_wait(&cond, &mutex);
// Unlock the mutex after accessing the shared variable
pthread_mutex_unlock(&mutex);
// Exit the loop when reaching the maximum number
if (current_number > MAX_NUMBER) {
break;
}
}
pthread_exit(NULL);
}
int main() {
// Initialize the mutex and condition variable
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
// Create the threads
pthread_t oddThread, evenThread;
pthread_create(&oddThread, NULL, printOdd, NULL);
pthread_create(&evenThread, NULL, printEven, NULL);
// Wait for the threads to finish
pthread_join(oddThread, NULL);
pthread_join(evenThread, NULL);
// Destroy the mutex and condition variable
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
OUTPUT: