fork函数

fork函数

计算机系统基础-fork()
fork函数
运行结果
结果分析

1.函数说明
一个现有进程可以调用fork函数创建一个新进程。新创建的进程称为子进程(child),相对应的称为父进程(parent)。
子进程是父进程的副本,它将获得父进程代码、数据段、堆、栈等资源的副本。子进程获得与父进程的虚拟地址空间相同的(但是独立的)副本,这意味着父子进程间不共享这些存储空间,子进程有了独立的地址空间。父子进程最大的区别在于PID(进程标识符)的不同。
子进程被创建时,系统中有两个活动的进程:父进程和子进程。它们并发执行;调用一次,返回两次。

2.fork()函数需要包含的头文件

#include <sys/types.h>
#include <unistd.h>

3.fork()函数举例

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> 
#include <signal.h>

void fork0() 
{
    if (fork() == 0) {
 printf("Hello from child\n");
    }
    else {
 printf("Hello from parent\n");
    }
}

void fork1()
{
    int x = 1;
    pid_t pid = fork();
    if (pid == 0) {
 printf("Child has x = %d\n", ++x);
    } 
    else {
 printf("Parent has x = %d\n", --x);
    }
    printf("Bye from process %d with x = %d\n", getpid(), x);
}

void fork2()
{
    printf("L0\n");
    fork();
    printf("L1\n");    
    fork();
    printf("Bye\n");
}

void fork3()
{
    printf("L0\n");
    fork();
    printf("L1\n");    
    fork();
    printf("L2\n");    
    fork();
    printf("Bye\n");
}

void fork4()
{
    printf("L0\n");
    if (fork() != 0) {
    printf("L1\n");    
    if (fork() != 0) {
        printf("L2\n");
        }
    }
    printf("Bye\n");
}

void fork5()
{
    printf("L0\n");
    if (fork() == 0) {
    printf("L1\n");    
    if (fork() == 0) {
        printf("L2\n");
        }
    }
    printf("Bye\n");
}

void cleanup(void) {
    printf("Cleaning up\n");
}

void fork6()
{
    atexit(cleanup);
    fork();
    exit(0);
}

void fork7()
{
    if (fork() == 0) {
 /* Child */
 printf("Terminating Child, PID = %d\n", getpid());
 exit(0);
    } else {
 printf("Running Parent, PID = %d\n", getpid());
 while (1)
     ; /* Infinite loop */
    }
}

void fork8()
{
    if (fork() == 0) {
 /* Child */
 printf("Running Child, PID = %d\n",
        getpid());
 while (1)
     ; /* Infinite loop */
    } else {
 printf("Terminating Parent, PID = %d\n",
        getpid());
 exit(0);
    }
}

void fork9()
{
    int child_status;
    if (fork() == 0) {
        printf("HC: hello from child\n");
        exit(0);
    } else {
    printf("HP: hello from parent\n");
    wait(&child_status);
    printf("CT: child has terminated\n");
    }
    printf("Bye\n");
}

#define N 5

void fork10()
{
    pid_t pid[N];
    int i, child_status;
    for (i = 0; i < N; i++)
	 if ((pid[i] = fork()) == 0) {
    		 exit(100+i); /* Child */
	 }  
    for (i = 0; i < N; i++) { /* Parent */
 	pid_t wpid = wait(&child_status);
    if (WIFEXITED(child_status))
     	printf("Child %d terminated with exit status %d\n",wpid, WEXITSTATUS(child_status));
    else
     	printf("Child %d terminate abnormally\n", wpid);
    }
}

void fork11()
{
    pid_t pid[N];
    int i;
    int child_status;
    for (i = 0; i < N; i++)
	 if ((pid[i] = fork()) == 0)
     exit(100+i); /* Child */
    for (i = N-1; i >= 0; i--) {
	 pid_t wpid = waitpid(pid[i], &child_status, 0);
	 if (WIFEXITED(child_status))
    	     printf("Child %d terminated with exit status %d\n",wpid, WEXITSTATUS(child_status));
    	 else
    	     printf("Child %d terminate abnormally\n", wpid);
    }
}

