C语言 基本命令.docx
C语言 基本命令Bupu: mogugongyuan 第一章:绪论? 内核版本号格式:x.y.zz-wwwx为主版本号,y为次版本号,zz为次次版本号,www为发行号次版本号改变说明内核有重大变革,其偶数为稳定版本,奇数为尚在开发中的版本 第二章:基础? 文件种类:-:txt,二进制d:目录l:链接文件b:区块设备文件c:字符设备文件p:管道 目录结构:bin:可执行boot:开机引导dev:设备文件etc:系统配置文件lib:库文件mnt:设备挂载点var:系统日志 命令:rmdir:删除空目录find path expressiontouch命令还可以修改指定文件的最近一次访问时间tar -czvf usr.tar.gz pathtar zxvf usr.tar.gztar cjvf usr.tar.bz2 pathtar jxvf usr.tar.bz2 gcc:预处理:-gI在头文件搜索路径中添加目录,L在库文件搜索路径中 gdb:设置断点:b查看断点信息:info Makefile:make f other_makefile<:第一个依赖文件的名称:目标文件的完整名称:所有不重复的依赖文件+:所有依赖文件 第三章:文件IO read:read(fd, temp, size); 读fd中长度为size的值到temp返回0表示file为NULL write:write(fd, buf, buf_size); 写长度为buf_size的buf内容到fd中 lseek:lseek(fd, offset, SEEK_SET); 从文件开头向后增加offset个位移量 unlink:从文件系统中删除一个名字 open1:int open(const char * pathname, int flags, mode_t mode);flags为读写方式mode为权限设置O_EXCL:测试文件是否存在O_TRUNC:若存在同名文件则删除之并新建 open2:注意O_NONBLOCK mmap.1:void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offsize); mmap.2:mmap(start_addr, flength, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); fcntl:上锁int fcntl(int fd, int cmd, struct flock * lock);对谁;做什么;设置所做内容 select:fd_max+1,回传读状况,回传写状况,回传异常,select等待的时间NULL为永远等待0为从不等待凡需某状况则用之,反则(fd_set *)NULL之 FD_*那几个函数 一般出错则返回-1 第四章:文件与目录 硬链接与符号链接? chdir改变目录 0:in1:out2:err 第五章:内存管理 可执行文件存储时:代码区、数据区和未初始化区 栈:by编译器,向低址扩展,连续,效率高堆:by程序员 /etc/syslog.conf,系统log记录文件优先级为-20时最高 第六章:进程和信号 程序代码、数据、变量、文件描述符和环境init的pid为1 execl族:int execl(const char * path, const char * arg, .);path即可执行文件的路径,一般为.最后一个参数以NULL结束 waitpid:waitpid(pid_t pid,int * status,int options);option:一般用WNOHANG,没有已经Bupu: mogugongyuan 结束的子进程则马上返回,不等待 kill:int kill(pid_t pid,int sig);发送信号sig给pid void (*signal(int signum, void(* handler)(int)(int);第一个参数被满足时,执行handler第一个参数常用:SIG_IGN:忽略信号SIG_DFL:恢复默认信号 第七章:线程 sem_init(sem_t *sem, int pshared, unsigned int value)pshared为0value即初始值 第八章:管道 1:write0:read 第九章:信号量、共享内存和消息队列 临界资源:操作系统中只允许一个进程访问的资源临界区:访问临界资源的那段代码 信号量:建立联系,然后初始化,PV操作,最后destroy 共享内存没有提供同步机制 第十章:套接字 UDP:无连接协议,无主客端的区分实时性 TCP:字节流数据可靠性网络可靠性 数据报:SOCK_STREAMSOCK_DGRAM 其它 管道一章的both_pipe即父子进程间的全双工管道通讯 关系到信号和互斥的服务器-客户端程序 线程一章的class的multi_thread文件夹下的thread8.c int main(void) int data_processed; int file_pipes_12; int file_pipes_22; char bufferBUFSIZ + 1; const char some_data = "123" const char ch2p = "this is the string from child to the parent!" const char p2ch = "this is the string from parent to the child!" pid_t fork_result; memset(buffer,'0',sizeof(buffer); if(pipe(file_pipes_1) = 0) if(pipe(file_pipes_2) = 0) fork_result = fork; switch(fork_result) case -1: perror("fork error"); exit(EXIT_FAILURE); case 0:/child Bupu: mogugongyuan close(file_pipes_11); close(file_pipes_20); printf("in the child!n"); read(file_pipes_10,buffer, BUFSIZ); printf("in the child, read_result is "%s"n",buffer); write(file_pipes_21,ch2p, sizeof(ch2p); printf("in the child, write_result is "%s"n",ch2p); exit(EXIT_SUCCESS); default:/parent close(file_pipes_10); close(file_pipes_21); printf("in the parent!n"); write(file_pipes_11, p2ch, sizeof(p2ch); printf("in the parent, write_result is "%s"n",p2ch); read(file_pipes_20,buffer, BUFSIZ); printf("in the parent, read_result is "%s"n",buffer); exit(EXIT_SUCCESS); #ifndef DBG #define DBG #endif #undef DBG #ifdef DBG #define PRINTF(fmt, args.) printf("file->%s line->%d: " fmt, _FILE_, _LINE_, #args) #else #define PRINTF(fmt, args.) dowhile(0); #endif int main(void) PRINTF("%sn", "hello!"); fprintf(stdout, "hello hust!n"); return 0; Bupu: mogugongyuan #define N 5 #define MAX 5 int nput = 0; char bufMAX50; char *buffer = "abcdefghijklmnopqrstuvwxyz0123456789" char buf_r100; sem_t mutex,full,avail; void *productor(void *arg); void *consumer(void *arg); int i = 0; int main(int argc, char *argv) int cnt = -1; int ret; int nput = 0; pthread_t id_produce10; pthread_t id_consume; ret = sem_init(&mutex, 0, 1); ret = sem_init(&avail, 0, N); ret = sem_init(&full, 0, 0); for(cnt = 0; cnt < 6; cnt + ) /pthread_create(&id_producecnt, NULL, (void *)productor, &cnt); pthread_create(&id_producecnt, NULL, (void *)productor, (void *)cnt); pthread_create(&id_consume, NULL, (void *)consumer, NULL); for(cnt = 0; cnt < 6; cnt +) pthread_join(id_producecnt, NULL); pthread_join(id_consume,NULL); sem_destroy(&mutex); sem_destroy(&avail); sem_destroy(&full); exit(EXIT_SUCCESS); void *productor(void *arg) Bupu: mogugongyuan while(1) sem_wait(&avail); sem_wait(&mutex); if(nput >= MAX * 3) sem_post(&avail); /sem_post(&full); sem_post(&mutex); return NULL; sscanf(buffer + nput, "%s", bufnput % MAX); /printf("write%d "%s" to the buffer%dn", (*(int*)arg), bufnput % MAX,nput % MAX); printf("write%d "%s" to the buffer%dn", (int)arg, bufnput % MAX,nput % MAX); nput +; printf("nput = %dn", nput); sem_post(&mutex); sem_post(&full); return NULL; void *consumer(void *arg) int nolock = 0; int ret, nread, i; for(i = 0; i < MAX * 3; i+) sem_wait(&full); sem_wait(&mutex); memset(buf_r, 0, sizeof(buf_r); strncpy(buf_r, bufi % MAX, sizeof(bufi % MAX); printf("read "%s" from the buffer%dnn",buf_r, i % MAX); sem_post(&mutex); sem_post(&avail); /sleep(1); return NULL;