Terminal, job control and daemon process

Source: Internet
Author: User
Tags terminates


Process groups, jobs, sessions

1 Process Groups

Each process, in addition to a process ID, belongs to a process group, which is a collection of one or more processes. They are associated with the same job and can receive various signals from the same terminal. as long as a process exists in a process group, the process group exists, regardless of whether its leader process terminates.

2 Jobs

The shell does not control the process but the job (job) or the process group; The shell can run a foreground job and any number of background jobs, which is called job control.

the difference between a job and a process group: If a program in the job creates a child process again, the child process does not belong to the job.

3 sessions

A session is a collection of one or more process groups, and a session can have a control terminal.

The process associated with the control terminal is called the control process

A session should include a control process (session first process Bash), a foreground process group, and any number of background process groups.

Terminal

In UNIX systems, users log into the system via a terminal and get a shell process, which becomes the control terminal of the shell process.

each terminal is opened and a session is created, and the first process is bash

Job Control


#include <stdio.h>

int main ()
{
while (1) {
Sleep (1);
printf ("Hello bit\n");
}
}

650) this.width=650; "title=" QQ picture 20160719132139.png "style=" Float:none; "alt=" Wkiom1enuvdqjcq_aaa7p5m2g_8638.png " src= "Http://s4.51cto.com/wyfs02/M01/84/55/wKiom1eNuVDQJCQ_AAA7p5M2g_8638.png"/>

First run the output hello bit in the foreground, ctrl-c stop the foreground process, add & make the foreground process switch to the background, jobs view background job, is a label of 1 running state, then kill%1 kills the background process

650) this.width=650; "title=" QQ picture 20160719132356.png "style=" Float:none; "alt=" Wkiom1enuvgdpszmaaa5jsskmok130.png " src= "Http://s2.51cto.com/wyfs02/M01/84/55/wKiom1eNuVGDPszMAAA5JSskmOk130.png"/>

The foreground run the program, CTRL-C stop the foreground process, add & make the foreground process switch to the background to run, jobs view background job, is a label of 1 running state, then FG 1 switch to the foreground run, ctrl-c stop foreground process

Daemon process

1. Concept

daemons are background processes, background processes are not necessarily daemons

Daemons run in the background, basically end with D; There is no user-level daemon, there is a kernel-level daemon

Daemons are self-formed, independent of terminal

2. Creating daemons

#include <unistd.h>

pid_t setsid (void)

(1) Call Umask to set the file mode creation mask to 0

(2) Call Fork Parent process exit, child process (non-leader process) call Setsid;

The current process becomes the leader process after calling Setsid, the current process becomes the first session, and the control terminal is detached from the relationship

(3) Change the current working directory to the root directory

(4) Closing file descriptors that are no longer needed

(5) Other: Ignore SIGCHLD signal

Job: Write the daemon to answer why some people fork two times when creating daemons? What is the purpose of fork two times?

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

void Creat_daemon (void)
{
int i;
int fd0;
pid_t pid;
struct Sigaction sa;
Umask (0); File Mode Create mask Word set to 0

if ((Pid=fork ()) <0) {
}else if (pid!=0) {
Exit (0); Terminate parent Process
}
Setsid (); Set up a new session

Sa.sa_handler=sig_ign;
Sigemptyset (&sa.sa_mask);
sa.sa_flags=0;

if (Sigaction (sigchld,&sa,null) <0) {//register child process exits ignoring signal
Return
}

if ((Pid=fork () <0)) {//fork again, terminates the parent process, guaranteeing that the child process is not the first session, thus guaranteeing that the successor is no longer associated with the terminal
printf ("fork error!");
Return
}else if (pid!=0) {
Exit (0);
}

if (ChDir ("/") <0) {//Change working directory to root directory
printf ("Child dir error");
Return
}
Close (0);
Fd0=open ("/dev/null", O_RDWR); Turn off standard input, redirect all standards (input and output errors) to/dev/null

Dup2 (fd0,1);
Dup2 (fd0,2);
}

int main ()
{
Creat_daemon ();
while (1)
{
Sleep (1);
}
}


(1) The role of the 1th Fork is to let the shell think this command has been terminated, do not hang on the terminal input . Another function is to let the parent process exit because the caller of SETSID cannot be the leader process. The parent process is the leader process at this time.

(2) The second main purpose of the fork is to prevent the process from opening a control terminal again, since the terminal can only be opened by the first session. Fork again, child process id! = SID (Sid is the SID of the process parent process). Therefore, it is not possible to open a new control terminal.

Terminal, job control and daemon process

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.