"Linux Advanced Programming" (chapter eighth) process management and program development

Source: Internet
Author: User
Tags session id

Concept:

process Number (PID): The system identifies a positive integer for a process

Parent Process Number (PPID): The process number of the parent process

Process Group number (PGID): A process group is a collection of one or more processes.

session : A collection of one or more process groups.

The meaning of the control terminal I don't understand.

Functions/Commands:

PS aux: View basic properties of all processes in the current system

getpid (): Gets the current process PID

getppid (): Gets the PID of the parent process

__pid_t Getpgid (__pid_t __pid): Gets the process group number of the specified process, and the input is 0 to get the current process group number

pid_t getpgrp (void): Gets the current process process group number

int Setpgid (pid_t pid, pid_t pgid): Joins a process to a process group

Parameter 1: Process pid,0 To modify Process group number indicates current process

Parameter 2: New process group number, 0 indicates the process group number is the same as parameter 1

__pid_t getsid (__pid_t __pid): Gets the session number of the specified process, 0 means the calling process

__pid_t setsid (void): Creates a new session. If the calling process is a process group leader, the function returns an error.

pid_t tcgetpgrp (int filedes): Filedes is an open terminal that gets the process group number of the foreground process group associated with the terminal

pid_t tcsetpgrp (int filedes, pid_t pgrpid): Sets the foreground process group ID of control terminal filedes to Pgrpid. Pgrpid should be the process group ID in the same session

pid_t tcgetsid (int filedes): Gets the session ID that controls the beginning of the terminal session

#include <sdtio.h>#include<unistd.h>#include<fcntl.h>#include<stdlib.h>intMain () {intFD;    pid_t pid; PID=Fork (); if(PID = =-1) perror ("Fork"); Else if(PID >0) {wait (NULL);    Exit (Exit_failure); }    Else    {        if(FD = open ("/dev/tty1", o_rdwr)) = =-1)//note tty1 is found in PS viewing process can not find in PS can not open perror ("Open"); printf ("pid=%d,ppid=%d\n", Getpid (), Getppid ());//get Process Number parent process numberprintf"sid=%d,tcgetsid=%d\n", GetSID (Getpid ()), Tcgetsid (FD));//get Session number Terminal session numberprintf"tcgetpgrp=%d\n", Tcgetpgrp (FD));//get foreground process process group numberprintf"pigd=%d\n", Getpgid (Getpid ()));//gets the current process process group ID    }}

You can see that the process group number, the terminal's foreground process group number, is the parent process number of the current process.

The session number of the current process is the same as the session number of the terminal.

"Linux Advanced Programming" (chapter eighth) process management and program development

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.