Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment 6

Creation of Multithreaded Processes using Pthread Library

Aim: Introduce the operations on threads, which include initialization, creation,


join and exit functions of thread using pthread library.

Lab Exercise(s):
1. Write a program using pthread to concatenate the strings, where multiple
strings are passed to thread function.

Output:
2. Write a program using pthread to find the length of string, where strings
are passed to thread function.

Output:

3. Write a program that performs statistical operations of calculating the


average, maximum and minimum for a set of numbers. Create three
threads where each performs their respective operations.
Output:
4. Write a multithreaded program where an array of integers is passed
globally and is divided into two smaller lists and given as input to two
threads. The thread will sort their half of the list and will pass the sorted
list to a third thread which merges and sorts the list. The final sorted list is
printed by the parent thread.
Output:

Viva Questions:
1. Provide two examples in which multithreaded process provide an
advantage over single threaded solution.
1) A web server that services each request in a separate thread.
2) 2) A web browser with separate threads playing sound,
downloading a file, collecting user input, etc.

2. What resources are used when a thread is created.


Since a thread is a part of the process, no additional resources are used
when a thread is created, instead, it shares the memory space of the
process from which this particular thread has been created.

3. What is the syntax for creating a thread.


First, you can create a thread using the thread class (extend syntax).
This provides you with constructors and methods for creating and
operating on threads. The thread class extends the object class and
implements a runnable interface.

4. What is the use of pthread_join().


The pthread_join() function waits for a thread to terminate, detaches
the thread, then returns the threads exit status. If the status parameter
is NULL, the threads exit status is not returned.
5. What is the use of pthread_exit().
The pthread_exit() function terminates the calling thread, making its
exit status available to any waiting threads. Normally, a thread
terminates by returning from the start routine that was specified in the
pthread_create() call which started it.

6. Which pthread function passes the value from thread function to main
function.
The pthread_create() routine permits the programmer to pass one
argument to the thread start routine.

You might also like