Unix环境编程------Unix编程实例------进程组&&会话

来源:互联网 发布:计算机四级有用吗 知乎 编辑:程序博客网 时间:2024/04/30 11:34
/*
编程实例:
编写一小段程序,要求调用fork并使子进程建立一个新的会话,
验证子进程变成了进程组组长且不再具有控制终端。


这个问题应当如何解决呢???


我认为有以下几个方面:
 1.如何建立一个新的会话???
 2.如何看待进程组组长???
   谁会是进程组组长???
什么样的进程没有控制终端???

 3.进程组组长有哪些权限???
 
 4.如何创建一个进程组???
 5.如何创建一个会话???
 6.如何获取一个进程的进程组ID???
 7.一个进程如何设置其进程组ID ??
 8.谁是会话 ???会话如何标识??
 9.会话有哪些权限???
 10.谁能能创建一个会话???
 11.如何创建一个会话???
 12.会话首进程有哪些权限???
 13.会话的功能是不是由会话首进程来代为实现。
 14.为什么进程组的组长不能创建一个新会话???
 
 
 
 
 
要点:
1.会话首进程是创建该会话的进程
2.进程调用setsid函数建立一个新会话
   /*
#include<unistd.h>
pid_t setsid(void )

*/
  
 
 
 
*/


::::::::::::


解疑答惑:
现在开始
(其实,我们该如何看待 进程组 会话呢??? 其实,我们不防从管理学的角度考虑。
为什么会出现 用户  、控制终端 、会话、进程组、进程  ???
我们不防把这看成一种管理策略,这是一种金字塔型的管理方法,所以 会话和进程组都只是用户为了管理进程而引出的一种管理机制。




:::1.谁会是进程组组长???
:组长进程的标识是: 其进程组ID等于其进程ID
 2.进程组组长有哪些权限???
: ().组长进程可以创建一个进程组,创建该组中的进程,然后终止。
  ().进程可以调用  setpgid()创建一个新的进程组。
  ().调用setpgid()可以加入一个现有的组
  ().
 3.进程可以调用  setsid()创建一个新的进程组
    /*
 
*/
 
 4.如何获取一个进程的进程组ID???
   /*
#include<unistd.h>
pid_t getpgid(pid_t pid)
//返回值为: 进程ID为 pid 的进程的进程组ID
 
函数原型:
  NAME
       setpgid, getpgid, setpgrp, getpgrp - set/get process group


SYNOPSIS
       #include <unistd.h>


       int setpgid(pid_t pid, pid_t pgid);
       pid_t getpgid(pid_t pid);


       pid_t getpgrp(void);                /* POSIX.1 version */
       pid_t getpgrp(psid_t pid);          /* BSD version */


       int setpgrp(void);                  /* System V version */
       int setpgrp(pid_t pid, pid_t pgid); /* BSD version */


   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):


       getpgid(): _XOPEN_SOURCE >= 500
       setpgrp() (POSIX.1): _SVID_SOURCE || _XOPEN_SOURCE >= 500


       setpgrp() (BSD), getpgrp() (BSD): _BSD_SOURCE && ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE || _XOPEN_SOURCE_EXTENDED || _GNU_SOURCE || _SVID_SOURCE)


DESCRIPTION
       All  of these interfaces are available on Linux, and are used for getting and setting the process group ID (PGID) of a process.  The preferred, POSIX.1-specified ways of
       doing this are: getpgrp(void), for retrieving the calling process鈥檚 PGID; and setpgid(), for setting a process鈥檚 PGID.


       setpgid() sets the PGID of the process specified by pid to pgid.  If pid is zero, then the process ID of the calling process is used.  If pgid is zero, then the PGID  of
       the  process  specified  by  pid is made the same as its process ID.  If setpgid() is used to move a process from one process group to another (as is done by some shells
       when creating pipelines), both process groups must be part of the same session (see setsid(2) and credentials(7)).  In this case, the pgid specifies an existing  process
       group to be joined and the session ID of that group must match the session ID of the joining process.


       The POSIX.1 version of getpgrp(), which takes no arguments, returns the PGID of the calling process.


       getpgid() returns the PGID of the process specified by pid.  If pid is zero, the process ID of the calling process is used.  (Retrieving the PGID of a process other than
       the caller is rarely necessary, and the POSIX.1 getpgrp() is preferred for that task.)


       The System V-style setpgrp(), which takes no arguments, is equivalent to setpgid(0, 0).


       The BSD-specific setpgrp() call, which takes arguments pid and pgid, is equivalent to setpgid(pid, pgid).


       The BSD-specific getpgrp() call, which takes a single pid argument, is equivalent to getpgid(pid).


RETURN VALUE
       On success, setpgid() and setpgrp() return zero.  On error, -1 is returned, and errno is set appropriately.


       The POSIX.1 getpgrp() always returns the PGID of the caller.


       getpgid(), and the BSD-specific getpgrp() return a process group on success.  On error, -1 is returned, and errno is set appropriately.
*/

5.如何设置一个进程的进程组ID ???

 /*
进程可以通过调用setpgid()来加入一个现有的组。
#include<unistd.h>
int setpgid(pid_t pid ,pid_t pgid);
注意: 一个进程只能为它自己或它的子进程设置进程组ID
   setpgid的函数原型:

 :
 
 */

0 0
原创粉丝点击