zoukankan      html  css  js  c++  java
  • Linux_fork 进程操作demo

    main.7

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        system("ls -l");
    
        printf("end!
    ");
    
        return 0;
    }
    

    main8.c

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char * env[] = {"PATH=/bin", 0};
        char * argv[] = {"ls", "-l", 0};
    
        printf("ready...
    ");
    
        //printf("****************execl**************
    ");
        //execl("ls", "ls", "-l", 0);
    
        printf("****************execlp**************
    ");
        execlp("ls", "ls", "-l", 0);
    
        //printf("****************execle**************
    ");
        //execle("/bin/ls", "ls", "-l", 0, env);
    
        //printf("****************execv**************
    ");
        //execv("/bin/ls", argv);
    
        //printf("****************execvp**************
    ");
        //execvp("ls", argv);
    
        //printf("****************execve**************
    ");
        //execve("/bin/ls", argv, env);
    
        printf("end!
    ");
        return 0;
    }
    

    main9.c

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int pid;
        char *msg;
        int n;
    
        printf("father process begin...
    ");
    
        pid = fork();
    
        if (pid == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pid == 0) {
            printf("In child process!
    ");
            msg = "child process ";
            n = 2;
        }  else {
            printf("In father process!
    ");
            msg = "father process ";
            n = 1;
        }
    
        while(n--) {
            printf("%s
    ", msg);
            sleep(1);
        }
    
        printf("%s end!
    ", msg);
    
        return 0;
    }

    main10.c

    #include <sys/types.h>
    #include <sys/wait.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int pid;
        char *msg;
        int status;
        int child_pid;
        int n;
    
        printf("father process begin...
    ");
    
        pid = fork();
    
        if (pid == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pid == 0) {
            printf("In child process!
    ");
            msg = "child process ";
            n = 3;
        }  else {
            printf("In father process!
    ");
            msg = "father process ";
            n = 1;
        }
    
        while(n--) {
            printf("%s
    ", msg);
            sleep(1);
        }
    
        if (pid) {
            printf("father process wait...
    ");
            child_pid = wait(&status);
            if (WIFEXITED(status)) {
                printf("Wait finished and child process terminated normally, child pid = %d
    ", child_pid);
            } else {
                printf("Wait finished and child process terminated unnormally, child pid = %d
    ", child_pid);
            }
        }
    
        printf("%s end!
    ", msg);
    
        return 0;
    }
    

    main11.c

    #include <sys/types.h>
    #include <sys/wait.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int pid;
        char *msg;
        int status;
        int child_pid;
        int n;
    
        printf("father process begin...
    ");
    
        pid = fork();
    
        if (pid == -1) {
            printf("fork error!
    ");
            exit(1);
        } else if (pid == 0) {
            printf("In child process!
    ");
            msg = "child process ";
            n = 1;
        }  else {
            printf("In father process!
    ");
            msg = "father process ";
            n = 10;
        }
    
        while(n--) {
            printf("%s
    ", msg);
            sleep(5);
        }
    
        if (pid) {
            printf("father process wait...
    ");
            child_pid = wait(&status);
            if (WIFEXITED(status)) {
                printf("Wait finished and child process terminated normally, child pid = %d
    ", child_pid);
            } else {
                printf("Wait finished and child process terminated unnormally, child pid = %d
    ", child_pid);
            }
        }
    
        printf("%s end!
    ", msg);
    
        return 0;
    }
    

    main12.c

    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        int pid;
        int i;
    
        for (i=0; i<10; i++) {
            pid = fork();
            if (pid == -1) {
    
                perror("fork error!
    ");
                exit(1);
            } else if (pid == 0) {
                //printf("Start run a new child pid, pid=%d
    ", getpid());          
            } else {
                //printf("Add a new child, pid = %d
    ", pid);
            }
        }
    
        printf("My pid = %d
    ", getpid());
    
        return 0;
    }
    
  • 相关阅读:
    fms中使用的页签(使用的模板)
    layui—layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案(转)
    mybatis--oracle 利用接口 实现简单的增删改查以及一对一的两表关联查询
    mybatis--oracle 利用配置 实现简单的增删改查
    Java DecimalFormat的主要功能及使用方法
    SpringMVC的@ResponseBody返回字符串乱码问题解决
    org.apache.commons.lang.exception.NestableRuntimeException等缺少jar包的解决办法
    Mybatis框架搭建和简单的增删改查
    Struts2上传
    Struts2转换器
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384222.html
Copyright © 2011-2022 走看看