Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   How to Create processes in Unix C (http://www.go4expert.com/articles/create-processes-unix-c-t24922/)

lionaneesh 10Feb2011 01:09

How to Create processes in Unix C
 
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 12Feb2011 10:56

Re: How to Create processes in Unix C
 
Thanks a ton for accepting...

_st4ck3D* 12Feb2011 15:29

Re: How to Create processes in Unix C
 
Another nice thread.
+rated.

lionaneesh 12Feb2011 15:31

Re: How to Create processes in Unix C
 
Quote:

Originally Posted by _st4ck3D* (Post 79183)
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 22Feb2011 18:13

Re: How to Create processes in Unix C
 
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 11Mar2011 10:21

Re: How to Create processes in Unix C
 
Extremely useful post, should do a world of good to most of the users!!..

lionaneesh 11Mar2011 10:22

Re: How to Create processes in Unix C
 
Quote:

Originally Posted by alexsmth114 (Post 80537)
Extremely useful post, should do a world of good to most of the users!!..

Thanks..
More articles coming up..

humpakistani 18Mar2011 16:40

Re: How to Create processes in Unix C
 
agree with you according to the statement and nice one

teacher 11Apr2011 15:42

Re: How to Create processes in Unix C
 
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 11Apr2011 15:49

Re: How to Create processes in Unix C
 
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...


All times are GMT +5.5. The time now is 23:18.