- 论坛徽章:
- 0
|
/*this .c file demonstrates how to use fork function to generate a new process*/
/*filename:fork.c
time:08/06/2009
author:creatory@mail.com
when fork success,the process scheduled sequence is unsure.
Every one can be scheduled by the scheduled process firstly.
So the result is not sure.
*/
#include
#include //fork
#include //exit
int main(void){
pid_t pid;
if((pid=fork())==-1){
printf("fork error.\n");
exit(1);
}else if(pid==0){
//child
printf("this is child process.\n");
}else{
//parent
printf("this is parent process.\n");
}
return 0;
}
/* this file demonstrates how to use vfork function
filename:vfork.c
time:08/06/2009
author:creatory@mail.com
Note:
Call vfork will hang up the parent process until the child process terminate or it call exec function.
So the result is always child process execute firstly,then parent process.It's sure.
*/
/*remain problem:why the end must be exit(0) not "return 0".
if "return 0" then generate "segmentation fault"
*/
#include //exit
#include //vfork
#include
#include //printf
int main(void){
pid_t pid;
if((pid=vfork())<0){
printf("vfork error!\n");
exit(1);
}
else if(pid==0){
//the child process
printf("Child process is printing.\n");
}else{
//the parent process
printf("the parent is printing.\n");
}
// return 0;
exit(0);
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56374/showart_2021842.html |
|