天天看点

Linux常用C函数

可以参考这个:

​​http://net.pku.edu.cn/~yhf/linux_c/​​

/第三章   文件IO******************************************/

#define STDIN_FILENO 0

#define STDOUT_FILENO 1

#define STDERR_FILENO 2

函数: fpathconf或pathconf ——     

    查询目录具体支持何种行为,如文件名最大值,路径名最大值

函数: pread和pwrite ——

    随机读写函数

ssize_t pread(int fd, void *buf, size_t count, off_t offset);

ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);    

*****

int dup(    int oldfd);             //复制表述符

int dup2(    int oldfd,     int newfd)  //用fd参数指定新描述符的值

*****

void     sync(        void);   //把所有的缓冲写到磁盘

int        syncfs(        int fd); //指定文件

int     fdatasync(    int fd);  //只写文件的数据部分

*****

int fcntl(int fd, int cmd, ... /* arg */ ); //var = fcntl(fd, F_GETFL, 0)

*****

int ioctl(    int d,     int request,     ...);//是IO操作的杂物箱

*****

/dev/fd/0   

int rename(const char *oldpath, const char *newpath);  //为文件更换名字

int getdtablesize(void);  //返回进程能打开的最大文件描述符的数,Linux时1024

/第四章      文件和目录******************************************/

int stat(    const char *path,     struct stat *buf);  //获得文件的各种信息

int fstat(    int fd,             struct stat *buf);

int lstat(    const char *path,     struct stat *buf); //返回符号链接本身

int fstatat(    int dirfd,         const char *pathname,     struct stat *buf,    int flags);

int access(                const char *pathname,     int mode);  //检测文件对于进程的属性

int faccessat(    int dirfd, const char *pathname,         int mode,     int flags);

mode:

R_OK(读)    W_OK(可写)  X_OK(可执行)    F_OK(文件是否存在)

mode_t umask(mode_t mask); //文件权限

chmod 1777 filename  //设置文件粘着位

int chown(    const char *path,   uid_t owner,     gid_t group);

int fchown(   int fd,                     uid_t owner,     gid_t group);

int lchown(    const char *path,    uid_t owner,    gid_t group);

int fchownat(  int dirfd,                const char *pathname,   uid_t owner,    gid_t group,   int flags);

cat core1 > core2 //把空洞文件填满

int truncate(const char *path, off_t length);//文件截断

int ftruncate(int fd, off_t length);

命令:truncate -s 0 mask //文件变为空

int link(const char *oldpath, const char *newpath); //创建硬链接

int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);

int unlink(const char *pathname);

int unlinkat(int dirfd, const char *pathname, int flags);

int remove(const char *pathname);  //解除链接

link filename filename_link  //创建硬链接

ln filename filename_link  //创建硬链接

ln  -s  [源文件或目录]  [目标文件或目录]  //创建软链接

int symlink(    const char *oldpath,     const char *newpath); //创建软链接

int symlinkat(    const char *oldpath,     int newdirfd,         const char *newpath);

ssize_t readlink(const char *path,      char *buf,       size_t bufsiz); //读符号链接

int readlinkat(    int dirfd,     const char *pathname,  char *buf,  size_t bufsiz);

int futimens(int fd, const struct timespec times[2]);//修改最后访问时间

int utimensat(int dirfd, const char *pathname,const struct timespec times[2], int flags);

int utimes(const char *path, const struct timeval times[2]);

int mkdir(    const char *pathname,                 mode_t mode); //创建文件夹

int mkdirat(    int dirfd,         const char *pathname,     mode_t mode);

int rmdir(    const char *pathname);  //删除文件夹

int dirfd(DIR *dirp);  //把文件夹变文件描述符

DIR *opendir(const char *name); //打开文件夹

DIR *fdopendir(int fd);

struct dirent *readdir(DIR *dirp);  //每次只能读一个文件

int  readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);

void rewinddir(DIR *dirp);

int  closedir(DIR *dirp);   //关闭文件夹

long telldir(DIR *dirp);

void seekdir(DIR *dirp, long loc);

int   chdir(const char *path);//改变当前进程的工作目录

int   fchdir(int fd);

char *getcwd(char *buf, size_t size);//获取当前进程的工作目录

unsigned int major(dev_t dev); //主设备

unsigned int minor(dev_t dev); //次设备

/第五章      标准IO库******************************************/

