0% found this document useful (0 votes)
3 views

OS_LAB_11_I200958

The document contains code for four tasks involving multithreading in C using pthreads. Each task demonstrates different functionalities: printing a message from multiple threads, calculating the sum of an array, calculating the sum with dynamic memory allocation, and finding the maximum value in an array. Each task includes the necessary code and error handling for thread creation and joining.
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 views

OS_LAB_11_I200958

The document contains code for four tasks involving multithreading in C using pthreads. Each task demonstrates different functionalities: printing a message from multiple threads, calculating the sum of an array, calculating the sum with dynamic memory allocation, and finding the maximum value in an array. Each task includes the necessary code and error handling for thread creation and joining.
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/ 11

OS-LAB-11

NAME: Sheir Muhammad


ROLL NUMBER: 20I-0958
SECTION: G

Task1:
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void* print_hello(void* arg) {


int thread_number = *(int*)arg;
printf("Hello from thread %d\n", thread_number);
free(arg);
return NULL;
}

int main() {
int num_threads = 5;
pthread_t threads[num_threads];

for (int i = 0; i < num_threads; i++) {


int* thread_number = malloc(sizeof(int));
if (thread_number == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
*thread_number = i;

if (pthread_create(&threads[i], NULL, print_hello,


thread_number) != 0) {
fprintf(stderr, "Error creating thread %d\n", i);
free(thread_number);
exit(1);
}
}

for (int i = 0; i < num_threads; i++) {


pthread_join(threads[i], NULL);
}
return 0;
}

ScreenShot:

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

struct ArrayData {
int *array;
int length;
};
void* calculate_sum(void* arg) {
struct ArrayData *data = (struct ArrayData*)arg;
int sum = 0;

for (int i = 0; i < data->length; i++) {


sum += data->array[i];
}

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


return NULL;
}

int main() {
int array[] = {1, 2, 3, 4, 5};
int length = sizeof(array) / sizeof(array[0]);

struct ArrayData data;


data.array = array;
data.length = length;
pthread_t thread;

if (pthread_create(&thread, NULL, calculate_sum, &data) !=


0) {
fprintf(stderr, "Error creating thread\n");
return 1;
}

pthread_join(thread, NULL);

return 0;
}

Screenshot:
Task3:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct ArrayData {
int *array;
int length;
};

void* calculate_sum(void* arg) {


struct ArrayData *data = (struct ArrayData*)arg;
int *sum = malloc(sizeof(int));
*sum = 0;

for (int i = 0; i < data->length; i++) {


*sum += data->array[i];
}

pthread_exit(sum);
}

int main() {
int array[] = {1, 2, 3, 4, 5};
int length = sizeof(array) / sizeof(array[0]);

struct ArrayData data;


data.array = array;
data.length = length;

pthread_t thread;
int *result;

if (pthread_create(&thread, NULL, calculate_sum, &data) !=


0) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
if (pthread_join(thread, (void**)&result) != 0) {
fprintf(stderr, "Error joining thread\n");
return 1;
}

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

free(result);

return 0;
}

Screenshot:

Task4:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct ArrayData {
int *array;
int length;
};

void* find_max(void* arg) {


struct ArrayData *data = (struct ArrayData*)arg;
int *max_value = malloc(sizeof(int));
if (max_value == NULL) {
perror("Failed to allocate memory");
pthread_exit(NULL);
}

*max_value = data->array[0];
for (int i = 1; i < data->length; i++) {
if (data->array[i] > *max_value) {
*max_value = data->array[i];
}
}
return max_value;
}

int main() {
int array[] = {3, 2, 9, 7, 6};
int length = sizeof(array) / sizeof(array[0]);

struct ArrayData data;


data.array = array;
data.length = length;

pthread_t thread;
int *result;

if (pthread_create(&thread, NULL, find_max, &data) != 0) {


fprintf(stderr, "Error in creating thread\n");
return 1;
}
if (pthread_join(thread, (void**)&result) != 0) {
fprintf(stderr, "Error in sjoining thread\n");
return 1;
}

printf("Maximum value in array is: %d\n", *result);

free(result);

return 0;
}

Screenshot:

You might also like