CSDN专家博客精华版

为人民服务!
  首页  :: 新随笔  :: 管理

进程编程3 - UNIX高级环境编程第9章读书笔记

Posted on 2007-12-17 10:17  csdnexpert  阅读(98)  评论(0编辑  收藏  举报

 

9 Process Relationships

1 Process Groups

1.     每个进程属于一个Process Group,这个process Group从同样的Terminal获得Signal

2.     Getpgrp可以获得process Group ID,也用pid_t结构表示:

#include <unistd.h>

 

pid_t getpgrp(void);

 

返回调用进程所属于的Process Group ID

3.     getpgid可以获得某个进程的Process Group ID

#include <unistd.h>

 

pid_t getpgid(pid_t pid);

 

返回pid制定的进程所属于的Process Group ID

如果参数pid = 0,则返回调用进程所属的ProcessGroupID

4.     每个Group都有一个Leader,这个LeaderProcessID = Process Group ID

5.     一个进程调用setpgid来参加或者创建一个process Group

#include <unistd.h>

 

int setpgid(pid_t pid, pid_t pgid);

 

成功返回0,错误返回-1

注意如果pid=pgid,则指定进程成为Process Group Leader

如果pid=0,则指定进程为调用进程

2 Sessions

1.     Session是一个或者多个Process Group

2.     调用setsid函数来创建一个新的session

#include <unistd.h>

 

int setsid(void);

 

成功返回0,错误返回-1

 

3.     调用setsid函数,如果该进程不是Process Group Leader,则函数会创建一个新的Session

a.     进程成为SessionSession Leader

b.     成为新的Process GroupLeader

c.     进程没有Controlling Terminal

4.     Single UNIX Specification没有Session ID,不过我们可以认为一个SessionSession LeaderProcess ID = Session ID

5.     getsid可以获得Session ID

#include <unistd.h>

 

pid_t getsid(pid_t pid);

 

成功返回Session LeaderProcess ID,错误返回-1

同样的,pid = 0标明是调用进程

 

3 Controlling Terminal

SessionProcess Group有下面特性:

1.     Session只能有一个Controlling Terminal

2.     Session LeaderControlling Terminal建立联系,称之为Controlling Process

3.     Session中的Process Group可以被分为一个Foreground process group和多个Background process group

4.     按下Interrupt Key (DELETE or CTRL+C)或者Quit Key (Ctrl+\)signal会发送给Foreground Process Group中的所有Process

5.     如果network/modem disconnect被检测到,则Controlling Process会收到一个hang-up signal

6.     大部分时候Controlling Terminal就是我们Login时候的Terminal

4 tcgetpgrp, tcsetpgrp, tcgetsid

1.     下面这些函数可以被用来告诉Kernel那些Process GroupForeground,那些是Background

#include <unistd.h>

 

pid_t tcgetpgrp(int filedes);

 

成功返回Foreground Process GroupID,错误返回-1

 

int tcsetpgrp(int filedes, pid_t pgrpid);

 

成功返回0,错误返回-1

 

2.     tcgetpgrp返回filedes对应的TerminalForeground process group ID,而tcsetpgrp可以设置foreground process group id

3.     tcgetsid函数可以获得filedes所对应的Session ID,也就是Session LeaderProcess Group ID

#include <termios.h>

 

pid_t tcgetsid(int filedes);

 

成功返回Session ID,错误返回-1

 

作者:      ATField
E-Mail:  
atfield_zhang@hotmail.com
Blog:     
http://blog.csdn.net/atfield



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1540823