Add Book to My BookshelfPurchase This Book Online

Chapter 11 - Processes

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Job Control
As discussed at the beginning of the chapter, sessions and process groups exist for the purposes of performing job control. A process group is a group of related processes, such as those in a pipeline. A session is a group of related process groups, such as all of the jobs currently being run by a user on a specific terminal. Usually, sessions are created by the system login process and process groups are managed by a job control shell; the average program doesn't have to worry about them. However, sometimes it is desirable to be able to manipulate them directly.
A new session is created with the setsid function:
    #include <sys/types.h>
    #include <unistd.h>
    pid_t setsid(void);
If the process is not already a process group leader, three things happen when setsid is called:
 1.The process becomes the session leader of a new session. The session ID of this new session will be the same as the process' process ID.
 2.The process becomes the process group leader of a new process group. The process group ID of this new process group will be the same as the process' process ID (and thus the session ID).
 3.If the calling process was associated with a controlling terminal, that association is broken. If the process later opens a terminal device, the first device opened will become the process' controlling terminal.
A process that is already a process group leader may not call setsid. To make sure, call fork and have the parent process terminate and the child process continue. If a new session is created, setsid returns the session ID of the session. Otherwise, -1 is returned and errno is set to the error condition.
A process may create a new process group, or join an existing one, by calling setpgid:
    #include <sys/types.h>
    #include <unistd.h>
    int setpgid(pid_t pid, pid_t pgid);
This function sets the process group ID of the process with process ID pid to pgid. If pgid is equal to pid, the process becomes a process group leader. A process may only change the process group of itself and its children. If setpgid succeeds, it returns 0. Otherwise, it returns -1 and stores the reason for failure in errno.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback