在編程中遇到的一個可笑的問題,雖然解決,但是根本不知為什么能夠解決,兩種代碼在功能上是完全
等效的,且全部符合語法規范,但是前者的錯誤及其荒謬,而后者卻可以很好的執行,真是怪事!!!!!!
在編程中遇到的一個可笑的問題,雖然解決,但是根本不知為什么能夠解決,兩種代碼在功能上是完全
等效的,且全部符合語法規范,但是前者的錯誤及其荒謬,而后者卻可以很好的執行,真是怪事!!!!!!
求高手關于問題的答案,誰可以解釋這種現象?
系統:solaris linux(問題是一樣的)
工具:gclearcase/" target="_blank" >cc
-------------------------一個模擬system()的函數版本1-----------------------------------
#include"sysError.h"
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<signal.h>
int debug;
extern int errno;
int systemexec(const char* cmdstring){
pid_t pid;
int status;
struct sigaction ignore,saveintr,savequit;
sigset_t childmask,savemask;
if(cmdstring ==NULL)
err_sys("fork error");
ignore.sa_handler=SIG_IGN;
sigemptyset(&ignore.sa_mask);
ignore.sa_flags =0;
if(sigaction(SIGINT,&ignore, &saveintr)<0)
return -1;
if(sigaction(SIGQUIT,&ignore,&savequit)<0)
return -1;
sigemptyset(&childmask);
sigaddset(&childmask,SIGCHLD);
if(sigprocmask(SIG_BLOCK,&childmask,&savemask)<0)
return -1;
if(pid=fork()<0)//使用if else語句,出錯!!!!!!!改用switch則可以正常的運行
err_sys("fork error");
else if(pid == 0){
sigaction(SIGINT,&saveintr,NULL);
sigaction(SIGQUIT,&savequit,NULL);
sigprocmask(SIG_SETMASK,&savemask,NULL);
system("/bin/sh -c date");
//_exit(127);
}
else{
while(waitpid(pid,&status,0)<0)
if(errno=EINTR){
status=-1;
break;
}
}
if(sigaction(SIGINT,&saveintr,NULL)<0)
return (-1);
if(sigaction(SIGQUIT,&savequit,NULL)<0)
return (-1);
if(sigprocmask(SIG_SETMASK,&savemask,NULL)<0)
return (-1);
return (status);
}
int main(){
printf("the system was called now\n");
if ((debug=systemexec("date"))<0)
printf("the system call error is %d\n",debug);
printf("\n");
}
----------------------------------------------------------------------------------------
運行的結果:
五月 4日 2 00:27:04 CST 2005
the system call error is -1073746636
-bash-2.05b$ 五月 4日 2 00:27:04 CST 2005
the system call error is -1073746636
1:子進程運行了兩次,且execl函數不能返回;
2:(調試時出現SIG_TARP硬件中斷信號,試過多種處理方法,比如阻塞,忽略都不能奏效)
-----------------------------修改后的代碼--------------------------------------------
#include "sysError.h"
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
int debug;
int systemexec(const char* cmdstring){
pid_t pid;
int status;
char* cmd=cmdstring;
struct sigaction ignore,saveintr,savequit;
sigset_t childmask,savemask;
if(cmdstring ==NULL)
err_sys("the command paramter error");
ignore.sa_handler=SIG_IGN;
sigemptyset(&ignore.sa_mask);
ignore.sa_flags=0;
if(sigaction(SIGINT,&ignore,&saveintr)<0)
return -1;
if(sigaction(SIGQUIT,&ignore,&savequit)<0)
return -1;
sigemptyset(&childmask);
sigaddset(&childmask,SIGCHLD);
if(sigprocmask(SIG_BLOCK,&childmask,&savemask)<0)
return -1;
switch(pid=fork()){//使用switch語句,運行結果正常...
case -1:
status=-1;
printf("the fork operation error");
break;
case 0:
sigaction(SIGINT,&saveintr,NULL);
sigaction(SIGQUIT,&savequit,NULL);
sigprocmask(SIG_SETMASK,&savemask,NULL);
execl("/bin/sh","sh","-c" ,cmd ,(char*)0);
_exit(127);
default:
while(waitpid(pid,&status,0)<0)
if(errno=EINTR){
status=-1;
break;
}
if(sigaction(SIGINT,&saveintr,NULL)<0)
return (-1);
if(sigaction(SIGQUIT,&savequit,NULL)<0)
return (-1);
if(sigprocmask(SIG_SETMASK,&savemask,NULL)<0)
return (-1);
}
return (status);
}
int main(){
if ((debug=systemexec("date"))<0)
printf("the system call error is %d\n",debug);
printf("\n");
}
------------------------------------------------------------------------------------
運行結果:
五月 4日 2 00:34:12 CST 2005
運行正常,測試正常:
------------------------------------------------------------------------------------