When the program is executed, how the main function is called, how the command line parameters are passed to the program, what the typical storage space layout looks like, how to allocate other storage space, how the process uses environment variables, the termination of the process, etc. These are the basics of process control.
Startup and termination of the process
When the kernel executes a c program, it uses the exec function to call a special startup routine, which is in the kernel Get command line parameters and environment variable values.
Process termination situations
5 normal termination situations:
(1)从main函数返回; (2)调用exit; (3)调用_exit和_Exit函数; (4)最后一个线程调用pthread_exit; (5)最后一个线程从其启动例程返回;
3 abnormal termination situations
(1)调用abort; (2)接到一个信号; (3)最后一个线程对取消请求做出响应;
Process startup and termination diagram
atexit function
A process can register up to 32 functions (for example: signal function), which are automatically called by the exit function. These functions are called when the program terminates to form a termination handler to perform finishing work before ending the process. The exit function uses the registration record of the atexit function to determine which functions to call.
exit function
This function is defined by ISO C and its operations include processing the termination handler and then closing all standard I/O streams. It should be noted that it does not handle file descriptors, multi-process (parent-child process) and job control.
_e(E)xit function
ISO C The purpose of defining this function is to provide a method for the process to terminate the program without running a termination handler or signal handling function. But whether ISO C flushes the standard I/O stream depends on the implementation of the operating system. In unix, flushing is not performed.
Status codes of exit and _e(E)ixt functions
No matter how the process ends, it will execute the same code on the kernel (as can be seen from the process startup and exit diagram). This code closes all file descriptors and releases all storage space.
After the program exits, use the exit code to inform the parent process of the process. The parent process uses the wait or waitpid function to complete the aftermath of the child process (obtain information about the child process and release the resources occupied by the child process). If the parent process does not handle the exit status of the child process, the child process becomes a zombie process. On the contrary, if the parent process terminates before the child process, the child process becomes orphan process. The orphan process will be received by process No. 1 (init process). The general process is as follows:
(1)进程终止时,内核逐个检查所有活动的进程; (2)分析查找该终止进程的子进程; (3)将该进程的子进程的父进程ID改为1;
wait and waitpid functions
When the program terminates normally or abnormally, the kernel will send a SIGNAL signal to the parent process. The termination of the child process is an asynchronous event, so this signal is also an asynchronous signal. This signal is generally ignored by the parent process by default. Or provide a signal processing function to deal with the aftermath. The wait and waitpid functions are part of the signal processing function.
The difference between wait and waitpid functions is as follows:
(1)wait会阻塞调用者进程等待直至第一个终止的子进程到来; (2)waitpid可以通过参数设置,来实现调用者进程不阻塞,或选择要阻 塞等待的子进程;
The caller here refers to the parent process
Environment table and environment variables
Environment table structure diagram
Each program receives an environment table
Environment The table is also an array of character pointers
enrivon is called the environment pointer
The array of pointers is called the environment table
The string pointed to by each pointer is called the environment string
Environment variable
The unix kernel does not check the environment string, and their interpretation is completely Depends on each application process
Usually set environment variables in a shell startup file to control the actions of the shell
When modifying or adding environment variables , can only affect the environment of the current process and any child processes generated and called subsequently (previously not allowed), but cannot affect the environment of its parent process
Functions related to environment variables As follows:
#include<stdlib.h> char *getenv(const char *name); 返回值:指向与name关联的value的指针;若未找到,返回NULL int putenv(char *str); 返回值:若成功,返回0;若出错,返回非0 int setenv(const char *name, const char *value, int rewrite); int unsetenv(const char *name); 两个函数返回值:若成功,返回0;若出错,返回-1</stdlib.h>
How these functions modify the environment table
The environment table and environment string are usually stored at the high address (top) of the memory space. Therefore, when modifying its value, the memory cannot continue to extend to high addresses; but because there are stack frames below it, it cannot extend downwards. The process of how to modify its value is as follows:
(1) Modify the environment table
1)新value = 旧value,调用malloc函数,在堆区开辟新的存储空间, 将新value复制到这里,再将这片存储区首地址写到环境表相应的位置处。
(2) Add a new environment table
1)新增一个环境变量,调用malloc函数开辟新的存储空间,将原来的环 境表复制到该存储区,其次再添加一个环境变量,然后在尾部赋值为NULL, 最后将environ指向该区域; 2)在 1)过程的基础上,调用realloc函数,多次添加环境变量;
Note:Environment variables modified in this way are only valid when the current program is running. When the program ends, the corresponding storage area is recycled by the system, and these modifications will become invalid.
Supplementary explanation of memory storage structure
Memory management structure diagram
##Uninitialized data segment ( block started by symbol): Before the program starts executing the line, the kernel initializes the data in this segment to 0 or a null pointer;
Stack: Every time a function is called, its return address and the caller's environment information (such as the value of some machine registers) are stored on the stack;
共享库:只需在所有进程都可引用的存储区中保存这种库例程的一个副本;
存储空间分配函数
#include<stdlib.h> void *malloc(size_t size); void *calloc(size_t nojy, size_t size); void *realloc(void *ptr, size_t newsize); 3个函数返回值:若成功,返回非空指针;若出错,返回NULL</stdlib.h>
malloc函数:初始值不确定;底层通过调用sbrk函数实现;
calloc函数:初始值为0;
realloc函数:增加或减少以前分配区的长度;当增加长度时,可能将以前分配区的内容移到另一个足够大的区域,以便在分配区末尾增加存储区,而新增存储区初始值不确定(例如:可变数组的使用);
注意:这些动态分配的函数一般在分配存储空间时,会比要求的大。因为在开辟空间的前后部分存储记录管理信息。因此,在使用时,千万不要越界访问,以免造成不可预知的后果。
函数间跳转策略
在c语言中,goto语句是不能跨函数跳转的。尤其是在函数深层调用时的跳转需求,在出错处理的情况下非常有用。
#include<setjmp.h> int setjmp(jmp_buf env); 返回值:若直接调用,返回0;若从longjmp返回,返回非0 void longjmp(jmp_buf env, int val);</setjmp.h>
变量值回滚问题:自动变量和寄存器变量会存在回滚现象。利用volatile属性来避免此类情况的发生。(在给变量赋值时,赋的值回首先存储在内存(存储器变量)中,然后在由cpu取走,存储在cpu的寄存器上(寄存器变量)。在做系统优化时,那些频繁使用的变量,会直接存储到寄存器中而不经过内存。)
寄存器变量会存在回滚现象的探究
在调用setjmp函数时,内核会把当前的栈顶指针保存在env变量中,所以在调用longjmp函数返回该位置时,全局变量、静态变量、易失变量和自动变量如果在调用setjmp和longjmp函数之间它们的值被修改过,是不会回滚到setjmp函数调用之前的值(当然,编译器将auto变量优化为寄存器变量除外)。因为,这些存储器变量的值是存储在内存相应的段中,回到原先栈顶状态时,同样访问的还是原先的内存空间。
然而,对于寄存器变量来说,首先要明确一点:寄存器变量是用动态存储的方式。意思是寄存器变量的值可能存在不同的寄存器中。如果在调setjmp和longjmp函数之间它们的值被修改过,这个值可能不会存到setjmp之前的对其赋值的寄存器中,而在调用longjmp函数后,又回到了调用setjmp函数时的状态。这个时候再读取寄存器变量的值时,读到的是原先那个寄存器中存储的值而不是修改过的那个寄存器中存储的值,所以出现的回滚现象。
The above is the detailed content of Process environment--process management. For more information, please follow other related articles on the PHP Chinese website!

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

Configuring a Debian mail server's firewall is an important step in ensuring server security. The following are several commonly used firewall configuration methods, including the use of iptables and firewalld. Use iptables to configure firewall to install iptables (if not already installed): sudoapt-getupdatesudoapt-getinstalliptablesView current iptables rules: sudoiptables-L configuration

The steps to install an SSL certificate on the Debian mail server are as follows: 1. Install the OpenSSL toolkit First, make sure that the OpenSSL toolkit is already installed on your system. If not installed, you can use the following command to install: sudoapt-getupdatesudoapt-getinstallopenssl2. Generate private key and certificate request Next, use OpenSSL to generate a 2048-bit RSA private key and a certificate request (CSR): openss


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software