Creation of a separate process using fork () system call.Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call. A child process uses the same pc(program counter), same CPU registers, same open files which use in the parent process.
It takes no parameters and returns an integer value. Below are different values returned by fork().
Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of newly created child process.
Program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
printf("Hello world…….!!!\n");
return 0;
}
Output:
Hello world…….!!!
Hello world…….!!!
Displaying process id using fork() system call
Each process is named by a process ID number, a value of type pid_t. A process ID is allocated to each process when it is created. Process IDs are reused over time. The lifetime of a process ends when the parent process of the corresponding process waits on the process ID after the process has terminated. A process ID uniquely identifies a process only during the lifetime of the process. As a rule of thumb, this means that the process must still be running. Process IDs can also denote process groups and sessions.
On Linux, threads created by pthread_create also receive a thread ID. The thread ID of the initial (main) thread is the same as the process ID of the entire process. Thread IDs for subsequently created threads are distinct. They are allocated from the same numbering space as process IDs. Process IDs and thread IDs are sometimes also referred to collectively as task IDs. In contrast to processes, threads are never waited for explicitly, so a thread ID becomes eligible for reuse as soon as a thread exits or is cancelled. This is true even for joinable threads, not just detached threads. Threads are assigned to a thread group.
Data Type: pid_t
The pid_t data type is a signed integer type which is capable of representing a process ID.
Function: pid_t getpid (void)
The getpid function returns the process ID of the current process. Function: pid_t getppid (void)
The getppid function returns the process ID of the parent of the current process.
Function: pid_t gettid (void)
The gettid function returns the thread ID of the current thread. The returned value is obtained from the Linux kernel and is not subject to caching.
#include<stdio.h>
#include<unistd.h>
void main()
{
pid_t p;
printf("pid of main program:\t%d\n", getpid());
p = fork();
if(p == 0)
{
printf("In child process,\tpid:\t%d,\tppid:\t%d\n", getpid(),
getppid());
}
else
{
printf("In parent,\t \tpid: \t%d, \tfork returned:\t %d\n", getpid(), p);
}
}
Output
pid of main program: 4717
In parent, pid: 4717, fork returned: 4718 In child process, pid: 4718, ppid: 4717