int fwide(FILE *stream, int mode); //设置流的定向

void setbuf(FILE *stream, char *buf); //用buf当作这个流的缓冲

void setbuffer(FILE *stream, char *buf, size_t size);

void setlinebuf(FILE *stream);

int  setvbuf(FILE *stream, char *buf, int mode, size_t size);//用buf当作这个流的缓冲,且设置缓冲类型为全,行或不带缓冲

int fflush(FILE *stream); //冲洗流

FILE *fopen(const char *path, const char *mode);  //打开流

FILE *freopen(const char *path, const char *mode, FILE *stream);

FILE *fdopen(int fd, const char *mode);

//输入*****************************

int   fgetc(FILE *stream);

int   getc(FILE *stream);

int   getchar(void);  //相当于getc(stdin)

int   ungetc(int c, FILE *stream);  //把字符c压回缓冲

char *fgets(char *s, int size, FILE *stream);  //输入一行

char *gets(char *s);

//输出*****************************

int fputc(int c, FILE *stream);

int putc(int c, FILE *stream);

int putchar(int c);

int puts(const char *s);

int fputs(const char *s, FILE *stream); //输出一行

void clearerr(FILE *stream); //清除错误标志和结束标志

int  feof(FILE *stream);

int  ferror(FILE *stream);  //测试给定流 stream 的错误标识符。

int  fileno(FILE *stream);  //通过流获得文件描述符

size_t fread(void *ptr,         size_t size,     size_t nmemb,     FILE *stream);//二进制读

size_t fwrite(const void *ptr,     size_t size,     size_t nmemb,        FILE *stream);//二进制写

long  ftell(FILE *stream);  //告诉文件位置

int   fseek(FILE *stream, long offset, int whence);//移动文件指针

void  rewind(FILE *stream);  //把文件指针移动到开头

off_t ftello(FILE *stream);  //自己定义文件位置类型

int   fseeko(FILE *stream, off_t offset, int whence);

int   fgetpos(FILE *stream, fpos_t *pos);

int   fsetpos(FILE *stream, fpos_t *pos);

int printf(const char *format, ...);

int fprintf(FILE *stream, const char *format, ...);

int sprintf(char *str, const char *format, ...);  //写到指定数组

int snprintf(char *str, size_t size, const char *format, ...);

int vprintf(const char *format, va_list ap);

int vfprintf(FILE *stream, const char *format, va_list ap);

int vsprintf(char *str, const char *format, va_list ap);

int vsnprintf(char *str, size_t size, const char *format, va_list ap);

int scanf(const char *format, ...);

int fscanf(FILE *stream, const char *format, ...);

int sscanf(const char *str, const char *format, ...);

int vscanf(const char *format, va_list ap);

int vsscanf(const char *str, const char *format, va_list ap);

int vfscanf(FILE *stream, const char *format, va_list ap);

int  fileno(FILE *stream);  //通过流获得对应的文件描述符

char *tmpnam(char *s); //创建一个临时文件,函数结束就自动删除或就没创建

FILE *tmpfile(void); //创建一个临时文件,进程结束就自动删除

int mkstemp(char *template);  //创建/tmp/dirXXXXXX临时文件,返回文件描述符

int mkostemp(char *template, int flags);

int mkstemps(char *template, int suffixlen);

int mkostemps(char *template, int suffixlen, int flags);

/第六章   系统数据文件和信息******************************************/

struct spwd *getspnam(const char *name);  //获取密文文件信息

struct spwd *getspent(void);

void   setspent(void);

void   endspent(void);

int uname(struct utsname *name);  //可以读出主机和操作系统的信息如CPU信息       

struct utsname

  {

    /* Name of the implementation of the operating system.  */

    char sysname[_UTSNAME_SYSNAME_LENGTH];

    /* Name of this node on the network.  */

    char nodename[_UTSNAME_NODENAME_LENGTH];

    /* Current release level of this implementation.  */

    char release[_UTSNAME_RELEASE_LENGTH];

    /* Current version level of this release.  */

    char version[_UTSNAME_VERSION_LENGTH];

    /* Name of the hardware type the system is running on.  */

    char machine[_UTSNAME_MACHINE_LENGTH];

    /* Name of the domain of this node on the network.  */

    char __domainname[_UTSNAME_DOMAIN_LENGTH];

  };

/第七章   进程环境******************************************/

int atexit(void (*function)(void));  //注册结束处理函数

