日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

linux下常用linux c函數(shù)--lhzw2001的筆記

 SkySeraph 2010-07-25
linux下常用linux c函數(shù)
1.     進(jìn)程ID為0的進(jìn)程通常是調(diào)度進(jìn)程,常常被稱為交換進(jìn)程
進(jìn)程ID為1的進(jìn)程通常是init進(jìn)程,在自舉過程結(jié)束時(shí)由內(nèi)核調(diào)用
進(jìn)程ID為2的進(jìn)程頁守護(hù)進(jìn)程,負(fù)責(zé)支持虛擬存儲(chǔ)系統(tǒng)的分頁操作
2.     pid_t getpid( void ); 返回值:調(diào)用進(jìn)程的進(jìn)程ID     #i nclude <unistd.h>
3.     pid_t getppid( void ); 返回值:調(diào)用進(jìn)程的父進(jìn)程ID  
4.     uid_t getuid( void ); 返回值:調(diào)用進(jìn)程的實(shí)際用戶ID
5.     uid_t geteuid( void ); 返回值:調(diào)用進(jìn)程的有效用戶ID
6.     gid_t getgid( void ); 返回值:調(diào)用進(jìn)程的實(shí)際組ID
7.     gid_t getegid( void ); 返回值:調(diào)用進(jìn)程的有效組ID
8.     pid_t fork( void );創(chuàng)建子進(jìn)程,返回值:子進(jìn)程返回0,父進(jìn)程返回子進(jìn)程ID,出錯(cuò)-1
9.     #i nclude<sys/wait.h> pid_t wait(int *statloc);//statloc 保存進(jìn)程終止?fàn)顟B(tài)的指針
10.     #i nclude<sys/wait.h>pid_t waitpid(pid_t pid,int *statloc,int options);
pid ==-1 等待任一子進(jìn)程
pid >0 等待其子進(jìn)程ID與pid相等的子進(jìn)程
pid == 0 等待其組ID等于調(diào)用進(jìn)程組ID的任一子進(jìn)程
pid <-1 等待其組ID等于pid絕對(duì)值的任一子進(jìn)程
options:
WCONTINUED 若實(shí)現(xiàn)支持作業(yè)控制,那么由pid指定的任一子進(jìn)程在暫停后已經(jīng)繼續(xù),但其狀態(tài)尚未報(bào)告,則返回其狀態(tài)
WNOHANG 若由pid指定的子進(jìn)程并不是立即可用的,則waitpid阻塞,此時(shí)其返回0
WUNTRACED 若實(shí)現(xiàn)支持作業(yè)控制,而由pid指定的任一子進(jìn)程已處于暫停狀態(tài),并且其狀態(tài)自暫停以來還未報(bào)告過,則返回其狀態(tài)
11.#i nclude<unistd.h> int setuid(uid_t uid); 設(shè)置實(shí)際實(shí)際用戶ID和有效用戶ID;
int setgid(gid_t gid); 設(shè)置實(shí)際組ID和有效組ID;成功返回0,錯(cuò)誤-1
12.#i nclude<stdlib.h>int system(const char *cmdstring)
system返回值如下            
-1出現(xiàn)錯(cuò)誤  
    0調(diào)用成功但是沒有出現(xiàn)子進(jìn)程  
    >0   成功退出的子進(jìn)程的id


(二)線程


1. #i nclude<thread.h> int pthread_equal(pthread_t tid1, pthread_t tid2);
//相等返回非0,否則返回0
2. pthread_t pthread_self(void);返回調(diào)用線程的ID
3. int pthread_create(pthread_t *restrict tidp,
  const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg) ;
創(chuàng)建線程:成功返回0,否則返回錯(cuò)誤編號(hào)
4. void pthread_exit(void *rval_ptr);//終止線程
5. int pthread_join(pthread_t thread, void **rval_ptr);
//自動(dòng)線程置于分離狀態(tài),以恢復(fù)資源。成功返回0,否則返回錯(cuò)誤編號(hào)
6. int pthread_cancel(pthread_t tid);
//請(qǐng)求取消同一進(jìn)程中的其他線程;成功返回0,否則返回錯(cuò)誤編號(hào)
7. void pthread_cleanup_push(void (*rtn)(void *), void *arg);
    //建立線程清理處理程序
8. void pthread_cleanup_pop(int execute);//調(diào)用建立的清理處理程序
9. int pthread_detach(pthread_t tid);//使線程進(jìn)入分離狀態(tài),已分離也不出錯(cuò)
10.int pthread_mutex_init(pthread_mutex_t *restrict mutex,
  const pthread_nutexattr_t *restrict attr)//初始化互斥量;成功0,失敗返回錯(cuò)誤編號(hào)
