Update Post “2023-11-26-kernel-creation-of-new-process”
This commit is contained in:
		
							parent
							
								
									709ca84b96
								
							
						
					
					
						commit
						f8322dd5fe
					
				| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
---
 | 
			
		||||
title: Kernel, creation of new process
 | 
			
		||||
title: Kernel, Process creation
 | 
			
		||||
date: 2023-11-26T07:44:51.449Z
 | 
			
		||||
slug: kernel-new-process-creation
 | 
			
		||||
slug: kernel-process-creation
 | 
			
		||||
description: "Deep dive Linux Kernel #3"
 | 
			
		||||
---
 | 
			
		||||
```c
 | 
			
		||||
| 
						 | 
				
			
			@ -41,6 +41,8 @@ static int init(void * unused)
 | 
			
		|||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
At `open("/dev/console") is opens fd 0, and with two `dup(0)`, fd 0, 1, 2 as stdin, stdout, stderr opens. When `execve()` or `fork()` happens, it simply copies calling process's `task_struct` as process context, so all file descriptors are also passed down.
 | 
			
		||||
In this `init/main.c`, after basic kernel initialization things, `open("/dev/console")` and two `dup(0)` opens three file descriptor 0, 1, 2 as stdin, stdout, stderr. Later when `execve()` or `fork()` happens, it simply copies calling process's `task_struct` as process context, so all file descriptors are also passed down as-is.
 | 
			
		||||
 | 
			
		||||
All processes' file descriptor 0, 1, 2 are all originates to `init`'s file descriptor, set up like above.
 | 
			
		||||
All processes' file descriptor 0, 1, 2 originates to `init`'s file descriptor.
 | 
			
		||||
 | 
			
		||||
If calling process opens PTY or TTY, which is common when we opens new terminal to run a process, it is also passed down to exec/forked process.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue