Linux system Programming process

Source: Internet
Author: User

Process

Process numbers are not typically reused

Idle Process PID 0, the kernel runs the first process init PID 1.

Kernel Search init order, if not found, the kernel will issue panic hang

/sbin/init

/etc/init

/bin/etc

/bin/sh

Process number maximum 32768 old Unix with 16-bit PID, can be modified in the value of/proc/kernel/pid_max

#include <sys/types.h>

#include <unistd.h>

pid_t Getpid (); Returns the PID of the calling process

pid_t Getppid (); Returns the parent process PID of the calling process

1. Exec

#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 *const argv[]);

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

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

Path points to the image of the file to be called, such as a path, ARG is the first parameter, usually a file name, followed by a mutable parameter, and finally a null end.

After performing the substitution, all the data in the original process is lost, the signal processing function and the pending signal are lost, the original process is missing the thread settings, and the process statistics are lost. However, the process PID is constant.

Detailed Explanation: http://blog.csdn.net/lianghe_work/article/details/47704105

2. Fork ()

#include <sys/types.h>

#include <ubistd.h>

pid_t Fork ();

Generate a process that is almost identical to the parent process

Different points:

PID and Ppid

Child process resource statistics are zeroed

Pending signals and file locks do not inherit

3. Exit

#include <stdlib.h>

Void exit (int status);

Status flag exits state & 0377 will be returned to the parent process for confirmation

Exit_success 0 exit_failure non-zero

To close the process:

Reverse register in the system call atexit or OnExit

Clear all open standard I/O

Delete all temporary files created by Tmpfile ()

Call _exit to let the kernel handle the remaining work of terminating the process

Kernel cleanup: Requested memory, open file, System V signal

The process can call _exit directly, but it is unreasonable for some processes to process cleanup to be incomplete. Only vfork be sure to call _exit.

Other ways to end the process:

The main function returns or calls exit (0).

SIGTERM and Sigkill are triggered.

Program segment error or memory exhaustion kernel forced kill

4. atexit

#include <stdlib.h>

Int atexit (void (*function) (void));

A successful return of the function will be called when the function is registered to the normal end of the process.

If the process calls exec, then atexit will not be called, the process will not be called by the end of the signal, and the registered function should be non-parametric and return, the order of invocation and the order of registration is reversed.

At the end of the process, the kernel sends a signal sigchild to the parent process, which is generally ignored. If necessary, you can use signal or sigaction to handle

Waiting for the death of the process will keep some information waiting for the parent process to view, the parent process after the child process is seen dead, if not viewed will become a zombie process

5.wait

#include <sys/types.h>

#include <sys/wait.h>

pid_t Wait (int *status);

Returns the status of the end of a child process that has been terminated status

Block if no child process ends

6.waitpid

#include <sys/types.h>

#include <sys/wait.h>

pid_t waitpid (pid_t pid,int *status,int options);

Pid value

-1 PID absolute value for all child processes of the process group

0 arbitrary processes for the same process group

>0 the wait for the specified child process

Options value:

Wnohang does not block

The actual user ID of the real user ID, cannot be modified, will be inherited in various places root can modify his

Valid user ID The user ID used by the process, permission validation using this ID, using setuid can modify

Save the user ID of the setting

File System User ID

#include <sys/types.h>

#include <unistd.h>

Int setuid (uid_t uid); Root user can set any value, non-root set to actual ID and save ID

Int setgid (gid_t gid);

Int Seteuid (uid_t euid); Ditto

Int Setegid (gid_t egid);

Int setreuid (uid_t ruid,uid_t euid);

Int Setregid (gid_t rgid,gid_t egid);

Daemon process:

    1. Fork () to create a new process,
    2. Call exit in the daemon parent process
    3. Call Setsid to allow the process to have a new process group and a new session
    4. Use ChDir to change the current working directory to the root directory
    5. Close all file descriptors
    6. Open the 0,1,2 file descriptor and redirect to/dev/null.

Thread:

Each thread has an exclusive virtual processor, a single set of registers, an instruction counter, and a processor state.

Threads in the same process share the same address space (dynamic memory, mapping files, target code, and so on), open files, and other kernel resources.

System calls that let out the processor

7.sched_yield ();

#include <sched.h>

Int Sched_yield ();

Interrupt the process, the kernel will run a new process, but if no other process is in place, the process will recover immediately, so this call generally does not make much sense.

Process priority is the smaller the value, the higher the priority, the faster the start, the longer the execution time

8.nice

#include <unistd.h>

int NIC (int inc);

A successful return to the new priority on the current priority of Add Inc. Negative values can be used only if you have Cap_sys_nice (root)

Because the priority can return a negative value, all unsuccessful returns require a errno test, and the errno is zeroed before use

10.getpriority, SetPriority

#include <sys/time.h>

#include <sys/resource.h>

int getpriority (int which,int who);

int setpriority (int which,int who,int prio);

Which value:

Prio_process Prio_pgrp Prio_user

The corresponding WHO

Process ID, process group ID, user ID

Only a process with cap_sys_nice (root) can reduce the value of nice and increase the priority

11.ioprio_get Ioprio_set

int ioprio_get (int which,int who);

int ioprio_set (int which,int who,int prio);

12. Getlimit Setlimit

#include <sys/time.h>

#include <sys/resource.h>

Struct rlimit{

rlimit_t rlim_cur; Limitations of soft limit kernel execution

rlimit_t rlimit_max;//Hard Limit

}

int getlimit (int resource, struct rlimit *rlim);

int setlimit (int resource, const struct RLIMIT *rlim);

Resource Rlimit_cup

Processes that do not have cap_sys_nice (root) can only be lowered by hard limits

Rlimit_fsize

Rlimit_core

Rlim_infinity

Name

Significance

Rlimit_as

Maximum number of memory sizes available for a process

Rlimit_core

The maximum size of the core file, if 0 indicates that the core file cannot be created

Rlimit_cpu

Maximum CPU time (in seconds)

Rlimit_data

Maximum value of the data segment size

Rlimit_fsize

Maximum size of the created file

Rlimit_locks

Maximum number of file locks that a process can establish

Rlimit_memlock

Maximum size of locked memory in process using Mlock

Rlimit_nofile

Maximum number of open files in a process

Rlimit_nproc

Maximum number of child processes per real user ID

Rlimit_rss

Maximum resident Store Size

Rlimit_sbsize

Maximum size of the socket buffer

Rlimit_stack

Maximum size of the stack

Rlimit_vmem

=rlimit_as

Linux system Programming process

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.