UNIX Process Control 2

Source: Internet
Author: User
1. Multiple racecondition processes attempt to process shared data in a certain way, and the final result depends on the order in which the process runs, which creates a competitive condition. If a logic after fork is explicit or implicitly dependent on the execution sequence of the Parent and Child processes after fork (this sequence is unpredictable and determined by kernel scheduling), it is also the result of racecondition.

1. race condition

Multiple processes attempt to process shared data in a certain way, and the final result depends on the order in which the process runs, which leads to a race condition. If a logic after fork is explicit or implicitly dependent on the execution sequence of the Parent and Child processes after fork (this sequence is unpredictable and determined by kernel scheduling ), it is also a frequent occurrence of race condition. If a process wants to wait for a sub-process to terminate, it must call the wait function. If a process is waiting for its parent process to terminate, the following loop can be used: while (getpp Id()! = 1) Sleep(1) The problem with this kind of loop (called polling) is that it wastes c p u time because the caller is awakened every 1 second, then perform a conditional test. Generally, the signal and IPC mechanisms are used. Implement the TELL_PARENT () and WAIT_PARENT () functions/Macros in the child process, and implement the WAIT_CLILD and TELL_CHILD Macros in the parent process. 2. ExWhen an ec process calls an exec function, the process is completely replaced by a new program, while the new program starts to run from its main function. Because calling exec does not create a new process, the process ID does not change. Exec, fork, wait, and exit are basic process control primitives. functions such as popen and system are constructed based on these basic calls. # I NcLude Int execl (const char * pathname, const char * arg0 ,... /* (char *) 0 */); int execv (const char * pathname, char * const argv []); int execle (const char * pathname, const char * arg0 ,... /* (char *) 0, char * const envp [] */); int execve (const char * pathname, char * const argv [], char * const envp []); int execlp (const char * FileName, const char * arg0 ,... /* (char *) 0 */); int execvp (const char * filename, char * const argv []);/* six functions return: if an error occurs, it is-1. If the error is successful, no */is returned. If the last two values contain/, it is considered as a path name, otherwise, the file name is used to find the corresponding executable file in the PATH environment variable. This executable file can also be an interpreter file (#!). In these functions, l Represents list and v represents vector. Execl, execle, and execlp of list indicate that the parameters are in the form of a Variable Parameter list. Finally, a null pointer should be followed to indicate that the two functions (execle and execve) ending with e (char *) 0) you can pass a pointer to the Environment string pointer array. The other four functions use the environ variable in the calling process to copy the existing environment for the new program. (If the system supports putenv and SetEnv can also be modified in the child process generated later, but cannot affect the environment of the parent process. Use/to display all the environment variables of the current process: for (char ** p Tr= Environ; * ptr! = 0; ptr ++) printf ("% s \ n", * ptr); after exec is executed, the process ID remains unchanged. In addition, the process executing the new program maintains the following features of the original process: • process ID and parent process ID. • Actual user ID and actual group ID. • Add group ID. • Process Group ID. • Dialog period ID. • Control terminal. • Remaining Time for the alarm. • Current working directory. • Root directory. • Create a blocked word in file mode. • File lock. • Process signal shielding. • Pending signals. • Resource restrictions. • Tms_u Time, Tms_stime, tms _ CutIme and tm SuStime value. Processing of opened files is related to the exec close flag value (FDCLOEXEC) of each descriptor. If this flag is set, the descriptor is disabled when exec is executed; otherwise, the descriptor is still enabled. Unless this flag is specially set with f c n t l, the default operation of the system is to keep this descriptor open after exec. 3. setuid (set real UID) getuid, setreuid, seteuid, setfsuid # include Int setuid (uid_t uid) // if the execution is successful, 0 is returned. If the execution fails,-1 is returned. The error code is stored in errno. Setuid () is used to reset the UID for executing the current process. When the valid UID must be 0 (root. In Linux, when root uses setuid () to convert to UID in the parameter (both EUID and UID are changed to uid_t uid in the parameter), that is, this process will no longer have the setuid () permission in the future. If you only temporarily discard the root permission and want to retrieve the permission again later, you must use seteuid (). If a non-root user uses the setuid function, it can only be used to reduce the system security risks caused by this program when writing a program with the setuid root, after using the root permission, we recommend that you immediately execute setuid (getuid (); to discard the root permission. In addition, if the process uid and euid are inconsistent, the Linux system will not generate core DuMp 4. system function: execute a string Command. # Include Int system (const char * character string); fork, exec, and waitpid are called in implementation. 5. process accounting when this option is selected, the kernel writes an accounting record whenever the process ends. A typical accounting record is a 3-2-byte binary data, including the command name, the total CPU time used, the user ID and group ID, and the start time. Each data (each c p u time, number of characters transmitted, etc.) required for accounting records is stored in the table by the kernel, and set the initial value when a new process is created (for example, after fork, In the child process, the kernel initializes a record for the child process, rather than when the new program exec. So the accounting records correspond to processes rather than programs .). Write an accounting record when the process ends. This means that the order recorded in the accounting documents corresponds to the order in which processes are terminated, rather than the order in which they are started. To determine the startup sequence, you need to read all accounting files and sort them by the START calendar time. # Include # Include # Define ACCFILE "/var/adm/pacct" ...... struct acct CdAta ;...... if (fp = fopen (ACCTFILE, "r") = NULL) err_sys ("can't open file", ACCTFILE); while (fread (& acdata, sizeof (acdata), 1, fp) = 1) {printf (...... acdata. ac_comm... ac. etime ).......} 6. the user login name, not the user sign. # Include Char * getlogin (void); if you get the login name, you can use getpwnam to find the corresponding records in the password file to determine the logon shell. 7. process time # include Clock_t times (struct tms * buf); obtain a start time and end time, and then StatIc long clktck = 0; clktck = sysconf (_ SC _CLK_TCK); start_times-> tms_utime/(double) clktck. Struct tms {clock_t tms_utime;/* CPU time */clock_t tms_stime;/* system CPU time */clock_t tms_cutime;/* user CPU time, te RmInat EdChildren */clock_t tms_cstime;/* system CPU time, terminated children */} textbook: APUE
Related Article

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.