zoukankan      html  css  js  c++  java
  • Create process in UNIX like system

              In UNIX, as we’ve seen, each process is identified by its process identifier,
    which is a unique integer. A new process is created by the fork() system call. The new process consists of a copy of the address space of the original process. This mechanism allows the parent process to communicate easily with its child process. Both processes (the parent and the child) continue execution at the instruction after the fork(), with one difference: the return code for the fork() is zero for the new (child) process, whereas the (nonzero) process identifier of the child is returned to the parent.

             Typically, the exec() system call is used after a fork() system call by one of the two processes to replace the process’s memory space with a new program. The exec() system call loads a binary file into memory (destroying the memory image of the program containing the exec() system call) and starts its execution. In this manner, the two processes are able to communicate and then go their separate ways. The parent can then create more children; or, if it has nothing else to do while the child runs, it can issue a wait() system call to move itself off the ready queue until the termination of the child.

     eg:

    #include <sys/types.h>
    #include <stdio.h>
    #include <unistd.h>
    int main()
    {
    pid t pid;
    /* fork a child process */
    pid = fork();
    if (pid < 0) { /* error occurred */
    fprintf(stderr, "Fork Failed");
    return 1;
    }
    else if (pid == 0) { /* child process */
    execlp("/bin/ls","ls",NULL);
    }
    else { /* parent process */
    /* parent will wait for the child to complete */
    wait(NULL);
    printf("Child Complete");
    }
    }
    return 0;
    





  • 相关阅读:
    考研路线及北大光华学院MBA的一些知识
    返回下表中所有同学语文成绩最低的1次考试成绩, pandas系列
    elasticsearch ES使用文档
    flask 基础用法(自己看的笔记)
    pigx集成sharding jdbc
    从rtmp拉流后进行python鼻子定位demo
    物流-门店监控-在线直播系统搭建-nginx核心部分
    Java中的锁分类与使用
    数据库中的锁
    【转】常见的性能测试缺陷
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3241213.html
Copyright © 2011-2022 走看看