Linux 1th Day

Source: Internet
Author: User

Conceptual aspects

The file is an abstract representation of the I/O device, the virtual memory is an abstract representation of main memory and disk I/O devices, and the process is an abstract representation of the processor, main memory, and I/O devices

Interrupt

In the early days there was no concept of process, and the concept of process arose after the interruption of technology.

Fractional operating system

Based on time slice rotation

A process is an abstraction of an operating system to a resource, a process: A code snippet, a data segment, a stack segment, A + Process control block (PCB)

The PCB is an important data structure for the operating system-aware process (the CPU controls the process through the Process Control block)

Fork ()

The child process obtained by using the Fork function inherits the entire process's address space from the parent process, including: process context, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory, resource limit, control terminal, etc., process PCB control block.

The difference between a child process and a parent process is:

1, the parent process sets the lock, the child process does not inherit

2, the respective process ID and the parent process ID are different

3. The pending alarm of the sub-process is cleared;

4. The pending signal set of the sub-process is set to empty.

After understanding the 1:fork system call, the parent-child process will be executed alternately.

Understanding 2: How to understand a call 2 times back? The parent-child process is returned in its own process space.

Understanding 3: How to understand, fork return value greater than 0 is the parent process, why this design:? It is better to get the parent process Simple getppid () through the child process than to get the child process through the parent process, rather than to set the process PID to 0

Understanding 4: How do you understand branching after fork, rather than the beginning of the parent process main function? the child process inherits the parent process's. Process context, process stack, memory information, open file descriptor, signal control settings, process priority, process group number, current working directory, root directory, resource limit, control terminal, etc., process PCB control block.

Copying copy on Write

If multiple processes are reading copies of their own portion of the resource, then replication is unnecessary when inheriting.

Each process simply saves a pointer to the resource that does not need to be written to the data.

If a process modifies its own copy of the resource, the page that corresponds to that resource is copied. That's what it means when it's written.

Cause analysis: To speed up, the Linux kernel is a section-page management mechanism (since section management starts with 0), or it can be called a page-based management mechanism. Copies only the corresponding pages. Missing pages, in the interrupt query, and then assign values.

The child process is also inherited from the file that is opened to the parent process. Then the file in the file table is opened with a refcnt reference plus 1

Fork and Vfork

1) Before the fork has implemented copy on write. UNIX designers are concerned about the waste of address space caused by exec immediately after fork, so the vfork system call is introduced.

2) Vfork There is a limit, the child process must immediately execute the _exit or EXEC function.

Even if the fork implements copy on write, the efficiency is not vfork high, but we do not recommend the use of vfork, because almost every vfork implementation, there are more or less a certain problem.

Conclusion:

1:fork child process copies the data segment of the parent process

Vfork the child process to share the data segment with the parent process;

2:fork the order of execution of parent and child processes is indeterminate

Vfork: The child process runs first and then runs after the parent process;

exec function Family

Execve replacing a process image (loader) Note EXECVE is a system call;

Substitution means: The code snippet, data segment, stack segment, Process Control block PCB are all replaced.

int main (void) {int fd;pid_t pid;signal (SIGCHLD, sig_ign);//Do not receive sigchild signal, the parent process closes the CBD process directly to 1th Process Control printf ("Befor fork pid:%d \ n ", Getpid ()); g_num = 10;pid = Vfork ();//unstable, error may occur when exiting. if (PID = =-1) {printf (" pid < 0 err.\n "); return-1;} if (PID > 0) {printf ("parent:pid:%d \ n", Getpid ());} else if (PID = = 0) {printf ("Child:%d, parent:%d \ n", Getpid (), Getppid ()), char *const argv[] = {"ls", "-lt", Null};execve ("/bin/ls", argv, NULL);//Note: There is a/bin/ls program to end the program, there will be no coredump phenomenon printf ("Test this sentence also executed \ n");//exit (0);//1 vfork only need to use exit ( 0) _exit (0)//2 Test return 0; Difference}printf ("fork after....\n"); return 0;}

  

5 ways to terminate a process

Normal exit

Returns from the main function

Calling exit clears the buffer and then calls the Exit function in the kernel

Call _exit to call the Exit function in the kernel directly

Exception exit

Call abort to generate sigabout signal forced shutdown

By signal termination CTRL + C SIGINT forced shutdown

Atexit can register termination handler, ANSI C rules can register up to 32 termination handlers, it is actually a callback function, passed the function pointer

EXEC-associated function family

Q Contains header files <unistd.h>

The Q function uses the EXEC function to replace the current process with a new one. exec name is a complete series of multiple correlation functions, header file <unistd.h>

Q Prototypes

/*

#include <unistd.h>

extern char **environ;

int execl (const char *path, const char *arg, ...);

int EXECLP (const char *file, const char *arg, ...); PATH

int execle (const char *path, const char *arg,

..., char * const envp[]);

int execv (const char *path, char *const argv[]);

int EXECVP (const char *file, char *const argv[]);

EXECLP, EXECVP, and execle

 path parameter indicates the name of the program you want to start, including the path name

  ARG parameter indicates the parameters of the initiator

  Return value: Successfully returned 0, failed to return 1

  Execl,execlp,execle (both with "L" The number of arguments is variable, and the argument ends with a null pointer. The second parameter of

  EXECV and EXECVP is an array of strings that, when started, passes the parameters given in the argv array to main

  These functions are usually implemented with EXECVE. This is a conventional practice, and it is not a must.

  Name The last letter of the "P" function searches the PATH environment variable to find the executable file for the new program. If the executable is not on the path defined by path, you must pass the absolute file name, including the subdirectory, as a parameter to these functions

Summary: l represents a mutable parameter list, and p represents the search for a file in the PATH environment variable. ENVP represents an environment variable.

int main (void) {//The demo program is completely replaced//replaced, PID does not change//note that after printf the \ n cannot be forgotten, otherwise the main function cannot be printed,        //Because   \ n has the function of clearing buffer printf ("  Main getpid:%d\n ", Getpid ());//EXECLP (" ls "," ls ","-lt ",  NULL); int ret = EXECLP ("./testpid2 ", NULL, NULL); if (ret = = -1) {perror ("ERR:");} printf ("fork after....\n"); return 0;}

  

For an environment variable int main (int argc, char *argv[]) {printf ("main  egin...\n"),//1) if the ENVP environment parameter list is not filled out, the TESTPIDANDENV program uses the default list of environment parameters 2) If the ENVP environment parameter list is filled out, then the TESTPIDANDENV program will use the list of environment parameters you filled out char *ars[] = {"aa=111", "bb==222", null};int ret = execle ("./ Testpidandenv "," testpidandenv ", null,null);//int ret = Execle ("./testpidandenv "," testpidandenv ", null,ars); if (ret = = -1) {perror ("main:");} printf ("Main end...\n"); return 0;}

  

Learn more about daemons, and enhance shell knowledge by the way

When we log on to the Linux server with Telnet and verify the account password, a session period is established, and the server runs a shell for the account on the login

At this point, all the program commands that we run on this terminal are based on this session period, and once the terminal is closed, all programs running during this session are closed, and the Linux kernel provides a function setsid

Out of the terminal, out of the session, just like the Windows service program, resides in Linux, so the name Incredibles, Daemon.

Note: The call to Setsid cannot be a process leader (typically a parent process), and it is common practice to fork a child process before calling Setsid,

After the call, you need to change the current path to the root path, because when the program starts, it is bound to the boot path, and if it does not change, it will fail in the future to make some changes to the boot path.

In addition, the standard input, output, error file descriptor are re-fixed to "dev/null", to avoid waste of resources

Linux 1th Day

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.