首页 > 其他分享 >c语言中fork,exec和system函数的理解

c语言中fork,exec和system函数的理解

时间:2024-09-26 17:51:11浏览次数:1  
标签:fork exec -- sub1 system 进程 main root

fork

用于创建子进程。

由fork创建的新进程被称为子进程(child process)。fork函数被调用一次,但返回两次。

  1. 在父进程中,fork返回新创建子进程的进程ID。
  2. 在子进程中,fork返回0。
  3. 如果出现错误,fork返回一个负值。

包含在<unistd.h>中,是Unix系统特有的文件(Mac os并不太清楚),因此需要用gnu标准进行编译。

fork的作用是创建一个子进程。网上有不少博客,但其中有相当一部分并没有点明fork之后会怎么样。
实验证明:fork之后的子进程不是重新运行整个程序,而是从fork处接着向后运行
比如下面的程序:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
    int sum=0,i;
    char cmd[1023][20], ch;
    int cnt=1, status;
    printf("before fork\n");
    while(cnt<=2) {
            pid_t id=fork();
            printf("father: id=%d, cnt=%d\n",id,cnt);
            if (id == 0) {
                printf("\e[33mSon\e[0m\n");
            }
        cnt++;
    }
    printf("\e[31mafter fork\e[0m\n");
    // printf("finally: id=%d\n",id);
    return 0;
}

其结果应该如下:(父进程和子进程之间没有固定的先后顺序,看运气)

如果画个进程运行图,应该是这样:

%%{ init: { 'flowchart': { 'curve': 'linear' } } }%% flowchart LR classDef main fill:#f96 classDef sub1 fill:#56b800 classDef sub2 fill:#7abceb classDef sub3 fill:#ebe01a root(root):::main-->a(fork) a:::main-->c((dad)):::main c-->d(fork):::main d-->e((dad)):::main a-->b((son)):::sub1 d-->f((son)):::sub2 b-->g(fork):::sub1 g-->h((subdad)):::sub1 g-->i((son)):::sub3

流程如下:

  1. 程序从main进入,此时的身份我们假设为root
  2. 当运行到fork时,产生一个子进程sub1(此时cnt=1)。
    两个进程都向后运行。父进程无其它输出,子进程输出Son
  3. rootsub1都进入下一轮循环,
    此时cnt=2。这意味着rootsub1都会进行一次fork
    分别产生sub2sub3

标签:fork,exec,--,sub1,system,进程,main,root
From: https://www.cnblogs.com/mitnick/p/18433979

相关文章

  • WindowSystemEvent
    Qt中为WindowSystemEvent事件定义了处理函数Handler,通过宏定义和模版来声明定义----QT_DEFINE_QPA_EVENT_HANDLERMatches(25in1files)----qwindowsysteminterface.cpp(gui\kernel)line199:#defineQT_DEFINE_QPA_EVENT_HANDLER(ReturnType,HandlerName,...)\QT......
  • RME40002 Mechatronics Systems Design – Portfolio Tasks Description
    RME40002MechatronicsSystemsDesign–PortfolioTasksDescriptionSchoolofScience,ComputingandEngineeringTechnologiesRME40002MechatronicsSystemsDesignPortfolioTasksDescriptionSemester2,2024Page1of21RME40002MechatronicsSystemsDesig......
  • 10.Lab Nine —— file system-上
    首先切换分支到fsgitcheckoutfsmakeclean预备知识mkfs程序创建xv6文件系统磁盘映像,并确定文件系统的总块数,这个大小在kernel/param.h中的FSSIZE写明//kernel/params.h#defineFSSIZE   200000//sizeoffilesysteminblocksMakeFile文件系统和内核文......
  • CS-350 - Fundamentals of Computing Systems
    CS-350-FundamentalsofComputingSystemsHomeworkAssignment#2-EVALDueonSeptember26,2024—Latedeadline:September28,2024EoDat11:59pmEVALProblem1InthisEVALassignmentwewillstarttoexplorehowtodiscovercharacteristicsofincom......
  • COMP2240/COMP6240 - Operating Systems
    Schoolof InformationandPhysicalSciencesCOMP2240/COMP6240-OperatingSystemsAssignment2(15%)SubmitusingCanvasby 11:59pm,Friday27th September2024Tasks:Problem 1,and2arebothCOMP2240& COMP6240 students.Problem3isonlyfor COMP6......
  • system的使用
    <stdio.h>    std是一个标准库,i=input  o=output    标准输入输出库  .h头文件system的使用功能:在已经运行的程序中执行另外一个外部程序参数:外部可执行程序名称返回:成功:0失败:任意数字......
  • 文本摘要综述—从统计方法到大型语言模型综述介绍,原文阅读:A Systematic Survey of Tex
    ASystematicSurveyofTextSummarization:FromStatisticalMethodstoLargeLanguageModels文本摘要的系统综述:从统计方法到大型语言模型paper:https://arxiv.org/abs/2406.11289文章目录~原文阅读Abstract1.Introduction1.1.MajorDifferences1.2.MainContri......
  • Ubuntu 通过Supervisor 或者 systemd 管理 .Net应用
    在Ubuntu上安装.NET8.0,通过supervisor或systemd管理.NET应用服务,确保应用能够自动启动、运行以及在崩溃时重启。1.安装.NET8.0最新的Ubuntu版本已经不需要注册Microsoft包存储库了,具体的可以参考微软官方文档安装,在Ubuntu上安装.NETSDK或.NET运行时2.使用Sup......
  • WPF Image show picture in high resolution periodically via System.Timers.Timer
    <Windowx:Class="WpfApp411.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • exec 在函数和生成器中
    我需要在python中编写一个自定义exec函数(出于多种目的,但这不是这里的问题,所以这个名为myExec的自定义exec的功能将与现在的exec完全一样)。我进入了这个问题:defmyExec(code):exec(code)code="""a=1print(a)u=[aforxinrange(3)......