简体   繁体   English

C语言如何同时执行两个命令

[英]How to execute two commands simultaneously in C

Thank you all for the advices and proposed answers, i finally solved it myself and this is how it should look:谢谢大家的建议和建议的答案,我终于自己解决了,它应该是这样的:

int main(int argc, char *argv[]) {

    int i;
    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "+") == 0) {
            break;
        }
    }

    argv[i] = NULL;

    if (fork() == 0) {
        execvp(argv[1], &argv[1]);
        exit(0);
    }

    if (fork() == 0) {
        execvp(argv[i+1], &argv[i+1]);
        exit(0);
    }

    wait(NULL);
    wait(NULL);

    exit(0);

}

Building on the OP code基于 OP 代码构建

int main(int argc, char *argv[]) {

    int i =1 ;

    // Argument list for first call
    i1 = i ;
    char *prog1[argc];
    while (strcmp(argv[i], "+") != 0) {
            prog1[i-i1] = argv[i];
            i++;
    }
    prog[i-1] = NULL ;

    // Argument list for second call
    int i2 = i ;
    char *prog2[argc];
    whlie ( i < argc ) {
        prog2[i-i2] = argv[i] ;
    } ;
    prog2[i-i2] = NULL ;

    pid_t pid1 = fork() ;
    if ( pid1 == 0) {
        execvp(prog1[0], prog1);
        exit(0);
    }

    pid_t pid2 = fork() ;
    if ( pid2 == 0) {
        execvp(prog2[0], prog2);
        exit(0);
    }

    // Wait for all childs
    int status ;
    while ( wait(&status) ) { } ;

    exit(0);
}

Forking works fine for parallel execution, but I'd recommend using threads instead. Forking 适用于并行执行,但我建议改用线程。 Either use a higher level threading API or use the standard for POSIX, pthreads.要么使用更高级别的线程 API,要么使用 POSIX、pthreads 的标准。 When you compile just make sure you link with -lpthread .编译时只需确保与-lpthread链接。

Something like:就像是:

#include <pthread.h>

typedef struct
{
  const char* command;
} thread_arg;

void* command_thread(void* v_arg)
{
  thread_arg* arg = (thread_arg*) v_arg;
  system(arg->command);
}

int main(int argc, char** argv)
{
  pthread_t thread0, thread1;
  thread_arg arg0, arg1;

  /* specify arguments */
  arg0.command = [command to execute on thread 0];
  arg1.command = [command to execute on thread 1];

  /* create threads */
  pthread_create(&thread0, NULL, command_thread, &arg0);
  pthread_create(&thread1, NULL, command_thread, &arg1);

  /* wait for threads to complete */
  pthread_join(thread0, NULL);
  pthread_join(thread1, NULL);

  return 0;
}

would work, if arguments were properly delimited.如果参数被正确分隔,就会起作用。

If you don't have to use + to delimit two arguments I'd just use double quotes when calling the program and then just take argv[1] and argv[2] as the commands.如果您不必使用+来分隔两个参数,我只会在调用程序时使用双引号,然后将argv[1]argv[2]作为命令。

If this is an assignment and you must use + to delimit, then you should check if + is even in the list of arguments.如果这是一个赋值并且您必须使用+来分隔,那么您应该检查+是否在参数列表中。

int p = 0;
unsigned int i = 0;

/* check if any argument is '+' */
for(unsigned int j = 1; j < argc; j++) {
  if(strcmp(argv[j], "+") == 0) {
    p = 1;
    i = j;
  }
}

/* feel free to resize as needed */
char command0[256];
char command1[256];

if(p) {
  /* plus found, separate strings */

  /* get string before plus */
  for(unsigned int j = 1; j < i; j++) {
    strcat(command0, argv[j]);
    strcat(command0, " ");
  }

  /* get string after plus, i + 1 as i is zero based */
  for(unsigned int j = i + 1; j < argc; j++)
  {
    strcat(command1, argv[j]);
    strcat(command1, " ");
  }
}

strcat just copies the second string argument onto the end of the first, while also assuring the command is properly zero terminated. strcat只是将第二个字符串参数复制到第一个的末尾,同时还确保命令正确地以零结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM