linux: 使用多进程,编写简单命令my_exec

/*************************************************************************
	> File Name: 2.100_child.c
	> Author: suyelu
	> Mail: suyelu@126.com
	> Created Time: Sat 17 Oct 2020 03:05:43 PM CST
 ************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
    pid_t pid;
    int x;
    for (int i = 1; i <= 100; i++) {
        x = i;
        if ((pid = fork()) < 0) {
            perror("fork");
            exit(1);
        }
        if (pid == 0) {
            break;
        }
    }
    if (pid == 0) {
        printf("I'm %dth Child!\n", x);
        sleep(1);
    } else {
        for (int i = 1; i <= 100; i++) {
            int wstatus;
            pid_t rpid = wait(&wstatus);
            printf("Child %d returns with code %d\n", rpid, wstatus);
        }
        pid_t tmp = wait(NULL);
        printf("%d\n", tmp);
    }
    return 0;
}
/*************************************************************************
	> File Name: 3.exec.c
	> Author: suyelu
	> Mail: suyelu@126.com
	> Created Time: Sat 17 Oct 2020 04:55:30 PM CST
 ************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main() {
    pid_t pid;
    if ((pid = fork()) < 0) {
        perror("fork()");
        exit(1);
    }
    if (pid == 0) {
        printf("I'm Child!\nI'm do a great job now!\n");
        execlp("cat", "cat", "3.exec.c", NULL);
        printf("I'm si le!\n");
    } else {
        printf("I'm Father!\n");
        wait(NULL);
    }

    return 0;
}
/*************************************************************************
	> File Name: 4.my_exec.c
	> Author: suyelu
	> Mail: suyelu@126.com
	> Created Time: Sat 17 Oct 2020 06:44:49 PM CST
 ************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

int main(int argc, char **argv) {
    int opt;
    int m_flag = 0;
    char msg[512] = {0};
    while ((opt = getopt(argc, argv, "m:")) != -1) {
        switch (opt) {
            case 'm':
                printf("have m!\n");
                m_flag = 1;
                strcpy(msg, optarg);
                break;
            default:
                fprintf(stderr, "Usage : %s -m msg\n", argv[0]);
                exit(1);
        }
    }
    if (m_flag == 1) {
        printf("msg = %s\n", msg);
        return 0;
    }

    pid_t pid;
    if ((pid = fork()) < 0) {
        perror("fork()");
        exit(1);
    }

    if (pid == 0) {
        // vim
        printf("We will open a Vim!\n");
        sleep(1);
        execl("/usr/bin/vim", "vim", "tmp.txt", NULL);
    } else {
        pid_t pid1;
        wait(NULL);
        printf("Vim is Out!\n");
        if ((pid1 = fork()) < 0) {
            perror("fork()");
            exit(1);
        }
        if (pid1 == 0) {
            sleep(1);
            printf("Cat will be do!\n");
            sleep(1);
            execlp("cat", "cat", "tmp.txt", NULL);
        } else {
            wait(NULL);
            printf("Cat is Over!\n Delete tmp.txt in 2s!\n");
            sleep(1);
            execlp("rm", "rm", "./tmp.txt", NULL);
        }
        //exec
    }
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值