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

Lab 07 - Programming threads

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 07 - Programming threads

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Faculty of Computing

CS-330 Operating System

BESE – 14AB

14th March 2025

Lab Engineer: Mr. Junaid Sajid


Instructor: Engr Taufeeq Ur Rehman

Submitted By:
Name CMS ID

Ushba Fatima 467212

Jarrar Haider Nemati 467291

CS330: Operating Systems Page 1


Lab 07: Programming Threads
Task 1 : ( Done by Jarrar )

Implement a simple program using for loop to calculate the sum of first 100 million positive
integers (i.e., 100,000,000) elements in an array.

CODE :

#include <stdio.h>

int main() {
long long sum = 0;
int n = 100000000; // 100 million integers

for (int i = 1; i <= n; i++) {


sum += i;
}

printf("Sum of first 100 million integers is: %lld\n", sum);


return 0;
}

OUTPUT:

CS330: Operating Systems Page 2


Task 2 : ( Done by Jarrar )

Convert the above code into multi-threaded program using pthreads library, which runs 4 threads
in parallel.

CODE :

#include <stdio.h>
#include <pthread.h>
#define N 100000000
#define THREADS 4
typedef struct {
long long start;
long long end;
long long sum;
} ThreadData;

// Function that each thread will run to compute the sum of its
range
void* compute_sum(void* arg) {
ThreadData* data = (ThreadData*) arg;
data->sum = 0;

for (long long i = data->start; i <= data->end; i++) {


data->sum += i;
}

return NULL;
}

int main() {
pthread_t threads[THREADS];
ThreadData thread_data[THREADS];
long long total_sum = 0;

long long range_size = N / THREADS;

// Create threads
for (int i = 0; i < THREADS; i++) {
thread_data[i].start = i * range_size + 1;

CS330: Operating Systems Page 3


thread_data[i].end = (i == THREADS - 1) ? N : (i + 1) *
range_size;
pthread_create(&threads[i], NULL, compute_sum,
&thread_data[i]);
}

// Wait for threads to finish


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

// Combine the results from all threads


for (int i = 0; i < THREADS; i++) {
total_sum += thread_data[i].sum;
}

printf("Sum of first 100 million integers is: %lld\n",


total_sum);
return 0;
}

OUTPUT:

CS330: Operating Systems Page 4


Task 3 : ( Done by Ushba )

Run the above code several time to verify that the threads runs concurrently and threads
execution varies in each run.

The code is modified to include debug print statements inside the thread function to observe
when each thread starts and ends. This will help visualize the concurrent execution.

CODE:
#include <stdio.h>
#include <pthread.h>
#define N 100000000
#define THREADS 4

typedef struct {
long long start;
long long end;
long long sum;
int thread_id;
} ThreadData;

// Function that each thread will run to compute the sum of its range
void* compute_sum(void* arg) {
ThreadData* data = (ThreadData*) arg;
data->sum = 0;

printf("Thread %d started: range [%lld - %lld]\n", data->thread_id,


data->start, data->end);

for (long long i = data->start; i <= data->end; i++) {


data->sum += i;
}

printf("Thread %d finished computing sum: %lld\n", data->thread_id,


data->sum);

return NULL;
}
int main() {
pthread_t threads[THREADS];
ThreadData thread_data[THREADS];
long long total_sum = 0;

CS330: Operating Systems Page 5


long long range_size = N / THREADS;
for (int i = 0; i < THREADS; i++) {
thread_data[i].start = i * range_size + 1;
thread_data[i].end = (i == THREADS - 1) ? N : (i + 1) *
range_size;
thread_data[i].thread_id = i;
pthread_create(&threads[i], NULL, compute_sum,
&thread_data[i]);
}
for (int i = 0; i < THREADS; i++) {
pthread_join(threads[i], NULL);
}
for (int i = 0; i < THREADS; i++) {
total_sum += thread_data[i].sum;
}
printf("Sum of first 100 million integers is: %lld\n", total_sum);
return 0;
}

OUTPUT:

CS330: Operating Systems Page 6


Each time the program is run, it is noticed that the threads do not always start in the same order.
The execution order of thread completion may change across runs. The final sum remains correct
(should always be 5000000050000000).

Task 4 : ( Done by Ushba )

Use the code in task 1 and 2 to print the time each program takes to execute. Which program
runs faster and why?

To measure the time taken for execution of each code, we add new statements that measure the
start and end times and finally the time interval (end-start).

Task 1:

CODE:

#include <stdio.h>
#include <time.h>
int main() {
long long sum = 0;
int n = 100000000; // 100 million integers

clock_t start = clock(); // Start time

for (int i = 1; i <= n; i++) {


sum += i;
}
clock_t end = clock(); // End time
double time_taken = (double)(end - start) / CLOCKS_PER_SEC; //
Convert to seconds

printf("Sum of first 100 million integers is: %lld\n", sum);


printf("Time taken: %.6f seconds\n", time_taken);

return 0;
}

OUTPUT:

CS330: Operating Systems Page 7


Task 2:

CODE:

#include <stdio.h>
#include <time.h>
#include <pthread.h>

#define N 100000000
#define THREADS 4

typedef struct {
long long start;
long long end;
long long sum;
} ThreadData;

// Function that each thread will run to compute the sum of its range
void* compute_sum(void* arg) {
ThreadData* data = (ThreadData*) arg;
data->sum = 0;

for (long long i = data->start; i <= data->end; i++) {


data->sum += i;
}

return NULL;
}

int main() {
pthread_t threads[THREADS];
ThreadData thread_data[THREADS];
long long total_sum = 0;

long long range_size = N / THREADS;

clock_t start = clock(); // Start time

// Create threads
for (int i = 0; i < THREADS; i++) {
thread_data[i].start = i * range_size + 1;
thread_data[i].end = (i == THREADS - 1) ? N : (i + 1) *
range_size;

CS330: Operating Systems Page 8


pthread_create(&threads[i], NULL, compute_sum,
&thread_data[i]);
}
for (int i = 0; i < THREADS; i++) {
pthread_join(threads[i], NULL);
}
for (int i = 0; i < THREADS; i++) {
total_sum += thread_data[i].sum;
}

clock_t end = clock(); // End time


double time_taken = (double)(end - start) / CLOCKS_PER_SEC;
printf("Sum of first 100 million integers is: %lld\n", total_sum);
printf("Time taken: %.6f seconds\n", time_taken);
return 0;
}

OUTPUT:

Single threaded code time: 0.48 s


Multi threaded code time: 0.63 s
Intuitively, the multithreaded approach should be faster since it splits the workload, reducing
complexity from O(n) to O(n/threads). However, in practice, the single-threaded version runs
faster for smaller inputs due to better cache efficiency, reduced memory contention, and no
thread management overhead. The overhead of thread creation and synchronization can slow
down performance, especially when the workload is not large enough to justify parallel
execution. However, as the input size grows significantly, the benefit of parallelism outweighs
the overhead, and the multithreaded approach will eventually outperform the single-threaded one
by distributing computations more efficiently across CPU cores.

CS330: Operating Systems Page 9

You might also like