命令:size   //显示一个可执行文件的内存分布

void *malloc(size_t size);

void *calloc(size_t nmemb, size_t size);

void *realloc(void *ptr, size_t size);  //重新分配地址

void  free(void *ptr);

char *getenv(const char *name);  //获取指定的环境变量

int   setenv(const char *name, const char *value, int overwrite);  //设置环境变量

int   putenv(char *string);  //添加环境变量,若存在,则删除

int   unsetenv(const char *name);  //删除环境变量,即使没有也不会出错

int  setjmp(jmp_buf env);  //设置异常返回点

int  sigsetjmp(sigjmp_buf env, int savesigs);

void longjmp(jmp_buf env, int val);  //回跳到异常返回点

void siglongjmp(sigjmp_buf env, int val);

int getrlimit(int resource, struct rlimit *rlim);  //获得限制

int setrlimit(int resource, const struct rlimit *rlim);  //设置限制

int prlimit(pid_t pid, int resource, const struct rlimit *new_limit,struct rlimit *old_limit);

/第八章    进程控制******************************************/

pid_t getpid(void);  //进程id

pid_t getppid(void);  //父进程id

uid_t getuid(void);  //用户ID

uid_t geteuid(void);       

gid_t getgid(void);  //组ID

gid_t getegid(void);

pid_t fork(void);  //创建进程

pid_t vfork(void);  //创建进程,但与父进程共享空间

pid_t wait(int *stat_loc);  //等待子进程中止,并得到返回状态

pid_t waitpid(pid_t pid, int *stat_loc, int options);

//判断是否为exit状态:WIFEXITED(stat_loc)

//把返回的值转换出来:WEXITSTATUS(stat_loc)

int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);  //功能更多,可以等待指定的组ID里面的进程

pid_t wait3(int *status, int options,struct rusage *rusage);  //可以

pid_t wait4(pid_t pid, int *status, int options,struct rusage *rusage);

int execl(const char *path, const char *arg, ...);

int execlp(const char *file, const char *arg, ...);

int execle(const char *path, const char *arg,..., char * const envp[]);

int execv(const char *path, char *const argv[]);

int execvp(const char *file, char *const argv[]);

int execvpe(const char *file, char *const argv[],char *const envp[]);

int fexecve(int fd, char *const argv[], char *const envp[]);

//L后缀:表示arg参数是一个一个传

//V后缀:表示是把arg整个数组全部传

//E后缀:表示传递环境变量数组

//P后缀:表示去filename作为参数

int setuid(uid_t uid);  //设置实际用户ID

int setgid(gid_t gid);

int setreuid(uid_t ruid, uid_t euid);   //设置实际用户ID,和有效用户ID

int setregid(gid_t rgid, gid_t egid);

int seteuid(uid_t uid);  //设置有效用户ID

int setegid(gid_t gid);

int system(const char *command);  //在函数中直接执行命令

int acct(const char *filename);  //把"filename"文件作为进程记录的文件

char *getlogin(void);   //获得当前登录用户名

int getlogin_r(char *buf, size_t bufsize);

struct passwd *getpwnam(const char *name);  //使用登录名可以获得下面结构的信息

struct passwd *getpwuid(uid_t uid);

int    getpwnam_r(const char *name, struct passwd *pwd,char *buf, size_t buflen, struct passwd **result);

int    getpwuid_r(uid_t uid, struct passwd *pwd,char *buf, size_t buflen, struct passwd **result);

/*********************/

struct passwd {

               char   *pw_name;       /* username */

               char   *pw_passwd;     /* user password */

               uid_t   pw_uid;        /* user ID */

               gid_t   pw_gid;        /* group ID */

               char   *pw_gecos;      /* user information */

               char   *pw_dir;        /* home directory */

               char   *pw_shell;      /* shell program */

           };

/*********************/

int nice(int inc);  //管理进程调度的时间

clock_t times(struct tms *buf);   //获得程序执行过程中的某一段的时间信息

/第九章   进程关系******************************************/

9.2:

命令: tty

    显示当前终端的文件位置

函数: ttyname

    显示当前终端的文件位置

9.3:

函数: getpgrp  ——    pid_t getpgrp(void);

    返回调用进程的进程组ID

函数: getpgid  ——   pid_t getpgid(pid_t pid); 

    返回调用进程的进程组ID

