我的 switch 语句没有从我的 for 循环中获取值
My switch statement isn't taking the value from my for loop
在 class 中,我被分配了一个项目来编写代码来创建三个 children 并使用 switch 语句提供 pid。我的问题是我不知道如何让我的 switch 语句从创建 children 的循环中获取值并执行开关内部的操作。
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;
}
此外,我相当确定我的循环比预期的要多 children。任何帮助都是有帮助的,谢谢!
编辑:我正在使用 xcode,并且在我的 switch 语句出现后立即收到有关断点的通知。
编辑:这是我更新的代码和输出。它仍然不会打印 pid,那么如何让它打印 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;
}
输出:
类项目
PID TTY 时间命令
/Applications/Xcode.app/Contents/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin
程序以退出代码结束:0
您的 if
循环内有多个问题:
- 那里有多个
forks
- 您的
if
- 条件包含赋值而不是比较
我猜它应该看起来像
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;
}
}
在 class 中,我被分配了一个项目来编写代码来创建三个 children 并使用 switch 语句提供 pid。我的问题是我不知道如何让我的 switch 语句从创建 children 的循环中获取值并执行开关内部的操作。
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;
}
此外,我相当确定我的循环比预期的要多 children。任何帮助都是有帮助的,谢谢!
编辑:我正在使用 xcode,并且在我的 switch 语句出现后立即收到有关断点的通知。
编辑:这是我更新的代码和输出。它仍然不会打印 pid,那么如何让它打印 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;
}
输出:
类项目 PID TTY 时间命令 /Applications/Xcode.app/Contents/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin 程序以退出代码结束:0
您的 if
循环内有多个问题:
- 那里有多个
forks
- 您的
if
- 条件包含赋值而不是比较
我猜它应该看起来像
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;
}
}