UNIX环境C语言编程(1)-概述
来源:互联网 发布:营销软件破解论坛 编辑:程序博客网 时间:2024/05/22 14:21
1、用户登录
登录名称:密文口令:用户ID:组ID:注释信息:HOME:SHELL
示例:billing:!:207:202::/billing:/usr/bin/ksh
2、文件与目录
char *getcwd (char *Buffer,size_tSize) //获取当前工作目录
int chdir (const char *Path) //改变当前工作目录
#include <stdio.h>#include <dirent.h>int main(int argc, char *argv[]){ DIR *dp; struct dirent *dirp; if( argc != 2 ) { printf("usage: %s <directory_name>\n", argv[0]); exit(0); } /* 打开目录 */ if( (dp = opendir(argv[1])) == NULL ) { perror(argv[1]); exit(1); } /* 逐一读取目录条目 */ while( (dirp = readdir(dp)) != NULL ) { printf("%s\n", dirp->d_name); } /* 关闭目录 */ closedir(dp); exit(0);}
rm -- -1
3、输入/输出
操作对象为文件描述符,非负小整数
特殊描述符:STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO
相关调用示例:open()、read()、write()、lseek()、close() …
例子:
#include <stdio.h>#include <unistd.h>int main(void){ int n; char buf[BUFSIZ]; /* 读取标准输入 */ while( (n = read(STDIN_FILENO, buf, BUFSIZ)) > 0 ) { /* 写入标准输出 */ if( write(STDOUT_FILENO, buf, n) != n ) { perror("write"); exit(-1); } } if( n < 0 ) { perror("read"); } exit(0);}
操作对象为文件指针,FILE *
特殊指针:stdin、stdout、stderr
相关调用示例:printf()、fgets()、fputs() …
4、程序与进程
#include <stdio.h>#include <sys/wait.h>int main(void){ pid_t pid; int status; char buf[1024]; /* print prompt (%% to print %) */ printf("%% "); /* 读取标准输入 */ while( fgets(buf, sizeof(buf), stdin) != NULL ) { /* replace newline with null */ if( buf[strlen(buf) - 1] == '\n' ) { buf[strlen(buf) - 1] = 0; } /* 派生子进程 */ if( (pid = fork()) < 0 ) { perror("fork"); exit(-1); } else if( pid == 0 ) /* child */ { /* 执行读取的指令 */ execlp(buf, buf, (char *)0); perror(buf); exit(127); } /* parent */ if( (pid = waitpid(pid, &status, 0)) < 0 ) { perror("waitpid"); } printf("%% "); } exit(0);}
5、错误处理
函数执行成功并不清除上次的errno取值;
函数不会将errno设置为0;
char *strerror (intErrorNumber) //获取错误编号对应的错误信息
void perror (const char *String) //显示错误信息
uid_t getuid (void) //获取用户ID
gid_t getgid (void) //获取组ID
允许一个用户属于多个组,示例:
uid=207(billing)gid=202(dba) groups=205(billing)
7、信号
#include <stdio.h>#include <signal.h>#include <errno.h>#include <sys/wait.h>void sig_int(int signo){ printf("interrupt\n%% ");}int main(void){ pid_t pid; int status; char *p, buf[1024]; struct sigaction sa; /* 初始化 */ sigemptyset(&sa.sa_mask); sa.sa_flags = 0; /* 设置信号处理动作 */ sa.sa_handler = sig_int; sigaction(SIGINT, &sa, NULL); /* print prompt (%% to print %) */ printf("%% "); while( 1 ) { errno = 0; /* 读取指令 */ p = fgets(buf, sizeof(buf), stdin); if( p == NULL ) { if( errno == EINTR ) continue; else break; } /* replace newline with null */ if( buf[strlen(buf) - 1] == '\n' ) { buf[strlen(buf) - 1] = 0; } /* 派生子进程 */ if( (pid = fork()) < 0 ) { perror("fork"); exit(-1); } else if( pid == 0 ) /* child */ { /* 执行指令 */ execlp(buf, buf, (char *)0); perror(buf); exit(127); } /* parent,等待子进程终止 */ if( (pid = waitpid(pid, &status, 0)) < 0 ) { perror("waitpid"); } printf("%% "); } exit(0);}
8、时间值
日历时间,表示自1970.1.1零点以来UTC的秒数累计
进程时间,用以度量进程对CPU资源的占用
时钟时间的取值与系统活动有关
time find / -name a.txt
9、系统调用与库函数
- UNIX环境C语言编程(1)-概述
- UNIX环境C语言编程(9)-信号
- UNIX环境C语言编程(14)-sockets
- UNIX环境C语言编程(17)-线程
- Linux下C语言编程环境概述
- UNIX环境C语言编程(6)-进程环境
- UNIX环境C语言编程(2)-文件IO
- UNIX环境C语言编程(3)-文件与目录
- UNIX环境C语言编程(4)-标准IO库
- UNIX环境C语言编程(5)-系统数据文件
- UNIX环境C语言编程(7)-进程控制
- UNIX环境C语言编程(8)-进程关系
- UNIX环境C语言编程(10)-守护进程
- UNIX环境C语言编程(11)-高级IO
- UNIX环境C语言编程(12)-进程间通信
- UNIX环境C语言编程(13)-高级进程间通信
- UNIX环境C语言编程(15)-终端IO
- UNIX环境C语言编程(16)-伪终端
- sql access 一个access数据库的表复制到另一个access数据库
- 有用的SAP Basis T-CODE
- erlang分布式开发中遇到的一些问题
- TF-IDF结合余弦相似性 判断文章相似性
- Many-to-Many
- UNIX环境C语言编程(1)-概述
- 如何修改discuz论坛的图像地址
- arcgis api for javascript 定义layerinfo2种方式
- Java内部类总结
- 关于++i 与 i++等诸如问题
- iPhone开发 判断当前的网络是3g还是wifi
- 可扩展hdfs——HDFS Federation(HDFS 联盟)介绍
- iOS 推送
- poj-1258 Agri-Net