Question: Threads ( ) In system programming, threads are the smallest unit of execution within a process. A thread is a sequence of instructions that can

Threads()
In system programming, threads are the smallest unit of execution within a process. A thread is a sequence of
instructions that can be scheduled and executed independently by the operating system's scheduler. Multiple
threads can exist within a single process, sharing the process's resources such as memory, file descriptors, and
global variables
Thread vs. Process:
Process: A process is an independent program in execution. It has its own memory space, system resources,
and execution context.
Thread: A thread is a lightweight execution unit within a process. Multiple threads within the same process
share the process's resources but have their own execution state (program counter, stack, and registers).
Types of Threads:
User Threads: Managed at the user level by libraries without kernel support. They are fast but can block the
entire process if one thread makes a blocking system call.
Kernel Threads: Managed directly by the operating system kernel. The kernel schedules them, and blocking
in one thread doesnt affect other threads in the process.
5
Program to create Threads in Linux
Syntax-
#include
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void arg);
pthread_t *thread:
Meaning: A pointer to a pthread_t variable, which will hold the thread identifier (ID) of the newly created thread. After
the thread is successfully created, this variable will store the ID of the new thread.
Usage: You pass the address of a pthread_t variable, like &thread, where thread is declared as pthread_t thread;.
const pthread_attr_t *attr:
Meaning: A pointer to a thread attribute object (pthread_attr_t), which specifies the thread's attributes (e.g., whether the
thread is joinable, stack size, scheduling policies). You can specify attributes such as whether the thread is detached or
joinable.
Usage: If you want to use default thread attributes, pass NULL. If you need specific attributes, create and initialize a
pthread_attr_t object
6
void *(*start_routine)(void *):
Meaning: A pointer to the function that the thread will execute. This function should take a void* argument and
return a void*. This function is called the "start routine," and the thread begins executing at this point.
Usage: You pass the name of the function you want the thread to run.
void *arg:
Meaning: A pointer to the argument that will be passed to the start_routine function. This is how you provide data
to the thread function when the thread starts running.
Usage: You can pass any type of data (e.g., integer, structure) by casting it to void*. If you don't need to pass an
argument, you can pass NULL.
Note: To compile any program which involves creation of thread(s) use pthread library (lpthread)
Suppose the program is named Thread.c, then to compile write
$gcc Thread.c -lpthread
To run the command remains same
$./a.out
7
Thread() Example-1
#include
#include
#include
#include
void *thread_function(void *arg);
int i,j;
int main(){
pthread_t a_thread; //thread declaration
pthread_create(&a_thread, NULL, thread_function, NULL);
//thread is created
pthread_join(a_thread, NULL); //process waits for thread to finish .//Comment this line to see the
difference
printf("Inside Main Program
");
for(j=20;j<25;j++)
{
printf("%d
",j);
sleep(1);
}
}
8
void *thread_function(void *arg){
// the work to be done by the thread is defined in this
function
printf("Inside Thread
");
for(i=0;i<5;i++)
{
printf("%d
",i);
sleep(1);
}
}
Example 2, Thread returns a value to main()
#include
#include
#include
#include
#include
void *thread_function(void *arg);
int i,n,j;
int main(){
char *m="5";
pthread_t a_thread; //thread declaration
void *result;
pthread_create(&a_thread, NULL, thread_function, m); //thread is
created
pthread_join(a_thread, &result);
printf("Thread joined
");
for(j=20;j<25;j++)
{
printf("%d
",j);
sleep(1);
}
printf("thread returned %s
",(char *)result);
}9
void *thread_function(void *arg){
int sum=0;
n=atoi(arg);
for(i=0;i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!