- 论坛徽章:
- 0
|
if, else if, else 组合 与 switch 在三个选项的情况下,哪个快一点。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main() {
int pid;
if((pid = fork()) < 0) {
printf("Fork failed!");
exit(1);
}
else if(!pid) {
printf("Start Mysql... \n");
char* mysql[] = {"mysqld", "start", NULL};
execv("/etc/init.d/mysqld", mysql);
perror("Mysql failed! \n");
}
else {
wait(NULL);
printf("Start Apache... \n");
char* apache[] = {"httpd", "start", NULL};
execv("/etc/init.d/httpd", apache);
perror("Apache failed! \n");
exit(0);
}
}
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main() {
int pid;
switch(pid = fork()) {
case -1:
printf("Fork failed!");
exit(1);
case 0:
printf("Start Mysql... \n");
char* mysql[] = {"mysqld", "start", NULL};
execv("/etc/init.d/mysqld", mysql);
perror("Mysql failed! \n");
default:
wait(NULL);
printf("Start Apache... \n");
char* apache[] = {"httpd", "start", NULL};
execv("/etc/init.d/httpd", apache);
perror("Apache failed! \n");
exit(0);
}
} |
|
|