Processes are the primitive units for alocation of system resources...Each process have their own address space and usually one thread of control. The process which makes another process is called the parent and the process which it makes is called a child process..As we know every process have its own pid(Process ID)...The parent process have 0 as pid while child process has a random pid like 2030 ,etc etc...
We'll be using a basic C program to demonsrate our article...
process.c
Explanation :-
First we declare a pid_t variable called pid this will...Actually pid_t is simply a define for unsigned_int...
Then there is a fork() call
Syntax
fork() call returns the pid of the created process , The Child.. or returns -1 on error...
fork() call makes a child process which again steps through the whole program...
Next we check whether the pid is zero (Meaning whether the process running is a child or not..)
if the process is a child then it prints 'I am a child' with the pid...
if the process is a parent then it prints 'I am the Parent'...
Compiling
Running
Now the output should be quite self-explanatory...And I hope you find it easy to understand...
Thats all for this article … Please stay tuned for more..
The Code
We'll be using a basic C program to demonsrate our article...
process.c
Code:
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid == -1)
{
printf("Error making Process!\n");
return(-1);
}
if(!pid) // if child process is running
{
printf("Hey I am Parent\n");
sleep(1);
}
else
{
printf("Hey i am the Child with PID : %d\n",pid);
sleep(1);
return(0);
}
}
First we declare a pid_t variable called pid this will...Actually pid_t is simply a define for unsigned_int...
Then there is a fork() call
Syntax
Code:
pid_t fork(void)
fork() call makes a child process which again steps through the whole program...
Next we check whether the pid is zero (Meaning whether the process running is a child or not..)
if the process is a child then it prints 'I am a child' with the pid...
if the process is a parent then it prints 'I am the Parent'...
Compiling
Code:
gcc process.c -o process
Code:
aneesh@aneesh-laptop:~/articles/C$ ./process Hey i am the Child with PID : 3847 Hey I am Parent
Thats all for this article … Please stay tuned for more..


