库的使用
头文件:.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 可执行程序路径

//wait.c代码
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> void child(int delay){
sleep(delay);
exit();
} void parent(int *status){
wait(status);
} main(){
pid_t pid;
int status;
printf("Before:%d\n",getpid());
pid=fork();
if(pid == ){
child();
}
if(pid > ){
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 <sys/time.h>
#include <sys/resource.h> 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函数的参数形式

#include <stdio.h>

int main(int argc,char* argv[]){
int i=;
for(;i<argc;i++){
printf("argv[%d]=%s\n",i,argv[i]);
}
return ;
}
/*
[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  库函数里查找

#include <unistd.h>

int 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h> int main(int argc,char **argv)
{
int result;
opterr=;
while( (result = getopt(argc,argv,"ab:c::")) !=- )
{
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=;result<argc;result++){
printf("\nat the end ---argv[%d]=%s\n",result,argv[result] );
}
return ;
}
[root@centos1 c]# ./getopt -b b1 -a1 -cc1 d
option=b,optopt=,optarg=b1
argv[]=-a1
option=a,optopt=,optarg=(null)
argv[]=-a1
option=?,optopt=,optarg=(null)
argv[]=-cc1
option=c,optopt=,optarg=c1
argv[]=d
result=-,optind=
-------argv[]=d at the end ---argv[]=-b at the end ---argv[]=b1 at the end ---argv[]=-a1 at the end ---argv[]=-cc1 at the end ---argv[]=d

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

#include <unistd.h>

       int getopt(int argc, char * const argv[],
const char *optstring); extern char *optarg;
extern int optind, opterr, optopt; #include <getopt.h> 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",,NULL,'h'},
{"output",,NULL,'o'},
{"version",,NULL,'v'}
}

长选项使用

getopt_long.c

#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h> int main(int argc,char *argv[])
{
int opt;
struct option longopts[] = {
{"initialize",,NULL,'i'},
{"file",,NULL,'f'},
{"list",,NULL,'l'},
{"restart",,NULL,'r'},
{"help",,NULL,'h'},
{"output",,NULL,'o'},
{"version",,NULL,'v'} }; while((opt = getopt_long(argc, argv, "if:lrho:v", longopts, NULL)) != -){
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=,optarg=(null)
opt=i,optind=,optarg=(null)
opt=f,optind=,optarg=a.php
opt=l,optind=,optarg=(null)
./getopt_long: unrecognized option '--eee'
unknown option:
./getopt_long: option requires an argument -- 'o'
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. Python3基础 print(,end=) 输出内容的末尾加入空格

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. [参考]ASCII对照表 及 字符与二进制、十进制、16进制之间的转化(C/C++)

    第1节 ASCII码对照表 1.1 ASCII控制字符 1.2 ASCII可显示字符 第2节字符的进制转换 2.1 获取字符(8位)的上四位和下四位 2.2 获取字符(上表中的‘图形’)所对应的十六进 ...

  3. (转载)ubuntu卸载opencv并重装opencv3.0.0

    ubuntu卸载opencv并重装opencv3.0.0 一. 卸载opencv2.4.9: Going to the "build" folder directory of op ...

  4. 获取lambda表达式类型,获取attributes是注意事项

    1.获取lambda表达式的MemberExpression所属类的类型,要使用:m.Expression.Type   而不要使用 m.Member.DeclaringType: 后者获取的是实际定 ...

  5. NOI导刊 2009 提高二

    开灯 题目大意 对编号为\([i \times a]\)的灯进行操作,找出操作数为奇数的那一个 题目分析 难度: 入门 因为看到操作数为奇数,因此直接进行位运算,做亦或和 打砖块 题目分析 第一眼看上 ...

  6. POJ 1239 Increasing Sequences(经典的两次dp)

    http://poj.org/problem?id=1239 题意:给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大. 思路:先从头到尾进行一次dp, ...

  7. 测试工程师 技能要求:java编程,数据库,linux操作系统,自动化测试开发,性能测试,安全测试

    1.具有开发和测试经验,能负责带领大型项目测试 2.掌握或熟悉java编程,熟悉mysql,oracle等db,memcache,nosql等,有互联网行业从业经验 3.可撰写自动化测试工具以及搭建自 ...

  8. MaintainableCSS 《可维护性 CSS》 --- 模板篇

    什么是模块(Modules) ? 模块是一个特别的独立单元,可以与其他模块组合以形成更复杂的结构. 在客厅里,我们可以认为电视,沙发和墙艺术是模块.它们聚在一起创造一个可用的房间. 如果我们把其中一个 ...

  9. m_Orchestrate learning system---二十八、字體圖標iconfont到底是什麼

    m_Orchestrate learning system---二十八.字體圖標iconfont到底是什麼 一.总结 一句话总结: 阿里巴巴 图标库 iconfont-阿里巴巴矢量图标库 1.表格的t ...

  10. @ResponseBody和@ResponseEntity注解

    如果需要返回json格式的数据,那么添加该注解就行了@ResponseBody对于ResponseEntity确切的说是ResponseEntity ,如果你即需要返回数据,又需要确定数据的状态,就用 ...