Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1877877
  • 博文数量: 217
  • 博客积分: 4362
  • 博客等级: 上校
  • 技术积分: 4180
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-20 09:31
文章分类

全部博文(217)

文章存档

2017年(1)

2015年(2)

2014年(2)

2013年(6)

2012年(42)

2011年(119)

2010年(28)

2009年(17)

分类: LINUX

2009-11-25 13:57:15

1.     进程ID为0的进程通常是调度进程,常常被称为交换进程
进程ID为1的进程通常是init进程,在自举过程结束时由内核调用
进程ID为2的进程页守护进程,负责支持虚拟存储系统的分页操作
2.     pid_t getpid( void ); 返回值:调用进程的进程ID     #include
3.     pid_t getppid( void ); 返回值:调用进程的父进程ID  
4.     uid_t getuid( void ); 返回值:调用进程的实际用户ID
5.     uid_t geteuid( void ); 返回值:调用进程的有效用户ID
6.     gid_t getgid( void ); 返回值:调用进程的实际组ID
7.     gid_t getegid( void ); 返回值:调用进程的有效组ID
8.     pid_t fork( void );创建子进程,返回值:子进程返回0,父进程返回子进程ID,出错-1
9.     #include pid_t wait(int *statloc);//statloc 保存进程终止状态的指针
10.     #includepid_t waitpid(pid_t pid,int *statloc,int options);
pid ==-1 等待任一子进程
pid >0 等待其子进程ID与pid相等的子进程
pid == 0 等待其组ID等于调用进程组ID的任一子进程
pid <-1 等待其组ID等于pid绝对值的任一子进程
options:
WCONTINUED 若实现支持作业控制,那么由pid指定的任一子进程在暂停后已经继续,但其状态尚未报告,则返回其状态
WNOHANG 若由pid指定的子进程并不是立即可用的,则waitpid阻塞,此时其返回0
WUNTRACED 若实现支持作业控制,而由pid指定的任一子进程已处于暂停状态,并且其状态自暂停以来还未报告过,则返回其状态
11.#include int setuid(uid_t uid); 设置实际实际用户ID和有效用户ID;
int setgid(gid_t gid); 设置实际组ID和有效组ID;成功返回0,错误-1
12.#includeint system(const char *cmdstring)
system返回值如下            
-1出现错误  
    0调用成功但是没有出现子进程  
    >0   成功退出的子进程的id


(二)线程


1. #include int pthread_equal(pthread_t tid1, pthread_t tid2);
//相等返回非0,否则返回0
2. pthread_t pthread_self(void);返回调用线程的ID
3. int pthread_create(pthread_t *restrict tidp,
  const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg) ;
创建线程:成功返回0,否则返回错误编号
4. void pthread_exit(void *rval_ptr);//终止线程
5. int pthread_join(pthread_t thread, void **rval_ptr);
//自动线程置于分离状态,以恢复资源。成功返回0,否则返回错误编号
6. int pthread_cancel(pthread_t tid);
//请求取消同一进程中的其他线程;成功返回0,否则返回错误编号
7. void pthread_cleanup_push(void (*rtn)(void *), void *arg);
    //建立线程清理处理程序
8. void pthread_cleanup_pop(int execute);//调用建立的清理处理程序
9. int pthread_detach(pthread_t tid);//使线程进入分离状态,已分离也不出错
10.int pthread_mutex_init(pthread_mutex_t *restrict mutex,
  const pthread_nutexattr_t *restrict attr)//初始化互斥量;成功0,失败返回错误编号
11.int pthread_mutex_destroy(pthread_mutex_t *mutex);
//若有调用malloc动态分配内存则用该函数释放;成功0,失败返回错误编号
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,否则返回错误编号
13.int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr)//初始化读写锁
  int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);//释放资源,在释放内存之前使用
成功返回0,否则返回错误编号
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,否则返回错误编号
16.int pthread_cond_init(pthread_cond_t *restrict cond, pthread_condattr_t * restrict attr)
//初始化条件变量
  int pthread_cond_destroy(pthread_cond_t *cond);//去除初始化条件变量
成功返回0,否则返回错误编号
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);
  //等待条件变为真,如果在给定的时间内条件不能满足,那么会生成一个代表出错码的返回变量 ;成功返回0,错误返回错误编号
18.int pthread_cond_signal(pthread_cond_t *cond);//唤醒等待该条件的某个线程
  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);//释放内存空间(动态分配时调用)
