Linux c Note Process Control (iv)

Source: Internet
Author: User

A change user i d and Group I D
You can set the actual user ID and valid user ID with the setuid function. Similarly, you can set the actual group ID and valid group ID with the Setgid function.
int Setgid (gid_t gid);
Two function returns: 0 if successful, or 1 if an error occurs
There are several rules about who can change the ID. Now consider the rules for changing user I D (all that is described here about the user ID applies to the group ID).
(1) If the process has superuser privileges, the SETUID function sets the actual user ID, the valid user ID, and the saved settings-user-ID to UID.
(2) If the process does not have superuser privileges, but u i d equals the actual user I d or the saved settings-user-ID, then setuid only sets the active user I d to UID. Does not change the actual user ID and saved settings-user-ID.
(3) If none of the above two conditions are met, the errno is set to Eperm and an error is returned.
Here it is assumed that _posix_saed_ids is true. If this feature is not provided, the above-mentioned settings for the Save-user-I-D section are not valid.
Also note the following points regarding the three user I D maintained by the kernel:
(1) Only the super user process can change the actual user I D. Typically, the actual user I D is set by the login (1) program when the user logs on, and will never change it. Because login is a super user process, when it calls Setuid, it sets all three users I D.
(2) The EXEC function sets the active user I d only when the set-user-I D-bit is set on the program file. If the set-user-I D-bit is not set, the EXEC function does not change the active user I d, but maintains it as the original value. You can call setuid at any time, set a valid user I d to the actual user I D or save the settings-user-i d. Naturally, you cannot set a valid user I d to any random value.
(3) Saved settings-user-I D is copied by exec from a valid user ID. After exec has set the active user i d by file user I d, the copy is made and the copy is saved.


Two Change the priority of a process
The priority of the process can be set to ensure the priority of the process, under Linux, through the system called Nice can change the priority of the process, nice system call to change the priority of the calling process
#include <unistd.h>
int nice (int increment);
Return Description:
When executed successfully, returns the new nice value. Failed return-1

Understand the use of two important functions first: GetPriority and SetPriority

GetPriority: This function returns the priority of a set of processes
#include <sys/time.h>
#include <sys/resource.h>
int getpriority (int which, int who);

Error returned-1, the cause of the error is stored in errno.
Esrch parameter which or who may be wrong and cannot find a compliant process
EINVAL parameter which value error.

The which parameter of the getpriority () is used to specify the priority object to get: process, process group, or user ID.
Which parameters:

The prio_process:who parameter specifies the process ID and returns the priority of the process.
The prio_pgrp:who parameter specifies the process group ID and returns the priority of the process group.
The prio_user:who parameter specifies the user ID, which is passed back to the user's priority.

SetPriority: This function is used to set the priority of the specified process

#include <sys/time.h>
#include <sys/resource.h>
int setpriority (int which,int who, int prio);

Error returned-1, the cause of the error is stored in errno.
Esrch parameter which or who may be wrong and cannot find a compliant process
EINVAL parameter which value error.
Eperm Not enough permissions to complete Setup
Eacces general users cannot lower priority

SetPriority () can be used to set process execution priorities for processes, process groups, and users. parameter which has three kinds of values, and who has different definitions according to which value
which the meaning of the WHO representative
Prio_process Who for process identification code
Prio_pgrp who is the group identification code for the process
Prio_user who is the user identification code


Nice's system call is equivalent to
int nice (int increment)
{
int oldpro = getpriority (prio_process, Getpid ());
Return SetPriority (Prio_process, Getpid (), Oldpro + increment);
}

Three: Concept
1. Input/Output redirection
In the Linux command-line mode, if the command requires input that is not from the keyboard, but from the specified file, this is the input redirection. Similarly, the output of the command can be written to the specified file instead of being displayed on the screen, which is the output redirect.

Input redirection and output redirection
# WC Aa.txt
# ls > Home.txt

The first command takes the file Aa.txt as input to the WC command, counting the number of rows, words, and characters aa.txt. The second command saves the output of the LS command in a file named Home.txt. If the file behind the > symbol already exists, the file will be rewritten.

2. Piping
Using the pipe symbol "|" provided by Linux Separates the two commands, and the output from the left command of the pipe character is entered as the command to the right of the pipe. Continuous use of the pipe means that the output of the first command will be entered as the second command, and the output of the second command will be entered as the third command, and so on.

Take advantage of a pipeline
# Rpm-qa|grep Licq

This command uses a pipe symbol "|" Set up a pipeline. The pipeline lists the output of the RPM-QA command (including all installed RPM packages in the system) as input to the grep command, listing the RPM packets with LICQ characters.

Linux c Note Process Control (iv)

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.