函数: setpgid  ——   int setpgid(pid_t pid, pid_t pgid);

    可以加入一个现有的进程组或者创建一个新进程组

9.5:

函数: setsid  ——     pid_t setsid(void);

    建立一个新的会话

函数: getsid  ——    pid_t getsid(pid_t pid);

    获得会话id

9.7:

函数: tcgetpgrp  ——     

    返回前台进程组ID

函数: tcsetpgrp  ——    

    设置前台进程组ID

函数: tcsetpgrp  ——    

    获得会话首进程的进程ID

/第十章    信号******************************************/

core 文件:    

            # ulimit -c   //默认core文件大小为0,所以一般不产生core文件

            # ulimit -c 1024

            # ulimit -c

            1024

            # ./a.out 

            段错误 (核心已转储)

            # ls

            3.c  a.out core

            #gdb a.out core 

            (gdb)where  或(bt)

            #0  0x000000000040053c in core_test ()

            #1  0x0000000000400559 in main ()

10.3

gcc -S -o 1.S 1.c //生成汇编

typedef void (*sighandler_t)(int);  //信号处理函数

sighandler_t signal(    int signum,     sighandler_t handler);  //设置信号处理函数

int sigaction(int sig, const struct sigaction *restrict act,struct sigaction *restrict oact);

struct sigaction

{

    void(*sa_handler) (int signo) //信号处理函数

    sigset_t          sa_mask  //信号集

    int sa_flags            //标志位          

    void(*sa_sigaction) (void *, siginfo_t *, void *)    //指向信号捕捉功能的指针。

}

int pause(void);  //休眠,直到收到一个信号

命令:kill -l    //显示所有信号的宏名

int kill(pid_t pid, int sig);  //给进程发送信号

int raise(int sig);  //发送给所有进程

unsigned int alarm(unsigned int seconds);  //闹钟函数

int sigemptyset(sigset_t *set);  //清除空间

int sigfillset(sigset_t *set);  //清除空间,并加入所有信号

int sigaddset(sigset_t *set, int signum);  //添加信号

int sigdelset(sigset_t *set, int signum);  //删除信号

int sigismember(const sigset_t *set, int signum);  //测试信号signum是否再信号集set中

int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); //设置信号屏蔽集

                                            //SIG_BLOCK ,把原来的信号屏蔽集与set的信号集,构成一个并集,变成一个新的信号屏蔽集

                                            //SIG_SETMASK ,把原来的信号屏蔽集与set的信号集,构成一个交集,变成一个新的信号屏蔽集

                                            //SIG_UNBLOCK ,把set的信号集变成一个新的信号屏蔽集

void siglongjmp(sigjmp_buf env, int val);  //跳转

int  sigsetjmp(sigjmp_buf env, int savesigs);  //设置跳转点

int sigpending(sigset_t *set);  //测试信号

void abort(void);

int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);  //纳米级休眠

int clock_nanosleep(clockid_t clock_id, int flags,const struct timespec *rqtp, struct timespec *rmtp);  //基于特定时钟的休眠

int sigqueue(pid_t pid, int sig, const union sigval value);  //信号排队发送函数

/第十一章   线程******************************************/

int pthread_equal(pthread_t t1, pthread_t t2);  //对两个线程ID进行比较

pthread_t pthread_self(void);    //获得自身的线程ID

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);  //创建线程

void pthread_exit(void *retval);  //主动退出线程

int pthread_join(pthread_t thread, void **retval);   //等待线程结束并,并接收返回值

int pthread_cancel(pthread_t thread);  //同一进程的线程取消其他线程

void pthread_cleanup_push(void (*routine)(void *),void *arg);   //注册线程清理函数

void pthread_cleanup_pop(int execute);   //触发清理函数

/********************************  互斥量  *******/

int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);  //互斥锁初始化

int pthread_mutex_lock(pthread_mutex_t *mutex);   //互斥锁上锁

int pthread_mutex_trylock(pthread_mutex_t *mutex);  //互斥锁判断上锁

int pthread_mutex_unlock(pthread_mutex_t *mutex);   //互斥锁解锁

int pthread_mutex_destroy(pthread_mutex_t *mutex);  //消除互斥锁

int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,const struct timespec *restrict abs_timeout);  //在绝对时间内等待锁

/********************************  读写锁  *******/

int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);   //读写锁初始化  

int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);   //读写锁上 读 锁

