第四讲 UNIX进程环境、进程控制和进程关系
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
inode
files_struct count close_on_exec open_fs fd[0] fd[1] …… fd[255]
inode f_mode f_pcs f_flags f_count f_owner f_inode f_op f_version file
file operation routines
Memory allocation
#include <stdlib.h> void *calloc(size_t nmemb, size_t size); void *malloc(size_t size); void *realloc(void *ptr, size_t size); void free(void *ptr); #include <alloca.h> void *alloca(size_t size);
call call call call return return return _-up startroutine
exec
Kernel
Memory layout of a Program
Historically, a C program has been composed of the following pieces:
Text segment segment:This is the machine instructions that are executed by the CPU. It is shareable. Initialized data segment It contains variables that are specifically segment. initialized in the program. E.g.:
long sum[1000];
Stack. Stack This is where automatic variable are stored, along with information that is saved each time a function is called. Heap. Heap Dynamic memory allocation takes place on the heap.
register Fun. register Fun. Standard I/O cleanup _exit
call
return
exit main exit function doesn’t return func.
User function
call call call call return return return return
int main ( int argc, char *argv[] );
CommandCommand-line arguments
int main(int argc, char **argv) { int i; printf("argument number is %d\n",argc); for(i=0;i<argc;i++) printf("argv[%d] : %s\n",i,argv[i]); exit(0); }
Example: (doatexit)
static void my_exit1(void), my_exit2(void); int main(void) { if (atexit(my_exit2) != 0) err_sys("can't register my_exit2"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); printf("main is done\n"); return(0); } static void my_exit1(void) { printf("first exit handler\n"); } static void my_exit2(void) { printf("second exit handler\n"); }
How a C program is started and terminates
User process User process User process User process User process User process User process User process User process User process User process User process User process User process User process User process
exit and _exit Function
void exit( int status ); /* <stdlib.h> */ void _exit (int status ); /* <unistd.h> */
Two functions terminate a program normally: _exit ,which returns to the kernel immediately, and exit exit which performs certain cleanup processing and then returns to the kernel. The exit function has always performs a clean shutdown of the standard I/O library: the fclose function is called for all open stream. This causes all buffered output data to be flushed.
Process termination
There are five ways for process to terminate Normal termination • Return from main; ; • Calling exit • Calling _exit Abnormal termination • Calling abort; • Terminated by a signal.
Linux的进程结构 Linux的进程结构
task_struct vm_area_struct vm_end vm_start vm_flags vm_inode vm_ops process virtual Memory
mm
mm_struct count pgd
data
mmap mmap_avl mmap_sem
vm_next code
vm_end vm_start vm_flags vm_inode vm_ops
vm_next
vm_area_struct
Linux的进程结构 Linux的进程结构
task_struct fs_struct count umask *root *pwd inode
fs files
Process status
用户态运行 中断、 中断、中断返回 1 返回到 用户态
9 僵死
退出
核心态 2 运行
抢先
7 被抢先
在内存 中睡眠
4
唤醒
在内存中就绪 3 创建 换 出
换 出 唤醒
换 入 5
8
fork
6 睡眠且换出
就绪且换出
Linux的进程组织 Linux的进程组织
进程的物理组织结构
Proc 1 Proc 1 Proc 1 Proc 1 Proc 1
Process start
A C program starts execution with a function main. main When a C program is started by the kernel, a special start-up routine is called before main function is called, which obtains address of main and arguments from main It main. would copy these information to u-area of process control block .
Lecture four: the environment of a Unix process、process control and process、 processes relationship
Ren Liyong Computer Science of UESTC
Contents
Process status transition diagram Process start and termination Non local jump Create and terminate process Race condition Process relationship
Current 进程的逻辑组织结构
P1
P1
P1
P1
P1
P1
P1
P1
Linux的进程结构 Linux的进程结构
进程状态(State) 进程状态(State) 调度信息(Schedule) 调度信息(Schedule) 标识(Identifiers) 标识(Identifiers) 进程间通信(Inter进程间通信(Inter-Process Communication) Communication) 时间和定时器( 时间和定时器(Times and Timers) Timers) 文件系统( system) 文件系统(File system) 。。。。 。。。。 虚拟内存( memory) 虚拟内存(Virtual memory)
theenvironmentofaunixprocessprocesscontrolandprocessesrelationshiprenliyongcomputerscienceofuestccontents?processstatustransitiondiagram?processstartandtermination?nonlocaljump?createandterminateprocess?racecondition?processrelationshipprocessstatus用户态运行127943658返回到用户态被抢先创建fork就绪且换出唤醒睡眠且换出在内存中睡眠唤醒在内存中就绪换入换出换出抢先核心态运行僵死退出中断中断返回linux的进程组织proc1proc1proc1proc1proc1?进程的物理组织结构currentp1p1p1p1p1p1p1p1?进程的逻辑组织结构linux的进程结构进程状态state调度信息schedule标识identifiers进程间通信interprocesscommunication时间和定时器timesandtimers文件系统filesystem
Typical memory arrangement
High address CommandCommand-line argument and Environment variables stack
heap Uninitialized data (bss) Initialized data text Low address read from program file by exec initialized to 0 or NULL by exec
calloc. calloc Allocates space for a specified number of object of a specified size. The space is initialized to all 0 bits. malloc. malloc Allocates a specified number of bytes of memory, the initial value of the memory is indeterminate. realloc. realloc Changes the size of a previously allocated area. sbrk, The above three allocation call sbrk which allocates memory from heap But alloca <alloca.h> function heap. allocates memory in the stack frame of the caller. This temporary space is automatically freed
int maxcount = 99;
Uninitialized data segment It is often called the ‘bss’ segment. segment. Data in this segment is initialized by the kernel to 0 or NULL pointer before the program start executing:
atexit Function
#include <stdlib.h> int atexit ( void ( *func )(void) ); Int on_exit ( void (*func)( int, void * ), void *arg );
With ANSI C a process can register up to 32 functions that are automatically called by exit exit. These are registered by atexit or on_exit function The exit function calls these functions in reverse order of their registration.