1 / 69

CENG 334 – Operating Systems 02- Processes

CENG 334 – Operating Systems 02- Processes. Assoc. Prof. Yusuf Sahillio ğlu. Computer Eng. Dept, , Turkey. How a Modern Computer Works. Kernel is always in memory. When a program is run, it is brought to memory from disk (device).

tburgoon
Télécharger la présentation

CENG 334 – Operating Systems 02- Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CENG 334 – Operating Systems02- Processes Assoc. Prof. Yusuf Sahillioğlu Computer Eng. Dept, , Turkey

  2. How a Modern Computer Works • Kernel is always in memory. • When a program is run, it is brought to memory from disk (device). • When a program is run, it becomes a process. • Then CPU executes process’ instructions.

  3. Central Processing Unit • Transition from user to kernel mode and vice versa. • User code may call an OS routine (system call) to get a service done. • This mechanism protects the system. • OS touches the I/O devices, memory locations, etc. • User code cannot harm them.

  4. System Calls • Programming interface to the services provided by the OS.

  5. System Calls • Why use APIs rather than system calls directly? • Going from API to SysCall is tricky. • Call thread machine instruction; set some registers; etc. • API does them for me.

  6. System Programs • Come with OS CD. • copy, rename, date, grep, ftp, ssh, etc. • They are application programs calling system calls.

  7. Processes • Process: a program (piece of code) in execution. • Program is passive entity, process is active. • Program becomes process when executable file loaded into memory. • We will learn process scheduling, creation/termination, synchronization, communication, deadlock handling.

  8. Processes • What resources does a process need?

  9. Processes • What resources does a process need? • CPU: to get executed. • Memory: to be stored. • I/O: to get value from keyboard. • Files: to open/read/close files (disk I/O).

  10. Process Address Space

  11. Process in Memory 0xFFFFFFF //temporary data (function parameters, return //addresses, local variables); grows downwards. //dynamically allocated memory; grows upwards. //global variables. //program code (current line pointed by Program Counter). Address space 0x0000000

  12. Process in Memory • C++ run-time stack example.

  13. Process in Memory • C++ run-time stack example. • During execution, each recursive call generates an activation record that is pushed onto a stack. • That's why we can get stack overflow error if a function makes makes too many recursive calls.

  14. Process in Memory • C++ run-time stack example.

  15. Process in Memory • C++ run-time stack example.

  16. Migration of int A from Disk to Register • Multitasking environments must be careful to use most recent value, no matter where it is stored in the storage hierarchy. • Movement b/w levels of storage.

  17. Process Execution • How can several processes run on one CPU? • OS makes this happen by ensuring: • Fair scheduling: each process gets fair chance to run. • Protection: processes do not modify each others properties. • Process states: • new: The process is being created. • running: Instructions are being executed. • waiting: The process is waiting for some event to occur. • ready: The process is waiting to be assigned to a processor. • terminated: The process has finished execution.

  18. Process States

  19. Process Control Block (PCB) • When switching from process to process (context switch), we need to know information associated with each process: • Process ID //unique identifier • User ID //who is running? • Process state //ready, waiting? • Program counter //code line • CPU registers //computations storage • CPU scheduling information //priority of this process • Memory-management information //max memo given to this process • Accounting information //total CPU time, memo usage • I/O status information //is I/O ready?

  20. Process Control Block (PCB) • Contents of PCB (not all shown below): Calculations ‘till the interrupt are stored here so that I can resume efficiently.

  21. Context Switch • Important ‘cos it allows new processes run by the processor. • Overhead ‘cos while switching the context no work is done for the processes.

  22. Context Switch • Context switch is kernel code. • Process is user code. Process A Process B user code context switch kernel code Time user code context switch kernel code user code

  23. Context Switch • Context overhead in Ubuntu 9.04 is 5.4 usecs on a 2.4GHz Pentium 4. • This is about 13.200 CPU cycles. • Not quite that many instructions since CPI > 1

  24. Process Representation in Linux • PCB for a process is in the structure task_struct. • Use ps command to see the started processes in the system. struct task_struct { long state; /* state of the process */ …. pid_t pid; /* identifier of the process */ … unisgned int time_slice; /* scheduling info */ … struct files_struct *files; /* open files this process is working on */ …. struct task_struct *parent; /* this process’s parent */ … }

  25. Process Scheduling • Scheduler interleaves processes in order to give every process the illusion of having its own CPU, aka concurrency. • Even with one CPU (instruction executer), you can multitask: music, code, download, .. • Process scheduler selects among available processes for next execution on CPU. • Maintains scheduling queues of processes • Job queue – set of all processes in the system • Ready queue – set of all processes ready to execute • Device queues – set of processes waiting for an I/O device //scanf(“%d”, &number); • Processes migrate among the various queues

  26. Process Scheduling e.g., sleep(1000);

  27. Schedulers • Long-term scheduler (or job scheduler): selects which processes should be brought into the ready queue (from the job queue). • Controls the degree of multitasking (all ready processes execute). • Short-term scheduler (or CPU scheduler): selects which process should be executed next and allocates CPU. • Sometimes the only scheduler in a system.

  28. First Come First Served Scheduling • An unfair non-preemptive CPU scheduler, aka FCFS or FIFO. • Idea: run until done! • Example:

  29. First Come First Served Scheduling • An unfair non-preemptive CPU scheduler, aka FCFS or FIFO. • Idea: run until done! • Example:

  30. Round Robin Scheduling • A fair preemptive CPU scheduler. • Idea: each process gets a small amount of CPU time (time quantum). • Usually 10-100 milliseconds. Comments on this value? • Example with quantum = 20:

  31. Shortest Job Next • An unfair non-preemptive CPU scheduler. • Idea: run the shortest jobs first. • Run time estimate is an issue  • A variant: Shortest Remaining Time Next. • Still needs those estimates  • Preemptive version of Shortest Job Next.

  32. Priority Scheduling • An unfair non-preemptive CPU scheduler. • Idea: e.g., admin jobs favored!

  33. Process Creation • One process can create, or fork, another process. • The original process is the parent. //original variables, assignments, .. • New process is the child. //copy of parent with its own address space.

  34. Why do we fork? • Exxample usages of fork(): • Your shell uses fork to run programs you invoke from the command line. • Google Chrome uses fork to handle each page within a separate process. This will prevent client-side code on one page from bringing your whole browser down. • Web servers like Apache use fork to create multiple server processes, each of which handles requests in its own address space. If one dies or leaks memory, others are unaffected, so it functions as a mechanism for fault tolerance. • Your word processor uses fork to print a document while you can still look/edit different pages. • Unix system starts with a process called init. It then forks new processes. Without fork you could only run init in Unix systems, which totally blows. • Your virus forks new processes to draw annoying stuff on screen, to produce weird noises, etc., once it is settled to the system (common work). When user obeys you, you may want to kill some annoying child processes as a reward.

  35. PID 4109 State: Running PID 4110 State: Ready PC PC Registers Registers PID 4277 State: Ready PID 4110 State: Ready PID 4391 State: Ready PC PC PC Registers Registers Registers UNIX fork Mechanism • fork() system call creates a new process (already discussed 2 slides ago). • This creates an exact duplicate of the parent process. • Creates and initializes a new PCB; Creates a new address space. • Copies entire content of parent’s address space into the child. • Initializes CPU (registers) & OS resources (open files) to a copy of the parent’s. • Places new PCB on ready queue. • copy state • Process calls fork() • Add to the end • of the queue • Ready queue

  36. UNIX fork Mechanism • fork() system call creates a new process. • This creates an exact duplicate of the parent process. • We have 2 separate address spaces that do not interrelate. • Anything I do in 1 address space does not affect the other (unless I use IPC).

  37. Process-related Unix Commands • pstree –p #print processes with the parent-child hierarchy. • ps #print snapshot of current processes started in this shell. • ps –A #print all processes regardless of who started, which shell started, etc. • ps –A | grep firefo #print all processes whose name has `firefo’ string. • ps –A | grep –v firefo #print all processes not containing`firefo’ string. • top #print all process with CPU/memo info; like Win.TaskManager • ./myprog & #start myprog as a new process in the background. • kill 777 #kill/terminate the process whose ID is 777.

  38. Process-related C Library • Dead simple fork() program: • Cooler ones in live coding session now. • Also check out: https://www.geeksforgeeks.org/fork-system-call/ and https://youtu.be/WcsZvdlLkPw • gedit myprog.c //include stdio.h and unistd.h in this one. • gcc myprog.c –o myprog • ./myprog void main() { int pid = fork(); if (pid == 0) print(“hello from child”); else print(“hello from parent”); }

  39. Process-related C Library • Parent process may wait (be suspended/blocked) for child to terminate (via wait() or waitpid(pidOfTheChildToWaitFor). • Parent process may terminate/kill a child process (it knows child’s pid after fork). • Parent process terminates, all children are terminated on some systms • Parent process terminates, children become orphan on some systems

  40. Process-related C Library • Zombie process: is a process that has completed execution but still has an entry in process table (ps). (occurs for child processes). • Entry needed to allow the parent process to read its child’s exit status. • read: exit status is read via wait() system call. After wait() is executed, zombie entry is removed from process table. This operation is called reaping. • What if parent does not reap? • Child will be reaped by init process. • When does parent not reap? • Terminates before child. • Forgets calling wait(), i.e., forgets doing reaping. • Don’t panic ‘cos init process does the reaping eventually. • wait() system call: suspends execution of the calling process until one of its children terminates. • waitpid() system call: suspends execution of the calling process until the child specified by pid terminates. • waitpid(-1, &status, 0); == wait(&status)

  41. Process-related C Library • wait(): synchronizing with children.

  42. Process-related C Library • wait(): get info about exit status via macros (upper cases below).

  43. Process-related C Library • waitpid(): suspends current process until specific process terminates.

  44. Process-related C Library • Any process, including a child process, may load a different program into its memory space (via exec() system call). • Replaces entire program code of the process w/ the program code from a different program • New programs are started by forking an existing process and exec’ing the new child process • execlp(“\bin\ls”, “ls”, NULL) • Does not fork a new process. • Rather replaces the address space & CPU state of the current process p. • Loads the new address space from executable file and starts it from main(). • When executable finishes, it does not return to the calling process p. • p is reaped, i.e., not a zombie. • So, to start a new program use fork() followed by exec() • which is what system() call does. • which is what init process does when you double click on Chrome browser. • There are variants of execlp() which again load and run programs.

  45. Process-related C Library • exec(): replacing current process with a new one.

  46. Process-related C Library • exec(): Linux process hierarchy. Can be seen via pstree command.

  47. Interprocess Communication (IPC) • Cooperating vs. independent. • By definition processes are isolated. • Own address space, own resources, etc. • They may need to communicate to do interesting stuff (serv-browser). • Info sharing. • Computation speedup. • Modularity (app is divided into sub-tasks). • cook cut vegetables • read newspaper

  48. Models of IPC • Shared memory. • Read write through memeory. • Shared board on the refrigerator. • Message passing. • send() and receive() messages. • Blocking/synchronous: • send(): postman knocks on your door and waits ‘till you open. • receive(): You (client) wait on the waiter (server) to order food. • Non-blocking/asynchronous: • send(): Lazy postman knocks and leaves. • receive(): You occasionally look for the waiter and order if he is there; otherwise continue chatting.

  49. Models of IPC

  50. Shared Memory • One process creates shared memory w/ unique address/key. • It and the other processes attach to this shared memory.

More Related