linux 学习笔记之 Process Environment
2007-10-03 17:45
169 查看
Process Termination
There are eight ways for a process to terminate. Normal termination occurs in file ways:
Return from main
Calling exit
Calling _exit or _Exit
Return of the last thread from its start routine
Calling pthread_exit from the last thread
Abnormal termination occurs in three ways:
Calling abort
Receipt of a signal
Response of the last thread to a cancellation request
2. Exit Functions
#include<stdlib.h>
void exit(int status);
void _Exit(int status);
#include<unistd.h>
void _exit(int status);
_exit and _Exit, which return to the kernel immediately, and exit, which performs certain cleanup processing (use fclose function for all open streams) and then return to kernel.
If any of these functions is called without an exit status, main does a return without a return value, or the main function is not declared to return a integer, the exit status of the process is undefined.
3. Memory layout of a C program
Test segment, the machine instructions that the CPU executes. Usually, the test segment is read-only and sharable.
Initialized data segment, containing variables that are specifically initialized in the program(ex: int data=100).
Uninitialized data segment, data in this segment is initialized by the kernel to arithmetic 0 or NULL pointers before the program starts executing.
Stack: where automatic variables are stored, along with information that is saved each time a function is called.
Heap: where dynamic memory allocation usually takes place. Historically, the heap has been located between the uninitialized data and the stack.
4. Memory Allocation
void * malloc(size_t size);
void * calloc(size _t nobj, size_t size);
void * realloc(void *ptr, size_t newsize);
malloc, which allocates a specified number of bytes of memory. The initial value of the memory is indeterminate.
calloc, which allocates space for a specified number of objects of a specified size. The space is initialized to all 0 bits.
realloc, which increase or decrease the size of a previously allovated area. The initial value of the space between the old contents and the end of the new area is indeterminate.
One additional function is also worth mentioning. The function alloca has the same calling sequence as malloc; however, instead of allocating memory from the heap, the memory is allocated from the stack frame of the current function. The advantage is that we don't have to free the space; it goes away automatically when the function returns. The disadvantage is that some system don't support alloca , it it's impossible to increase the size of the stack frame after the function has been called.
5. setjmp and longjmp functions
In C, we can not goto a lablel that is in another function. Instead, we must use the setjmp and longjmp functions to perform this type of branching. These two functions are useful for handling error conditions that occur in a deeply rested function call.
#include<setjmp.h>
int setjmp(jmp_buf env);
int longjmp(jmp_buf env, int val);
Argument val decide return value on setjmp, we can use this to check which function call longjmp.
The setjmp(3) manual page on one system states that variables stored in memory will have values as of the time of the longjmp, whereas variables in the CPU and floating-point registers are restored to their values when setjmp was called. If you have an automatic variable that you don't want to rolled back, define it with the volatile attribute.
There are eight ways for a process to terminate. Normal termination occurs in file ways:
Return from main
Calling exit
Calling _exit or _Exit
Return of the last thread from its start routine
Calling pthread_exit from the last thread
Abnormal termination occurs in three ways:
Calling abort
Receipt of a signal
Response of the last thread to a cancellation request
2. Exit Functions
#include<stdlib.h>
void exit(int status);
void _Exit(int status);
#include<unistd.h>
void _exit(int status);
_exit and _Exit, which return to the kernel immediately, and exit, which performs certain cleanup processing (use fclose function for all open streams) and then return to kernel.
If any of these functions is called without an exit status, main does a return without a return value, or the main function is not declared to return a integer, the exit status of the process is undefined.
3. Memory layout of a C program
Test segment, the machine instructions that the CPU executes. Usually, the test segment is read-only and sharable.
Initialized data segment, containing variables that are specifically initialized in the program(ex: int data=100).
Uninitialized data segment, data in this segment is initialized by the kernel to arithmetic 0 or NULL pointers before the program starts executing.
Stack: where automatic variables are stored, along with information that is saved each time a function is called.
Heap: where dynamic memory allocation usually takes place. Historically, the heap has been located between the uninitialized data and the stack.
4. Memory Allocation
void * malloc(size_t size);
void * calloc(size _t nobj, size_t size);
void * realloc(void *ptr, size_t newsize);
malloc, which allocates a specified number of bytes of memory. The initial value of the memory is indeterminate.
calloc, which allocates space for a specified number of objects of a specified size. The space is initialized to all 0 bits.
realloc, which increase or decrease the size of a previously allovated area. The initial value of the space between the old contents and the end of the new area is indeterminate.
One additional function is also worth mentioning. The function alloca has the same calling sequence as malloc; however, instead of allocating memory from the heap, the memory is allocated from the stack frame of the current function. The advantage is that we don't have to free the space; it goes away automatically when the function returns. The disadvantage is that some system don't support alloca , it it's impossible to increase the size of the stack frame after the function has been called.
5. setjmp and longjmp functions
In C, we can not goto a lablel that is in another function. Instead, we must use the setjmp and longjmp functions to perform this type of branching. These two functions are useful for handling error conditions that occur in a deeply rested function call.
#include<setjmp.h>
int setjmp(jmp_buf env);
int longjmp(jmp_buf env, int val);
Argument val decide return value on setjmp, we can use this to check which function call longjmp.
The setjmp(3) manual page on one system states that variables stored in memory will have values as of the time of the longjmp, whereas variables in the CPU and floating-point registers are restored to their values when setjmp was called. If you have an automatic variable that you don't want to rolled back, define it with the volatile attribute.
相关文章推荐
- Linux学习——Linux下网络编程笔记(一)
- 【Linux】Linux命令学习(笔记一)
- Linux netfilter 学习笔记 之十一 ip层netfilter的NAT模块初始化以及NAT原理
- 【马哥linux学员学习笔记】MySQL多实例详解
- Linux基础学习笔记之文件的三个时间戳
- Linux进程通信---学习笔记(二)
- linux学习笔记-CentOS目录结构
- Linux学习笔记(二)
- Gradle for Android 学习笔记(一) 下载并安装gradle(Linux 环境),小白也能看懂的gradle教程
- linux内核学习笔记:中断与异常
- Centos学习笔记--linux用户管理
- Linux 学习笔记(二) -----文件处理命令
- Linux 学习笔记(六) -----其他
- Linux学习笔记之(1)~Linux有趣的历史概览
- rh9 linux服务配置学习笔记--------dns
- Linux基础学习笔记之切换终端的方法
- Linux 学习笔记 之 1 Linux系统的概述
- Linux学习笔记——常用文件操作命令
- 【linux c】c语言变量_学习笔记_007
- 写得蛮好的linux学习笔记