int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);   //读写锁判断 可读

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);   //读写锁上 写 锁

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);   //读写锁判断 可写

int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);      //读写锁解锁

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);   //读写锁消除

int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock,const struct timespec *restrict abs_timeout);  //在绝对时间内等待 写 锁

int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock,const struct timespec *restrict abs_timeout);  //在绝对时间内等待 读 锁

/********************************  条件变量 *******/

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);     //使用初始化函数

int pthread_cond_destroy(pthread_cond_t *cond);          //条件变量的销毁函数

int pthread_cond_timedwait(pthread_cond_t *restrict cond,         //条件变量等待函数

              pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);

int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);

int pthread_cond_broadcast(pthread_cond_t *cond);  //通知所有消费者

int pthread_cond_signal(pthread_cond_t *cond);   //只能唤醒其中任意一个消费者

/********************************  自旋锁  *******/

int pthread_spin_lock(pthread_spinlock_t *lock);     //上锁

int pthread_spin_trylock(pthread_spinlock_t *lock);   //自旋锁判断

int pthread_spin_unlock(pthread_spinlock_t *lock);   //释放自旋锁

int pthread_spin_destroy(pthread_spinlock_t *lock);   //清除自旋锁

int pthread_spin_init(pthread_spinlock_t *lock, int pshared);   //初始化

/********************************  屏障  *******/

int pthread_barrier_destroy(pthread_barrier_t *barrier);  //销毁

int pthread_barrier_init(pthread_barrier_t *restrict barrier,   //count指定所有线程继续运行之前,必须到达屏障的线程数量

                        const pthread_barrierattr_t *restrict attr, unsigned count);

int pthread_barrier_wait(pthread_barrier_t *barrier);    //等待所有线程达到

/第十二章   线程控制******************************************/

int pthread_attr_init(pthread_attr_t *attr);   //属性初始化

int pthread_attr_destroy(pthread_attr_t *attr);       //销毁attr    

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);  //设置分离状态属性

int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);  //获取分离状态属性                        

                                    //PTHREAD_CREATE_DETACHED  ———— 分离态                        

                                    //PTHREAD_CREATE_JOINABLE  ———— 正常态                    

int pthread_attr_setstack(pthread_attr_t *attr,void *stackaddr, size_t stacksize);  //设置栈的大小

int pthread_attr_getstack(pthread_attr_t *attr,void **stackaddr, size_t *stacksize);  //获取栈的大小

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);  //设置栈的最先大小,如PTHREAD_STACK_MIN (16384) bytes.

int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);      //获取栈的最大大小                    

int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);  //设置栈溢出后,缓冲区的大小

int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize);  //获得栈溢出后,缓冲区的大小                    

int pthread_attr_setstack();  //设置栈的大小

int pthread_attr_getstack();  //获取栈的大小

int pthread_attr_setstacksize();  //设置栈的最先大小,如PTHREAD_STACK_MIN (16384) bytes.

int pthread_attr_getstacksize();      //获取栈的最大大小                    

int pthread_attr_setguardsize();  //设置栈溢出后,缓冲区的大小

int pthread_attr_getguardsize();  //获得栈溢出后,缓冲区的大小                            

int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);  //互斥量属性销毁

int pthread_mutexattr_init(pthread_mutexattr_t *attr);  //互斥量属性初始化                        

//线程取消                        

int pthread_setcancelstate(int state, int *oldstate);

                             //PTHREAD_CANCEL_ENABLE 线程是可取消的,这是所有新线程的默认取消状态

                             //PTHREAD_CANCEL_DISABLE 线程是不可取消的,如果接收到取消请求,它将被阻塞,直到可以celability启用。

int pthread_setcanceltype(int type, int *oldtype);   //在还没到达取消点时,可以通过这个修改取消类型                             

void pthread_testcancel(void);  //自己添加取消点                        

//线程信号                        

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);  //    屏蔽字修改函数                

int pthread_kill(pthread_t thread, int signo);    //向指定线程发送信号                          

int sigwait(const sigset_t *restrict set, int *restrict signop);  //等待信号集的任意一个信号                        

int pthread_atfork(void (*prepare)(void), void (*parent)(void),void (*child)(void));  //线程创建进程                        

/第十三章  守护进程******************************************/

void openlog(const char *ident, int option, int facility);  //打开日志文件/var/log/syslog

