进程:是指计算机中已运行的程序。进程本身不是基本的运行单位,而是线程的容器。程序本身只是指令、数据及其组织形式的描述,进程才是程序(那些指令和数据)的真正运行实例。
进程是程序动态的形式。
进程生命周期Linux操作系统属于多任务操作系统,系统中的每个进程能够分时复用CPU时间片,通过有效的进程调度策略实现多任务并行执行。而进程在被CPU调度运行,等待CPU资源分配以及等待外部事件时会属于不同的状态。进程之间的状态关系:
运行:该进程此刻正在执行。
等待:进程能够运行,但没有得到许可,因为CPU分配给另一个进程。调度器可以在
下一次任务切换时选择该进程。
睡眠:进程正在睡眠无法运行,因为它在等待一个外部事件。调度器无法在下一次任
务切换时选择该进程。
Linux内核涉及进程和程序的所有算法都围绕一个名为task_struct的数据结构建立,该结构定义在include/linux/sched.h中。这是系统中主要的一个结构。在阐述调度器的实现之前,了解一下Linux管理进程的方式是很有必要的。task_struct包含很多成员,将进程与各个内核子系统联系,task_struct定义如下:
1 struct task_struct { 进程状态
2 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
3 void *stack; 内核栈
4 atomic_t usage; 有几个进程在使用此结构
5 unsigned int flags; /* per process flags, defined below */
6 unsigned int ptrace; 断点调试使用
7 多处理使用
8 #ifdef CONFIG_SMP
9 struct llist_node wake_entry;
10 int on_cpu;
11 unsigned int wakee_flips;
12 unsigned long wakee_flip_decay_ts;
13 struct task_struct *last_wakee;
14
15 int wake_cpu;
16 #endif
17 int on_rq;
18
19 int prio, static_prio, normal_prio;
20 unsigned int rt_priority; 优先级 //进程调度
21 const struct sched_class *sched_class;
22 struct sched_entity se;
23 struct sched_rt_entity rt;
24 #ifdef CONFIG_CGROUP_SCHED
25 struct task_group *sched_task_group;
26 #endif
27 struct sched_dl_entity dl;
28
29 #ifdef CONFIG_PREEMPT_NOTIFIERS
30 /* list of struct preempt_notifier: */
31 struct hlist_head preempt_notifiers;
32 #endif
33
34 #ifdef CONFIG_BLK_DEV_IO_TRACE
35 unsigned int btrace_seq;
36 #endif
37
38 unsigned int policy;
39 int nr_cpus_allowed;
40 cpumask_t cpus_allowed;
41
42 #ifdef CONFIG_PREEMPT_RCU
43 int rcu_read_lock_nesting;
44 union rcu_special rcu_read_unlock_special;
45 struct list_head rcu_node_entry;
46 struct rcu_node *rcu_blocked_node;
47 #endif /* #ifdef CONFIG_PREEMPT_RCU */
48 #ifdef CONFIG_TASKS_RCU
49 unsigned long rcu_tasks_nvcsw;
50 bool rcu_tasks_holdout;
51 struct list_head rcu_tasks_holdout_list;
52 int rcu_tasks_idle_cpu;
53 #endif /* #ifdef CONFIG_TASKS_RCU */
54
55 #ifdef CONFIG_SCHED_INFO
56 struct sched_info sched_info;
57 #endif
58
59 struct list_head tasks;
60 #ifdef CONFIG_SMP
61 struct plist_node pushable_tasks;
62 struct rb_node pushable_dl_tasks;
63 #endif
64
65 struct mm_struct *mm, *active_mm;
66 /* per-thread vma caching */
67 u32 vmacache_seqnum;
68 struct vm_area_struct *vmacache[VMACACHE_SIZE];
69 #if defined(SPLIT_RSS_COUNTING)
70 struct task_rss_stat rss_stat;
71 #endif
72 /* task state */
73 int exit_state;
74 int exit_code, exit_signal;
75 int pdeath_signal; /* The signal sent when the parent dies */
76 unsigned long jobctl; /* JOBCTL_*, siglock protected */
77
78 /* Used for emulating ABI behavior of previous Linux versions */
79 unsigned int personality;
80
81 /* scheduler bits, serialized by scheduler locks */
82 unsigned sched_reset_on_fork:1;
83 unsigned sched_contributes_to_load:1;
84 unsigned sched_migrated:1;
85 unsigned :0; /* force alignment to the next boundary */
86
87 /* unserialized, strictly 'current' */
88 unsigned in_execve:1; /* bit to tell LSMs we're in execve */
89 unsigned in_iowait:1;
90 #ifdef CONFIG_MEMCG
91 unsigned memcg_may_oom:1;
92 #endif
93 #ifdef CONFIG_MEMCG_KMEM
94 unsigned memcg_kmem_skip_account:1;
95 #endif
96 #ifdef CONFIG_COMPAT_BRK
97 unsigned brk_randomized:1;
98 #endif
99
100 unsigned long atomic_flags; /* Flags needing atomic access. */
101
102 struct restart_block restart_block;
103
104 pid_t pid; 进程和父进程的pid
105 pid_t tgid;
106
107 #ifdef CONFIG_CC_STACKPROTECTOR
108 /* Canary value for the -fstack-protector gcc feature */
109 unsigned long stack_canary;
110 #endif
111 /*
112 * pointers to (original) parent process, youngest child, younger sibling,
113 * older sibling, respectively. (p->father can be replaced with
114 * p->real_parent->pid)
115 */ 父进程
116 struct task_struct __rcu *real_parent; /* real parent process */ 接收终止进程
117 struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
118 /*
119 * children/sibling forms the list of my natural children
120 */ 子进程链表
121 struct list_head children; /* list of my children */ 兄弟进程链表
122 struct list_head sibling; /* linkage in my parent's children list */ 线程组组织
123 struct task_struct *group_leader; /* threadgroup leader */
124
125 /*
126 * ptraced is the list of tasks this task is using ptrace on.
127 * This includes both natural children and PTRACE_ATTACH targets.
128 * p->ptrace_entry is p's link on the p->parent->ptraced list.
129 */ 系统调用调试使用
130 struct list_head ptraced;
131 struct list_head ptrace_entry;
132
133 /* PID/PID hash table linkage. */
134 struct pid_link pids[PIDTYPE_MAX];
135 struct list_head thread_group;
136 struct list_head thread_node;
137
138 struct completion *vfork_done; /* for vfork() */
139 int __user *set_child_tid; /* CLONE_CHILD_SETTID */
140 int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
141 utime 用户态执行时间 stime 内核态执行时间
142 cputime_t utime, stime, utimescaled, stimescaled;
143 cputime_t gtime;
144 struct prev_cputime prev_cputime;
145 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
146 seqlock_t vtime_seqlock;
147 unsigned long long vtime_snap;
148 enum {
149 VTIME_SLEEPING = 0,
150 VTIME_USER,
151 VTIME_SYS,
152 } vtime_snap_whence;
153 #endif
154 unsigned long nvcsw, nivcsw; /* context switch counts */
155 u64 start_time; /* monotonic time in nsec */
156 u64 real_start_time; /* boot based time in nsec */
157 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
158 unsigned long min_flt, maj_flt;
159
160 struct task_cputime cputime_expires;
161 struct list_head cpu_timers[3];
162
163 /* process credentials */
164 const struct cred __rcu *real_cred; /* objective and real subjective task
165 * credentials (COW) */
166 const struct cred __rcu *cred; /* effective (overridable) subjective task
167 * credentials (COW) */
168 char comm[TASK_COMM_LEN]; /* executable name excluding path
169 - access with [gs]et_task_comm (which lock
170 it with task_lock())
171 - initialized normally by setup_new_exec */
172 /* file system info */
173 struct nameidata *nameidata;
174 #ifdef CONFIG_SYSVIPC
175 /* ipc stuff */
176 struct sysv_sem sysvsem;
177 struct sysv_shm sysvshm;
178 #endif
179 #ifdef CONFIG_DETECT_HUNG_TASK
180 /* hung task detection */
181 unsigned long last_switch_count;
182 #endif
183 /* filesystem information */
184 struct fs_struct *fs;
185 /* open file information */
186 struct files_struct *files;
187 /* namespaces */
188 struct nsproxy *nsproxy;
189 /* signal handlers */
190 struct signal_struct *signal;
191 struct sighand_struct *sighand;
192
193 sigset_t blocked, real_blocked;
194 sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */
195 struct sigpending pending;
196
197 unsigned long sas_ss_sp;
198 size_t sas_ss_size;
199
200 struct callback_head *task_works;
201
202 struct audit_context *audit_context;
203 #ifdef CONFIG_AUDITSYSCALL
204 kuid_t loginuid;
205 unsigned int sessionid;
206 #endif
207 struct seccomp seccomp;
208
209 /* Thread group tracking */
210 u32 parent_exec_id;
211 u32 self_exec_id;
212 /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
213 * mempolicy */
214 spinlock_t alloc_lock;
215
216 /* Protection of the PI data structures: */
217 raw_spinlock_t pi_lock;
218
219 struct wake_q_node wake_q;
220
221 #ifdef CONFIG_RT_MUTEXES
222 /* PI waiters blocked on a rt_mutex held by this task */
223 struct rb_root pi_waiters;
224 struct rb_node *pi_waiters_leftmost;
225 /* Deadlock detection and priority inheritance handling */
226 struct rt_mutex_waiter *pi_blocked_on;
227 #endif
228
229 #ifdef CONFIG_DEBUG_MUTEXES
230 /* mutex deadlock detection */
231 struct mutex_waiter *blocked_on;
232 #endif
233 #ifdef CONFIG_TRACE_IRQFLAGS
234 unsigned int irq_events;
235 unsigned long hardirq_enable_ip;
236 unsigned long hardirq_disable_ip;
237 unsigned int hardirq_enable_event;
238 unsigned int hardirq_disable_event;
239 int hardirqs_enabled;
240 int hardirq_context;
241 unsigned long softirq_disable_ip;
242 unsigned long softirq_enable_ip;
243 unsigned int softirq_disable_event;
244 unsigned int softirq_enable_event;
245 int softirqs_enabled;
246 int softirq_context;
247 #endif
248 #ifdef CONFIG_LOCKDEP
249 # define MAX_LOCK_DEPTH 48UL
250 u64 curr_chain_key;
251 int lockdep_depth;
252 unsigned int lockdep_recursion;
253 struct held_lock held_locks[MAX_LOCK_DEPTH];
254 gfp_t lockdep_reclaim_gfp;
255 #endif
256
257 /* journalling filesystem info */
258 void *journal_info;
259
260 /* stacked block device info */
261 struct bio_list *bio_list;
262
263 #ifdef CONFIG_BLOCK
264 /* stack plugging */
265 struct blk_plug *plug;
266 #endif
267
268 /* VM state */
269 struct reclaim_state *reclaim_state;
270
271 struct backing_dev_info *backing_dev_info;
272
273 struct io_context *io_context;
274
275 unsigned long ptrace_message;
276 siginfo_t *last_siginfo; /* For ptrace use. */
277 struct task_io_accounting ioac;
278 #if defined(CONFIG_TASK_XACCT)
279 u64 acct_rss_mem1; /* accumulated rss usage */
280 u64 acct_vm_mem1; /* accumulated virtual memory usage */
281 cputime_t acct_timexpd; /* stime + utime since last update */
282 #endif
283 #ifdef CONFIG_CPUSETS
284 nodemask_t mems_allowed; /* Protected by alloc_lock */
285 seqcount_t mems_allowed_seq; /* Seqence no to catch updates */
286 int cpuset_mem_spread_rotor;
287 int cpuset_slab_spread_rotor;
288 #endif
289 #ifdef CONFIG_CGROUPS
290 /* Control Group info protected by css_set_lock */
291 struct css_set __rcu *cgroups;
292 /* cg_list protected by css_set_lock and tsk->alloc_lock */
293 struct list_head cg_list;
294 #endif
295 #ifdef CONFIG_FUTEX
296 struct robust_list_head __user *robust_list;
297 #ifdef CONFIG_COMPAT
298 struct compat_robust_list_head __user *compat_robust_list;
299 #endif
300 struct list_head pi_state_list;
301 struct futex_pi_state *pi_state_cache;
302 #endif
303 #ifdef CONFIG_PERF_EVENTS
304 struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];
305 struct mutex perf_event_mutex;
306 struct list_head perf_event_list;
307 #endif
308 #ifdef CONFIG_DEBUG_PREEMPT
309 unsigned long preempt_disable_ip;
310 #endif
311 #ifdef CONFIG_NUMA
312 struct mempolicy *mempolicy; /* Protected by alloc_lock */
313 short il_next;
314 short pref_node_fork;
315 #endif
316 #ifdef CONFIG_NUMA_BALANCING
317 int numa_scan_seq;
318 unsigned int numa_scan_period;
319 unsigned int numa_scan_period_max;
320 int numa_preferred_nid;
321 unsigned long numa_migrate_retry;
322 u64 node_stamp; /* migration stamp */
323 u64 last_task_numa_placement;
324 u64 last_sum_exec_runtime;
325 struct callback_head numa_work;
326
327 struct list_head numa_entry;
328 struct numa_group *numa_group;
329
330 /*
331 * numa_faults is an array split into four regions:
332 * faults_memory, faults_cpu, faults_memory_buffer, faults_cpu_buffer
333 * in this precise order.
334 *
335 * faults_memory: Exponential decaying average of faults on a per-node
336 * basis. Scheduling placement decisions are made based on these
337 * counts. The values remain static for the duration of a PTE scan.
338 * faults_cpu: Track the nodes the process was running on when a NUMA
339 * hinting fault was incurred.
340 * faults_memory_buffer and faults_cpu_buffer: Record faults per node
341 * during the current scan window. When the scan completes, the counts
342 * in faults_memory and faults_cpu decay and these values are copied.
343 */
344 unsigned long *numa_faults;
345 unsigned long total_numa_faults;
346
347 /*
348 * numa_faults_locality tracks if faults recorded during the last
349 * scan window were remote/local or failed to migrate. The task scan
350 * period is adapted based on the locality of the faults with different
351 * weights depending on whether they were shared or private faults
352 */
353 unsigned long numa_faults_locality[3];
354
355 unsigned long numa_pages_migrated;
356 #endif /* CONFIG_NUMA_BALANCING */
357
358 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
359 struct tlbflush_unmap_batch tlb_ubc;
360 #endif
361
362 struct rcu_head rcu;
363
364 /*
365 * cache last used pipe for splice
366 */
367 struct pipe_inode_info *splice_pipe;
368
369 struct page_frag task_frag;
370
371 #ifdef CONFIG_TASK_DELAY_ACCT
372 struct task_delay_info *delays;
373 #endif
374 #ifdef CONFIG_FAULT_INJECTION
375 int make_it_fail;
376 #endif
377 /*
378 * when (nr_dirtied >= nr_dirtied_pause), it's time to call
379 * balance_dirty_pages() for some dirty throttling pause
380 */
381 int nr_dirtied;
382 int nr_dirtied_pause;
383 unsigned long dirty_paused_when; /* start of a write-and-pause period */
384
385 #ifdef CONFIG_LATENCYTOP
386 int latency_record_count;
387 struct latency_record latency_record[LT_SAVECOUNT];
388 #endif
389 /*
390 * time slack values; these are used to round up poll() and
391 * select() etc timeout values. These are in nanoseconds.
392 */
393 unsigned long timer_slack_ns;
394 unsigned long default_timer_slack_ns;
395
396 #ifdef CONFIG_KASAN
397 unsigned int kasan_depth;
398 #endif
399 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
400 /* Index of current stored address in ret_stack */
401 int curr_ret_stack;
402 /* Stack of return addresses for return function tracing */
403 struct ftrace_ret_stack *ret_stack;
404 /* time stamp for last schedule */
405 unsigned long long ftrace_timestamp;
406 /*
407 * Number of functions that haven't been traced
408 * because of depth overrun.
409 */
410 atomic_t trace_overrun;
411 /* Pause for the tracing */
412 atomic_t tracing_graph_pause;
413 #endif
414 #ifdef CONFIG_TRACING
415 /* state flags for use by tracers */
416 unsigned long trace;
417 /* bitmask and counter of trace recursion */
418 unsigned long trace_recursion;
419 #endif /* CONFIG_TRACING */
420 #ifdef CONFIG_MEMCG
421 struct mem_cgroup *memcg_in_oom;
422 gfp_t memcg_oom_gfp_mask;
423 int memcg_oom_order;
424
425 /* number of pages to reclaim on returning to userland */
426 unsigned int memcg_nr_pages_over_high;
427 #endif
428 #ifdef CONFIG_UPROBES
429 struct uprobe_task *utask;
430 #endif
431 #if defined(CONFIG_BCACHE) || defined(CONFIG_BCACHE_MODULE)
432 unsigned int sequential_io;
433 unsigned int sequential_io_avg;
434 #endif
435 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
436 unsigned long task_state_change;
437 #endif
438 int pagefault_disabled;
439 /* CPU-specific state of this task */
440 struct thread_struct thread;
441 /*
442 * WARNING: on x86, 'thread_struct' contains a variable-sized
443 * structure. It *MUST* be at the end of 'task_struct'.
444 *
445 * Do not put anything below here!
446 */
447 };
并非所有进程都具有相同的重要性。除了大多数我们所熟悉的进程优先级之外,进程还有不同的关键度类别,以满足不同需求先进行比较粗糙的划分,进程可以分为 实时进程和 非实时进程 (普通进程)。实时进程优先级(0-99)都比普通 进程的优先级(100-139)高。当系统中有实时进程 运行时,普通进程几乎无法分到赶时间片(只能分到5%的CPU时间)。
讨论fork和exec系列系统调用的实现。通常这些调用不是由应用程序直接发出的,而是通过一个中间层调用,即负责与内核通信的C标准库。从用户状态切换到核心态的方法,依不同的体系结构而各有不同。
1、进程复制
传统的UNIX中用于复制进程的系统调用是fork。但它并不是Linux为此实现的唯一调用,
实际上Linux实现了3个。
(1) fork是重量级调用,因为它建立了父进程的一个完整副本,然后作为子进程执行。为减少与该调用相关的工作量,Linux使用了写时复制(copy-on-write)技术。
(2) vfork类似于fork,但并不创建父进程数据的副本。相反,父子进程之间共享数据。这节省了大量CPU时间(如果一个进程操纵共享数据,则另一个会自动注意到)。
(3) clone产生线程,可以对父子进程之间的共享、复制进行精确控制。
五、进程系统调用【写时复制】
内核使用了写时复制(Copy-On-Write,COW)技术,以防止在fork执行时将父进程的所有数据复制到子进程。在调用fork时,内核通常对父进程的每个内存页,都为子进程创建一个相同的副本。
【执行系统调用】
fork、vfork和clone系统调用的入口点分别是sys_fork、sys_vfork和sys_clone函数。其定义依赖于
具体的体系结构,因为在用户空间和内核空间之间传递参数的方法因体系结构而异。
所有3个fork机制最终都调用kernel/fork.c中的do_fork(一个体系结构无关的函数),其代码流程
如图所示。
内核线程是直接由内核本身启动的进程。内核线程实际上是将内核函数委托给独立的
进程,与系统中其他进程”并行”执行(实际上,也并行于内核自身的执行)。内核线程经常
称之为(内核)守护进程。它们用于执行下列任务。
• 周期性地将修改的内存页与页来源块设备同步(例如,使用mmap的文件映射)。
• 如果内存页很少使用,则写入交换区。
• 管理延时动作(deferred action)。
• 实现文件系统的事务日志。
3、退出进程
进程必须用exit系统调用终止。这使得内核有机会将该进程使用的资源释放回系统。见kernel/exit.c——>do_exit。简而言之,该函数的实现就是将各个引用计数器减1,如果引用计数器归0而没有进程再使用对应的结构,那么将相应的内存区域返还给内存理模块。
exit () 系统调用一般由编译器在main函数结束位置添加
Original: https://www.cnblogs.com/thotf/p/16261566.html
Author: thotf
Title: 什么是进程
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582270/
转载文章受原作者版权保护。转载请注明原作者出处!