fork
fork
函数的功能是创建子进程:
#include <unistd.h>
pid_t fork ( void );
fork
的奇妙之处在于:它被调用一次,却返回两次。它可能有3
种不同的返回值:
- 在父进程中,
fork
返回新创建的子进程的PID
。 - 在子进程中,
fork
返回0
。 - 如果出现错误,
fork
返回一个负值。
fork实例1
代码实例:
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
int main() {
pid_t pid;
pid = fork();
if ( pid > 0 ) {
printf ( "This is father process\n" );
exit ( 0 );
} else {
printf ( "This is child process\n" );
exit ( 0 );
}
}
执行结果:
This is father process
This is child process
在pid = fork();
之前,只有一个进程在执行,但在这条语句执行之后,就变成两个进程在执行了。
这两个进程共享代码段,将要执行的下一条语句都是if ( pid > 0 ) {}
。
原来就存在的进程被称作父进程
,新出现的进程被称作子进程
,父子进程的区别在于PID
不同。
fork实例2
代码实例:
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
int main() {
pid_t pid;
int count = 0;
pid = fork();
count++;
printf ( "count is %d\n", count );
exit ( 0 );
}
执行结果:
count = 1
count = 1
对fork
函数,子进程的数据空间、堆栈空间都会从父进程得到一个拷贝,而不是共享。
在子进程中,对count
进行加1
的操作,并没有影响到父进程中的count
,父进程中的count
仍然为0
。
vfork
vfork
函数的功能是创建子进程:
#include <sys/types.h>
#include <unistd.h>
pid_t vfork ( void );
vfork
和fork
的区别如下:
fork
:子进程拷贝父进程的数据段,父、子进程的执行次序不确定。vfork
:子进程与父进程共享数据段,子进程先运行,父进程后运行。
vfork实例1
代码实例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main ( void ) {
pid_t pid;
int var = 88;
if ( ( pid = vfork() ) < 0 ) {
printf ( "vfork error" );
exit ( -1 );
} else if ( pid == 0 ) { /* 子进程 */
var++;
exit ( 0 );
}
printf ( "pid = %d, var = %d\n", getpid(), var );
return 0;
}
执行结果:
pid = 283, var = 89
注意,从子进程中退出必须使用exit
函数,不能使用return
函数。
vfork实例2
代码实例:
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
int main() {
pid_t pid;
int count = 0;
pid = vfork();
count++;
printf ( "count is %d\n", count );
exit ( 0 );
}
执行结果:
count is 1
count is 2