void syslog(int priority, const char *format, ...);  //向日志文件写内容

void closelog(void);                                  //关闭日志文件    

/第十四章  高级IO******************************************/                        

O_NONBLOCK  //实现非阻塞打开                        

int fcntl(int fd, int cmd, ... /* arg */ );   //文件锁                        

//————————————————————— IO多路复用 ——————————————                        

int pselect(int nfds, fd_set *restrict readfds,

              fd_set *restrict writefds, fd_set *restrict errorfds,

              const struct timespec *restrict timeout,

              const sigset_t *restrict sigmask);

int select(int nfds, fd_set *restrict readfds,

              fd_set *restrict writefds, fd_set *restrict errorfds,

              struct timeval *restrict timeout);

void FD_CLR(int fd, fd_set *fdset);

int  FD_ISSET(int fd, fd_set *fdset);

void FD_SET(int fd, fd_set *fdset);

void FD_ZERO(fd_set *fdset);                        

int poll(struct pollfd fds[], nfds_t nfds, int timeout);                        

/  异步I/O

int aio_read(struct aiocb *aiocbp);  /* 提交一个异步读 */

int aio_write(struct aiocb *aiocbp); /* 提交一个异步写 */

int aio_cancel(int fildes, struct aiocb *aiocbp); /* 取消一个异步请求(或基于一个fd的所有异步请求,aiocbp==NULL) */

int aio_error(const struct aiocb *aiocbp);        /* 查看一个异步请求的状态(进行中EINPROGRESS?还是已经结束或出错?) */

ssize_t aio_return(struct aiocb *aiocbp);         /* 查看一个异步请求的返回值(跟同步读写定义的一样) */

int aio_suspend(const struct aiocb * const list[], int nent, const struct timespec *timeout); /* 阻塞等待请求完成 */

int aio_fsync(int op, struct aiocb *aiocbp);

int aio_error(const struct aiocb *aiocbp);

int lio_listio(int mode, struct aiocb *const aiocb_list[],

                      int nitems, struct sigevent *sevp);

struct sigevent {

    int sigev_notify; //通知类型

    int sigev_signo; //信号的编号

    union sigval sigev_value; //sigev_notify_function传递的参数

    void (*sigev_notify_function)(union sigval); /* 异步IO请求完成后,执行的函数 */

    pthread_attr_t *sigev_notify_attributes; /* notify attrs */

};

//分散读和集中写

ssize_t readv(int fd, const struct iovec *iov, int iovcnt);  

ssize_t writev(int fd, const struct iovec *iov, int iovcnt);

ssize_t preadv(int fd, const struct iovec *iov, int iovcnt,off_t offset);

ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt,off_t offset);

struct iovec {

    void  *iov_base;    /* Starting address */

    size_t iov_len;     /* 要读的区域的长度 */

};

//存储IO映射

void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);  //建立存储空间

int   munmap(void *addr, size_t length);  //解除存储空间

int mprotect(void *addr, size_t len, int prot);  //更改现有映射的权限

int msync(void *addr, size_t length, int flags);  //冲洗空间

/第十五章  进程间通信******************************************/                        

int pipe(int pipefd[2]);  //创建无名管道

FILE *popen(const char *command, const char *type);  //创建标准流管道   如:popen(“ls -a”, "r")

int   pclose(FILE *stream);                            //关闭标准流管道

int mkfifo(const char *pathname, mode_t mode);  //创建有名管道

//信号量

int semget(key_t key, int nsems, int semflg);  //创建或获取信号量

int semop(int semid, struct sembuf *sops, size_t nsops)  //获得或释放一个信号量

int semctl(int semid, int semnum, int cmd, union semun arg);  //信号量控制

key_t ftok(const char *pathname, int proj_id);  //创建IPC的key(键)

//共享内存

int   shmget(key_t key, size_t size, int shmflg);  //从内存中获得一段共享内存区域

void *shmat(int shmid, const void *shmaddr, int shmflg);  //映射共享内存

int   shmdt(const void *shmaddr);   //撤销共享内存

int   shmctl(int shmid, int cmd, struct shmid_ds *buf);  //共享内存控制

ipcs -m  //显示系统中的共享内存

//消息队列

int     msgget(key_t key, int msgflg);  //创建或打开消息队列

int     msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);  //添加消息

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);//读取消息

int     msgctl(int msqid, int cmd, struct msqid_ds *buf);  //控制消息队列

