Linux Process Groups, sessions, and daemon

Source: Internet
Author: User

Linux Process Groups, sessions, and daemon

I. Process Group ID

Each process belongs to a process group. Each process group has a leading process. A process group is a collection of one or more processes. Generally, it is associated with a group of jobs and can receive various signals from the same terminal. Each process group has a unique process group ID (an integer, which can also be stored in the pid_t type ). A process group is uniquely identified by the Process Group ID. In addition to the process number (PID), the process group ID is also one of the essential attributes of a process.

Getpgrp: Get the process group id, that is, the pid of the lead Process
# Include <unistd. h>
Pid_t getpgrp (void );
// Return value; ID of the process group that calls the process

# Include <unistd. h>
Pid_t getpgid (pid_t pid );
// If the process group id is returned successfully,-1 is returned if the group fails.

Each process group has a leader process. The process ID of the leader process is equal to the Process Group ID. A leader process can create a process group and a process in the group. As long as a process in a process group exists, the process group exists, regardless of whether the leader process is terminated. The time interval from the process group creation to the time when the last process leaves becomes the lifetime of the Process Group. The last process in the process group can be terminated or transferred to another process group.
Displays the group IDs of child and parent processes.

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>

Int main (){
Pid_t pid;

If (pid = fork () <0 ){
Printf ("fork error! ");
} Else if (pid = 0 ){
Printf ("The child process PID is % d. \ n", getpid ());
Printf ("The Group ID is % d. \ n", getpgrp ());
Printf ("The Group ID is % d. \ n", getpgid (0 ));
Printf ("The Group ID is % d. \ n", getpgid (getpid ()));
Exit (0 );
}

Sleep (3 );
Printf ("The parent process PID is % d. \ n", getpid ());
Printf ("The Group ID is % d. \ n", getpgrp ());

Return 0;
}

Program Execution result:

Ii. Session
A session is a collection of one or more Process Groups. For example:


# Include <unistd. h>
Pid_t setsid (void );
If the process that calls this function is not the leader of a process group, the function creates a new session and three things occur:
1. The process becomes the first process of the new session. This process is the only process in the new session.
2. The process becomes the leader process of a process group. The new process group ID is the ID of the called process.
3. The process has no control terminal. If the process has a control terminal before calling setsid, this connection will also be broken.
If the process is already the leader of a process group, this function returns an error. To ensure that this will not happen, fork is usually called first, and then the parent process is terminated, while the child process continues. Because the child process inherits the group ID of the parent process, and its ID is newly allocated, the two cannot be equal, so it ensures that the child process will not be a process leader.

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>

Int main (){
Pid_t pid;

If (pid = fork () <0 ){
Printf ("fork error! ");
Exit (1 );
} Else if (pid = 0 ){
Printf ("The child process PID is % d. \ n", getpid ());
Printf ("The Group ID of child is % d. \ n", getpgid (0 ));
Printf ("The Session ID of child is % d. \ n", getsid (0 ));
Sleep (10 );
Setsid (); // The sub-process is not the leader process, so it becomes the first process of the new session and becomes the leader process. The process group id is the session process.
Printf ("Changed: \ n ");
Printf ("The child process PID is % d. \ n", getpid ());
Printf ("The Group ID of child is % d. \ n", getpgid (0 ));
Printf ("The Session ID of child is % d. \ n", getsid (0 ));
Sleep (20 );
Exit (0 );
}

Program Execution result:

3. Daemon
In linux or unix systems, many services are enabled during system boot. These services are called daemon processes. To increase flexibility, the root user can select the system enabling mode, which is called the running level. Each running level configures the system in a certain way. A daemon is a process that runs in the background and is out of the terminal. The daemon is separated from the terminal to prevent the information in the execution process from being displayed on any terminal and the process is not interrupted by any terminal information generated by any terminal.

Daemon programming steps
1. Create a child process and the parent process exits.
• All work is performed in sub-Processes
• The form is separated from the control terminal
2. Create a session in the sub-process
• Setsid () function
• Make sub-processes completely independent from control
3. Change the current directory to the root directory.
• Chdir () function
• Prevents the use of detachable file systems
• You can also switch to another path.
4. Reset the File Permission mask.
• Umask () function
• Prevents the creation of blocked characters in inherited files from rejecting certain Permissions
• Increase the flexibility of the daemon process
5. Disable the file descriptor
• Inherited open files are not used, which wastes system resources and cannot be uninstalled.
• Getdtablesize ()
• Return the number of objects in the file descriptor table of the process, that is, the number of files opened by the Process

The instance first creates a daemon and then writes a sentence to/tmp/dameon. log every 10 seconds.

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <fcntl. h>
# Include <sys/types. h>
# Include <unistd. h>
# Include <sys/wait. h>

# Deprecision MAXFILE 65535
Int main ()
{
Pid_t pc;
Int I, fd, len;
Char * buf = "This is a Dameon \ n ";
Len = strlen (buf );
Pc = fork ();
If (pc <0 ){
Printf ("error fork \ n ");
Exit (1 );
} Else if (pc> 0)
Exit (0 );
Setsid ();
Chdir ("/");
Umask (0 );
For (I = 0; I <MAXFILE; I ++)
Close (I );
While (1 ){
If (fd = open ("/tmp/dameon. log", O_CREAT | O_WRONLY | O_APPEND, 0600) <0 ){
Perror ("open ");
Exit (1 );
}
Write (fd, buf, len + 1 );
Close (fd );
Sleep (10 );
}
}

We can see that the program writes content to the corresponding file every 10 s.

This article permanently updates the link address:

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.