c++ - 了解 C++ (Linux) 中的 fork、exec 和 wait

标签 c++ linux fork exec wait

我对在 Linux 中使用这些不同类型的系统调用还很陌生,这让我很困惑。有了这个,我只要求朝着正确的方向/开始而不是完成。 使用 forkexecwait,我已经阅读了它们,但这对我的情况仍然没有真正帮助。 我要做的是以下内容,

打印提示并等待用户输入最多包含四个参数或选项的命令。 “退出”将停止程序。

一些示例,mkdir blah,创建目录,然后提示输入新命令,touch blah/this blah/that blah/there

我要调用fork创建一个子进程来执行输入的命令,然后在子进程中调用exec让子进程成为要执行的程序被执行(这部分让我更加困惑),最后在父进程中调用 wait 以便解释器在命令完成之前不会打印下一个提示。

实现此目标的最佳方法是什么?比如,读取命令/参数/选项然后执行它们的最佳方法是什么? 我认为这样做会更好是一个 do..while ,其中 while 条件是检查“退出”

我知道我所做的很少,也不多。

int main() {
    char command[25];
    pid_t  pid;
    int rs;
    cout << "Enter command: ";
    cin >> command;
    
    while(strcmp(command, "exit") != 0) {
        pid = fork();
    
        if (pid == 0) { //child process
            rs = execl("echo", command);
        } else {    // parent process
            cout << "Enter a command: ";
            cin >> command; 
        }
    }
    return 0;
}

最佳答案

每个系统调用的作用的一般分类:

fork: fork 当前进程。从字面上看,当调用 fork 时,执行在调用 fork 时暂停,整个程序被复制到一个新的进程空间中,该空间是原始进程的子进程。然后两个进程在 fork 调用之后继续并行执行。您需要获取 PID 才能判断当前正在执行的程序是子程序还是父程序。

exec:暂停当前进程的执行,用指定的新程序删除内存中的当前进程以运行。然后它运行新程序。

wait:暂停当前进程,直到至少一个子进程终止。它是 waitpid() 的包装器,它允许您暂停当前进程的执行并等待当前进程的子进程状态发生变化(它可能是自身的克隆或由 执行)

这是我在大学上课时演示等待和 fork (但没有执行)的一些代码:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
const int BUFFER_SIZE = 1000;

void copySegment(int i, int segmentSize, FILE * fin, FILE * fout) {
    // Does not need to be shown to prove point
}

int main(int argc, char * argv[]) {
    int i;
    sem_t * sem;
    pid_t pid;
    FILE * fin, * fout;
    struct stat sbuf;
    int fileSize, fileDescriptor, segmentSize;
    fin = fopen(argv[1], "r");
    fout = fopen("forkcopy.txt", "w");
    fileDescriptor = fileno(fin);
    fstat(fileDescriptor, &sbuf);
    fileSize = sbuf.st_size;
    segmentSize = fileSize / 4;
    sem = sem_open("sem", O_CREAT | O_EXCL, 0644, 4);
    sem_unlink("sem");
    for (i = 0; i < 4; i++) {
        pid = fork();
        if (pid < 0)
            printf("Fork error.\n");
        else if (pid == 0)
            break;
    }
    if (pid != 0) {
        while (pid = waitpid(-1, NULL, 0)) {
            if (errno == ECHILD)
                break;
        }
        sem_destroy(sem);
        fclose(fin);
        fclose(fout);
        exit(0);
    } else {
        sem_wait(sem);
        copySegment(i, segmentSize, fin, fout);
        sem_post(sem);
        exit(0);
    }
}

关于c++ - 了解 C++ (Linux) 中的 fork、exec 和 wait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754510/

相关文章:

c - Fork() 和 Posix 队列。发送和接收字符串

c - 收到 parent 的信号后如何向 child 的 child 发送信号

c++ - Windows Phone 8.0 上的 GetModuleHandle

c++ - 使用 c-plus-plus 连接到 firebird db

linux - 替换平面文件中的第二行

linux - 管道输出到 bash 命令的中间

c++ - 为什么数组的大小作为常量变量在 C 中是不允许的,而在 C++ 中是允许的?

c++ - 如何通过它的第 0 个位置引用解析这个 vector

c - fopen 打开目录?

c - execve() 仅在第一个循环上返回错误