简体   繁体   中英

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

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. Either use a higher level threading API or use the standard for POSIX, pthreads. When you compile just make sure you link with -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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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