11.int pthread_mutex_destroy(pthread_mutex_t *mutex);
//若有調(diào)用malloc動(dòng)態(tài)分配內(nèi)存則用該函數(shù)釋放;成功0,失敗返回錯(cuò)誤編號(hào)
12.int pthread_mutex_lock(pthread_mutex_t *mutex);//鎖住互斥量
  int pthread_mutex_trylock(pthread_mutex_t *mutex);//嘗試上鎖
  int pthread_mutex_unlock(pthread_mutex_t *mutex);//解鎖
  成功返回0,否則返回錯(cuò)誤編號(hào)
13.int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr)//初始化讀寫鎖
  int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);//釋放資源,在釋放內(nèi)存之前使用
成功返回0,否則返回錯(cuò)誤編號(hào)
14.int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);//在讀模式下鎖定讀寫鎖
  int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);//在寫模式下鎖定讀寫鎖
  int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);//鎖住讀寫鎖
15.int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);//嘗試在讀模式下鎖定讀寫鎖
  int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);//嘗試在寫模式下鎖定讀寫鎖
成功返回0,否則返回錯(cuò)誤編號(hào)
16.int pthread_cond_init(pthread_cond_t *restrict cond, pthread_condattr_t * restrict attr)
//初始化條件變量
  int pthread_cond_destroy(pthread_cond_t *cond);//去除初始化條件變量
成功返回0,否則返回錯(cuò)誤編號(hào)
17.int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex)
  int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex,
  const struct timespec *restrict timeout);
  //等待條件變?yōu)檎?,如果在給定的時(shí)間內(nèi)條件不能滿足,那么會(huì)生成一個(gè)代表出錯(cuò)碼的返回變量 ;成功返回0,錯(cuò)誤返回錯(cuò)誤編號(hào)
18.int pthread_cond_signal(pthread_cond_t *cond);//喚醒等待該條件的某個(gè)線程
  int pthread_cond_broadcast(pthread_cond_t *cond)//喚醒等待該條件的所有線程
19.int pthread_attr_init(pthread_attr_t *attr);//初始化線程屬性
  int pthread_attr_destroy(pthread_attr_t *attr);//釋放內(nèi)存空間(動(dòng)態(tài)分配時(shí)調(diào)用)
成功返回0,否則返回錯(cuò)誤編號(hào)
20.int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate);
//獲取線程的分離狀態(tài)
  int pthread_attr_setdetachstate(const pthread_attr_t *restrict attr, int detachstate);
  //設(shè)置分離狀態(tài) PTHREAD_CREATE_DETACHED:以分離狀態(tài)啟動(dòng)線程
  PTHREAD_CREATE_JOINABLE:正常啟動(dòng)線程,應(yīng)用程序可以獲取線程的終止?fàn)顟B(tài)
    成功返回0,否則返回錯(cuò)誤編號(hào)
21.int pthread_attr_getstack(const pthread_attr_t *restrict attr,void **restrict stackaddr, size_t *restrict stacksize);//獲取線程的棧位置
int pthread_attr_setstack(const pthread_attr_t *attr, void *stackaddr, size_t *stacksize)
//設(shè)置新建線程的棧位置 ;成功返回0,否則返回錯(cuò)誤編號(hào)


(三)消息隊(duì)列


1.每個(gè)隊(duì)列都有一個(gè)msqid_ds結(jié)構(gòu)與之相關(guān)聯(lián):
    struct msqid_ds{
      struct ipc_perm msg_perm;
          msgqnum_t msg_qnum; //消息的數(shù)量
      msglen_t msg_qbytes; //最大消息的長(zhǎng)度
      pid_t msg_lspid;   //最后一個(gè)發(fā)送到消息隊(duì)列的進(jìn)程ID
      pid_t msg_lrpid;   //最后一個(gè)讀取消息的進(jìn)程ID
      time_t msg_stime;   //最后一次發(fā)送到消息隊(duì)列的時(shí)間
      time_t msg_rtime;   //最后一次讀取消息的時(shí)間
      time_t msg_ctime; //最后一次改變的時(shí)間
      。
      。
      。
  };
  struct ipc_perm{
      uid_t uid;//擁有者有效的用戶ID
      gid_t gid;//擁有者有效的組ID
      uid_t cuid;//創(chuàng)建者有效的用戶ID
      uid_t cgid;//創(chuàng)建者有效的組ID
      mode_t mode; //權(quán)限
      。
      。
  }
2.#i nclude <sys/msg.h> int msgget(key_t key, int flag);
//打開一個(gè)現(xiàn)存的隊(duì)列或創(chuàng)建一個(gè)新隊(duì)列;成功返回0,出錯(cuò)返回-1
3.int msgctl(int msqid, int cmd, struct msqid_ds *buf);//對(duì)消息隊(duì)列執(zhí)行多種操作
cmd 可選:
IPC_STAT 取此消息隊(duì)列的msqid_ds結(jié)構(gòu),并將它放在buf指向的結(jié)構(gòu)
  IPC_SET:按由buf指向結(jié)構(gòu)中的值,設(shè)置與此隊(duì)列相關(guān)結(jié)構(gòu)中的下列四個(gè)字段:msg_perm.uid,msg_perm.gid,msg_perm.mode和msg_qbytes.此命令只有下列兩種進(jìn)程才能執(zhí)行(1)其有效用戶ID等于msg_perm.cuid或msg_perm.uid;(2)具有超級(jí)用戶特權(quán)的進(jìn)程
  IPC_RMID:從系統(tǒng)中刪除消息隊(duì)列以及仍在該隊(duì)列中的所有數(shù)據(jù)。
成功返回0,失敗返回-1
4.int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag)//發(fā)送消息到消息隊(duì)列中
成功返回0, 不成功返回-1并設(shè)置errno,錯(cuò)誤碼:
EACCES   對(duì)調(diào)用程序來說,調(diào)用被否定
EAGAIN   操作會(huì)阻塞進(jìn)程,但(msgflg & IPC_NOWAIT) != 0
EIDRM     msqid已經(jīng)從系統(tǒng)中刪除了
EINTR     函數(shù)被信號(hào)中斷
EINVAL     參數(shù)msqid無效,消息類型<1,或者msgsz越界了
flag可以指定為IPC_NOWAIT 則不會(huì)阻塞直接返回EAGAIN
注:參數(shù)msgp指向用戶定義的緩沖區(qū),他是如下的結(jié)構(gòu)
struct mymsg
{
long mtypes;     消息類型
char *mtext;   消息文本
}mymsg_t
5.ssize_t msgrcv(int msqid, void *ptr, size_t nbytes, long type, int flag);//讀取消息
成功則返回消息的數(shù)據(jù)部分的長(zhǎng)度,出錯(cuò)則返回-1
type: type==0返回隊(duì)列中的第一個(gè)消息
  type>0 返回隊(duì)列中消息類型為type的第一個(gè)消息
  type<0返回隊(duì)列中消息類型值小于或等于type絕對(duì)值的消息(多個(gè)取類型值最小的)


(四) 信號(hào)量


1. 內(nèi)核為每個(gè)信號(hào)量集合設(shè)置了一個(gè)semid_ds結(jié)構(gòu):
  struct demid_ds{
    struct ipc_perm sem_perm;
    unsigned short sem_nsems; //信號(hào)量的個(gè)數(shù)
    time_t sem_otime; //上一次semop的時(shí)間
    time_t sem_ctime;//上一次change的時(shí)間
    。
    。
 };
2#i nclude<sys/sem.h>. int semget(key_t key, int nsems, int flag);//創(chuàng)建信號(hào)量
成功返回一個(gè)對(duì)應(yīng)于信號(hào)量集標(biāo)識(shí)符的非負(fù)整數(shù),不成功返回-1并設(shè)置errno,錯(cuò)誤碼:
EACCES   存在key的信號(hào)量,但沒有授予權(quán)限
EEXIST   存在key的信號(hào)量,但是
    ( (semflg & IPC_CREATE) && (semflg & IPC_EXCL) ) != 0
EINVAL   nsems <= 0或者大于系統(tǒng)的限制,或者nsems與信號(hào)量集的大小不符
ENOENT   不存在key的信號(hào)量,而且(semflg & IPC_CTEATE) == 0
ENOSPC   要超出系統(tǒng)范圍內(nèi)對(duì)信號(hào)量的限制了
功能:
函數(shù)返回與參數(shù)key相關(guān)的信號(hào)量集標(biāo)識(shí)符。
如果鍵值為IPC_PRIVATE,或者semflg&IPC_CREAT非零且沒有信號(hào)量集或標(biāo)識(shí)符關(guān)聯(lián)于key,那么函數(shù)就創(chuàng)建標(biāo)識(shí)符及與之相關(guān)的信號(hào)量集。
參數(shù)nsems指定了集合中信號(hào)量元素的個(gè)數(shù),可用0到nsems-1的整數(shù)來引用信號(hào)量集合中的單個(gè)信號(hào)量元素。
參數(shù)semflg指定信號(hào)量集的優(yōu)先級(jí),權(quán)限的設(shè)置與文件權(quán)限設(shè)置相同,并可以通過semclt來修改權(quán)限值,在使用信號(hào)量元素之前,應(yīng)該用semctl對(duì)其進(jìn)行初始化。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多