/*

*

*******************************          驱动          **************************

*

*/

lsmod(list module,将模块列表显示)

insmod(install module,安装模块)

modinfo(module information,模块信息)

rmmod(remove module,卸载模块)

modprobe、depmod,可以添加依赖

dmesg  //正常得不到驱动的输出信息,可以用这个显示

//创建设备文件

mknod filename type major minor   //如:mknod /dev/test c  251 0

register_chrdev_region()   //新的指定注册

alloc_chrdev_region()      //系统自动分配

unregister_chrdev_region() //注销

static inline int register_chrdev(unsigned int major, const char *name,

                  const struct file_operations *fops)          //老的指定注册

cat /proc/devices      //打印所有设备

//用户空间和内核空间的转换

copy_from_user(void *to, const void __user *from, unsigned long n)  //用来将数据从  用户  空间复制到  内核  空间

copy_to_user(void __user *to, const void *from, unsigned long n)   //用来将数据从  内核  空间复制到  用户  空间

//如果要复制的内存是简单类型,如char、 int、 long等

put_user()

get_user()

int val;                    /* 内核空间整型变量*/

get_user(val, (int *) arg); /* 用户→内核, arg是用户空间的地址 */

put_user(val, (int *) arg); /* 内核→用户, arg是用户空间的地址 */

///

request_mem_region(start,n,name)  //向内核申请(报告)需要映射的内存资源

release_mem_region(start,n)    //销毁内存资源

ioremap(cookie,size)             //真正用来实现映射,传给他物理地址他给你映射返回一个虚拟地址

iounmap(cookie)               //销毁映射

/*************  新的指定注册  方式        *************/

int register_chrdev_region(dev_t from, unsigned count, const char *name)        //函数用于已知起始设备的设备号的情况

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,const char *name)//用于设备号未知,向系统动态申请未被占用的设备号的情况,

                                                                                       //函数调用成功之后,会把得到的设备号放入第一个参数dev中

unregister_chrdev_region    (dev_t from, unsigned count)                       //释放原先申请的设备号

cdev_alloc             //用于动态申请一个cdev内存

void cdev_init(struct cdev *, struct file_operations *)       //用于初始化cdev的成员,并建立cdev和file_operations之间的连接

int cdev_add(struct cdev *, dev_t, unsigned)               //注册,它的调用通常发生在字符设备驱动模块加载函数中

void cdev_del(struct cdev *)                                 //注销,它的函数的调用则通常发生在字符设备驱动模块卸载函数中

struct cdev {

2 struct kobject kobj; /* 内嵌的kobject对象 */

3 struct module *owner; /* 所属模块*/

4 struct file_operations *ops; /* 文件操作结构体*/

5 struct list_head list;

6 dev_t dev; /* 设备号*/

7 unsigned int count;

}

//处理设备号的宏定义

MAJOR(dev_t dev)                    //从设备号中提取major  

MINOR(dev_t dev)                   //从设备号中提取major和minor

MKDEV(int major,int minor);         //通过major和minor构建设备号dev_t

一般用法:

register_chrdev_region + cdev_init + cdev_add   //注册

cdev_del + unregister_chrdev_region              //销毁

/******************************************************/

/*************    创建设备文件   **************/

class_create(owner, name)  //创建一个设备类 

owner:THIS_MODULE

name  : 名字

//创建后会产生/sys/class/ljj_class,/sys/devices/virtual/ljj_class

//和  ls  /sys/class/ljj_class/test/

-r--r--r--    1 root     0             4096 Jan  1 12:01 dev

drwxr-xr-x    2 root     0                0 Jan  1 12:03 power

lrwxrwxrwx    1 root     0                0 Jan  1 12:03 subsystem -> ../../../../class/ljj_class

-rw-r--r--    1 root     0             4096 Jan  1 12:03 uevent

void class_destroy(struct class *cls)   //销毁一个设备类

struct device *device_create(struct class *class, struct device *parent,

            dev_t devt, void *drvdata, const char *fmt, ...)  //创建一个字符设备文件

struct class *class  :类

struct device *parent:NULL

dev_t devt  :设备号

void *drvdata  :NULL

const char *fmt  :名字

device_destroy(struct class *class, dev_t devt);  //销毁一个字符设备文件

/*************************************************/

readb(c)        //c为要读的地址

writel(v,  c)   //v为要写的值,c为要写的地址