首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何逐个使用execvp()

如何逐个使用execvp()
EN

Stack Overflow用户
提问于 2022-09-22 22:06:19
回答 1查看 57关注 0票数 1

我想对两个命令使用execvp两次1乘1,我想要唤醒的新程序是带有几个参数的hcp,我正在这样使用。

代码语言:javascript
复制
int pid = 0;
char* spec_command[] = {"/usr/bin/hcp", "--enable-product","Performance", NULL};
char* spec_command2[] = {"/usr/bin/hcp", "--enable-product","Threat Prevention", NULL};
pid = fork();
if (pid == 0) 
{
    execvp("/usr/bin/hcp", spec_command);
    execvp("/usr/bin/hcp", spec_command2);
}

但他们中只有一个被处决了?我怎么能让他们俩一起工作呢?谢谢

编辑

不起作用:(我的代码

代码语言:javascript
复制
const char* spec_command[] = {"/usr/bin/hcp", "--enable-product","Performance", NULL};

const char* spec_command2[] = {"/usr/bin/hcp", "--enable-product","Threat Prevention", NULL}; 

disableThreatPrevention(spec_command); 

disableThreatPrevention(spec_command2);



void disableThreatPrevention(const char* spec_command[]) { 

int pid = 0 ;
 pid = fork(); 
if (pid == 0) 
{ 
   execvp("/usr/bin/hcp", spec_command) 
   exit(1);
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-22 22:13:23

您需要fork()两次,因为execvp完全替换了当前的进程,并且永远不会返回(除非失败)。

示例:

代码语言:javascript
复制
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

void run(const char* file, char* argv[]) {
    pid_t pid = fork();
    if (pid == 0)
    {
        execvp(file, argv);
        exit(1);
    }
}

int main(void) {
    char* spec_command[] = {"/usr/bin/hcp", "--enable-product", "Performance",
                            NULL};
    char* spec_command2[] = {"/usr/bin/hcp", "--enable-product",
                             "Threat Prevention", NULL};
    run("/usr/bin/hcp", spec_command);
    run("/usr/bin/hcp", spec_command2);

    int wstatus;
    while(wait(&wstatus) != -1) {}
}

这两个命令并行运行。如果您想在启动第二个run之前等待第一个run完成,也可以在第一个run之后放置一个run --或者在run函数中。如果您想要在不使用wait的情况下继续程序以使进程完成,请跳过wait

如果您想按顺序运行这些命令--但仍然在后台,您可以进一步扩展上述命令,方法是将所有命令作为数组运行到run并循环它们,并在后台进程中逐一执行它们。

代码语言:javascript
复制
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

typedef struct {
    const char *filepath;
    char **argv;
} command;

pid_t run_in_sequence_in_background(command cmds[], size_t count) {
    pid_t pid = fork();
    if (pid == 0) {
        // in first child
        // loop over all the commands and run them one by one in sequence
        for(size_t i = 0; i < count; ++i) {
            pid = fork();
            if(pid == 0) {
                // in grandchild
                execvp(cmds[i].filepath, cmds[i].argv);
                fprintf(stderr, "FAILED TO START %s\n", cmds[i].filepath);
                exit(1);
            } else if(pid != -1) {
                // in first child, wait for the grandchild
                int wstatus;
                waitpid(pid, &wstatus, 0);
            }
        }
        exit(0);
    }
    return pid;
}


int main() {
    char* spec_command[] = {"/usr/bin/hcp", "--enable-product", "Performance",
                            NULL};
    char* spec_command2[] = {"/usr/bin/hcp", "--enable-product",
                             "Threat Prevention", NULL};

    command cmds[] = {
        {"/usr/bin/hcp", spec_command},
        {"/usr/bin/hcp", spec_command2}
    };

    pid_t pid = run_in_sequence_in_background(cmds, sizeof cmds / sizeof *cmds);

    // do other stuff - the hcp commands runs in the background now

    // if you want to wait for the background process:
    if(pid != -1) {
        int wstatus;
        waitpid(pid, &wstatus, 0);
    }
}

或者更简单地说,让argv[0]在您的spec_command中成为要执行的文件名:

代码语言:javascript
复制
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

pid_t run_in_sequence_in_background(char **cmds[], size_t count) {
    pid_t pid = fork();
    if (pid == 0) {
        // in first child
        // loop over all the commands and run them one by one in sequence
        for(size_t i = 0; i < count; ++i) {
            pid = fork();
            if(pid == 0) {
                // in grandchild
                execvp(cmds[i][0], cmds[i]);
                fprintf(stderr, "FAILED TO START %s\n", cmds[i][0]);
                exit(1);
            } else if(pid != -1) {
                // in first child, wait for the grandchild
                int wstatus;
                waitpid(pid, &wstatus, 0);
            }
        }
        exit(0);
    }
    return pid;
}


int main() {
    char* spec_command[] = {"/usr/bin/hcp", "--enable-product", "Performance",
                            NULL};
    char* spec_command2[] = {"/usr/bin/hcp", "--enable-product",
                             "Threat Prevention", NULL};

    char** cmds[] = {spec_command, spec_command2};

    pid_t pid = run_in_sequence_in_background(cmds, sizeof cmds / sizeof *cmds);

    // do other stuff - the hcp commands runs in the background now

    // if you want to wait for the background process:
    if(pid != -1) {
        int wstatus;
        waitpid(pid, &wstatus, 0);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73821121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档