C Programming

Pthread_create 3 C Function

“Inside a process, a thread is a small channel flow. Because they share many of the properties of processes, threads are often considered lightweight operations. Because threads, with the exception of processes, are not completely self-contained, they share their scripts, data, and operating system features like open files and signals with other threads. However, a thread, like a task, does have its program counter (PC), registry setting, and stack memory. Several sessions inside a browser, for instance, might represent distinct threads.”

MS Word uses many threads, one for textual formatting, another one for processing inputs, and so on. Within this article, we will let you know about the use of the pthread_create 3 C function to create a thread using the Kali Linux system. Let’s get started within the shell of Kali Linux now. We need to create a new C file within which we will be creating a C program for threads. So, we can be using the touch instruction to create with the title “create thread” and “.c” extension at its end. The file has been created successfully as per the list of “ls” instructions and shows the contents of a current working directory.

To open this file in a Vim editor to create code, we have been using the “vim” instruction as shown below. The command must contain the full name of a file along with the “vim” keyword. The empty vim editor will open your C file successfully.

Example 01

It’s time to create an example for the pthread_create function to make a new thread within the C script. For this, we will be starting our C code with the main standard libraries included with the “#include” keyword. For standard input and output flow within the program, we need the “stdio.h” library along with the “stdlib.h” and “unistd.h” library.

Along with that, we have been using the “pthread.h” library of C to make use of thread functions in our C code. Without this library, the use of pthread functions might cause errors. We have initialized a global variable “global” and assigned it a value “0” at the start of the code after the headers. This variable will be utilized in the rest of the code.

Let’s get started with the explanation of this code with the main() function of this code. The main thread has been declaring a variable “var” and “threadId” variable for the thread using the keyword “pthread_t.” The “For” loop of 5 iterations has been used to create a thread 5 times with the use of the “pthread_create” function within it. We have been passing it a “threadId” variable to count the thread number and passing it a “func” thread function within it with the same “threadId” variable pointer.

After the creation of 5 threads using the “for” loop, we will be out of the loop, and the “pthread_exit” function will be used to exit the thread’s executions and terminate the program. Now, let’s take a look at the thread pointer function “func,” taking thread “ID” from the main() method to title itself accordingly. The argument “id” passed by the main() would be saved to the pointer “threadId” of integer type. A static integer variable “var” is initialized to 0, and we have been post incrementing the value of variables “var” and “global.”

Due to post-increment, the value would be incremented first, i.e., “ID,” and then saved to the variable. The printf statement is here to display the Thread id, static variable value, and global variable value at a time. The values for threads would be 2, 4, 6, 8, and 10. Let’s just save our code and see what happens after the compilation.

Firstly, we will be compiling this C code with the “gcc” compiler along with the “-lpthread” command for the Linux thread function containing the “-o” option to create the execution file “createThread.out,” as shown below. The file has been compiled and created successfully.

Let’s run the compiled error-free execution file with the “./” command as shown below. The outputs of 5 threads have been displaying the thread ID, static variable value, and global variable value with the increment of 2.

Example 02

Within this example, we will be using the same format of code. Starting from the main() method, we have created two integer variables and a “thread_Id” variable with the “pthread_t” keyword. The “threadId” variable has been taking information of a main() method using the “pthread_self()” function and passing it to the “func” function that has been used to create a new thread via the “pthread_create()” function. Whatever the newly created thread would return will be saved to the variable “createReturn” and checked within the “if” statement. If some error occurs, it will use the printf statement to display that, and the program will be terminated. Otherwise, after the creation of a thread, the program sleeps for 1 second and uses the printf statement to display that the thread has been successfully created.

After that, the program will be terminated. The func() thread function will display its thread information using the pthread_self() function and the data of the main() function passed by argument as shown using the printf function.

We have compiled this file and created an execution source code file with the “Gcc” instruction.

After execution, we have got the information Id of the func() function and the main() thread as well.

Each time, the execution of this program will generate different data for both threads.

Conclusion

Threads are a common approach to promoting synchronicity in applications. Threads run quicker than tasks for a range of factors, i.e., thread generation is a lot quicker, context switching among threads seems to be much quicker, thread termination is so much easier, and thread interaction is significantly faster. Within this article, we have discussed the use of the pthread_create function along with some other thread functions to display that. We hope you like it.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.