库的使用
头文件:.h 里面的函数及变量的声明 比如#include <stdio.h> ,Linux下默认头文件的搜索路径
系统定义的头文件:
/usr/include
/usr/local/include
/usr/target/include (平台不同路径不同)
库文件:/lib64
c库函数

root@centos1 c]# ls /lib64/libc.so.6
/lib64/libc.so.6

查看一个程序使用了哪些库
ldd 可执行程序路径

  1. //wait.c代码
  2. #include <sys/wait.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. void child(int delay){
  9. sleep(delay);
  10. exit();
  11. }
  12.  
  13. void parent(int *status){
  14. wait(status);
  15. }
  16.  
  17. main(){
  18. pid_t pid;
  19. int status;
  20. printf("Before:%d\n",getpid());
  21. pid=fork();
  22. if(pid == ){
  23. child();
  24. }
  25. if(pid > ){
  26. printf("pid =%d\n",getpid());
  27. parent(&status);
  28. printf("status =%d\n",status);
  29. }
  30. }

[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来设置

  1. man getrlimit
  2. #include <sys/time.h>
  3. #include <sys/resource.h>
  4.  
  5. int getrlimit(int resource, struct rlimit *rlim);
  6. int setrlimit(int resource, const struct rlimit *rlim);
  7.  
  8. struct rlimit {
  9. rlim_t rlim_cur; /* Soft limit */
  10. rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
  11. };
  12. resource 的一些值
  13. RLIMIT_CORE:core文件的最大字节数. core文件是系统某个文件出现异常退出时,系统为其保存的上下文信息,在gdb调试时常需要用
  14. RLIMIT_CPU:cpu时间最大值(秒)
  15. RLIMIT_DATA:一个进程数据段的最大字节数
  16. RLIMIT_FSIZE:可创建文件的大小最大值
  17. RLIMIT_NOFILE:每个进程可以打开的文件的个数
  18. RLIMIT_STACK:进程栈空间的最大值,使系统不会自动的动态修改这个限制
  19. RLIMIT_VMEM:虚拟地址空间的最大值
  20. RLIMIT_AS:系统进程可用内存空间最大值

 命令行参数

选项:-l -a -i
参数: -l /etc
main函数的参数形式

  1. #include <stdio.h>
  2.  
  3. int main(int argc,char* argv[]){
  4. int i=;
  5. for(;i<argc;i++){
  6. printf("argv[%d]=%s\n",i,argv[i]);
  7. }
  8. return ;
  9. }
  10. /*
  11. [root@centos1 c]# ./arg -a -bl c
  12. argv[0]=./arg
  13. argv[1]=-a
  14. argv[2]=-bl
  15. argv[3]=c
  16. */

命令行选项很多,提取时无需知道命令行参数的顺序
getopt
getopt_long
长选项(一个字符串)和短选项(一个字符)

man 3 getopt  库函数里查找

  1. #include <unistd.h>
  2.  
  3. int getopt(int argc, char * const argv[],const char *optstring);
  4.  
  5. extern char *optarg;
  6. 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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. int main(int argc,char **argv)
  6. {
  7. int result;
  8. opterr=;
  9. while( (result = getopt(argc,argv,"ab:c::")) !=- )
  10. {
  11. switch(result)
  12. {
  13. case 'a':
  14. printf("option=a,optopt=%c,optarg=%s\n",optopt,optarg);
  15. break;
  16. case 'b':
  17. printf("option=b,optopt=%c,optarg=%s\n",optopt,optarg);
  18. break;
  19. case 'c':
  20. printf("option=c,optopt=%c,optarg=%s\n",optopt,optarg);
  21. break;
  22. case '?':
  23. printf("option=?,optopt=%c,optarg=%s\n",optopt,optarg);
  24. break;
  25. default:
  26. printf("option=default,optopt=%c,optarg=%s\n",optopt,optarg);
  27.  
  28. }
  29. printf("argv[%d]=%s\n",optind,argv[optind]);
  30. }
  31.  
  32. printf("result=%d,optind=%d\n",result,optind);
  33.  
  34. for(result=optind;result<argc;result++)
  35. {
  36. printf("-------argv[%d]=%s\n",result,argv[result] );
  37. }
  38.  
  39. for(result=;result<argc;result++){
  40. printf("\nat the end ---argv[%d]=%s\n",result,argv[result] );
  41. }
  42. return ;
  43. }
  1. [root@centos1 c]# ./getopt -b b1 -a1 -cc1 d
  2. option=b,optopt=,optarg=b1
  3. argv[]=-a1
  4. option=a,optopt=,optarg=(null)
  5. argv[]=-a1
  6. option=?,optopt=,optarg=(null)
  7. argv[]=-cc1
  8. option=c,optopt=,optarg=c1
  9. argv[]=d
  10. result=-,optind=
  11. -------argv[]=d
  12.  
  13. at the end ---argv[]=-b
  14.  
  15. at the end ---argv[]=b1
  16.  
  17. at the end ---argv[]=-a1
  18.  
  19. at the end ---argv[]=-cc1
  20.  
  21. at the end ---argv[]=d

 长选项:这个选项由一个字符串组成,在选项很多的时候容易记忆

  1. #include <unistd.h>
  2.  
  3. int getopt(int argc, char * const argv[],
  4. const char *optstring);
  5.  
  6. extern char *optarg;
  7. extern int optind, opterr, optopt;
  8.  
  9. #include <getopt.h>
  10.  
  11. int getopt_long(
  12. int argc,
  13. char * const argv[],
  14. const char *optstring,
  15. const struct option *longopts,
  16. int *longindex);

optstring:一般为一个字符串常量,代表所有的短选项,就是一般以"-"开头的选项,
  如果选项后带参数,则必须在相应的字符后面加":",如"ab:cde:"

longindex参数如果没有设置为NULL,那么它就指向一个变量,这个变量会被赋值为寻找到的长选项在longopts中的索引值,这可以用于错误诊断
几种返回值
0 getopt_long()设置一个标志,它的值与option结构中的val字段的值一样
1 每碰到一个命令行参数,optarg都会记录它
'?' 无效选项
':' 缺少选项参数
'x' 选项字符'x'
-1 选项解析结束

  1. struct option {
  2. const char *name; //长选项名
  3. int has_arg; //是否有参数 0、1、2,分别表示没有参数、有参数、参数可选
  4. int *flag;
  5. int val; //返回值,短选项值
  6. };

flag如果为NULL,函数返回val的值,
否则将val的值写入flag指向的变量中,
一般情况下,如果flag为NULL,则val的值为该长选项对应的短选项

短选项 长选项
-h --help
-o filename --output filename
-v --version
第三个参数 :短选项方式的格式
ho:v
第四个参数struct

  1. struct option my_option={
  2. {"help",,NULL,'h'},
  3. {"output",,NULL,'o'},
  4. {"version",,NULL,'v'}
  5. }

长选项使用

getopt_long.c

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <getopt.h>
  4. #include <stdlib.h>
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8. int opt;
  9. struct option longopts[] = {
  10. {"initialize",,NULL,'i'},
  11. {"file",,NULL,'f'},
  12. {"list",,NULL,'l'},
  13. {"restart",,NULL,'r'},
  14. {"help",,NULL,'h'},
  15. {"output",,NULL,'o'},
  16. {"version",,NULL,'v'}
  17.  
  18. };
  19.  
  20. while((opt = getopt_long(argc, argv, "if:lrho:v", longopts, NULL)) != -){
  21. switch(opt){
  22. case ':':
  23. printf("option needs a value\n");
  24. break;
  25. case '?':
  26. printf("unknown option: %c\n",optopt);
  27. break;
  28. default:
  29. printf("opt=%c,optind=%d,optarg=%s\n",opt,optind,optarg);
  30. }
  31.  
  32. }
  33. }

