版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1Processes and ThreadsCreation and TerminationStatesUsageImplementationsWhat is a process? A task created by the OS, running in a restricted virtual machine environment a virtual CPU, virtual memory environment, interface to the OS via system calls The unit of execution The unit of scheduling Thread
2、 of execution + address space Is a program in execution Sequential, instruction-at-a-time execution of a program.The same as “job” or “task” or “sequential process”What is a program?A program consists of: Code: machine instructions Data: variables stored and manipulated in memory initialized variabl
3、es (globals) dynamically allocated variables (malloc, new) stack variables (C automatic variables, function arguments) DLLs: libraries that were not compiled or linked with the program containing code & data, possibly shared with other programs mapped files: memory segments containing variables
4、(mmap() used frequently in database programs A process is a executing programPreparing a Programsourcefilecompiler/assembler.o filesLinkerExecutable file(must follow standard format,such as ELF on Linux, Microsoft PE on Windows)HeaderCodeInitialized dataBSSSymbol tableLine numbersExt. refsstatic lib
5、raries(libc, streams)Running a program OS creates a “process” and allocates memory for it The loader: reads and interprets the executable file sets processs memory to contain code & data from executable pushes “argc”, “argv”, “envp” on the stack sets the CPU registers properly & calls “_star
6、t()” Part of CRT0 Program start running at _start(), which calls main() we say “process” is running, and no longer think of “program” When main() returns, CRT0 calls “exit()” destroys the process and returns all resourcesProcess != ProgramHeaderCodeInitialized dataBSSSymbol tableLine numbersExt. ref
7、sCodeInitialized dataBSSHeapStackDLLsmapped segmentsExecutableProcess address spaceProgram is passive Code + dataProcess is running program stack, regs, program counterExample:We both run IE:- Same program - Separate processesC Language in Address SpaceCodeInitialized dataBSSHeapStackDLLsmapped segm
8、entsProcess address space#include int a=10;int c;main(int argc, char* argv) int *b; b=(int *)malloc (sizeof(int); *b=a; 8Processes Program in execution (cf. recipe vs. cooking) Multiprogramming - pseudo-parallelism(vs. true hardware parallelism of multiprocessor systems)9The Process Model Multiprogr
9、amming of four programs Conceptual model of 4 independent, sequential processes Only one program active at any instant10Process CreationPrincipal events that cause process creation1. System initialization (background a daemon processes)2. Execution of a process creation system call (data from networ
10、k)3. User request to create a new process4. Initiation of a batch jobUNIX: fork system call (+ execve)Windows: CreateProcess function callCreating a Process - Fork Creating a process and executing a program are two different things in UNIX Fork duplicates a process so that instead on one process you
11、 get two- But the code being executed doesnt change! Fork returns 0 if child -1 if fork fails Childs PID if parent process Child gets new program counter, stack, file descriptors, heap, globals, pid!Examplemain(int argc, char *argv) char *myName = argv1; int cpid = fork(); if (cpid = 0) printf(“The
12、child of %s is %dn”, myName, getpid(); exit(0); else printf(“My child is %dn”, cpid); exit(0); What does this program print?Bizarre But Reallace:tmp cc a.clace:tmp ./a.out foobarThe child of foobar is 23874My child is 23874ParentChildOperating Systemfork()retsysv0=0v0=23874UNIX Example (Example 3.6,
13、 p.65)#include #include #include int main(void) pid_t parentpid; pid_t childpid; Reference: Kay Robbins, Steven Robbins, UNIX Systems Programming, Communication, Concurrency, and Threads,人民邮电出版社, ISBN 7-115-14984-4if (childpid = fork() = -1) perror(cant create a new process);exit(1); else if (childp
14、id = 0) /* child process executes */printf(“child: childpid = %d, parentpid = %d n”, getpid(), getppid();exit(0); else /*parent process executes */printf(“parent: childpid = %d, parentpid = %d n”, childpid, getpid();exit(0);What Happens? (Ex. 3.17, p66)#include #include #include int main(void) pid_t
15、 childpid; pid_t mypid; mypid=getpid(); childpid=fork(); if(childpid=-1) perror(Failed to fork); return 1; if(childpid=0) printf(I am child %ld, ID=%ldn, (long)getpid(),(long)mypid); else printf(I am parent %ld, ID=%ldn,(long)getpid(),(long)mypid); return 0;Chain and FanChildChildParentParentChildCh
16、ildpid_t childpid = 0;for (i=1;in;i+) if (childpid = fork() break;pid_t childpid = 0;for (i=1;in;i+) if (childpid = fork() =0) break;ChainFan19Process TerminationConditions which terminate processes1. Normal exit (voluntary) - (exit, ExitProcess)2. Error exit (voluntary)3. Fatal error (involuntary),
17、 e.g. program bug4. Killed by another process (involuntary) - kill, TerminateProcess20Process Hierarchies Parent creates a child process, child processes can create its own process Forms a hierarchy UNIX calls this a process group”init Windows has no concept of process hierarchy all processes are cr
18、eated equal21Process States (1) Possible process states running blocked ready Transitions between states shown22Process States (2) Lowest layer of process-structured OS handles interrupts, scheduling Above that layer are sequential processes23Implementation of Processes (1)Fields of a process table
19、entry24Implementation of Processes (2)Skeleton of what lowest level of OS does when an interrupt occursMultithreaded ProcessesThreads vs. Processes A thread has no data segment or heap A thread cannot live on its own, it must live within a process Inexpensive creation Inexpensive context switching I
20、f a thread dies, its stack is reclaimedA process has code/data/heap & other segmentsThere must be at least one thread in a processExpensive creationExpensive context switchingIf a process dies, its resources are reclaimed & all threads die27ThreadsProcess = resource grouping (code, data, ope
21、n files, etc.)+execution (program counter, registers, stack)Multithreading: multiple execution takes place in the same process environment co-operation by sharing resources (address space, open files, etc.)28The Thread Model (1)(a) Three processes each with one thread(b) One process with three threa
22、ds29The Thread Model (2) Items shared by all threads in a process Items private to each thread30The Thread Model (3)Each thread has its own stack to keep track execution history (called procedures)31Advantages Pseudo-parallelism with shared address space and data Easier to create and destroy than pr
23、ocesses Better performance for I/O bound applications32Thread Usage (1)A word processor with three threadsWriting a book: interactive and background threads sharing the same file33Thread Usage (2)A multithreaded Web server34Thread Usage (3) Rough outline of code for previous slide(a) Dispatcher thre
24、ad(b) Worker thread35Thread Usage (4)Three ways to construct a server36Implementing Threads in User SpaceA user-level threads package37(Dis)advantages+:no specific OS support neededfaster than kernel instructionsprocess-specific scheduling algorithms-:blocking system calls (select)page faults38Imple
25、menting Threads in the KernelA threads package managed by the kernel39(Dis)advantages+:handling blocking and page faults-:more costly (but recycling threads)40Hybrid Implementations Multiplexing user-level threads onto kernel- level threads41Scheduler Activations Goal: mimic functionality of kernel
26、threads gain performance of user space threads Avoids unnecessary user/kernel transitions Kernel assigns virtual processors to each process lets runtime system allocate threads to processors, upcall Problem: Fundamental reliance on kernel (lower layer) calling procedures in user space (higher layer)
27、42Pop-Up Threads Creation of a new thread when message arrives(a) before message arrives(b) after message arrives (quick)43Making Single-Threaded Code Multithreaded (1)Conflicts between threads over the use of a global variable44Making Single-Threaded Code Multithreaded (2)Threads can have private global variables.But non-reentrant library procedures.POSIX Threads Pthread_create Pthread_exit Pthread_joinCode Example in P.10645Program 12.2, P.414#include #include #include #includ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年规范化工程维修协议样本
- 2024年度防火涂料施工承包协议
- 2024公司股东股权转让协议
- 2024商业合作协议模板
- 2024届安徽省阜阳市第一中学高三高考全真模拟卷(七)数学试题
- 2024年专业建材购销协议格式
- 2023-2024学年重庆一中高三招生统考(二)数学试题模拟试卷
- 2024年幼儿照护服务协议范例
- 2024专业不锈钢定制加工协议范本
- 2024定制大客车租赁业务协议
- 十二指肠溃疡伴穿孔的护理查房
- 市场营销策划(本)-形考任务三(第八~十章)-国开(CQ)-参考资料
- 中信证券测评真题答案大全
- 部编版小学六年级道德与法治上册全册知识点汇编
- 数字时代的数字化政府
- 文旅推广短片策划方案相关7篇
- 2023-2024学年高中主题班会燃激情之烈火拓青春之华章 课件
- 中医药文化进校园-中医药健康伴我行课件
- 市政管道开槽施工-市政排水管道的施工
- 居住建筑户型分析
- 机电一体化职业生涯
评论
0/150
提交评论