linux 学习笔记之 Process Environment

本文介绍了进程终止的八种方式,包括正常终止和异常终止,并详细解析了exit、_Exit和_exit函数的区别。此外,还阐述了C程序的内存布局,包括测试段、初始化数据段等部分,并解释了malloc、calloc及realloc等内存分配函数的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 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.

 

 

 

 

 

 

 

 

 

 

 

 

 

<think>好的,我现在需要帮用户查找关于操作系统TEP相关的笔记、资料或教程。首先,我需要确认用户提到的“TEP”具体指什么。操作系统领域常见的缩写可能有TEP,但不确定具体含义。可能的情况包括: 1. **Thread Execution and Process (TEP)**:可能与线程执行和进程管理相关的内容。根据引用[3]提到的线程上下文切换和TCB(线程控制块),这可能涉及线程调度和进程管理的实现细节。 2. **Thread Execution Package (TEP)**:某些操作系统或教材中可能用TEP来指代线程执行相关的模块或工具包,比如线程池的实现。引用[1]提到了线程池的生产者-消费者模型,这可能与线程执行机制有关。 3. **Typographical Error(拼写错误)**:用户可能想输入其他缩写,如“TEB”(Thread Environment Block)或“PCB”(Process Control Block),但需要进一步确认。不过根据用户明确写出的TEP,暂时排除拼写错误。 接下来,结合用户提供的引用资料,分析可能的关联点: - 引用[1]讨论了线程池的生产者-消费者模型,涉及任务队列和线程协作。 - 引用[3]详细说明了线程上下文切换和TCB的结构,属于操作系统线程管理的核心内容。 - 引用[4]涉及临界区问题和锁机制,可能属于并发控制的TEP相关知识点。 因此,推测用户可能关注操作系统中的线程执行、进程管理及并发控制相关内容。接下来,我需要整理与这些主题相关的资料,并参考用户提供的引用内容进行关联。 可能的资源方向包括: 1. **线程与进程管理**:TCB、PCB的结构,线程切换机制,调度算法。 2. **并发与同步**:锁的实现(如自旋锁、互斥锁),临界区问题,生产者-消费者模型。 3. **操作系统课程资料**:如MIT 6.S081、CMU 15-213等课程中关于线程和进程的章节。 4. **开源操作系统实例**:如xv6、Linux内核中线程管理的源码分析。 需要确保提供的资料准确且符合用户需求,同时结合引用中的知识点,如线程池、上下文切换、锁机制等,进行扩展说明。此外,应推荐权威教程或笔记,如大学公开课资料、GitHub上的开源项目、经典教材(如《操作系统导论》、《现代操作系统》)。</think>关于操作系统中的线程执行与进程管理(TEP相关)资料整理如下: ### 一、核心概念解析 1. **线程控制块(TCB)** - 每个线程拥有独立的TCB,保存寄存器状态、线程ID和优先级等信息[^3] $$ \text{TCB} = \{\text{thread\_id}, \text{stack\_ptr}, \text{registers}, \text{priority}, ...\} $$ 2. **线程切换机制** - 上下文切换包含保存当前TCB、加载目标TCB的过程 - 典型切换耗时在微秒级(不同系统有差异) 3. **生产者-消费者模型** - 线程池中任务队列的线程协作模式[^1] ```c // 伪代码示例 while(1) { task = queue.pop(); execute(task); } ``` ### 二、关键问题与解决方案 1. **临界区竞争问题** - 不完善的锁机制会导致多个线程同时进入临界区[^4] - 正确实现需原子操作保证: ```armasm ; x86汇编实现自旋锁 lock: mov eax, 1 spin: xchg eax, [lock_var] test eax, eax jnz spin ``` 2. **线程调度算法** - 时间片轮转(Round-Robin) - 完全公平调度器(CFS)权重计算: $$ \text{vruntime} = \frac{\text{实际运行时间} \times \text{权重}}{1024} $$ ### 三、推荐学习资料 1. **经典教材章节** - 《Operating Systems: Three Easy Pieces》第25-28章(免费电子书) - 《现代操作系统》第2章进程与线程 2. **实践项目推荐** - MIT xv6实验:实现线程调度器(含TCB扩展) - Linux内核线程池源码分析(kernel/workqueue.c) 3. **公开课程** - [Berkeley CS162](https://cs162.org) 的Locking相关课程视频 - [CMU 15-213](https://www.cs.cmu.edu/~213/) 的并发编程实验
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值