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

Lab 10

The document describes a lab on multithreading in Linux. It provides details about the course, lab title, duration, OS used, and objective of implementing multithreading in UNIX. It then discusses threads, multithreading, and basic thread routines like creation, termination, detach and join, self and equal. It provides examples of multithreading programs in C using pthreads. The lab tasks section asks students to write a program that sorts an array in ascending order using bubble sort and descending order using selection sort with multithreading.

Uploaded by

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

Lab 10

The document describes a lab on multithreading in Linux. It provides details about the course, lab title, duration, OS used, and objective of implementing multithreading in UNIX. It then discusses threads, multithreading, and basic thread routines like creation, termination, detach and join, self and equal. It provides examples of multithreading programs in C using pthreads. The lab tasks section asks students to write a program that sorts an array in ascending order using bubble sort and descending order using selection sort with multithreading.

Uploaded by

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

LAB 10

Summary

Items Description
Course Title Operating System
Lab Title Multithreading
Duration 3 Hours
Operating System Linux Operating System
/Tool/Language
Objective Implement the multithreading in UNIX

Threads
A thread is a single sequence stream within in a process. Threads have some of the properties of
processes; they are sometimes called lightweight processes. In a process, threads allow multiple
executions of streams. In many respect, threads are popular way to improve application through
parallelism. The CPU switches rapidly back and forth among the threads giving illusion that the
threads are running in parallel. Like a traditional process i.e., process with one thread, a thread can
be in any of several states (Running, Blocked, Ready or Terminated). Each thread has its own
stack. Since thread will generally call different procedures and thus a different execution history.
This is why thread needs its own stack. An operating system that has thread facility, the basic unit
of CPU utilization is a thread. A thread has or consists of a program counter (PC), a register set,
and a stack space. Threads are not independent of one other like processes as a result threads shares
with other threads their code section, data section, OS resources also known as task, such as open
files and signals.

Multithreading is the ability of a program or an operating system process to manage its use by
more than one user at a time and to even manage multiple requests by the same user without having
to have multiple copies of the programming running in the computer. Each user request for a
program or system service (and here a user can also be another program) is kept track of as
a thread with a separate identity. As programs work on behalf of the initial request for that thread
and are interrupted by other requests, the status of work on behalf of that thread is kept track of
until the work is completed.

Thread Programming:

Some of the basic thread routines are as follows:

Creation:
int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *),
void *arg);

o Analogous to a combined fork and exec routine


o Returns a thread id in thread.
o When attr is NULL the default thread attributes are used.

Pthread_t: It is defining a thread pointer. When a thread is created identifier is written into the
variable to which the pointer points. This identifier helps to refer to thread.

Pthread_attr_t: It is used to set the thread attributes. If attr is NULL, the default attributes are used.

Name of function: The name of the function to be started by the thread for execution.

Arguments to be passed to the function: When a new thread is created it executes the function
pointed by the function variable name.

On success, pthread_create ( ) returns 0, and on error, it returns an error number.

Termination
void pthread_exit(void * return_value);

o Analogous to exit
o The exit routine kills all threads and exits the process
o If the current thread is the last thread then the process terminates
o Returning from the start_routine is equivalent to calling pthread_exit
o Returning from the initial thread main is the equivalent to calling exit

Detach and Join


int pthread_detach(pthread_t thread);
int pthread_join(pthread_t thread, void ** status);

o Analogous to wait
o Must specify thread. There is no wait any.
o Current thread blocks until thread terminates
o The return value of thread is returned in status
o All threads must be detached or joined with.

pthread_join: It is used to wait for the thread represented in the thread_join call. It waits for the
thread represented in the call to finish executing. It waits for the specified thread to complete, and
gathers information about the thread's exit status.

Self and Equal


pthread_t pthread_self(void);
int pthread_equal(pthread_1 t1, pthread_t t2);

Example:

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

void *msg_function(void *ptr);

main()
{
pthread_t thread1, thread2;
char *msg1 = "I'm Thread1";
char *msg2 = "I'm Thread2";
int t1, t2;

/* create independent threads each of which will execute function */

t1 = pthread_create(&thread1, NULL, msg_function, (void*)msg1);


t2 = pthread_create(&thread2, NULL, msg_function, (void*)msg2);

/* Wait till threads are complete before main continues. Unless we wait we run the risk of executing
an exit which will terminate the process and all threads before the threads have completed. */

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
/* value return by the threads */

printf("Thread 1 returns: %d\n", t1);


printf("Thread 2 returns: %d\n", t2);
exit(0);
}

void *msg_function (void *ptr)


{
char *msg;
msg = (char*) ptr;
printf("%s\n", msg);
}

Compile: gcc o exp exp.c -pthread


Run: ./a

Example 2:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *msg_function(void *ptr);

main()
{
pthread_t thread1, thread2;
char *msg1 = "I'm Thread1";
char *msg2 = "I'm Thread2";
int t1, t2;

t1 = pthread_create(&thread1, NULL, msg_function, (void*)msg1);


t2 = pthread_create(&thread2, NULL, msg_function, (void*)msg2);

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

printf("Thread 1 returns: %d\n", t1);


printf("Thread 2 returns: %d\n", t2);
exit(0);
}
void *msg_function (void *ptr)
{
char *msg;
int i;
msg = (char*) ptr;
for (i=0; i<5; i++)
{
printf("%s - %d\n", msg,i);

/* sleep() causes the current thread to suspend execution for a specified period. This is an efficient
means of making processor time available to the other threads of an application or other
applications that might be running on a computer system. */

sleep(1);
}
}

LAB TASKS
o Write a program that sorts the array in ascending order using bubble sort and descending
order using selection sort by means of multithreading.

Note:
Program should prompt user to enter the size and values of each array.
Code should self explanatory.

Hint:

Ascending Order: From smallest to highest


Descending Order: From highest to smallest

Pseudo code for Bubble sort:

numElements = number of structures to be sorted


for ( inx = 0 ; inx < numElements - 1 ; ++inx )
for ( jnx = numElements - 1 ; jnx != inx ; --jnx )
if ( element( jnx ) < element( jnx - 1 ) )
swap( element( jnx ), element( jnx - 1 ) )

Pseudo code for Selection sort:

numElements = number of structures to be sorted


for ( inx = 0 ; inx < numElements - 1 ; ++inx )
least = inx
for ( jnx = inx + 1 ; jnx < numElements ; ++jnx )
if ( element( least ) > element( jnx ) )
least = jnx
swap( element( least ), element( inx ) )

Modify the pseudo code according to ascending and descending order

You might also like