




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 古詩(shī)文教學(xué)新思路:春江花月夜教學(xué)設(shè)計(jì)與實(shí)施案例分享
- 汽車機(jī)械維修技術(shù)實(shí)操測(cè)試卷
- 企業(yè)管理培訓(xùn)服務(wù)合同
- 墩、臺(tái)身和蓋梁工程現(xiàn)場(chǎng)質(zhì)量檢驗(yàn)報(bào)告單(二)
- 超前錨桿 現(xiàn)場(chǎng)質(zhì)量檢驗(yàn)報(bào)告單
- 酒水采購(gòu)合同
- 防控疫情知識(shí)培訓(xùn)課件
- 醫(yī)療護(hù)理操作規(guī)范測(cè)試題
- 武漢手房屋買(mǎi)賣合同書(shū)
- 教育范文選錄
- 單招面試技巧簡(jiǎn)介PPT幻燈片課件(PPT 59頁(yè))
- 【電子課件】4-1-高壓個(gè)人防護(hù)用具使用
- 迪士尼樂(lè)園主題PPT模板
- C形根管的形態(tài)識(shí)別和治療實(shí)用教案
- 部編版《道德與法治》四年級(jí)下冊(cè)第5課《合理消費(fèi)》優(yōu)質(zhì)課件
- 京東入駐流程(課堂PPT)
- 鍋爐巡檢制度
- 中國(guó)國(guó)際航空公司VI形象識(shí)別規(guī)劃提案
- 三菱PLC模擬量模塊fx2n4da中文手冊(cè)
- 金屬材料工程課程設(shè)計(jì)
- 學(xué)校突發(fā)公共衛(wèi)生事件應(yīng)急處置.ppt
評(píng)論
0/150
提交評(píng)論