Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.

Similar presentations


Presentation on theme: "1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing."— Presentation transcript:

1 1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing Process 1: fetch data, put them in buffer Process 2: get data from buffer, process Problems? System calls to create processes: fork() in UNIX and CreateProcess() in Win

2 2 fork() Create an exact clone of the calling process After the call, two processes: parent (the calling process) and child (the process created) Same: memory image, environment, opened files, global variables, etc. Different: PID, address spaces, PC Child Parent OS Parent OS before after PC=100 PC=102 PC=300

3 A Simple Example of fork() ret_code = fork(); If (ret_code<0) handle_error(); else if (ret_code>0) { //parent code goes here } else { //child code goes here } Parent Process Child Process OS ret_code>0 Process OS Before fork() After fork() ret_code=0

4 4 A Simple Shell Using fork() #define TRUE 1 while (TRUE){// repeat forever type_prompt();// display prompt on screen read_command(cmd, pmts);// read input from terminal if (fork()!=0){ // Parent code waitpid(-1, &status, 0);// wait for child to exit }else{ // child code execve(cmd, pmts, 0);// execute command }

5 5 Other Ways to Create Processes Typing a command/clicking an icon Submitting batch jobs to batch systems Common properties: A new process is created by a process creation system call issued by a existing process. So how is the first process created?

6 6 Process Termination Voluntary termination Process is terminated by itself. Normal exit (voluntary): have the job done Error exit (voluntary): find an error Involuntary termination Process is terminated by another process Error caused by process itself (involuntary) Killed by operating system Killed by another process (involuntary) kill() system call

7 7 Process Hierarchies UNIX case Strong hierarchy All processes in a tree with root init Process group: including all the ancestors, siblings and descendants. Processes in the same process group can send signals to each other. SIGALRM, SIGKILL Windows case No process hierarchy

8 8 States of Process Running Actually using the CPU at that time Ready Able to run, temporarily stopped to let another process run. Blocked Unable to run until some external event happens

9 9 Transitions of States Running BlockedReady Process blocks for input/output Scheduler picks another process Scheduler picks this process Input becomes available 1 2 3 4

10 Keep Track of Processes Process managementMemory management File management Registers Program counter Program status word Stack pointer Process state(?) Priority Scheduling parameters Process ID Parent process Process group …… Pointer to text segment Pointer to data segment Pointer to stack segment Root directory(?) Working directory File descriptors(!) User ID Group ID Process table: One entry per process

11 11 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling

12 12 Motivation: Word Processor Writing a 800 pages book as a very large file Scenario: Done: changed page 1 To do: change page 600 After having changed p1, three things to be done by program 1. Reformat page 1-800 2. Listen to user’s input, e.g.: which page to go? 3. save the updated file One process? Multiple process? We need multiple threads.

13 13 What We Need: Threads Thread 1: Listen to keyboard Thread 2: Reformatting pages All threads in the same process share same resources Kernel Thread 3: Save the file

14 14 Why Threads? Process: grouping resources + execution In some cases, the two should be separated Two kinds of resources: I/O and CPU resource Thread: concurrent execution within a process (sub-tasks) Listen to user interaction (I/O, foreground) Reformatting (CPU, background) Share the same resources Address space, files, global variables and their updates

15 15 Processes Versus Threads Three processes One thread/process Processes operate in different address spaces One process Three threads in the process Threads operate in the same address space Kernel User space Kernel User space

16 16 Sharing Among Threads No protection between threads Impossible and unnecessary (why?) Shared by threadsPrivate to each thread Address space Global variables Open files Child processes Pending alarms Signals & signal handlers Accounting info Program counter Registers Stack (why?) State

17 17 Thread’s Stack One frame for each procedure called but not yet returned from Procedure’s local variables and return address T1T2 T3 Kernel User space

18 18 States of Threads Running Ready Blocked Running BlockedReady

19 19 System Calls About Threads Create new thread: thread_create Not necessary a system call (why?) Parameter: procedure for the new thread to run Often no hierarchy, but sometimes does Exit: thread_exit Wait for another thread to exit: thread_wait (synchronization) Voluntarily give up CPU: thread_yield

20 20 Complications With Threads Should a child process copy ALL threads from its parent? No  function not properly Yes  conflicts Competition on resources Thread 1 closes a file which thread 2 is using (synchronization) Memory allocation (exclusion) Need careful thought and design

21 21 Implementations of Threads In user space Kernel knows nothing about threads In the kernel Kernel knows the existence of threads Each method has its own advantages and disadvantages Hybrid implementation

22 22 Good for OS not supporting threads Thread table in process Not involving kernel Thread management Scheduling Customized scheduling Better scalability Implement Threads in User Space Kernel Process table User space Threads Run-time system Thread table

23 23 Disadvantages One thread blocked  entire process blocked! A thread may run forever (thread_yield() ) Threads are wanted where the threads block often! Why we need multi-threads? When threads block?

24 24 Implement Threads in Kernel No run-time system Make kernel calls when creating or destroying threads. Thread table in kernel Blocking calls as system calls Scheduling in kernel Considerable greater cost Kernel Process table User space Threads Thread table

25 25 Pros & Cons of Threads in Kernel One thread blocked  another thread within the same process can still run The costs of system calls are substantial

26 26 User-level Versus Kernel Level User-levelKernel-level performance Some instructions, much faster A full context switch, change memory map, invalidate cache Blocking A thread blocks  the process blocks No problem Application- specific thread scheduling YesNo

27 27 Hybrid Implementations Multiplex user-level threads onto kernel threads Kernel is aware of only the kernel-level threads. Tradeoff Threads Kernel User space Kernel threads

28 28 Single-threaded  Multi-threaded Programming in multithreading is challenging How to convert single-threaded processes to multithreaded ones? Many challenges, very tricky Global variables Stack management Others

29 29 Global Variables Compete on global variables, e.g., errno Thread 1 Thread 2 Access (errno set to 1) Open (errno set to 2) errno inspected Time

30 30 Stack Management When a process’s stack overflows Kernel provides more spaces automatically Multiple threads  multiple stacks If the kernel is not aware of these stacks Kernel does not know if a stack overflows It cannot grow automatically

31 31 Summary Threads: light-weight processes Resource sharing Concurrent executions Differences between processes and threads Resources control Implementation: user or kernel level? Programming with multi-threading Challenging


Download ppt "1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing."

Similar presentations


Ads by Google