Process Control for Linux/UNIX (2)

Source: Internet
Author: User
Tags bit set

Process Control (2) competitive conditions

When multiple processes attempt to process shared data in a certain way, and the final result depends on the order in which the process runs, we think this is a competitive condition.

If a parent process wants to wait for a child process to terminate, it must call a wait function. If a process waits for its parent process to terminate, it can be cyclically in the following form:

While (getppid ()! = 1)

Sleep (1 );

This kind of Round Robin is a waste of CPU time.

To avoid competition conditions and polling, you can use the signal mechanism or various forms of inter-process communication (IPC ).

Exec Function

When a process calls an exec function, the Program executed by the process is completely replaced with a new program, and the new program is executed from its main function. Because exec is not called to create a process, the process ID does not change. Exec only replaces the body, Data, heap, and stack of the current process with a new program.

There are six exec functions available, which are often called exec functions.

# Include <unistd. h>

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

Int execlp (const char * file, const char * arg ,...);

Int execle (const char * path, const char * arg,..., char * const envp []);

Int execv (const char * path, char * constargv []);

Int execvp (const char * file, char * constargv []);

Int execve (const char * filename, char * const argv [], char * const envp []);

Among the six functions, only execve is the kernel system call, and the other five are database functions. They all need to call the system call.

Before and after exec is executed, the actual user ID and the actual user ID remain unchanged. Whether the valid ID is changed depends on whether the user ID and the group ID are set in the program file to be executed. If the user ID bit set for the new program has been set, the owner ID of the program file with the valid user ID remains unchanged.

Change User ID and group ID

# Include <sys/types. h>

# Include <unistd. h>

Int setuid (uid_t uid );

Int setgid (gid_t gid );

The setuid function can be used to set the actual user ID and valid user ID.

The setgid function can be used to set the actual group ID and valid group ID.

# Include <sys/types. h>

# Include <unistd. h>

Int setreuid (uid_t ruid, uid_t euid );

Int setregid (gid_t rgid, gid_t egid );

The functions of the above two functions are to exchange the actual user ID and valid user ID, and to exchange the actual group ID and valid group ID.

# Include <sys/types. h>

# Include <unistd. h>

Int seteuid (uid_t euid );

Int setegid (gid_t egid );

The preceding two function values change the valid user ID and valid group ID.

Process accounting

Most UNIX systems provide an option for process accounting. After this option is enabled, the kernel writes an accounting record whenever the process ends. Telecommunications accounting records contain a small amount of binary data, generally including command names. Total CPU time used, user ID, and group ID.

The following function is used to start and disable process accounting.

# Include <unistd. h>

Int acct (constchar * filename );

Process Time

When measuring the execution time of a process, the UNIX system uses three process time values:

Clock Time, user CPU time, and system CPU time.

Clock time is also called the wall time: it is the total number of processes running, and its value is related to the number of processes running at the same time in the system.

User CPU time: the time used to execute USER commands.

The system CPU time is the time that the kernel program lock has been executed for this process.

The sum of user CPU time and system CPU time is often referred to as CPU time.

# Include <sys/times. h>

Clock_t times (struct tms * buf );

Any process can call the times function to obtain the time values of the three processes of itself and the child process. This function is defined by buf pointing to the tms structure. The structure is defined as follows:

Struct tms {

Clock_t tms_utime;/* user time */

Clock_t tms_stime;/* system time */

Clock_t tms_cutime;/* user timeof children */

Clock_t tms_cstime;/* systemtime of children */

};

This structure does not contain any test value for wall clock time. As an alternative, the times function returns the wall clock time as its function value. Call twice, and subtract the previous return value from the last return value. The difference is the wall clock time.

System Functions

# Include <stdlib. h>

Int system (const char * command );

The system function uses the exec function to call command through the child process created by fork, while the wait function of the parent process waits for this process to execute the command system command.

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.