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..
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
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 */
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..
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...
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
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 .
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"
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"