复制进程镜像

来源:互联网 发布:mysql设置唯一约束 编辑:程序博客网 时间:2024/06/11 16:26
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
    pid_t pid;
    char *message;
    int n;
    printf("fork program starting\n");
    pid=fork();
    switch(pid)
    {
        case -1:
        perror("fork failed");
        exit(1);
        case 0:
        message="this is the child";
        n=5;
        break;
        default:
        message="this is the parent";
        n=3;
        break;
    }
    for(;n>0;n--)
    {
        puts(message);
        sleep(1);
    }
    exit(0);

}




首先介绍一下对于我们这种菜鸟学习linux来说使用ubuntu一个不错选择而且ubuntu软件中心有一个编译器codeblock可以计算出程序运行时间很适合ACM学者使用同时这种方式windows下面相似很适合初学者使用经过前面一系列准备现在算是正式一个开始fork复制一个进程这两个进程分别运行codeblock下面运行这个程序会看到程序结束之后子进程还在执行具体运行结果可以自己程序复制自己运行看看.