How to Create processes in Unix C

Discussion in 'C' started by lionaneesh, Feb 9, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    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..
     
  2. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks a ton for accepting...
     
  3. _st4ck3D*

    _st4ck3D* New Member

    Joined:
    Dec 7, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Another nice thread.
    +rated.
     
  4. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    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
     
  5. ashwani6508

    ashwani6508 New Member

    Joined:
    Feb 22, 2011
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    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 */
     
  6. alexsmth114

    alexsmth114 New Member

    Joined:
    Mar 7, 2011
    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    0
    Extremely useful post, should do a world of good to most of the users!!..
     
  7. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks..
    More articles coming up..
     
  8. humpakistani

    humpakistani New Member

    Joined:
    Mar 18, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.earnmoney.pk/
    agree with you according to the statement and nice one
     
  9. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    Code:
    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..
     
  10. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    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:
    #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...
     
  11. msdnguide

    msdnguide New Member

    Joined:
    May 14, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    msdnguide
    Location:
    Delhi
    Home Page:
    http://www.msdnguide.info/
    it is better to use vfork and then spawn. vfork does not creates a new process till the time we do spawn thereby saving system resources
     
  12. gandhisagar

    gandhisagar New Member

    Joined:
    Apr 17, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    one simple improvement I would like to have in this nice article .

    Code:
    #include<stdio.h>
    #include<unistd.h>
    
    /* include types.h for  unix system dependent types */
    #include <sys/types.h>
    
    /* include it for string operations */
    #include <string.h>
    
    char buf[1024] ;
    
    int main()
    {
    	pid_t pid;
    	pid = fork();
    	if(pid == -1)
    	{
    		sprintf(buf, "Error making Process!\n");
                    write(1,buf,strlen(buf));
    		return(-1);
    	}
    	if(!pid) 
    	{
    		sprintf(buf, "Hey this is parent , my pid is %d",getpid());
                    write(1,buf,strlen(buf));
    		sleep(1);
    	}
    	else
    	{
    		sprintf(buf, "Hey this is child , my pid is %d",getpid());
                    write(1,buf,strlen(buf));
    		sleep(1);
    		return(0);
    	}
    
        return 0 ;		
    }
    

    because printf() is "buffered," meaning printf() will group the output of a process together. While buffering the output for the parent process, the child may also use printf to print out some information, which will also be buffered. As a result, since the output will not be send to screen immediately, you may not get the right order of the expected result. Worse, the output from the two processes may be mixed in strange ways. To overcome this problem, you may consider to use the "unbuffered" write. .

    Code:
    #include <sys/types.h>
    pid_t is defined in it .


     
  13. gandhisagar

    gandhisagar New Member

    Joined:
    Apr 17, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Sir, vfork is generally used when you are expected to call "exec".
    and fork has "Copy on write" mechanism now so fork even doesn't duplicate address space until needed.

    Difference between the two is that, when vfork() is called, Parent leans it address space to Child and
    parent process is suspended, This continues until the child process either exits, or calls exec(), at which point the parent process continues.

    References :
    Unix Internals ... uresh vahliya


    "Jai Hind"
     
  14. gandhisagar

    gandhisagar New Member

    Joined:
    Apr 17, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Sir, vfork is generally used when you are expected to call "exec".
    and fork has "Copy on write" mechanism now so fork even doesn't duplicate address space until needed.

    Difference between the two is that, when vfork() is called, Parent leans it address space to Child and
    parent process is suspended, This continues until the child process either exits, or calls exec(), at which point the parent process continues.

    References :
    Unix Internals ... uresh vahliya


    "Jai Hindi"
     
  15. jaan

    jaan New Member

    Joined:
    Jun 11, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    very very Extremely useful post.....
     
    Last edited by a moderator: Jun 12, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice