zoukankan      html  css  js  c++  java
  • linux简单进程调用

    父进程调用子进程,可以用的命令date,who,pwd,ls,等无参数的

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

    #define MAXLINE 20

    int main(void)
    {
    char buf[MAXLINE]; /* from apue.h */
    pid_t pid;
    int status;

    printf("%% "); /* print prompt (printf requires %% to print %) */
    while (fgets(buf, MAXLINE, stdin) != NULL) {
    if (buf[strlen(buf) - 1] == '\n')
    buf[strlen(buf) - 1] = 0; /* replace newline with null */

    if ((pid = fork()) < 0) {
    puts("fork error");
    } else if (pid == 0) { /* child */
    printf("pid:%d\n", pid);

    execlp(buf, buf, (char *)0);
    printf("couldn't execute: %s\n", buf);
    exit(127);
    }

    /* parent */
    if ((pid = waitpid(pid, &status, 0)) < 0)
    {
    printf("waitpid error !status:%d\n", status);
    }
    printf("%% ");
    }
    exit(0);
    }



  • 相关阅读:
    树上差分
    循环数组最大子段和(带限制的最大子段和,单调队列优化)
    sprintf函数的用法详解
    VBS基础篇
    VBS基础篇
    VBS基础篇
    VBS基础篇
    VBS基础篇
    Android随笔
    Android随笔
  • 原文地址:https://www.cnblogs.com/xiangzi888/p/2246132.html
Copyright © 2011-2022 走看看