C语言的全部命令??

Python015

C语言的全部命令??,第1张

你是说关键字吧!

由ANSI标准定义的C语言关键字共32个 :

auto double int struct break else long switch

case enum register typedef char extern return union

const float short unsigned continue for signed void

default goto sizeof volatile do if while static

根据关键字的作用,可以将关键字分为数据类型关键字和流程控制关键字两大类。

1 数据类型关键字

A基本数据类型(5个)

void :声明函数无返回值或无参数,声明无类型指针,显式丢弃运算结果

char :字符型类型数据,属于整型数据的一种

int :整型数据,通常为编译器指定的机器字长

float :单精度浮点型数据,属于浮点数据的一种

double :双精度浮点型数据,属于浮点数据的一种

B 类型修饰关键字(4个)

short :修饰int,短整型数据,可省略被修饰的int。

long :修饰int,长整形数据,可省略被修饰的int。

signed :修饰整型数据,有符号数据类型

unsigned :修饰整型数据,无符号数据类型

C 复杂类型关键字(5个)

struct :结构体声明

union :共用体声明

enum :枚举声明

typedef :声明类型别名

sizeof :得到特定类型或特定类型变量的大小

D 存储级别关键字(6个)

auto :指定为自动变量,由编译器自动分配及释放。通常在栈上分配

static :指定为静态变量,分配在静态变量区,修饰函数时,指定函数作用域为文件内部

register :指定为寄存器变量,建议编译器将变量存储到寄存器中使用,也可以修饰函数形参,建议编译器通过寄存器而不是堆栈传递参数

extern :指定对应变量为外部变量,即在另外的目标文件中定义,可以认为是约定由另外文件声明的韵蟮囊桓觥耙�谩?

const :与volatile合称“cv特性”,指定变量不可被当前线程/进程改变(但有可能被系统或其他线程/进程改变)

volatile :与const合称“cv特性”,指定变量的值有可能会被系统或其他进程/线程改变,强制编译器每次从内存中取得该变量的值

2 流程控制关键字

A 跳转结构(4个)

return :用在函数体中,返回特定值(或者是void值,即不返回值)

continue :结束当前循环,开始下一轮循环

break :跳出当前循环或switch结构

goto :无条件跳转语句

B 分支结构(5个)

if :条件语句

else :条件语句否定分支(与if连用)

switch :开关语句(多重分支语句)

case :开关语句中的分支标记

default :开关语句中的“其他”分治,可选。

C 循环结构(3个)

for :for循环结构,for(123)4的执行顺序为1->2->4->3->2...循环,其中2为循环条件

do :do循环结构,do 1 while(2)的执行顺序是 1->2->1...循环,2为循环条件

while :while循环结构,while(1) 2的执行顺序是1->2->1...循环,1为循环条件

以上循环语句,当循环条件表达式为真则继续循环,为假则跳出循环。

第一章:绪论?

内核版本号格式:x.y.zz-www/x为主版本号,y为次版本号,zz为次次版本号,www为发行号/次版本号改变说明内核有重大变革,其偶数为稳定版本,奇数为尚在开发中的版本

第二章:基础?

文件种类:-:txt,二进制/d:目录/l:链接文件(link)/b:区块设备文件/c:字符设备文件/p:管道

目录结构:bin:可执行/boot:开机引导/dev:设备文件/etc:系统配置文件/lib:库文件/mnt:设备挂载点/var:系统日志/

命令:rmdir:删除空目录/find [path] [expression]/touch命令还可以修改指定文件的最近一次访问时间/tar -czvf usr.tar.gz path/tar –zxvf usr.tar.gz/tar –cjvf usr.tar.bz2 path/tar –jxvf usr.tar.bz2

gcc:预处理:-g/I在头文件搜索路径中添加目录,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:in/1:out/2: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,没有已经结束的子进程则马上返回,不等待

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为0/value即初始值

第八章:管道

1:write/0:read

第九章:信号量、共享内存和消息队列

临界资源:操作系统中只允许一个进程访问的资源/临界区:访问临界资源的那段代码

信号量:建立联系(semget),然后初始化,PV操作,最后destroy

共享内存没有提供同步机制

第十章:套接字

UDP:无连接协议,无主客端的区分/实时性

TCP:字节流/数据可靠性/网络可靠性

数据报:SOCK_STREAM/SOCK_DGRAM

其它

管道一章的both_pipe即父子进程间的全双工管道通讯

关系到信号和互斥的服务器-客户端程序

线程一章的class的multi_thread文件夹下的thread8.c

int main(void)

{

int data_processed

int file_pipes_1[2]

int file_pipes_2[2]

char buffer[BUFSIZ + 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

close(file_pipes_1[1])

close(file_pipes_2[0])

printf("in the child!\n")

read(file_pipes_1[0],buffer, BUFSIZ)

printf("in the child, read_result is \"%s\"\n",buffer)

write(file_pipes_2[1],ch2p, sizeof(ch2p))

printf("in the child, write_result is \"%s\"\n",ch2p)

exit(EXIT_SUCCESS)

default://parent

close(file_pipes_1[0])

close(file_pipes_2[1])

printf("in the parent!\n")

write(file_pipes_1[1], p2ch, sizeof(p2ch))

printf("in the parent, write_result is \"%s\"\n",p2ch)

read(file_pipes_2[0],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...) do{}while(0)

#endif

int main(void)

{

PRINTF("%s\n", "hello!")

fprintf(stdout, "hello hust!\n")

return 0

}

#define N 5

#define MAX 5

int nput = 0

char buf[MAX][50]

char *buffer = "abcdefghijklmnopqrstuvwxyz0123456789"

char buf_r[100]

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_produce[10]

pthread_t id_consume

ret = sem_init(&mutex, 0, 1)

ret = sem_init(&avail, 0, N)

ret = sem_init(&full, 0, 0)

for(cnt = 0cnt <6cnt ++ ){

//pthread_create(&id_produce[cnt], NULL, (void *)productor, &cnt)

pthread_create(&id_produce[cnt], NULL, (void *)productor, (void *)cnt)

}

pthread_create(&id_consume, NULL, (void *)consumer, NULL)

for(cnt = 0cnt <6cnt ++){

pthread_join(id_produce[cnt], NULL)

}

pthread_join(id_consume,NULL)

sem_destroy(&mutex)

sem_destroy(&avail)

sem_destroy(&full)

exit(EXIT_SUCCESS)

}

void *productor(void *arg)

{

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", buf[nput % MAX])

//printf("write[%d] \"%s\" to the buffer[%d]\n", (*(int*)arg), buf[nput % MAX],nput % MAX)

printf("write[%d] \"%s\" to the buffer[%d]\n", (int)arg, buf[nput % MAX],nput % MAX)

nput ++

printf("nput = %d\n", 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 * 3i++)

{

sem_wait(&full)

sem_wait(&mutex)

memset(buf_r, 0, sizeof(buf_r))

strncpy(buf_r, buf[i % MAX], sizeof(buf[i % MAX]))

printf("read \"%s\" from the buffer[%d]\n\n",buf_r, i % MAX)

sem_post(&mutex)

sem_post(&avail)

//sleep(1)

}

return NULL

}