linux pid t 头文件_linux系统调用相关头文件
Linux C 一些函數(shù) 所屬的頭文件 2011-03-07 10:25:07
分類: LINUX
在編寫(xiě)程序時(shí),有時(shí)總是不記得所使用的函數(shù)在哪個(gè)庫(kù)函數(shù)中。現(xiàn)在先把自己以前經(jīng)常用到的函數(shù)頭文件總結(jié)一下。 有不對(duì)的地方還請(qǐng)指教。
1,系統(tǒng)調(diào)用 文件的操作函數(shù)
#inlclude <fcntl.h>
int open(char *name,int how) 第二個(gè)參數(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("會(huì)出現(xiàn)錯(cuò)誤的函數(shù)名")
#include <string.h>
char *strerror(int errnum) 依據(jù)錯(cuò)誤代碼errnum來(lái)查找錯(cuò)誤原因字符串
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個(gè)字符串比較
2,進(jìn)程控制函數(shù)
#include <unistd.h>
pid_t fork(void) 子進(jìn)程中返回0 父進(jìn)程中返回子進(jìn)程ID 出錯(cuò)返回-1
pid_t getpid(void) pid_t getppid(void)
pid_t vfork(void)
exec函數(shù)族
進(jìn)程pid 的類型為pid_t 類型,它包含于#include <sys/types.h> 若定義一個(gè)進(jìn)程pid變量,則需要包含此頭文件
exit(n)結(jié)束進(jìn)程 父進(jìn)程可以由wait函數(shù)來(lái)獲得子進(jìn)程結(jié)束裝狀態(tài)。
在進(jìn)程結(jié)束的時(shí)候,會(huì)關(guān)閉文件描述符號(hào),做一些清理工作,只保留進(jìn)程返回狀態(tài)等信息
調(diào)用exit(),子進(jìn)程會(huì)關(guān)閉所有打開(kāi)的進(jìn)程描述符 exit會(huì)作清理工作,比如說(shuō),釋放內(nèi)存(在C++里面會(huì)主動(dòng)的調(diào)用析構(gòu)函數(shù),),關(guān)閉文件句柄的工作,包括刷新IO流。
_exit(n)直接退出,不會(huì)做一些清理工作,也不會(huì)關(guān)閉文件描述符。
#include <sys/wait.h>
pid_t wait(int *status) 等待任意子進(jìn)程結(jié)束。子進(jìn)程結(jié)束狀態(tài)值由status返回。
如WEXITSTATUS(status) 可以獲得exit(2)中返回的值,status=2,這樣就可以知道所等待的為哪個(gè)進(jìn)程。如果不用這個(gè)宏轉(zhuǎn)換,則status=512.
pid_t waitpid(pid_t pid,int status,int options) 可以指定等待某個(gè)進(jìn)程號(hào)pid的進(jìn)程結(jié)束
在使用 waitpid函數(shù)時(shí)還用到了pid參數(shù),所以還要加上#include <sys/types.h>
關(guān)于進(jìn)程等待函數(shù)還有很多宏將status轉(zhuǎn)換為需要的值,需要了解。
3,進(jìn)程間通信-管道
#include <unistd.h>
int pipe(int filedes[2])
4,進(jìn)程間通信-命名管道
#include <sys/types.h> #include <sys/stat.h>
int mkfifo(const char *pathname,mode_t mode)
對(duì)于命名管道的操作同普通文件的操作
5,消息隊(duì)列
數(shù)據(jù)類型key_t是在頭文件sys/types.h中定義的,它是一個(gè)長(zhǎng)整形的數(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系统调用相关头文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Pytorch的BatchNorm层使用
- 下一篇: linux 其他常用命令