异常控制流之fork()

本文详细探讨了fork()函数在程序中的使用,通过示例解析了逻辑运算符与fork()结合时的流程,强调了缓存问题及并发执行的特点。文章分析了不同阶段的fork流程,并介绍了wait()、waitpid()等函数在处理子进程结束时的作用,同时提到了进程间通信和信号处理。此外,还讨论了CTRL-C和CTRL-Z在中断和挂起进程方面的区别,并给出了家庭作业。
摘要由CSDN通过智能技术生成

t# fork与逻辑运算

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

int main(int argc,char *argv[])
{
   
	fork();
	fork()&&fork()||fork();
	fork();
	printf("main\n");
}

./try
try流程图
1.A&&B,若A = 0,则不会执行B。
A||B,若A != 0,则不会执行B。
2.特别提示:A&&B||C,当A=0时仅仅不执行B,但还会执行C。
3.如图所示,为fork()&&fork()||fork()的流程图。
由于fork()一次调用,两次返回的特性,无条件限制时fork()乘2,在此代码中表达式之前,之后各有一个fork(),故一共有5*4=20个程 序,除去原始的一个,则还余下新建的19个。

缓存的问题

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
 
int main(void)
{
   
   int i;
   for(i=0; i<2; i++){
   
      fork();
      printf("-");
   }
   wait(NULL);
   wait(NULL);
   return 0;
}

./buff
1.会打印8个“-”,因为没有\n,所以不会直接刷新缓存输出到设备上,在fork时,子进程会复制到父进程的缓存内容,导致多输出两个“-”。

main函数:

/*
 * forks.c - Examples of Unix process control
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> 
#include <signal.h>

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;
}

fork0

/*
 * fork0 - The simplest fork example
 * Call once, return twice
 * Creates child that is identical to parent
 * Returns 0 to child process
 * Returns child PID to parent process
 */
void fork0() 
{
   
    if (fork() == 0) {
   
	printf("Hello from child\n");
    }
    else {
   
	printf("Hello from parent\n");
    }
}

./fork0
1.调用一次,返回两次:fork函数被父进程调用一次,返回的是新创建的子进程和父进程。
2.并发:父进程和子进程独立运行,结果显示在我的系统上为父进程先完成printf,输出Hello from parent,子进程后完成printf,输出 Hello from child,但是在另外的系统上可能正好相反。
进程图如下所示:
fork0流程图

fork1

/* 
 * fork1 - Simple fork example 
 * Parent and child both run same code
 * Child starts with identical private state
 */
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", 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值