执行结果

  1. [root@centos1 c]# ./getopt_long -v -i --file a.php -l --eee -o
  2. opt=v,optind=,optarg=(null)
  3. opt=i,optind=,optarg=(null)
  4. opt=f,optind=,optarg=a.php
  5. opt=l,optind=,optarg=(null)
  6. ./getopt_long: unrecognized option '--eee'
  7. unknown option:
  8. ./getopt_long: option requires an argument -- 'o'
  9. unknown option: o

linux编程基本的更多相关文章

  1. 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结 转载

    基础篇:操作系统.计算机网络.设计模式 一:操作系统 1. 进程的有哪几种状态,状态转换图,及导致转换的事件. 2. 进程与线程的区别. 3. 进程通信的几种方式. 4. 线程同步几种方式.(一定要会 ...

  2. 【转】牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结

    基础篇:操作系统.计算机网络.设计模式 一:操作系统 1. 进程的有哪几种状态,状态转换图,及导致转换的事件. 2. 进程与线程的区别. 3. 进程通信的几种方式. 4. 线程同步几种方式.(一定要会 ...

  3. Linux 编程中的API函数和系统调用的关系【转】

    转自:http://blog.chinaunix.net/uid-25968088-id-3426027.html 原文地址:Linux 编程中的API函数和系统调用的关系 作者:up哥小号 API: ...

  4. linux编程获取本机网络相关参数

    getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程   ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...

  5. 面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结

    基础篇:操作系统.计算机网络.设计模式 一:操作系统 1. 进程的有哪几种状态,状态转换图,及导致转换的事件. 2. 进程与线程的区别. 3. 进程通信的几种方式. 4. 线程同步几种方式.(一定要会 ...

  6. Linux编程简介

    Linux编程可以分为Shell(如BASH.TCSH.GAWK.Perl.Tcl和Tk等)编程和高级语言(C语言,C++语言,java语言等)编程,Linux程序需要首先转化为低级机器语言即所谓的二 ...

  7. Linux编程return与exit区别

    Linux编程return与exit区别 exit  是用来结束一个程序的执行的,而return只是用来从一个函数中返回. return return 表示从被调函数返回到主调函数继续执行,返回时可附 ...

  8. linux 编程技术

    linux 编程技术No.1前期准备工作 GCC的编译过程分为预处理.生成汇编代码.生成目标代码和链接成可执行文件等4个步骤. 使用vim编写C 文件 : [lining@localhost prog ...

  9. Linux编程之给你的程序开后门

    这里说的"后门"并不是教你做坏事,而是让你做好事,搭建自己的调试工具更好地进行调试开发.我们都知道,当程序发生异常错误时,我们需要定位到错误,有时我们还想,我们在不修改程序的前提下 ...

  10. 笔记整理--Linux编程

    linux c编程open() read() write()函数的使用方法及实例 | 奶牛博客 - Google Chrome (2013/8/31 17:56:10) 今天把文件IO操作的一些东东整 ...

随机推荐

  1. [UI基础][QQ登陆界面]

    [目标] 1.QQ号码文本框要有“请输入QQ号码”的提示(用户输入时会自动消失) 2.QQ密码文本框要有“请输入QQ密码”的提示(用户输入文字会自动消失) 3.QQ号码文本框只能输入数字 4.QQ密码 ...

  2. exception disappear when forgot to await an async method

    https://github.com/aspnet/AspNetWebStack/issues/235 https://stackoverflow.com/questions/5383310/catc ...

  3. python swap

    swap里面的a,b 不会影响函数作用域外面的变量 java也不可以的吧:python里面没有指针,你可以认为所有的东西都是指向的内容,但是不要试图去改变指针的值 其实我觉得所有的对象都是不可变对象, ...

  4. Codeforces Round #398 (Div. 2) A,B,C,D

    A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. Missing artifact com.github.pagehelper:pagehelper:jar:3.4.2-fix的解决方法

    使用pagehelper.3.4.2.jar时报错,应该是无法从网络上下载该jar. 我的解决方案是: 从网络上下载一个pagehelper.3.4.2.jar包,然后复制到.m2目录中 如我的目录是 ...

  6. wpf 中关于Image中样式Style的一点总结

    第一种写法: (1):定义样式 <Style x:Key="imgStyle" TargetType="Image">  : <!-- Tar ...

  7. C#正则_取出标签内的内容(非贪婪)

    using System.Text.RegularExpressions; /// <summary>        /// 执行正则提取出值        /// </summar ...

  8. Rails Guide--Working with JavaScript in Rails; 如何把jquery转化为原生js

    1 An Introduction to Ajax 打开网页的的过程也叫:request response cycel. JavaScript也可以request然后parse the respons ...

  9. 使用yum安装pip

    PIP 简介:pip 是一个现代的,通用的 Python 包管理工具.提供了对 Python 包的查找.下载.安装.卸载的功能.功能类似于RedHat里面的yum 使用yum安装pip 因为测试环境搭 ...

  10. 前端ps切图,图文教程,详细。

    https://blog.csdn.net/OBKoro1/article/details/69817571 1.下载 我现在使用的版本号:PS-CS6,网上很多破解版本的自行搜索下载. 2.安装好P ...