Linux environment Programming process (VI): Process Group

Source: Internet
Author: User

Process Group

Each process belongs to a process group in addition to a process ID. A process group is a collection of one or more processes. Each process group has a unique process group ID. The process group ID is similar to the process id--it is an integer and can be stored in the pid_t data type. The function GETPGRP returns the process group ID of the calling process.

Each process group can have a leader process. The identification of the leader process is that its process group ID equals its process ID. The leader process can create a process group, create a process in that group, and then terminate. As long as a process exists in a process group, the process group exists, regardless of whether its leader process terminates. The period of time from the start of a process group creation to the end of which the last process leaves is the lifetime of the process group. The last process in a process group can be terminated, or transferred to another process group.

A process can either join an existing group by calling Setpgid or create a new process group. The Setpgid function sets the process group ID of the PID process to Pgid. If the two parameters are equal, the process programming process group leader is specified by the PID. If the PID is 0, the caller's process ID is used. Also, if the PID is 0, the process ID specified by the PID will be used as the process group ID. A process can only set the process group ID for itself or its child processes. After its child process has called the EXEC function, it can no longer change the process group ID of the child process.

/* *file name:pgrp.c *author    : libing *mail      : [email protected] *funciont  : Test getpid getpgrp getpgid */#i Nclude <stdio.h> #include <unistd.h>intmain (void) {pid_t pid = Getpid ();//Get Process idprintf ("pid =%d.\n") of the calling process PID);p id_t pgrp = Getpgrp ();//Gets the process group idprintf ("PGRP =%d.\n", pgrp) of the calling process; if (Setpgid (PID, 4325))//Call failed printf ("PGRP =%d.\n ", Getpgid (PID)); return 0;}
Show Results:

PID = 4536.PGRP = 4536.pgrp = 4536.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.