成功返回0,否则返回错误编号
20.int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate);
//获取线程的分离状态
  int pthread_attr_setdetachstate(const pthread_attr_t *restrict attr, int detachstate);
  //设置分离状态 PTHREAD_CREATE_DETACHED:以分离状态启动线程
  PTHREAD_CREATE_JOINABLE:正常启动线程,应用程序可以获取线程的终止状态
    成功返回0,否则返回错误编号
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)
//设置新建线程的栈位置 ;成功返回0,否则返回错误编号


(三)消息队列


1.每个队列都有一个msqid_ds结构与之相关联:
    struct msqid_ds{
      struct ipc_perm msg_perm;
          msgqnum_t msg_qnum; //消息的数量
      msglen_t msg_qbytes; //最大消息的长度
      pid_t msg_lspid;   //最后一个发送到消息队列的进程ID
      pid_t msg_lrpid;   //最后一个读取消息的进程ID
      time_t msg_stime;   //最后一次发送到消息队列的时间
      time_t msg_rtime;   //最后一次读取消息的时间
      time_t msg_ctime; //最后一次改变的时间
      。
      。
      。
  };
  struct ipc_perm{
      uid_t uid;//拥有者有效的用户ID
      gid_t gid;//拥有者有效的组ID
      uid_t cuid;//创建者有效的用户ID
      uid_t cgid;//创建者有效的组ID
      mode_t mode; //权限
      。
      。
  }
2.#include int msgget(key_t key, int flag);
//打开一个现存的队列或创建一个新队列;成功返回0,出错返回-1
3.int msgctl(int msqid, int cmd, struct msqid_ds *buf);//对消息队列执行多种操作
cmd 可选:
IPC_STAT 取此消息队列的msqid_ds结构,并将它放在buf指向的结构
  IPC_SET:按由buf指向结构中的值,设置与此队列相关结构中的下列四个字 段:msg_perm.uid,msg_perm.gid,msg_perm.mode和msg_qbytes.此命令只有下列两种进程才能执行(1)其 有效用户ID等于msg_perm.cuid或msg_perm.uid;(2)具有超级用户特权的进程
  IPC_RMID:从系统中删除消息队列以及仍在该队列中的所有数据。
成功返回0,失败返回-1
4.int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag)//发送消息到消息队列中
成功返回0, 不成功返回-1并设置errno,错误码:
EACCES   对调用程序来说,调用被否定
EAGAIN   操作会阻塞进程,但(msgflg & IPC_NOWAIT) != 0
EIDRM     msqid已经从系统中删除了
EINTR     函数被信号中断
EINVAL     参数msqid无效,消息类型<1,或者msgsz越界了
flag可以指定为IPC_NOWAIT 则不会阻塞直接返回EAGAIN
注:参数msgp指向用户定义的缓冲区,他是如下的结构
struct mymsg
{
long mtypes;     消息类型
char *mtext;   消息文本
}mymsg_t
5.ssize_t msgrcv(int msqid, void *ptr, size_t nbytes, long type, int flag);//读取消息
成功则返回消息的数据部分的长度,出错则返回-1
type: type==0返回队列中的第一个消息
  type>0 返回队列中消息类型为type的第一个消息
  type<0返回队列中消息类型值小于或等于type绝对值的消息(多个取类型值最小的)


(四) 信号量


1. 内核为每个信号量集合设置了一个semid_ds结构:
  struct demid_ds{
    struct ipc_perm sem_perm;
    unsigned short sem_nsems; //信号量的个数
    time_t sem_otime; //上一次semop的时间
    time_t sem_ctime;//上一次change的时间
    。
    。
 };
2#include. int semget(key_t key, int nsems, int flag);//创建信号量
成功返回一个对应于信号量集标识符的非负整数,不成功返回-1并设置errno,错误码:
EACCES   存在key的信号量,但没有授予权限
EEXIST   存在key的信号量,但是
    ( (semflg & IPC_CREATE) && (semflg & IPC_EXCL) ) != 0
EINVAL   nsems <= 0或者大于系统的限制,或者nsems与信号量集的大小不符
ENOENT   不存在key的信号量,而且(semflg & IPC_CTEATE) == 0
ENOSPC   要超出系统范围内对信号量的限制了
功能:
函数返回与参数key相关的信号量集标识符。
如果键值为IPC_PRIVATE,或者semflg&IPC_CREAT非零且没有信号量集或标识符关联于key,那么函数就创建标识符及与之相关的信号量集。
参数nsems指定了集合中信号量元素的个数,可用0到nsems-1的整数来引用信号量集合中的单个信号量元素。
参数semflg指定信号量集的优先级,权限的设置与文件权限设置相同,并可以通过semclt来修改权限值,在使用信号量元素之前,应该用semctl对其进行初始化。
注意:
函数如果试图创


转载地址

阅读(2089) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~