if和else不是只能进入一个么?那么下面的程序中是怎么进入了if又进入了else呢?求高手解答!
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>voidmain(){...
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void main()
{
pid_t pid1,pid2;
char ch1,ch2,ch3;
pid1=fork();
ch1='b';
ch2='c';
ch3='a';
if(pid1<0){
fprintf(stderr,"Fork 1 Failed");
exit(-1);
}
else if (pid1==0){
printf( "%c\n",ch1 );
}
else{
wait(NULL);
printf( "%c\n",ch3);
exit(0);
}
pid2=fork();
if(pid2<0){
fprintf(stderr,"Fork 2");
exit(-1);
}
else if (pid2==0){
printf( "%c\n",ch2 );
}
else{
wait(NULL);
printf( "%c\n",ch3);
exit(0);
}
} 展开
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void main()
{
pid_t pid1,pid2;
char ch1,ch2,ch3;
pid1=fork();
ch1='b';
ch2='c';
ch3='a';
if(pid1<0){
fprintf(stderr,"Fork 1 Failed");
exit(-1);
}
else if (pid1==0){
printf( "%c\n",ch1 );
}
else{
wait(NULL);
printf( "%c\n",ch3);
exit(0);
}
pid2=fork();
if(pid2<0){
fprintf(stderr,"Fork 2");
exit(-1);
}
else if (pid2==0){
printf( "%c\n",ch2 );
}
else{
wait(NULL);
printf( "%c\n",ch3);
exit(0);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询