linux pid t 头文件_linux系统调用相关头文件
Linux C 一些函數(shù) 所屬的頭文件 2011-03-07 10:25:07
分類: LINUX
在編寫程序時,有時總是不記得所使用的函數(shù)在哪個庫函數(shù)中。現(xiàn)在先把自己以前經(jīng)常用到的函數(shù)頭文件總結(jié)一下。 有不對的地方還請指教。
1,系統(tǒng)調(diào)用 文件的操作函數(shù)
#inlclude <fcntl.h>
int open(char *name,int how) 第二個參數(shù),O_RDONLY O_WRONLY O_RDWR O_CREAT
#include <unistd.h>
int close(int fd)
size_t read(int fd,void *buf, size_t count)
size_t write(int fd,const void *buf,size_t count)
sleep(1) 系統(tǒng)睡眠一秒鐘,最小單位為一秒。
#define msleep(x) usleep(x*1000)
msleep(500); 系統(tǒng)睡眠0.5秒
#include <stdio.h>
perror("會出現(xiàn)錯誤的函數(shù)名")
#include <string.h>
char *strerror(int errnum) 依據(jù)錯誤代碼errnum來查找錯誤原因字符串
char *strcpy(char *dest,const char *src)
int strcmp(char *s1,const char *s2) s1若等于s2的值則返回0值
int strncmp(char *s1,const char *s2,int n) 前n個字符串比較
2,進程控制函數(shù)
#include <unistd.h>
pid_t fork(void) 子進程中返回0 父進程中返回子進程ID 出錯返回-1
pid_t getpid(void) pid_t getppid(void)
pid_t vfork(void)
exec函數(shù)族
進程pid 的類型為pid_t 類型,它包含于#include <sys/types.h> 若定義一個進程pid變量,則需要包含此頭文件
exit(n)結(jié)束進程 父進程可以由wait函數(shù)來獲得子進程結(jié)束裝狀態(tài)。
在進程結(jié)束的時候,會關(guān)閉文件描述符號,做一些清理工作,只保留進程返回狀態(tài)等信息
調(diào)用exit(),子進程會關(guān)閉所有打開的進程描述符 exit會作清理工作,比如說,釋放內(nèi)存(在C++里面會主動的調(diào)用析構(gòu)函數(shù),),關(guān)閉文件句柄的工作,包括刷新IO流。
_exit(n)直接退出,不會做一些清理工作,也不會關(guān)閉文件描述符。
#include <sys/wait.h>
pid_t wait(int *status) 等待任意子進程結(jié)束。子進程結(jié)束狀態(tài)值由status返回。
如WEXITSTATUS(status) 可以獲得exit(2)中返回的值,status=2,這樣就可以知道所等待的為哪個進程。如果不用這個宏轉(zhuǎn)換,則status=512.
pid_t waitpid(pid_t pid,int status,int options) 可以指定等待某個進程號pid的進程結(jié)束
在使用 waitpid函數(shù)時還用到了pid參數(shù),所以還要加上#include <sys/types.h>
關(guān)于進程等待函數(shù)還有很多宏將status轉(zhuǎn)換為需要的值,需要了解。
3,進程間通信-管道
#include <unistd.h>
int pipe(int filedes[2])
4,進程間通信-命名管道
#include <sys/types.h> #include <sys/stat.h>
int mkfifo(const char *pathname,mode_t mode)
對于命名管道的操作同普通文件的操作
5,消息隊列
數(shù)據(jù)類型key_t是在頭文件sys/types.h中定義的,它是一個長整形的數(shù)據(jù)。
key=ftok(".",'A') #include <sys/types.h> #include <sys/ipc.h>
所屬頭文件:#include<sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>
int msgid;
msgid=msgget(k
總結(jié)
以上是生活随笔為你收集整理的linux pid t 头文件_linux系统调用相关头文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pytorch的BatchNorm层使用
- 下一篇: linux网卡有很多error,教你设置