第四章 Linux环境
来源:互联网 发布:淘宝如何做企业店铺 编辑:程序博客网 时间:2024/05/22 06:58
2016/9/19 10:08:46
main函数:
int main(int argc, char *argv[])1. argc表示参数的个数2. argv表示实际的参数
args.c
#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){ int arg; for (arg = 0; arg < argc; arg++){ if (argv[arg][0] == '-'){ printf("option: %s\n",argv[arg]+1); printf("%x,%x,%x",(int)argv[arg][0], (int)argv[arg]+1, (int)argv[arg+1]); } else printf("argument %d: %s\n", arg, argv[arg]); } exit(0);}
getopt
#include <unistd.h>int getopt(int argc, char *const argv[], const char *optstring);extern char *optarg;extern int optind, opterr, optopt;1. getopt的返回值是argv数组中的下一个选项字符 2. optstring:选项字符列表,如果一个选项字符列表后面跟了一个冒号(:),则表明该选项有一个关联值作为下一个参数3. 如果选项有一个关联值,则外部变量optarg指向该值4. 外部optind被设置为下一个待处理参数的索引5. 如果选项处理完毕,getopt返回-1.特殊参数--将使getopt停止扫描选项6. 如果一个选项要求有一个关联值,但是用户并没有提供这个值,getopt通畅返回一个问号(?)
argopt
#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(int argc, int argv[]){ int opt; while ((opt = getopt(argc, argv, ":if:lr")) != -1){ switch(opt){ case 'i': case 'l': case 'r': printf("option: %c\n", opt); break; case 'f': printf("filename: %s\n", optarg); break; case ':': printf("option needs a value\n"); break; case "?": printf("unknow option: %c\n", optopt); break; } } for (; optind < argc; optind++) printf("argument: %s\n", argv[optind]); exit(0);}
getopt_long
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#define _GNU_SOURCE#include <getopt.h>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'}, {0, 0, 0, 0}; } while ((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -1){ switch(opt){ case 'i': case 'l': case 'r': printf("option: %c\n", opt); break; case 'f': printf("filename: %s\n", optarg); break; case ':': printf("option need a value\n"); break; case '?': printf("unknow option: %c\n", optopt); break; } } for (; optind < argc; optind++) printf("argument: %s\n", argv[optind]); exit(0);}
struct option:该结构定义中getopt.h中,必须包含常量 _GNU_SOURCE才能使getopt_long功能生效
struct option = { char *name; int has_arg; int *flag; int val;}1. has_arg:设置为0表示不带关参数,1表示必须带参数,2表示有一个可选参数2. flag:设置为NULL表示找到该选项时,getopt_long返回在成员val里给出的值.否则getopt_long返回0,并将val的值写入flag指向的变量3. val:getopt_long为该选项返回的值
getenv, putenv
#include <stdlib.h>char *getenv(const char *name);int putenv(const char *string);
environ.c
#include <stdlib.h>#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){ char *val, *value; if (argc == 1 || argc > 3){ fprintf(stderr, "usage: environ var [value]\n"); exit(1); } var = argv[1]; value = getenv(var); if (value) printf("Variable %s has value %s\n", var, value); else printf("Variable %s has no value\n", var); if (argc == 3){ char *string; value = argv[2]; string = malloc(strlen(var)+strlen(value)+2); if (!string){ fprintf(stderr,"out of memory\n"); exit(1); } strcpy(string, var); strcat(string, "="); strcat(string, value); printf("Calling putenv with: %s\n", string); if (putenv(string) != 0){ fprintf(stderr, "putenv failed\n"); free(string); exit(1); } value = getenv(var); if (value) printf("Variable %s has value %s\n", var, value); else printf("Variable %s has no value\n", var); } exit(0);}
外部变量environ
#include <stdlib.h>extern char **environ;
environ.c
#include <stdlib.h>#include <stdio.h>extern char **environ;int main(){ char **env = environ; while (*env){ printf("%s\n", *env); env++; } exit(0);}
time and date
#include <time.h>time_t time(time_t *tloc);
envtime.c
#include <time.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(){ int i; time_t the_time; for(i = 0; i <= 10; i++){ the_time = time((time_t *)0); printf("The time is %ld\n", the_time); sleep(2); } exit(0);}
difftime
#include <time.h>double difftime(time_t time1, time_t time2);
gmtime
#include <time.h>struct tm *gmtime(const time_t timeval);
gmtime.c
#include <time.h>#include <stdio.h>#include <stdlib.h>int main(){ struct tm *tm_ptr; time_t the_time; (void) time(&the_time); tm_ptr = gmtime(&the_time); printf("Raw time is %ld\n", the_time); printf("gmtime gives:\n"); printf("date:%02d/%02d/%02d\n", tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday); printf("time:%02d/%02d/%02d\n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec); exit(0);}
localtime
#include <time.h>struct tm *localtime(const time_t *timeval);
mktime
#include <time.h>time_t mktime(struct tm *timeptr);
mktime.c
#include <time.h>#include <stdio.h>#include <stdlib.h>int main(){ struct tm *tm_ptr; time_t the_time; time_t my_time; (void)time(&the_time); printf("the_time is %ld\n", the_time); tm_ptr = gmtime(&the_time); my_time = mktime(tm_ptr); printf("my_time is %ld\n", my_time); exit(0);}
asctime and ctime
#include <time.h>char *asctime(const struct tm *timeptr);char *ctime(const time_t *timeval);
ctime.c
#include <time.h>#include <stdio.h>#include <stdlib.h>int main(){ time_t timeval; (void)time(&timeval); printf("The date is %s\n", ctime(&timeval)); exit(0);}
tmpnam
#include <stdio.h>char *tmpnam(char *s)
tmpfile
#include <stdio.h>FILE *tmpfile(void);1. 函数返回一个文件流指针,它指向一个唯一的临时文件,该文件以读写方式打开,当对它的所有引用全部关闭时,该文件会自动删除
tmpfilename.c
#include <stdio.h>#include <stdlib.h>int main(){ char tmpname[L_tmpnam]; char *filename; FILE *tmpfp; filename = tmpnam(tmpname); printf("Temporary file name is: %s\n", filename); tmpfp = tmpfile(); if (tmpfp) printf("Open a temporary file OK\n"); else perror("tmpfile"); exit(0);}
mktemp and mkstemp
#include <stdlib.h>char *mktemp(char *template);int mkstemp(char *template);1. template必须以6个X字符结尾,mktemp函数替换这些X字符为一个唯一的有效的文件字符的结合2. tmpfile和mkstemp创建并打开一个临时文件,tmpfile返回一个(FILE *)指针,mkstemp返回一个底层文件描述符
getuid and getlogin
#include <sys/types.h>#include <unistd.h>uid_t getuid(void);char *getlogin(void);
struct passwd
#include <sys/types.h>#include <pwd.h>struct passwd *getpwuid(uid_t uid);struct passwd *getpwnam(const char *name);密码数据库结构passwd定义在头文件pwd.h中,包含如下成员:1. char *pw_name: 用户登录名2. uid_t pw_uid: UID号3. gid_t pw_gid: GID号4. char *pd_dir: 用户home目录5. char *pw_gecos: 用户全名6. char *pw_shell: 用户默认shell
exit函数:该函数包含在头文件stdlib.h中
user.c
#include <sys/types.h>#include <pwd.h>#include <stdio.h>#include <unistd.h>#inlcude <stdlib.h>int main(){ uid_t uid; gid_t gid; struct passwd *pw; uid = getuid(); gid = getgid(); printf("User is %s\n", getlogin()); printf("User IDs: uid = %d, gid = %d\n", uid, gid); pw = getpwuid(uid); printf("UID passwd entry:\n name = %s, uid = %d, gid = %d, home = %s, shell = %s\n", pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_pw_shell); pw = getpwnam("root"); printf("root passwd entry:\n"); printf("name= %s, uid = %d, gid = %d, home = %s, shell = %s\n", pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell); exit(0);}
getpwent:扫描密码文件中的所有信息,依次取出文件数据项目
#include <pwd.h>#include <sys/types.h>void endpwent(void);struct passwd *getpwent(void);void setpwent(void);1. endpwent函数终止扫描密码文件条目2. setpwent函数重置读指针到密码文件的开始位置,这样下一个getpwent调用将重新开始一个新的扫描.
gethostname:函数把把机器的网络名写入name字符串
#include <unistd.h>int gethostname(char *name, size_t namelen);获取成功的话返回0,否则返回-1
uname:函数把主机信息写入到name指向的结构struct utsname中
#include <sys/utsname.h>int uname(struct utsname *name);如果获取函数返回一个非负整数,否则返回-1struct utsname结构定义在头文件sys/utsname.h中必须至少包含下列成员:1. char sysname[]: 操作系统名称2. char nodename[]: 主机名称3. char release[]: 系统的release等级4. char version[]: 系统版本号5. char machine[]: 硬件类型
hostget.c
#include <sys/utsname.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(){ char computer[256]; struct ntsname uts; if (gethostname(computer, 255) != 0 || uname(&uts) < 0){ fprintf(stderr, "could not get host information\n"); exit(1); } printf("conputer host name is %s\n", computer); printf("system is %s on %s hardware\n", uts.sysname, uts.machine); printf("nodename is %s\n", uts.nodename); printf("version is %s, %s\n", uts.release, uts.version); exit(0);}
syslog:函数想系统的日志设施发送一条日志信息
#include <syslog.h>void syslog(int priority, char *message, arguments...);priority:该参数是一个严重级别与一个设施值的按位或,严重级别控制日志信息的处理方式,设施值记录日志信息的来源严重级别按优先递减排列:1. LOG_EMERG: 紧急情况2. LOG_ALERT: 高优先级故障,例如数据库崩溃3. LOG_CRIT: 严重错误,例如硬件故障4. LOG_ERR: 错误5. LOG_WARNING: 警告6. LOG_NOTICE: 需要注意的特殊情况7. LOG_INFO: 一般信息8. LOG_DEBUG: 调试信息syslog创建的入职信息包含消息头和消息体.消息头跟进设施值及日期和时间创建.消息体根据syslog的message参数创建,该参数的作用类似printf中的格式字符串.转换控制符%m可用于插入与错误变量errno当前值对应的出错消息字符串.
syslog.c
#include <syslog.h>#include <stdio.h>#include <stdlib.h>int main(){ FILE *f; f = fopen("not_here", "r"); if (!f) syslog(LOG_ERR|LOG_USER, "oops - %m\n"); exit(0);}
open close set - log
#include <syslog.h>void closelog(void);void openlog(const char *ident, int logopt, int facility);int setlogmask(int maskpri);1. ident:该字符串会添加在日志信息的前面.可以用来指明哪一个程序创建了这条消息2. facility:该参数记录了后续调用syslog的默认设施值,默认值是LOG_USER3. logopt:该参数对后续调用syslog的行为进行配置,他是一个包含0个或多个参数的按位或4. openlog函数会分配并打开一个文件描述符号,并通过它来写日志5. setlogmask函数用来设置一个日志掩码,并通过它来控制日志的优先级logopt参数:1. LOG_PID:在日志信息中包含进程标识符,这是系统分配给每一个进程的一个唯一值2. LOG_CONS:发送消息到控制台,如果他不能被记录到日志文件中3. LOG_ODELAY:在第一次调用syslog的时候才打开日志设施4. LOG_NDELAY:立即打开日志设施,而不是等到第一次记录日志时
logmask.c
#include <syslog.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>int main(){ int logmask; openlog("logmask", LOG_PID|LOG_CONS, LOG_USER); syslog(LOG_INFO, "informatice message pid = %d", getid()); syslog(LOG_DEBUG, "debug message, should appear"); logmask = setlogmask(LOG_UPTO(LOG_NOTICE)); syslog(LOG_DEBUG, "debug message, should not appear"); exit(0);}
getpid & getppid
#include <sys/types.h>#include <unistd.h>pid_t getpid(void);ptd_t getppid(void);1. getpid:获取当前进程的标识符2. getppid:获取当前进程的父进程的标识符
limits.h constants
1. NAME_MAX: 文件名的最大长度2. CHAR_BIT: char类型值的位数3. CHAR_MAX: char类型的最大值4. INT_MAX: int类型的最大值
sys/resource.h:提供了资源操作方面的定义,其中包括对程序长度,执行优先级和文件资源等方面限制进行查询和设置的函数
#include <sys/resource.h>int getpriority(int which, id_t who);int setpriority(int which, id_t who, int priority);int getrlimit(int resource, struct rlimit *r_limit);int setrlimit(int resource, const struct rlimit *rlimit);int getrusage(int who, struct rusage *r_usage);1. id_t是一个整数类型,用于用户和组标识符.2. rusage结构定义在头文件sys/resource.h中,用来确定当前程序已耗费多少CPU时间 它至少包含两个成员: struct timeval ru_utime: 使用的用户时间 struct timeval ru_stime: 使用的系统时间3. timeval结构定义在头文件sys/time.h中,他包含: tv_sec: 秒 tv_usec: 微秒4. getrusage:函数把CPU时间信息写入到r_usage所指向的rusage结构中5. getrusage中who参数可以是以下常量之一: RUSAGE_SELF: 仅仅返回当前程序的使用信息 RUSAGE_CHILDREN: 还包括子进程的使用信息6. which参数指定了对待who参数的方式 PRIO_PROCESS: who是一个进程标识符 PRIO_PGRP: who是一个进程组 PRIO_USER: who是一个用户标识符7. priority = getpriority(PRIO_PROCESS, getpid());8. 默认的优先级为0,正优先级用于后台任务,负优先级使程序运行更加频繁9. getpriority返回有效的优先级如果成功,否则返回-1并设置errno变量,因为-1是一个有效的优先级10. setpriority返回0如果成功,否则返回-111. 系统资源限制能够被读取或者设置通过getrlimit和setrlimit.12. struct rlimit: rlim_t rlim_cur: 当前的软限制 rlim_t rlim_max: 硬限制13. resource参数:定义在头文件sys/resource.h中,包含如下数据: RLIMIT_CORE: 内核转储(coredump)文件的大小限制(以字节为单位) RLIMIT_CPU: CPU时间限制(以秒为单位) RLIMIT_DATA: 数据段限制(以字节为单位) RLIMIT_FSIZE: 文件大小限制(以字节为单位) RLIMIT_NOFILE: 可以打开的文件数量限制 RLIMIT_STACK: 栈大小限制(以字节为单位) RLIMIT_AS: 地址空间(栈和数据)限制(以字节为单位)
limit.c
#include <sys/types.h>#include <sys/resource.h>#include <sys/time.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <math.h>void work(){ FILE *f; int i; double x = 4.5; f = tmpfile(); for (i = 0; i < 10000; i++){ fprintf(f, "Do some output\n"); if (ferror(f)){ fprintf(stderr, "Error writing to temporary file\n"); exit(1); } } for (i = 0; i < 10000; i++) x = log(x*x + 3.21);}int main(){ struct rusage r_usage; sturct rlimit r_limit; int priority; work(); getrusage(RUSAGE_SELF, &r_usage); print("CPU usage: User = %d.%06ld, Syetem = %1d.%06ld\n", r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec, r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec); priority = getpriority(PRIO_PROCESS, getpid()); printf("current priority = %d\n", priority); getrlslimit(RLIMIT_FSIZE, &r_limit); printf("current FSIZE limit: soft = %ld, hard = %ld\n", r_limit.rlim_cur, r_limit.rlim_max); r_limit.rlim_cur = 2048; r_limit.rlim_max = 4096; printf("Setting a 2K file size limit\n"); setrlimit(RLIMIT_FSIZE, &rlimit); work(); exit(0);}
0 0
- 第四章 Linux环境
- 第四章 Linux环境
- 第四章 Linux环境
- 第四章 Linux环境
- 第四章 Linux环境
- Linux程序设计 第四章:linux环境
- Linux程序设计 第四章Lnux环境 笔记
- kali Linux 第四章 实验环境
- Linux程序设计-学习笔记-第四章Linux环境
- linux程序设计——linux环境(第四章)
- linux程序设计——linux环境(第四章)
- 第四章 环境搭建笔记
- LInux第四章
- Linux程序设计第四章
- UNIX环境高级编程第四章
- UNIX环境高级编程 第四章末
- Linux内核探讨-- 第四章
- 第四章:Linux的安装
- PAT甲级.1044. Shopping in Mars (25)
- Excel文件一键上传并解析导入数据库
- 自定义渐变式炫酷动画的ListView下拉刷新
- 快速排序
- 第三章 文件操作
- 第四章 Linux环境
- 第五章 终端
- 第六章 使用curses函数库管理基于文本的屏幕
- HDU 1000:A + B Problem
- 第七章 数据管理
- Linux中GIT仓库的使用
- 第九章 开发工具
- [oracle] 常用函数 - rank
- CSS之背景属性