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.
Program to create Threads in Linux
Syntax
#include
int pthreadcreatepthreadt thread const pthreadattrt attr void startroutinevoid void arg;
pthreadt thread:
Meaning: A pointer to a pthreadt 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 pthreadt variable, like &thread, where thread is declared as pthreadt thread;.
const pthreadattrt attr:
Meaning: A pointer to a thread attribute object pthreadattrt which specifies the thread's attributes eg 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
pthreadattrt object
void startroutinevoid :
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 startroutine function. This is how you provide data
to the thread function when the thread starts running.
Usage: You can pass any type of data eg 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 threads use pthread library lpthread
Suppose the program is named Threadc then to compile write
$gcc Thread.c lpthread
To run the command remains same
$aout
Thread Example
#include
#include
#include
#include
void threadfunctionvoid arg;
int ij;
int main
pthreadt athread; thread declaration
pthreadcreate&athread, NULL, threadfunction, NULL;
thread is created
pthreadjoinathread, NULL; process waits for thread to finish Comment this line to see the
difference
printfInside Main Program
;
forj;j;j
printfd
j;
sleep;
void threadfunctionvoid arg
the work to be done by the thread is defined in this
function
printfInside Thread
;
fori;i;i
printfd
i;
sleep;
Example Thread returns a value to main
#include
#include
#include
#include
#include
void threadfunctionvoid arg;
int inj;
int main
char m;
pthreadt athread; thread declaration
void result;
pthreadcreate&athread, NULL, threadfunction, m; thread is
created
pthreadjoinathread, &result;
printfThread joined
;
forj;j;j
printfd
j;
sleep;
printfthread returned s
char result;
void threadfunctionvoid arg
int sum;
natoiarg;
fori;i
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
