//转自陈颢文(老师)的blog
fork_home.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
pid_t pid;
printf("father is running...\n");
pid = fork();
if(pid < 0)//create child process is fail
{
printf("Fail to fork");
return -1;
}else if(pid > 0){
printf("Father PID : %d fork",getpid());
while(1);
}else if(pid == 0){ //create child process
printf("Child PID : %d fork",getpid());
while(1);
}
return 0;
}
=========================================
fork1.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main()
{
pid_t pid = 0;
printf("Father main is running!\n");
pid = fork();
//fork使用一次,返回两次。
//在父进程中返回子进程的pid号
//在子进程中返回 0
//
//for()之后,父子进程运行的程序都是从fork()之后的下一句代码开始执行的。
if(pid < 0)
{
return -1;
}
fprintf(stderr,"Fail to fork : %s\n",strerror(errno));
//pid_t getpid(void);
//功能:返回当前进程的pid号
//若是父进程调用就返回父进程的pid号
//若是子进程调用就返回子进程的pid号
printf("PID : %d fork ok! fork return pid = %d\n",getpid(),pid);
while(1); //写死循环让程序不结束,方便我们查看pid号
return 0;
}
==========================================================
fork2.c
#include <stdio.h>
#include <unistd.h>
int a = 10;
int main(int argc, const char *argv[])
{
pid_t pid;
printf("father is running...\n");
pid = fork();
if(pid < 0)//create child process is fail
{
printf("Fail to fork");
return -1;
}else if(pid > 0){
while(1)
{
printf("fater = a : %d\n",a);
sleep(1);
}
// printf("Father PID : %d fork ok! fork return pid = %d\n",getpid(),pid);
}else if(pid == 0){ //create child process
while(1)
{
printf("child = a : %d\n",a);
a++;
sleep(1);
}
}
return 0;
}
================================
fork3.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int global_var = 10;
int main(int argc, const char *argv[])
{
pid_t pid = 0;
int stack_var = 1000;
int *heap_var = (int *)malloc(sizeof(int));
memset(heap_var,0,sizeof(heap_var));
*heap_var = 100;
pid = fork();
if(pid < 0)
{
perror("Fail to fork");
return -1;
}else if(pid == 0){ //child do
printf("Child PID = %-2d\n",getpid());
stack_var = 500;
}else if(pid > 0){ //father do
sleep(2); //休眠2s,让子进程先运行
printf("==================================================\n");
printf("Father PID = %-2d\n",getpid());
global_var = 5;
}
printf("stack_var = %d global_var = %d *heap_var = %d\n",stack_var,global_var,*heap_var);
printf("&stack_var = %p &global_var = %p heap_var = %p\n",&stack_var,&global_var,heap_var);
free(heap_var);
return 0;
}
=================================================================
fork4.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define N 10
//a.out file
int main(int argc, const char *argv[])
{
pid_t pid = 0;
int fd = 0;
char buf[N] = {0};
int n = 0;
if(argc < 2)
{
fprintf(stderr,"Usage : %s filename\n",argv[0]);
return -1;
}
fd = open(argv[1],O_RDONLY);
if(fd < 0)
{
perror("Fail to open");
return -1;
}
pid = fork();
if(pid < 0){
perror("Fail to fork");
//child do
}else if(pid == 0){
sleep(1)
//father do
}else if(pid > 0){
}
return 0;
}