Linux process/thread in practice

本文详细阐述了Linux进程和线程的概念、Linux如何表示进程与线程、进程生命周期及进程间的关系,并且不涉及任务调度器。

This post tries to depict a inside-view about Linux process / thread. In addition to explain the relative theory / idea , you will see examples in real world. specifically, Linux 2.6.28 on ARM 11.


This post tries to figure out:

1. What is a Linux process/thread?

2. How Linux represents a process / thread? 

3. The life-cycle of Linux process / thread.

4. The relationships of processes / threads.

This post will NOT consider task scheduler.


1. What is Linux process / thread?

According to R. Love, a process is the executing program code as well as associated resources, such as open files, pending signals, a memory address space, and so forth.

Threads are objects of activity within the process.


From my point of view, the definition of process is quite simple and clear: process = running program (code section) + environment.

  Regarding threads, besides above definition, we need to realize:

a. Linux kernel schedules individual thread, not process.

b. To Linux, a thread is just a special kind of thread:

Processes provide 2 virtualizations: a virtualized processor / virtual memory. Threads share the virtual memory abstraction, whereas each receives its own virtualized processor.


2. How Linux represents a process / thread? 

Linux uses struct task_struct (defined in include/linux/sched.h) to describe each task / thread in system. The struct is quite huge (around 1.7KB on a 32-bits machine) because it describes everything regarding a task: the task's memory space, its state, what files can be accessed by the task, what signals will be handled by the task... tons of information. It is boring to list all fields in this post. Instead, only the most important fields (from my point of view) will be showed here.


struct task_struct {

/* task's state: -1 unrunnable, 0 runnable, >0 stopped */

volatile long state;

/* task's stack */

void *stack;

...

struct list_head tasks;

struct mm_struct *mm, *active_mm;

...

pid_t pid;

pid_t tpid;

...

struct task_struct *parent, *real_parent;

...

struct task_struct *group_leader;

...

struct fs_struct *fs;

struct files_struct *files;

What we need to pay attention to on task_struct includes:

a. The structure is allocated via the slab allocator.

b. Inside the kernel, tasks are typically referenced directly by a pointer to their task_struct structure. Consequently, it is useful to be able to quickly look up the descriptor of the currently executing task. To do this, a new structure, struct thread_info, was created and at the bottom of the process's kernel stack. 

struct thread_info{ //arch/arm/include/asm/thread_info.h

unsigned long flags;

int preempt_count;

mm_segment_t addr_limit;

struct task_struct *task;

struct exec_domain

...

}

Note that the member task is a pointer to the task's actual task_struct.

Each process's kernel stack is 8KB (2 page frames) in size, so we can get task's thread_info by masking out the 13 lower bits of the Stack Pointer. Then we can get task's descriptor via thread_info->task.


3. The life-cycle of Linux process / thread.

A. Process creation

Linux creates a process in two distinct steps:

1). fork(): creates descriptor by means of copying its parent's task_struct.

fork() creates a child process that is a copy of the current task. It differs from the parent only in its PID, PPID, and certain resources and statistics, such as pending signals, which are not inherited.

2). exec(): load and execute program

exec() loads a new executable into the address space and begins executing it.

内容概要:本文系统研究了基于豪猪优化算法(CPO)的多无人机协同集群在三维空间中的避障路径规划问题,聚焦于实现以最低成本为目标的航迹优化,综合考虑路径长度、飞行高度、威胁规避及转弯角度等多个关键因素。通过构建精细化的三维环境模型与多无人机协同机制,采用Matlab平台实现CPO算法的仿真与验证,充分展示了该算法在复杂动态障碍环境下的高效搜索能力与全局优化性能。研究不仅涵盖了路径规划的数学建模与目标函数设计,还深入探讨了算法的收敛特性与鲁棒性,为智能群体系统在实际场景中的应用提供了理论依据与技术支撑。; 适合人群:具备一定编程基础和优化算法背景,从事无人机系统控制、智能路径规划、群体协同、人工智能与自动化等相关领域的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①应用于多无人机协同执行侦察、灾害监测、应急救援、区域巡检等复杂任务中的自主路径规划;②为智能优化算法在三维动态环境下的路径决策问题提供可复现的技术范例;③支持研究人员对CPO算法与其他主流群智能算法(如PSO、GWO、WOA等)进行性能对比与改进研究,推动路径规划技术的发展。; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点理解目标函数的多维度建模方式与CPO算法的迭代优化流程,可通过调整环境参数与约束条件进行仿真实验,对比不同算法在相同场景下的路径质量与收敛速度,从而深入掌握其优势与适用边界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值