Hello and welcome!
In this video session we will discuss on the topic of threads in Linux.
We will have a introduction of Posix threads.
How do we create a thread?
What is a thread id and how do we terminate a thread?
So before starting the threads we discuss or on the Posix standards of threads.
So the Posix threads are called as p threads.
In Linux, Posix is nothing but a portable operating system interface, and this is a standard set by
IEEE, IEEE and the different operating system who make use of the threads have to follow the standards
that is set by the committee.
Now let's see the syntax.
How to create a thread p thread underscore create is the function call that is used to create a thread
within the process.
Threads are created within the process right?
We need to include the periods and Pthreadcreate is used to create the thread as mentioned.
And it takes the following parameters.
Thread and the attributes and pointer to a function and the arguments.
We will discuss the parameters used here when we discuss the program.
If pthreadcreate is successful, it returns a zero.
In case it does fail, it returns a minus one and it sets the error number.
So to see the general syntax pthreadcreate, it takes the thread which is nothing but of type pthread
underscore t, and this holds the thread id when the thread is created, and the second parameter usually
is used to send the set the attributes of the thread, and it can be null if we want some default attributes,
and this will be the function pointer, or.
This is the start of the function to which it jumps when the thread starts to execute.
And these are the arguments that are passed to this function.
And if the return is not equal to zero, that means the thread is not created.
Now let's see how to compile the program which has the threads in it.
So in order to compile the program, we have to specify the option minus LP thread.
By providing a minus LP thread option, we are informing the GCC to link the program with the thread
library.
Now let's see the thread IDs.
Each threads that are created in the process has a unique ID, and this id is used by the process or
the main thread to identify the different threads.
The thread id has its own specific application.
Now when we create the thread using the pthreadcreate.
For example, in this function, the first parameter is a pointer to the thread underscore t object.
When the thread is when the pthreadcreate is successfully created a thread.
This parameter.
Contains the thread id of the newly created thread.
So the main thread makes use of this and gets the thread id of the newly created thread.
In the same manner, the thread which has newly created also can get its own thread id by calling the
function pthread underscore self.
This pthread underscore self gives the thread id of its own thread id, which will be discussed in next
sessions.
In the next coming sessions.
And now we can also terminate the threads.
So the termination of the thread can be done in one of the following ways.
That is like when the thread start function calls a return with some values.
The thread is terminated and also when the thread calls pthread underscore exit, the thread will be
terminated.
The thread is canceled using Pthread cancel.
Then also the thread is terminated.
We will see what what is Pthread can cancel next.
Also, when the main function returns by calling a return from Maine.
All the threads belonging to that process will be terminated immediately.
P thread exit is specifically used inside the thread to terminate the thread, and the syntax of p thread
exit is.
We need to include a p h header file and p thread exit.
It takes a void start.
Parameters.
This is nothing but the return value that we can pass on to the other thread, or the main main thread
which has created the thread.
So this return value is being collected by the main thread, and when p thread underscore exit is called,
the thread is exited.
Also, if the main thread calls p thread exit, then the main thread alone is exited and all the other
threads that is created by the main thread will continue to execute.
We will see this example in the next program.
So one point to be remembered here is when a program runs, it has a process in memory.
And before creating any of the threads, the process itself is called as the main thread or the initial
thread, and the main thread creates the other threads, and the main thread has all the thread ids
and the p thread underscore exit can be called from different threads, and using the thread exit,
the threads exit their execution and they can return the value to the main thread.
Now let's see the examples in order to understand the concepts.
Now let's see a program to demonstrate how to create a thread.
In this program we include all the required header files and.
The execution begins from the main.
And here we create a thread before creating a thread.
The process is having no threads and the process itself is called as the main thread or the init thread,
and at this point it creates a new thread and the first parameter passed is of type p thread underscore
t, and we need to pass the address of the variable TT1 and the second parameter.
We are passing a null as we want the default attributes, and the third parameter is the pointer to
the function, which will be called as soon as the thread is scheduled.
And the fourth parameter is the arguments that will be passed on to the function.
Now if pthreadcreate is successful, it returns zero.
In other case, it returns a non-zero and we print the error.
And further, we print the process id of the main thread and then sleep for five seconds and later exit
the main thread.
So when the thread is created now, it begins its execution from this function that is defined here,
and the argument that is passed from the main thread is received as a void star, and then converted
to passed to the care star.
And now the star is a care star variable.
And here we print the argument passed to the thread.
Thread is str, so str holds The pointer to the variable.
Hello world!
And later the thread prints the process id and sleeps for two seconds and returns returns.
So now before executing this code we would share some things.
So here we have one process in memory.
When we run this program, we will be having one process in memory.
And the process has its own virtual memory.
And the thread that is created from the process will share the virtual memory of the process.
Now let's end the both the the thread and the main thread process ID will be same.
Now let's compile the program.
So here, in order to compile the pthreads we need to include.
We need to have option minus LP.
Thread This links the Pthread library and we are generating the output file here.
Now we have generated the output file.
Now let's run the program.
So here we can see that the main thread has printed its process ID.
And this line is coming from the thread function.
And this is the parameter that we have passed to the thread from the main thread.
So this hello world is what we are passing from the main thread to the thread that is being created
and that is printed here.
And if you observe the process ID of the uh, main thread and the process id in the created thread or
same, that means the thread that is created is part of the main, uh, it's part of the Process and
now the thread exits and later the main thread also exits.
Now let's see the second example.
In this example we will see how to use the exit function p thread exit.
So p thread exit is a function used to terminate the thread.
So in this case this function.
This program is similar to the previous program with the difference that we are using p thread exit
to return from the thread.
So in this case, the execution of the process begins from the main and at this point it creates a thread.
Pthreadcreate creates a new thread and the execution of the new thread begins from this thread func.
And the same hello world arguments are passed on here.
Now the main thread sleeps for five seconds.
Meanwhile, the.
The thread that is created sleeps for 15 seconds.
Here, if you see, there is one point to be noted.
In this case, the main thread is calling exit and the thread that is created is calling a thread exit.
So as we have discussed in one of the slides that when we call exit from the main thread, then it kills
the thread that is being created.
Let's compile this program and see the output.
Now we have compiled the program.
Let's run the program.
Before running the program, let's see the process here.
List of process.
So we don't have any process running with the two.
And score three it creates.
Now let's run this program.
Now we have run this.
Now let's see the process.
Now what what has happened is we can see here in this case.
And in this case there is a process created with the process ID 16679.
Then, then after sleeping for five seconds the main thread has called exit zero.
So as a result, even though the thread that is created was sleeping for 15 seconds was not, it performed
a thread exit.
So because of because the main thread had called exit, this thread that is created will also be terminated.
So this is the reason we can see here of the five seconds the process itself has terminated.
Now let's do a small change in the program.
So instead of calling exit, let's call a pthread exit in the main thread.
So now in the main thread we are calling a Pthread exit.
Also, in the thread that is created, we are calling a Pthread exit.
Now the behavior would be as follows.
Let's recompile this program once again.
Now before running, let's uh.
Before running, let's see what can be the output of this.
So when we run the program, uh, the main process creates a thread here.
And after the thread has been created, the thread sleeps for 15 seconds.
And meanwhile the main thread sleeps for five seconds, and then it exits by calling p thread exit.
But still, the thread that is created is not yet exited, and after completing 15 seconds, it executes
Pthread exit and then exits the, uh, thread.
Let's have a look into this.
Now let's run this.
Now let's run this as well.
So here what we are seeing is there is a defunct here.
Defunct means now the main thread has exited, but the still the thread that is created is still executing.
So let's rerun it once again.
So now if you run here now the thread is 16772.
Process ID is 16772 and after five seconds the main thread has exited.
But still the thread that we have created is still running.
It it runs for 15 seconds and then exits the program.
Let's see once again the output.
Now we have the thread running, so still it is running.
Then after five seconds the main thread exits.
Since the calling of p thread exit, the thread that we have created is still running and it exits only
after 15 seconds.
And now the thread has exited by calling p thread exit.
So the point to be noted here is if the main program exits by calling exit, then the complete process
and all the thread associated with this process will be terminated.
But if the main thread calls a p thread exit, then only the main thread will be terminated and all
other threads will still continue to run until the until it returns from the function.