void fork12()
{
    pid_t pid[N];
    int i;
    int child_status;
    for (i = 0; i < N; i++)
    if ((pid[i] = fork()) == 0) {
     /* Child: Infinite Loop */
     while(1)
 	 ;
     }
     for (i = 0; i < N; i++) {
 	printf("Killing process %d\n", pid[i]);
	 kill(pid[i], SIGINT);
     }
     for (i = 0; i < N; i++) {
	 pid_t wpid = wait(&child_status);
	 if (WIFEXITED(child_status))
    	     printf("Child %d terminated with exit status %d\n",wpid, WEXITSTATUS(child_status));
 	else
    	     printf("Child %d terminated abnormally\n", wpid);
      }
}

void int_handler(int sig)
{
    printf("Process %d received signal %d\n", getpid(), sig); /* Unsafe */
    exit(0);
}
     
void fork13()
{
    pid_t pid[N];
    int i;
    int child_status;
    signal(SIGINT, int_handler);
    for (i = 0; i < N; i++)
    if ((pid[i] = fork()) == 0) {
     /* Child: Infinite Loop */
     while(1)
 	 ;
 }
    for (i = 0; i < N; i++) {
 	printf("Killing process %d\n", pid[i]);
 	kill(pid[i], SIGINT);
    }
    for (i = 0; i < N; i++) {
 	pid_t wpid = wait(&child_status);
 	if (WIFEXITED(child_status))
    	     printf("Child %d terminated with exit status %d\n",wpid, WEXITSTATUS(child_status));
	 else
     	     printf("Child %d terminated abnormally\n", wpid);
    }
}

int ccount = 0;

void child_handler(int sig)
{
    int child_status;
    pid_t pid = wait(&child_status);
    ccount--;
    printf("Received SIGCHLD signal %d for process %d\n", sig, pid); /* Unsafe */
    fflush(stdout); /* Unsafe */
}
void fork14()
{
    pid_t pid[N];
    int i;
    ccount = N;
    signal(SIGCHLD, child_handler);
    for (i = 0; i < N; i++) {
	 if ((pid[i] = fork()) == 0) {
     	 sleep(1);
    	 exit(0);  /* Child: Exit */
 	}
    }
    while (ccount > 0)
 ;
}

void child_handler2(int sig)
{
    int child_status;
    pid_t pid;
    while ((pid = wait(&child_status)) > 0) {
 ccount--;
 printf("Received signal %d from process %d\n", sig, pid); /* Unsafe */
 fflush(stdout); /* Unsafe */
    }
}
void fork15()
{
    pid_t pid[N];
    int i;
    ccount = N;
    signal(SIGCHLD, child_handler2);
    for (i = 0; i < N; i++)
 if ((pid[i] = fork()) == 0) {
     sleep(1);
     exit(0); /* Child: Exit */
 }
    while (ccount > 0) {
 pause();
    }
}

void fork16() 
{
    if (fork() == 0) {
 printf("Child1: pid=%d pgrp=%d\n",
        getpid(), getpgrp());
 if (fork() == 0)
     printf("Child2: pid=%d pgrp=%d\n",
     getpid(), getpgrp());
 while(1);
    }
} 

void fork17() 
{
    if (fork() == 0) {
 printf("Child: pid=%d pgrp=%d\n",
        getpid(), getpgrp());
    }
    else {
 printf("Parent: pid=%d pgrp=%d\n",
        getpid(), getpgrp());
    }
    while(1);
}

int main(int argc, char *argv[])
{
    int option = 0;
    if (argc > 1)
     option = atoi(argv[1]);
    switch(option) {
    case 0: fork0();
	 break;
    case 1: fork1();
 	break;
    case 2: fork2();
	 break;
    case 3: fork3();
	 break;
    case 4: fork4();
	 break;
    case 5: fork5();
	 break;
    case 6: fork6();
	 break;
    case 7: fork7();
	 break;
    case 8: fork8();
	 break;
    case 9: fork9();
 	break;
    case 10: fork10();
 	break;
    case 11: fork11();
	 break;
    case 12: fork12();
	 break;
    case 13: fork13();
	 break;
    case 14: fork14();
	 break;
    case 15: fork15();
	 break;
    case 16: fork16();
	 break;
    case 17: fork17();
	 break;
    default:
	 printf("Unknown option %d\n", option);
 	break;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值