How to Create a Process in Linux
The Process is an instance of a program code which executes within the operating system. The Process is nothing but carry out of the task. Refer the previous tutorial Process in Linux. This tutorial will explain how to create a process in Linux.
Fork () system call
The process is created by fork () system call. Fork () creates a new process from the existing process. The existing process from which function called is known as parent process and newly created process is known as child process. Child process has its own process ID. Fork ( ) takes no argument and return process ID. If successfully fork ( ) system call run, it returns twice.
System call fork ( ) returns negative value if the process is not created.
System call fork ( ) returns zero if child process is created.
System call fork ( ) returns a child process ID and a positive value if it returns from parent process.
The following error may be occurring in fork ( ) system call:
EAGAIN – System cannot duplicate parent’s page table and task structure to child process due to limitation of sufficient memory.
EAGAIN – The child process does not possible create due to limitation of caller’s resource allocation to child process.
ENOMEM – fork ( ) failed to allocate the necessary kernel structures because memory is tight.
ENOSYS – fork ( ) is not supported on this platform (for example, hardware without a Memory-Management Unit).
System call fork ( ) duplicate the same address space of the parent process and allocate to the child process. The Child process does not inherit timer and semaphore adjustment from parent process. Resource initialization and timer counter is reset to zero in the child process. The Child process copy the task structure and descriptor of the parent process. Fork ( ) system call is located in <sys/types.h> library.
System call getpid ( ) returns the Process ID of the current process and getppid ( ) returns the process ID of the current process’s parent process.
Let’s see how to create child process using fork ( ) system call.
//********************************Process.c*********************************//
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main( )
{
pid_t child_pid;
child_pid = fork ( ); // Create a new child process;
if (child_pid >= 0)
{
if (child_pid == 0)
{
printf (“child process successfully created!!n”);
printf (“child PID = %d, parent PID = %dn”, getpid( ), getppid( ) );
exit(0);
}
}
else
{
perror(“fork”);
exit(0);
}
}
//********************************Process.c*********************************//
Save the file in a directory and compile it. After successful compilation, run the executable file from command terminal. You should refer the tutorial How to make first C program in Linux if you not aware of compilation and execution process.
pid_t is a data type which represents the process ID. It is created for process identification. Each process has a unique ID number. Next, we call the system call fork ( ) which will create a new process from calling process. The Calling function is a parent process, and a new process is a child process. The system call fork ( ) is returns zero or positive value if the process is successfully created.
If the system call fork ( ) return a Zero, following output id display on command terminal:
child process successfully created!!
child PID = 11457, parent PID = 2291
Here perror ( ) produce the message of error occurrence from the specific system call. It gives the error information if process is not created successfully.
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.