库的使用
头文件:.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. 解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题

    mybatis中mapUnderscoreToCamelCase的使用 mybatis-config.xml配置: <?xml version="1.0" encoding= ...

  2. vs显示行号

    1.工具 2.选项 3.文本编辑器 4.C\C++ 5.常规->显示行号

  3. postman中如何使用OAuth

    https://learning.getpostman.com/docs/postman/sending_api_requests/authorization/ Authorization The a ...

  4. POJ 1047 Round and Round We Go

    https://vjudge.net/problem/POJ-1047 题意: 给一个整数,它的长度为n,从1开始一直到n和该整数相乘,判断每次结果是否和原来的整数是循环的. 思路: 大整数的乘法. ...

  5. 探索解析微服务下的RabbitMQ

    概览 本文主要介绍如何使用RabbitMQ消息代理来实现分布式系统之间的通信,从而促进微服务的松耦合. RabbitMQ,也被称为开源消息代理,它支持多种消息协议,并且可以部署在分布式系统上.它轻量级 ...

  6. ActiveMQ---知识点整理

    本文来自于csdn,文章通过介绍ActiveMQ的安装,使用,搭建等等,简单整理了ActiveMQ. 本文转自:http://www.uml.org.cn/zjjs/201802111.asp 一.背 ...

  7. UVA-10603 Fill (BFS)

    题目大意:有三个已知体积但不知刻度的杯子,前两个杯子中初始时没有水,第三个装满水,问是否可以倒出d升水,如果倒不出,则倒出一个最大的d’,使得d’<=d,并且在这个过程中要求总倒水量最少. 题目 ...

  8. 平衡二叉树,AVL树之代码篇

    看完了第一篇博客,相信大家对于平衡二叉树的插入调整以及删除调整已经有了一定的了解,下面,我们开始介绍代码部分. 首先,再次提一下使用的结构定义 typedef char KeyType; //关键字 ...

  9. Asp.Net使用org.in2bits.MyXls.dll操作excel的应用

    首先下载org.in2bits.MyXls.dll(自己的在~\About ASP.Net\Asp.Net操作excel) 添加命名空间: using org.in2bits.MyXls;using ...

  10. 一个简单的观察者模式的JS实现

    这不是原创文章,主要是写给自己看的.原文比较详细容易让人,我提取最简单最好理解的部分,便于我以后用到.参考http://www.cnblogs.com/TomXu/archive/2012/03/02 ...