-1

I'm writing pseudo-shell and now write parallel-command functionality. In this question ( Differences between fork and exec ) I'm found how I can call execv with returning to my code execution. But I don't understand how I can call two parallel programms with continue after. How I see it:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() 
{
    char *argv_for_program_1[] = {"ls" , NULL};
    int pid_1 = fork();
    if (pid_1 == 0)
    {     
        execv("/usr/bin/ls" , argv_for_program_1); //print ls
    }
    char *argv_for_program_2[] = {"pwd" , NULL};
    int pid_2 = fork();
    if (pid_2 == 0)
    {     
        execv("/usr/bin/pwd" , argv_for_program_2); //print pwd
    }
    wait(0);
    printf("continue");  //print continue
}

But this code run ls -> continue -> pwd. What should I change?

bazylevnik0
  • 60
  • 1
  • 7
  • The code show does not appear to do what you claim. It will only run the first program once. And how do you expect the second program to ever run considering the code that attempts to do that is commented out? It's really not clear what your real code is actually doing and what you expect it to do. Please provide complete code as a [mre]. – kaylum Sep 29 '22 at 21:22
  • @kaylum ok. I added the whole program. – bazylevnik0 Sep 29 '22 at 21:27
  • Please reduce the code to a [mre]. It is not only required for Stack Overflow but will also help you debug and understand for yourself. For example, remove all the command line parsing and just hard code some commands. Then write a function to try and run those in parallel. – kaylum Sep 29 '22 at 21:38
  • @kaylum corrected... – bazylevnik0 Sep 29 '22 at 22:14

2 Answers2

1

Here is your problem with strtok from man : " The strtok() function breaks a string into a sequence of zero or more nonempty tokens. On the first call to strtok(), the string to be parsed should be specified in str. In each sub‐ sequent call that should parse the same string, str must be NULL."

this code should fix it :

...
          if ( found_parallel ) {            /// !!! this place
            printf("%s\n","parallel");
            char *name_of_program_1;
            name_of_program_1 = strtok(line, separator);
            char *temp;
            temp = strtok(NULL, separator);
            char *name_of_program_2;
            name_of_program_2 = strtok(NULL, separator);

            char path_1[100] = "/usr/bin/";
            strcat(path_1, name_of_program_1);
            char path_2[100] = "/usr/bin/";
            strcat(path_2, name_of_program_2);
...
l_-A-_l
  • 142
  • 8
0

This code should fix it( two wait) :

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main() 
{
    char *argv_for_program_1[] = {"ls" , NULL};
    int pid_1 = fork();
    if (pid_1 == 0)
    {     
        execv("/usr/bin/ls" , argv_for_program_1); //print ls
    }
    char *argv_for_program_2[] = {"pwd" , NULL};
    int pid_2 = fork();
    if (pid_2 == 0)
    {     
        execv("/usr/bin/pwd" , argv_for_program_2); //print pwd
    }
    wait(0);
    wait(0); //!!!
    printf("continue");  //print continue
}
bazylevnik0
  • 60
  • 1
  • 7