How to Create processes in Unix C

lionaneesh's Avatar author of How to Create processes in Unix C
This is an article on How to Create processes in Unix C in C.
Rated 5.00 By 2 users
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...

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);
	}
}
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
Code:
pid_t fork(void)
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

Code:
gcc process.c -o process
Running

Code:
aneesh@aneesh-laptop:~/articles/C$ ./process 

Hey i am the Child with PID : 3847

Hey I am Parent
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..
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Thanks a ton for accepting...
_st4ck3D*'s Avatar, Join Date: Dec 2010
Newbie Member
Another nice thread.
+rated.
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by _st4ck3D* View Post
Another nice thread.
+rated.
Thanks again...
And if you really wanna rate me..
Please press the thanks button on the bottom of where the article ends..
That would really increase my rating and thus , make me more motivated to post such articles.. Thanks
ashwani6508's Avatar, Join Date: Feb 2011
Light Poster
UNIX implements through the fork() and exec() system calls an elegant two-step mechanism for process creation and execution. fork() is used to create the image of a process using the one of an existing one, and exec is used to execute a program by overwriting that image with the program's one. This separation allows to perform some interesting housekeeping actions in between, as we'll see in the following lectures.

A call to fork() of the form:

#include <sys/types.h>

pid_t childpid;
...
childpid = fork(); /* child's pid in the parent, 0 in the child */
alexsmth114's Avatar, Join Date: Mar 2011
Go4Expert Member
Extremely useful post, should do a world of good to most of the users!!..
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by alexsmth114 View Post
Extremely useful post, should do a world of good to most of the users!!..
Thanks..
More articles coming up..
humpakistani's Avatar
Light Poster
agree with you according to the statement and nice one
teacher's Avatar, Join Date: Mar 2011
Contributor
Code: cpp
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);
    }

my dear this code is wrong..
teacher's Avatar, Join Date: Mar 2011
Contributor
Quote:
fork() call makes a child process which again steps through the whole program...
This statement is also wrong.

The child process again do not step through the whole program.If it does then you have infinite fork() calls.

The child process starts its execution after the fork() statement like
Code: cpp
#include <stdlib.h>
int main(){
printf("hello world\n");
fork();
/ * newly created child process will begin its execution from here */
printf("hello another world\n");
}

feel free to ask any doubts...