Linux System Programming Learning Note (v) Process management

Source: Internet
Author: User
Tags terminates

1. A process is one of the two most important underlying abstractions in a UNIX system (the other is a file)

A process is a running Programa thread are the unit of activity inside of a process the virtualization of memory is Associa Ted with the process, the threads all share the same memory address space 2. Pidthe Idle process has the PID 0The first process, the kernel executes after booting the system, called the INIT proc ESS, has the PID 1 by default, the kernel specifies a maximum process id value: 32768 (16-bit signed) kernel usesa strict linear approachAssigning a PID to a process, if all currently assigned PID values are up to 17, the next assigned PID value is 18 process level: Each process (except the INIT process) is children from another process normally belong to the same Process groups as their parents 3. Running new process in Unix, the act of loading into memory and executing a program image is separate from the act of creating a new proc EssexecSystem call:loads a binary program into memory, replacing the previous contents of the address space, and begins execution The new programForkSystem Call:create A new Processto execute a new program in a new Process:first a fork to create a new process, and then An exec-to-load a new binary into that process EXEC series function, only Execve is a system call, the rest of the functions are the C-language library functions that are encapsulated according to EXECVE. After the system call, the child process is almost identical to the parent process except for the following: A. The process ID is different B. The pending signal is cleared and the process inherits C. The file lock of the parent process inherits this "fork Plus exec "combination is frequent and simple:
pid_t PID =fork ();if(PID = =-1) {fprintf (stderr,"Fork error\n");}/* the Child*/if(PID = =0) {    Const Char* args[] = {"grep", NULL}; intret = EXECV ("Bin/grep", args); if(ret = =-1) {fprintf (stderr,"execv error\n");    Exit (Exit_failure); }}

4. Copy-on-write early Unix, the implementation fork is very simple, the kernel created copies of all internal data structures, duplicated the process ' s PAG e table entries, and then performed apage-by-page copy of the parent's address space into the child's new address space, which Sample copy implementation is undoubtedly time-consuming copy-on-write is a delay strategy that reduces the cost of resource replication.If multiple users request read operations on the resources it owns, copies of the resource copy are not required, and each user only needs to manipulate a pointer to the common resource. As long as no user attempts to modify this resource, the copy overhead can be avoided. If a user does need to modify this resource, then until this time, the resources are copied one copy of the resources are handed over to the users who need to modify, the remaining users continue to share the original resourcesIn the specific example of virtual memory, Copy-on-write is in memory pages. The memory page of the process is first marked as read-only and Copy-on-write, and if a process needs to modify the memory page, a page error is generated, at which point the kernel copies the memory page and the copy-on-write tag of the memory page is cleared. Modern machine architectures support Copy-on-write 5 on a hardware level. Exit when a process terminates, the kernel clears all the resources it contains: allocated memory, open files, semaphores, etc. the most classic way to end a program is not to explicitly call exit, but instead to "falling off the end" of the programs when a process terminates,the kernel sends a SIGCHLD signal to its parent process。 By default, the signal is ignored, and the parent process can also call sigaction to capture the signal6. Wait
#include <sys/types.h><sys/wait.h>/** *pid_t Wait (  int *intint int. options);
When Pid==-1, Waitpid waits for any child process, equal to wait, that is, wait (&status) equals Waitpid (? 1, &status, 0) 7. System
#define _xopen_source    /* If we want wexitstatus, etc.*/<stdlib.h>int system (  Constchar *command);
It is common to use System () to run a simple utility or shell script, often with the explicit goal of simply obtaining its Return Valueif command is NULL, System () returns a nonzero value if the shell/bin/sh are available, and 0 otherwise here is a sample implementation for system ():
/*My_system-sync Spwans and waits for the command */bin/sh-c <cmd>. * Return-1 On error of any sort, or t He exit code from * the launched process. Does not a block or ignore any signals. */intMy_system (Const Char*cmd) {    intstatus; pid_t PID=Fork (); if(PID = =-1) {fprintf (stderr,"Fork error\n"); return-1; } Else if(PID = =0) {        Const Char* argv[4]; argv[0] ="SH"; argv[1] ="- C"; argv[2] ="cmd"; argv[3] =NULL; EXECV ("bin/sh", argv); Exit (-1); }    if(Waitpid (PID, &status,0) == -1) {        return-1; } Else if(wifexited (status)) {returnwexitstatus (status); }    return-1;}

8. Zombie Process A process that have terminated but have not yet been waited upon by its parent is called a "zombie." Zombie processes continue to consume system resources, although only a small percentage if a parent process fork produces a child process, the parent process must be responsible for the wait Child process (reclaims the resources of the zombie child process) If a parent process does not wait for its child process before terminating?The kernel inspects all of its child processes and hangs them under the INIT process (to ensure that any process has a parent process at all times), and the INIT process periodically wait for all its child processes, these sub-processes will eventually be recycled, rather than being in the form of a zombie process for 9. Users and Groups
#include <sys/types.h><unistd.h>int  setuid (uid_t uid); int setgid (gid_t gid); uid_t getuid (void); gid_t getgid (void);
Each process is a member of a process group,process groups this contain more than one process is generally implementing j OB Control A command on the shell such as this one:
/*  */$ catship-inventory.txt | grep Booty | sort
10. Daemon A daemon is a process this runs in the background, not connecting to any controlling terminal usually starts when the system starts, and takes root Limited to run, handling system-level tasks, the name suffix is generally d a daemon must meet: A. is a child of the Init process B. Connection to terminal is not allowed

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.