����Linux�Ż�
Linux����

Linux system������һЩע������

����ʱ��:2016-11-10 15:04:34��Դ:linux��վ����:�¸���
���ճ��Ĵ������У����ǿ�������system  ����ȥ����һЩ�����Լ�����õ����� �� ����ȡ���ķ���ֵ��
 
������ԭ�����£�
int system(const char *command);
 
��һ�����Լ�д�Ĵ��룺
int main(void)   
{  
unsigned  int val ; 
val = system("./a.out") ;  
val = WEXITSTATUS(val);    
printf("vl = %d\n" , val) ;
return 0 ;   
}
 
��δ�����ֱ�ӵ����˵�ǰ�ļ���a.out�ļ�������ȡ�����ķ���ֵ��
 
�������ؼ�һ���������ֵ��ע������ȿ���man system  ������ڷ���ֵ��˵����
RETURN VALUE
The value returned is -1 on error (e.g.  fork(2) failed), and the  return
status  of  the  command  otherwise.  This latter return status is in the
format specified in wait(2).  Thus, the exit code of the command will  be
WEXITSTATUS(status).   In  case  /bin/sh  could not be executed, the exit
status will be that of a command that does exit(127).
If the value of command is NULL, system() returns nonzero if the shell is
available, and zero if not.
system() does not affect the wait status of any other children.
 
����ֵ��Ϊ3�ࣺ
��һ�ࣺ��� system �����ӽ���ʧ�ܣ����᷵�� -1;
�ڶ���: ��� system  ִ������ʧ�ܣ�����127;
�����ִࣺ�������ӽ�������ɹ��������ӽ��������ص�ֵ��
���ֵ��������ġ����ֵ��һ��16λ��ֵ��
��8λ����ŵ��������ִ������ʧ�ܣ�����ŵ�ֵ��127 == 7��1����
��8λ����ŵ�����ִ�е���������������ֵ��
�������8λ�����⣬�������ؽ�һ�£�
 
ͨ������ij���������һ���궨�壺
val = WEXITSTATUS(val);
���ֱ�����ƶ�8λ��
ע�����ֵֻ�а�λ�������������һ�����ڰ�λ��ֵ�����ͻ���������޷������һ��״��������ֵ�е㲻�����ǵ����룺
 
��һ���ҵ� a.out Դ���룺
int main(void)   
{  
printf("hello kitty \n") ; 
return 255 ; 
}
 
�����ҵķ���ֵ��255������ִ��һ��system �Ǹ����򣬵ó������
aplex@aplex:~/test/chen_test$ ./test 
hello kitty 
vl = 255
 
����ֵ���������ǣ����ٰѷ���ֵ��1��
int main(void)   
{  
printf("hello kitty \n") ; 
return 256 ; 
}
 
��ִ��system �������룺
aplex@aplex:~/test/chen_test$ ./test 
hello kitty 
vl = 0
û������ֻ�ܷ���0~255 ��ֵ������8λ�Ͳ����������ء�
 
�������ø��µ�ַ��//m.ajphoenix.com/linux/25898.html