Search code examples
cswitch-statementfork

My switch statement isn't taking the value from my for loop


In class i was assigned a project to write a code to create three children and give the pid using a switch statement. My issue is that i don't know how to get my switch statement to take the value from my loop which creates children and do what is inside the switch.

int main (){
int i;
pid_t childpid;
//first child
char *my_codeOne = "ls";
char *my_argvOne[] = {"ls", "-1", NULL};
//second child
char *my_codeTwo = "ps";
char *my_argvTwo[] = {"ps", NULL};
//third child
char *my_codeThree = "/bin/sh";
char *my_argvThree[] = {"/bin/sh", "-c", "echo $PATH", NULL};

for(i = 0; i < 3; i++){

    childpid = fork();

    if((childpid = fork()) == -1){
        printf("Error");
    }else if ((childpid = fork()) == 0){
        break;
    }
}

//make switch statement feed from loop
//use execvp (vector or array of param) p = the current path environment
//variable

switch(i) {
    case 0:
        execvp (my_codeOne, my_argvOne);
        printf("%ld", (long)childpid);
        break;
    case 1:
        execvp (my_codeTwo, my_argvTwo);
        printf("%ld", (long)childpid);
        break;
    case 2:
        execvp (my_codeThree, my_argvThree);
        printf("%ld", (long)childpid);
        break;
    default:
        break;
}

Also i'm fairly certain my loop is making way more children than it is supposed to. Any help is helpful, thanks!

EDIT: I'm using xcode, and am getting a notification about a breakpoint as soon as my switch statement shows up.

EDIT: This is my updated code and output. It still won't print the pid though, so how do it make it print the pid?

 for(i = 0; i < 3; i++){
    childpid = fork();
    //removed fork from if statement
    if(childpid == -1){
        printf("Error");
    }else if (childpid == 0){
        break;
    }
}
//printf("Exited");
//make switch statement feed from loop
//use execvp (vector or array of param) p = the current path environment variable
switch(i) {//removed breakpoint notifications so this would go through
    case 0:
        execvp (my_codeOne, my_argvOne);
        printf("%ld\n", (long)childpid);
        break;
    case 1:
        execvp (my_codeTwo, my_argvTwo);
        printf("%ld\n", (long)childpid);
        break;
    case 2:
        execvp (my_codeThree, my_argvThree);
        printf("%ld\n", (long)childpid);
        break;
    default:
        break;
}

Output:

ClassProject PID TTY TIME CMD /Applications/Xcode.app/Contents/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin Program ended with exit code: 0


Solution

  • Your if inside the loop has multiple problems:

    1. you have multiple forks there
    2. your if - conditions contain assignments rather than comparisions

    i guess it should look like

    for(i = 0; i < 3; i++){
        childpid = fork(); // do the fork
    
        if(childpid == -1){       // check if folk failed
            printf("Error");
        }else if (childpid == 0){ // am i the child?
            break;
        }
    }