DEPARTMENT OF COMPUTER & SOFTWARE
ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
Subject Name: Computer System Architecture
Lab Report No:
SUBMITTED TO:
Instructor Name:
Lab Engineer:
SUBMITTED BY:
Student Name:
Reg #
DE-
Submission Date:
Task No:1
Write a C program that creates a single thread to print "Hello from the thread!" and then the
main thread prints "Hello from the main thread!".
Code:
#include <stdio.h>
#include <pthread.h>
void* print_from_thread(void* arg) {
printf("Hello from the thread!\n");
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, print_from_thread, NULL);
pthread_join(thread, NULL);
printf("Hello from the main thread!\n");
return 0;
}
Output:
Task No:2
Modify the above program to create a thread that calculates the sum of the first N natural
numbers (where N is provided by the user). The main thread should wait for this thread to
complete and then print the sum.
Code:
#include <stdio.h>
#include <pthread.h>
// The thread function
void* calculate_sum(void* arg)
{
int N = *((int*)arg);
int sum = 0;
// Sum of N natural numbers
for (int i = 1; i <= N; i++) {
sum += i;
}
// Store the result in a separate memory location
int* result = (int*)malloc(sizeof(int));
*result = sum;
return result;
}
int main() {
pthread_t thread;
int N;
int* result;
printf("Enter a number N to calculate the sum of the first N natural numbers: ");
scanf("%d", &N);
// Create a thread to calculate the sum
pthread_create(&thread, NULL, calculate_sum, &N);
// Wait for the thread to finish and retrieve the result
pthread_join(thread, (void**)&result);
printf("The sum of the first %d natural numbers is: %d\n", N, *result);
// Free the allocated memory for the result
free(result);
return 0;
}
Output:
Task No:3
Write a C program that creates two threads:
• One thread calculates the factorial of a number provided by the user.
• The other thread calculates the Fibonacci series up to a number provided by the user.
The main thread should wait for both threads to finish and print the results.
Code:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h> // For malloc and free
// Thread function to calculate factorial
void* calculate_factorial(void* arg) {
int N = *((int*)arg);
int factorial = 1;
for (int i = 1; i <= N; i++) {
factorial *= i;
}
// Store the result in dynamically allocated memory and return it
int* result = (int*)malloc(sizeof(int));
*result = factorial;
return result;
}
// Thread function to calculate Fibonacci series up to N
void* calculate_fibonacci(void* arg) {
int N = *((int*)arg);
int* fibonacci = (int*)malloc((N + 1) * sizeof(int)); // Allocate space for N numbers
// First two numbers in Fibonacci series
if (N >= 1) fibonacci[0] = 0;
if (N >= 2) fibonacci[1] = 1;
for (int i = 2; i < N; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
return fibonacci;
}
int main(){
pthread_t thread1, thread2; // Two threads
int N_factorial, N_fibonacci;
int *factorial_result;
int *fibonacci_result;
// Get input from the user
printf("Enter a number to calculate its factorial: ");
scanf("%d", &N_factorial);
printf("Enter a number to calculate Fibonacci series up to: ");
scanf("%d", &N_fibonacci);
// Create the threads
pthread_create(&thread1, NULL, calculate_factorial, &N_factorial);
pthread_create(&thread2, NULL, calculate_fibonacci, &N_fibonacci);
// Wait for both threads to finish
pthread_join(thread1, (void**)&factorial_result);
pthread_join(thread2, (void**)&fibonacci_result);
// Print the results
printf("Factorial of %d is: %d\n", N_factorial, *factorial_result);
printf("Fibonacci series up to %d: ", N_fibonacci);
for (int i = 0; i < N_fibonacci; i++) {
printf("%d ", fibonacci_result[i]);
}
printf("\n");
//Free allocated memory
free(factorial_result);
free(fibonacci_result);
return 0;
}
Output:
Task 4:
Write a C program that creates multiple threads to calculate the sum of elements in an array.
Each thread should be responsible for summing a portion of the array, and the main thread
should aggregate the results from all the threads.
Code:
#include <stdio.h>
#include <pthread.h>
#define ARRAY_SIZE 5
#define NUM_THREADS 3
int array[ARRAY_SIZE];
int partial_sums[NUM_THREADS] = {0};
// Thread function thread
void* sum_portion(void* Argu) {
int thread_id = *((int*)Argu); // Get the thread ID
int start = thread_id * (ARRAY_SIZE / NUM_THREADS);
int end = (thread_id + 1) * (ARRAY_SIZE / NUM_THREADS);
int sum = 0;
// Calculate the sum for this portion of the array
for (int i = start; i < end; i++) {
sum += array[i];
}
// Store the partial sum for this thread
partial_sums[thread_id] = sum;
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
int total_sum = 0;
// Get input
printf("Enter %d elements for the array:\n", ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++) {
scanf("%d", &array[i]);
}
// Create threads to sum portions of the array
for (int i = 0; i < NUM_THREADS; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, sum_portion, &thread_ids[i]);
}
// threads to finish
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Summation of the results from all threads
for (int i = 0; i < NUM_THREADS; i++) {
total_sum += partial_sums[i];
}
printf("The total sum of the array is: %d\n", total_sum);
return 0;
}
Output: