0% found this document useful (0 votes)
7 views10 pages

Os Lab 12 I200958

The document contains code for three tasks involving multithreading in C. Task 1 performs basic arithmetic operations using threads, Task 2 calculates the square and square root of a number, and Task 3 demonstrates thread attributes such as stack size and scheduling policy. Each task includes relevant code snippets and prompts for user input.
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)
7 views10 pages

Os Lab 12 I200958

The document contains code for three tasks involving multithreading in C. Task 1 performs basic arithmetic operations using threads, Task 2 calculates the square and square root of a number, and Task 3 demonstrates thread attributes such as stack size and scheduling policy. Each task includes relevant code snippets and prompts for user input.
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/ 10

OS-LAB-12

NAME: Sheir Muhammad


ROLL NUMBER: 20I-0958
SECTION: G

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

double num1, num2;


double add_result, sub_result, mul_result, div_result;

void* addition(void* arg) {


add_result = num1 + num2;
pthread_exit(0);
}
void* subtraction(void* arg) {
sub_result = num1 - num2;
pthread_exit(0);
}

void* multiplication(void* arg) {


mul_result = num1 * num2;
pthread_exit(0);
}

void* division(void* arg) {


if (num2 != 0) {
div_result = num1 / num2;
} else {
printf("Error: The division by zero is undefined.\n");
div_result = 0;
}
pthread_exit(0);
}

int main() {

printf("Enter two numbers: ");


scanf("%lf %lf", &num1, &num2);

pthread_t add_thread, sub_thread, mul_thread, div_thread;

pthread_create(&add_thread, NULL, addition, NULL);


pthread_create(&sub_thread, NULL, subtraction, NULL);
pthread_create(&mul_thread, NULL, multiplication, NULL);
pthread_create(&div_thread, NULL, division, NULL);

pthread_join(add_thread, NULL);
pthread_join(sub_thread, NULL);
pthread_join(mul_thread, NULL);
pthread_join(div_thread, NULL);
printf("Addition: %.2lf\n", add_result);
printf("Subtraction: %.2lf\n", sub_result);
printf("Multiplication: %.2lf\n", mul_result);
printf("Division: %.2lf\n", div_result);

return 0;
}

ScreenShot:

Task2:
#include <stdio.h>
#include <pthread.h>
#include <math.h>
#include <stdlib.h>
void* calculate_square(void* arg) {
double* input = (double*) arg;
double* result = (double*) malloc(sizeof(double)); // Allocate
memory for result
*result = (*input) * (*input); // Calculate square
pthread_exit(result); // Return result
}

void* calculate_square_root(void* arg) {


double* input = (double*) arg;
double* result = (double*) malloc(sizeof(double)); // Allocate
memory for result
*result = sqrt(*input); // Calculate square root
pthread_exit(result); // Return result
}

int main() {
double number;
printf("Enter a number: ");
scanf("%lf", &number);
pthread_t square_thread, square_root_thread;
double *square_result, *square_root_result;

pthread_create(&square_thread, NULL, calculate_square,


&number);
pthread_create(&square_root_thread, NULL,
calculate_square_root, &number);

pthread_join(square_thread, (void**) &square_result);


pthread_join(square_root_thread, (void**)
&square_root_result);

printf("Square of %.2lf: %.2lf\n", number, *square_result);


printf("Square root of %.2lf: %.2lf\n", number,
*square_root_result);

free(square_result);
free(square_root_result);
return 0;
}

Screenshot:

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

#define STACK_SIZE 1024 * 1024 //Its equal to 1 MB

void* thread_function(void* arg) {


pthread_t thread_id = pthread_self();
pthread_attr_t* attr = (pthread_attr_t*) arg;
size_t stack_size;
int policy;

pthread_attr_getstacksize(attr, &stack_size);
pthread_attr_getschedpolicy(attr, &policy);

printf("Thread ID: %lu\n", thread_id);


printf("Stack size: %zu bytes\n", stack_size);

if (policy == SCHED_FIFO) {
printf("Scheduling policy: SCHED_FIFO\n");
} else if (policy == SCHED_RR) {
printf("Scheduling policy: SCHED_RR\n");
} else if (policy == SCHED_OTHER) {
printf("Scheduling policy: SCHED_OTHER\n");
} else {
printf("Unknown scheduling policy\n");
}

pthread_exit(NULL);
}

int main() {
pthread_t thread;
pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setstacksize(&attr, STACK_SIZE);

pthread_attr_setschedpolicy(&attr, SCHED_RR);

if (pthread_create(&thread, &attr, thread_function,


(void*)&attr) != 0) {
perror("Failed to create thread");
return 1;
}
pthread_join(thread, NULL);
pthread_attr_destroy(&attr);

printf("Thread completed.\n");
return 0;
}

Screenshot:

You might also like