Process Management and Threads

Process Management and Threads

In the last article we were discussing about the exec() family tree. In this article we will continue with the exec() system calls along with the basic concepts of threads in operating system.

1. execlp

In execlp() we send the $PATH of the file that we want to execute as the first argument followed by the name of the file and the arguments that are required in the file in the form of a list.

Article content

here we can see that we have imported some header files, lets go through them, stdlib.h imports functions like exit(), unistd.h imports system calls like execlp() and fork(), sys/wait.h imports functions like wait().

Now here the first argument that is passed in the execlp() is the path of the file that we want to execute, second is name of the file along with the arguments, this call does not take environment variable as argument.

Article content

This is the output after the execution.

2. execv()

In execv() we pass arguments in the form of an array, we will declare an array first and pass all of our arguments here and then pass it to the execv() system call. In the execv we will take the path first and then the array that we created.

Also we do not pass environment variable in it.

Article content
Article content

3. execvp()

Here the argument is an array of pointers to null terminated strings, here the first argument is the pointer to the filename and the rest are stored in an array of pointers just like the execv() system call.

Article content

Note the main difference between execv() and execvp() system calls is the first argument in them in execvp(), it searches for $PATH, here we can also execute any type of command but not in execv() it requires a relative or absolute location for it.

4. execvpe()

It is just like execvp() system call but we can also pass environment variable as an argument here. Here we will make two array of characters one for exnironment variable and one for arguments and pass them one by one.

Article content
Article content

These are all of the system calls from exec() family tree.

Threads

A Thread is the basic unit of a process. Threads share the same memory as the process but they work independently to handel different tasks or same tasks efficiently.

Whenever a process is created, it also creates a main thread in it. The main thread executes the program line by line. All of the instructions of the process is stored in the main thread.

Threads are mainly created to distribute or share the workload of a single process which also reduces the runtime of a program.

Threads works in two types of processing:

  1. Parallel Processing: In this processing, All the cores or multiple cores of the operating system will be running and multiple cores will be executed simultaneously.
  2. Concurrent Processing: In this processing, only 1 core will be running and context switching will keep happening in this one core.

Article content

Here pthread.h is a library which includes data type like pthread_t which intializes the thread, and functions like pthread_create which is used to create a thread and pthread_join which is used to complete the newly created thread before the execution of the main thread.

In pthread_create we have passed the arguments as the address of thread1 and the address of a pointer function helperOps .

Article content

Here we got the output as "Hello World" printed but if we comment or remove the line in which we use pthread_join then there are possibilities that helperOps function may not get executed.

Why? Because the main thread gets executed first before the thread1 and for the same reason that is to complete the execution of thread1 we use the function pthread_join(). It blocks the main thread until the specified thread gets executed.

After all this one more question arises, If we can do work with one core than why the need for more than one core? The answer is context switching in one core only gives the impression of more than one cores where it only stores the state in which the program was left before context switching and execution means the program stops there in that state which is hugely ineffecient if we are talking about multiprocessing on the other hand in parallel processing all the cores works and it will make it very fast.

Here is another example:

Article content

In this example the helperOps function is a counter to 1 million and there are two threads made namely thread1 and thread2 are initialized and created and also they are joined which means both threads will get completed. So what should be the expected output. Is it 2 million or something else. Let's see

Article content

While the answer is far from 2 million it is verymuch nearby 1 million. But why?

Will try answering this in the next article, please suggest feedback and improvements.Thanks Shubham Kumar for these great insights and Kalpavriksha Edu for having me. I'll be posting the soon again.


To view or add a comment, sign in

Others also viewed

Explore content categories