linux程式設計基礎,系統限制,指令行參數 getopt,getopt_long
庫的使用
頭檔案:.h 裡面的函數及變量的聲明 比如#include
root@centos1 c]# ls /lib64/libc.so.6
/lib64/libc.so.6
檢視一個程式使用了哪些庫
ldd 可執行程式路徑
//wait.c代碼#include#include #include #include #include void child(int delay){
sleep(delay);
exit(0);
}void parent(int *status){
wait(status);
}
main(){
pid_t pid; int status;
printf("Before:%d\n",getpid());
pid=fork(); if(pid == 0){
child(10);
} if(pid >0 ){
printf("pid =%d\n",getpid());
parent(&status);
printf("status =%d\n",status);
}
}
[root@centos1 c]# ldd ./wait
linux-vdso.so.1 => (0x00007ffebd1d2000)
libc.so.6 => /lib64/libc.so.6 (0x000000333a200000)
/lib64/ld-linux-x86-64.so.2 (0x0000003339e00000)
編譯時預設連結c庫,如果使用其它庫編譯時需要用-l
比如使用數學庫
gcc -o m.c -lm -lc
系統限制
本身平台的類型:32位,64位
資料類型的限制:
位置根據機器
/usr/include/limits.h
/usr/lib/gcc/x86_64-redhat-linux/4.4.4/include/float.h
系統本身的限制
指令行:ulimit來修改和擷取
程式設計時使用:
getrlimit來擷取
setrlimit來設定
man getrlimit
#include #include int getrlimit(int resource, struct rlimit *rlim);int setrlimit(int resource, const struct rlimit *rlim);struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
};resource 的一些值
RLIMIT_CORE:core檔案的最大位元組數. core檔案是系統某個檔案出現異常退出時,系統為其儲存的上下文資訊,在gdb調試時常需要用
RLIMIT_CPU:cpu時間最大值(秒)
RLIMIT_DATA:一個程序資料段的最大位元組數
RLIMIT_FSIZE:可建立檔案的大小最大值
RLIMIT_NOFILE:每個程序可以打開的檔案的個數
RLIMIT_STACK:程序棧空間的最大值,使系統不會自動的動态修改這個限制
RLIMIT_VMEM:虛拟位址空間的最大值
RLIMIT_AS:系統程序可用記憶體空間最大值
指令行參數
選項:-l -a -i
參數: -l /etc
main函數的參數形式
#includeint main(int argc,char* argv[]){ int i=0; for(;i<argc;i++){
printf("argv[%d]=%s\n",i,argv[i]);
} return 0;
}/*[root@centos1 c]# ./arg -a -bl c
argv[0]=./arg
argv[1]=-a
argv[2]=-bl
argv[3]=c*/
指令行選項很多,提取時無需知道指令行參數的順序
getopt
getopt_long
長選項(一個字元串)和短選項(一個字元)
man 3 getopt 庫函數裡查找
#includeint getopt(int argc, char * const argv[],const char *optstring);extern char *optarg;extern int optind, opterr, optopt;
選項:一個選項一般完成不同功能的操作
參數:在執行相應選項功能操作時傳入的資訊
-a:選項
參數:-h host -u root -p 123456
為了識别指令行輸入資訊,getopt函數第三個參數的約定
1.如果就是一個字元,表示某個選項
2.如果一個字元後有1個冒号,表示選項後面一定要跟一個參數,參數可以緊跟選項或者與選項相隔一個空格
3.如果一個字元後有2個冒号,表示選項後可以有一個參數或沒有參數,在選項後的參數一定不能跟字元以空格間隔
ab:c::d::
a是一個選項
b後有冒号,其後内容一定是個參數
c,d後雙冒号,其後内容可以後也可以沒有,有的話一定緊挨
./getopt -a -b host -chello -d word
a是選項,host是b的參數
hello是c的參數
word和-d沒任何關系
getopt每成功執行一次,将傳回目前的一個選項
并且
extern char *optarg;//将向下一個要掃描的參數
extern int optind, //索引為下一個要處理的指針小标
opterr, //oprerr=0 不将錯誤輸出的标準錯誤輸出裝置
optopt;//用于處處可能的錯誤或者不可知的資訊
getopt.c
#include#include #include int main(int argc,char **argv)
{ int result;
opterr=0; while( (result = getopt(argc,argv,"ab:c::")) !=-1 )
{ switch(result)
{ case 'a':
printf("option=a,optopt=%c,optarg=%s\n",optopt,optarg); break; case 'b':
printf("option=b,optopt=%c,optarg=%s\n",optopt,optarg); break; case 'c':
printf("option=c,optopt=%c,optarg=%s\n",optopt,optarg); break; case '?':
printf("option=?,optopt=%c,optarg=%s\n",optopt,optarg); break; default:
printf("option=default,optopt=%c,optarg=%s\n",optopt,optarg);
}
printf("argv[%d]=%s\n",optind,argv[optind]);
}
printf("result=%d,optind=%d\n",result,optind); for(result=optind;result<argc;result++)
{
printf("-------argv[%d]=%s\n",result,argv[result] );
} for(result=1;result<argc;result++){
printf("\nat the end ---argv[%d]=%s\n",result,argv[result] );
} return 0;
}
[root@centos1 c]# ./getopt -b b1 -a1 -cc1 d
option=b,optopt=,optarg=b1
argv[3]=-a1
option=a,optopt=,optarg=(null)
argv[3]=-a1
option=?,optopt=1,optarg=(null)
argv[4]=-cc1
option=c,optopt=1,optarg=c1
argv[5]=d
result=-1,optind=5-------argv[5]=d
at the end ---argv[1]=-b
at the end ---argv[2]=b1
at the end ---argv[3]=-a1
at the end ---argv[4]=-cc1
at the end ---argv[5]=d
長選項:這個選項由一個字元串組成,在選項很多的時候容易記憶
#includeint getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt;
#include int getopt_long( int argc,
char * const argv[], const char *optstring, const struct option *longopts,
int *longindex);
optstring:一般為一個字元串常量,代表所有的短選項,就是一般以"-"開頭的選項,
如果選項後帶參數,則必須在相應的字元後面加":",如"ab:cde:"
longindex參數如果沒有設定為NULL,那麼它就指向一個變量,這個變量會被指派為尋找到的長選項在longopts中的索引值,這可以用于錯誤診斷
幾種傳回值
0 getopt_long()設定一個标志,它的值與option結構中的val字段的值一樣
1 每碰到一個指令行參數,optarg都會記錄它
'?' 無效選項
':' 缺少選項參數
'x' 選項字元'x'
-1 選項解析結束
struct option { const char *name; //長選項名
int has_arg; //是否有參數 0、1、2,分别表示沒有參數、有參數、參數可選
int *flag; int val; //傳回值,短選項值
};
flag如果為NULL,函數傳回val的值,
否則将val的值寫入flag指向的變量中,
一般情況下,如果flag為NULL,則val的值為該長選項對應的短選項
短選項 長選項
-h --help
-o filename --output filename
-v --version
第三個參數 :短選項方式的格式
ho:v
第四個參數struct
struct option my_option={
{"help",0,NULL,'h'},
{"output",1,NULL,'o'},
{"version",0,NULL,'v'}
}
長選項使用
#include#include #include #include int main(int argc,char *argv[])
{ int opt; struct option longopts[] = {
{"initialize",0,NULL,'i'},
{"file",1,NULL,'f'},
{"list",0,NULL,'l'},
{"restart",0,NULL,'r'},
{"help",0,NULL,'h'},
{"output",1,NULL,'o'},
{"version",0,NULL,'v'}
}; while((opt = getopt_long(argc, argv, "if:lrho:v", longopts, NULL)) != -1){ switch(opt){ case ':':
printf("option needs a value\n"); break; case '?':
printf("unknown option: %c\n",optopt); break; default:
printf("opt=%c,optind=%d,optarg=%s\n",opt,optind,optarg);
}
}
}
[root@centos1 c]# ./getopt_long -v -i --file a.php -l --eee -o
opt=v,optind=2,optarg=(null)
opt=i,optind=3,optarg=(null)
opt=f,optind=5,optarg=a.php
opt=l,optind=6,optarg=(null)
./getopt_long: unrecognized option '--eee'unknown option:
./getopt_long: option requires an argument -- 'o'unknown option: o