一、函数fork
fork函数原型:
#include <unistd.h>
pid_t fork(void);
二、程序清单
1. 测试代码:
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <iostream>
using namespace std;
static int global_val = 0;
int main()
{
int *p = (int*)malloc(sizeof(int));
*p = 0;
int m = 2;
pid_t pid;
int fd = open("mytest", O_RDWR | O_CREAT, 0666);
if ((pid = fork()) < 0)
{
cout << "fork error" << endl;
}
else
{
if (pid == 0)
{
char buf[20] = "\0";
int res = read(fd, buf, 20);
cout << "pid is " << getpid() << " res is " << res << " fd is " << fd << " buf is " << buf << endl;
close(fd);
//sleep(1);
char bufs[8] = "shenlei";
lseek(fd, 0, SEEK_SET);
write(fd, bufs, strlen(bufs));
global_val++;
m++;
(*p)++;
}
else
{
sleep(1);
char buf[20] = "\0";
lseek(fd, 0, SEEK_SET);
int res = read(fd, buf, 20);
cout << "pid is " << getpid() << " res is " << res << " fd is " << fd << " buf is " << buf << endl;
cout << *p << " " << m << " " << global_val << endl;
}
}
return 0;
}
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
for(int i = 0; i < 4; ++i)
printf("--------i = %d\n", i);
pid = fork();
if(pid > 0)
printf("parent process, pid = %d\n", getpid());
else if(pid == 0)
printf("child process, pid = %d, ppid = %d\n", getpid(), getppid());
for(int i = 0; i < 4; ++i)
printf("i = %d\n", i);
return 0;
}
输出结果:
2. 测试代码:
#include <stdio.h>
#include <unistd.h>
int counter = 200;
int main()
{
int number = 5;
int i;
pid_t pid;
for(i = 0; i < number; ++i) {
pid = fork();
if(pid == 0) {
break;
}
}
if(i == 0){
counter += 200;
printf("first process, pid = %d\n", getpid());
printf("-----couner = %d\n", counter);
}
if(i == 1){
counter += 200;
printf("second process, pid = %d\n", getpid());
printf("-----couner = %d\n", counter);
}
if(i == 2){
counter += 200;
printf("thrid process, pid = %d\n", getpid());
printf("-----couner = %d\n", counter);
}
if(i == number){
sleep(3);
counter += 400;
printf("parent process, pid = %d\n", getpid());
printf("-----couner = %d\n", counter);
}
return 0;
}
输出结果:
3. 测试代码:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i;
pid_t pid;
printf("xxxxxxxxxxxxxxxxxxxxx\n");
for(i = 0; i < 5; ++i) {
pid = fork();
if(pid == -1) {
perror("fork error:");
exit(1);
} else if(pid == 0) {
break;
}
}
if(i < 5) {
sleep(i);
printf("I'am %d child, pid = %d\n", i+1, getpid());
} else {
sleep(i);
printf("I'am parent\n");
}
return 0;
}
输出结果:
三、父子进程共享
父子进程之间在fork后,有哪些相同,有哪些相异之处呢?
刚fork之后:
父子相同处:全局变量、.data、.text、栈、堆、环境变量、用户ID、宿主ID(家目录)、进程工作目录、信号处理方式
父子不同处:进程ID、fork返回值、父进程ID、进程运行时间、闹钟(定时器)、未决信号集
似乎,子进程复制了父进程0-3G用户空间内容,以及父进程的PCB、但pid不同,真的每fork一个进程都要将父进程完全程的0-3G地址空间完全拷贝一份,然后在映射至物理内存吗?
当然不是!父子进程间遵循读时共享写时复制的原则,这样设计,无论子进程执行父进程的逻辑还是执行自己的逻辑都能节省内存开销。
重点注意:躲避父子进程共享全局变量的知识误区!
【重点】:父子进程共享:1. 文件描述符(打开文件的结构体) 2. mmap建立的映射区
1. 测试代码:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int var = 24;
int main()
{
pid_t pid;
pid = fork();
if(pid == -1) {
perror("fork");
exit(1);
} else if(pid > 0) {
var = 55;
sleep(1);
printf("I'am parent pid = %d, parent ID = %d, var = %d\n", getpid(), getppid(), var);
} else if(pid == 0) {
var = 100;
printf("child pid = %d, parent ID = %d, var = %d\n", getpid(), getppid(), var);
}
printf(" var = %d\n", var);
return 0;
}
输出结果: