Lab 4 - Threads Creation BY 22BAI1324
Lab 4 - Threads Creation BY 22BAI1324
REG NO : 22BAI1324
CODE :
OUTPUT :
2. Create two threads and display the two different function (
Addition/ Odd or even ete.,) along with the corresponding
thread_id. (pthread_self() function returns thread id)
CODE :
OUTPUT :
3. Design two threads to count the vowels and consonants either
from text file or a given string
CODE :
OUTPUT :
4. Write a program to sum up an array of 1000000 elements where
elements are consecutive natural numbers..
CODE :
OUTPUT :
.
(ii) Create two or four threads for splitting the sum and find the
execution time
CODE :
OUTPUT :
(iii)Analyse and comment on the execution time and justify the
need of multithreaded programming.
By using multithreading, you can distribute the workload among multiple threads,
allowing for concurrent execution and potentially reducing the overall execution time. In
the case of summing up an array of 1,000,000 elements, splitting the task into two or
four threads can lead to improved performance.
The execution time of the multithreaded program will depend on various factors,
including the number of available CPU cores, the efficiency of the threading
implementation, and the size of the array. In general, multithreading can provide
performance benefits when the workload can be divided into independent tasks that can
be executed concurrently.
However, it's important to note that the benefits of multithreading may not always be
significant or even present. In some cases, the overhead of thread creation,
synchronisation, and communication can outweigh the performance gains. It's crucial to
analyse the specific requirements and characteristics of the problem at hand to
determine if multithreading is a suitable approach.