processes
play

Processes A process is an instance of a program running Modern OSes - PowerPoint PPT Presentation

Processes A process is an instance of a program running Modern OSes run multiple processes simultaneously Examples (can all run simultaneously): - gcc file_A.c compiler running on file A - gcc file_B.c compiler running on file B


  1. Processes • A process is an instance of a program running • Modern OSes run multiple processes simultaneously • Examples (can all run simultaneously): - gcc file_A.c – compiler running on file A - gcc file_B.c – compiler running on file B - emacs – text editor - firefox – web browser • Non-examples (implemented as one process): - Multiple firefox windows or emacs frames (still one process) • Why processes? - Simplicity of programming - Speed: Higher throughput, lower latency 1 / 40

  2. Speed • Multiple processes can increase CPU utilization - Overlap one process’s computation with another’s wait emacs wait for input wait for input gcc • Multiple processes can reduce latency - Running A then B requires 100 sec for B to complete 80s 20s A B - Running A and B concurrently makes B finish faster A B - A is slower than if it had whole machine to itself, but still < 100 sec unless both A and B completely CPU-bound 2 / 40

  3. A process’s view of the world • Each process has own view of machine - Its own address space - Its own open files - Its own virtual CPU (through preemptive multitasking) • *(char *)0xc000 different in P 1 & P 2 • Simplifies programming model - gcc does not care that firefox is running • Sometimes want interaction between processes - Simplest is through files: emacs edits file, gcc compiles it - More complicated: Shell/command, Window manager/app. 3 / 40

  4. Inter-Process Communication • How can processes interact in real time? (a) By passing messages through the kernel (b) By sharing a region of physical memory (c) Through asynchronous signals or alerts 4 / 40

  5. Outline (UNIX-centric) User view of processes 1 2 Kernel view of processes 3 Threads 4 Thread implementation details 5 / 40

  6. Creating processes • Original UNIX paper is a great reference on core system calls • int fork (void); - Create new process that is exact copy of current one - Returns process ID of new process in “parent” - Returns 0 in “child” • int waitpid (int pid, int *stat, int opt); - pid – process to wait for, or -1 for any - stat – will contain exit value, or signal - opt – usually 0 or WNOHANG - Returns process ID or -1 on error 6 / 40

  7. Deleting processes • void exit (int status); - Current process ceases to exist - status shows up in waitpid (shifed) - By convention, status of 0 is success, non-zero error • int kill (int pid, int sig); - Sends signal sig to process pid - SIGTERM most common value, kills process by default (but application can catch it for “cleanup”) - SIGKILL stronger, kills process always 7 / 40

  8. Running programs • int execve (char *prog, char **argv, char **envp); - prog – full pathname of program to run - argv – argument vector that gets passed to main - envp – environment variables, e.g., PATH , HOME • Generally called through a wrapper functions - int execvp (char *prog, char **argv); Search PATH for prog, use current environment - int execlp (char *prog, char *arg, ...); List arguments one at a time, finish with NULL • Example: minish.c - Loop that reads a command, then executes it • Warning: Pintos exec more like combined fork/exec 8 / 40

  9. minish.c (simplified) pid_t pid; char **av; void doexec () { execvp (av[0], av); perror (av[0]); exit (1); } /* ... main loop: */ for (;;) { parse_next_line_of_input (&av, stdin); switch (pid = fork ()) { case -1: perror ("fork"); break; case 0: doexec (); default: waitpid (pid, NULL, 0); break; } } 9 / 40

  10. Manipulating file descriptors • int dup2 (int oldfd, int newfd); - Closes newfd , if it was a valid descriptor - Makes newfd an exact copy of oldfd - Two file descriptors will share same offset ( lseek on one will affect both) • int fcntl (int fd, F_SETFD, int val) - Sets close on exec flag if val = 1, clears if val = 0 - Makes file descriptor non-inheritable by spawned programs • Example: redirsh.c - Loop that reads a command and executes it - Recognizes command < input > output 2> errlog 10 / 40

  11. redirsh.c void doexec (void) { int fd; if (infile) { /* non-NULL for "command < infile" */ if ((fd = open (infile, O_RDONLY)) < 0) { perror (infile); exit (1); } if (fd != 0) { dup2 (fd, 0); close (fd); } } /* ... do same for outfile → fd 1, errfile → fd 2 ... */ execvp (av[0], av); perror (av[0]); exit (1); } 11 / 40

  12. Pipes • int pipe (int fds[2]); - Returns two file descriptors in fds[0] and fds[1] - Data written to fds[1] will be returned by read on fds[0] - When last copy of fds[1] closed, fds[0] will return EOF - Returns 0 on success, -1 on error • Operations on pipes - read / write / close – as with files - When fds[1] closed, read(fds[0]) returns 0 bytes - When fds[0] closed, write(fds[1]) : ⊲ Kills process with SIGPIPE ⊲ Or if signal ignored, fails with EPIPE • Example: pipesh.c - Sets up pipeline command1 | command2 | command3 ... 12 / 40

  13. pipesh.c (simplified) void doexec (void) { while (outcmd) { int pipefds[2]; pipe (pipefds); switch (fork ()) { case -1: perror ("fork"); exit (1); case 0: dup2 (pipefds[1], 1); close (pipefds[0]); close (pipefds[1]); outcmd = NULL; break; default: dup2 (pipefds[0], 0); close (pipefds[0]); close (pipefds[1]); parse_command_line (&av, &outcmd, outcmd); break; } } . . . 13 / 40

  14. Why fork? • Most calls to fork followed by execve • Could also combine into one spawn system call (like Pintos exec ) • Occasionally useful to fork one process - Unix dump utility backs up file system to tape - If tape fills up, must restart at some logical point - Implemented by forking to revert to old state if tape ends • Real win is simplicity of interface - Tons of things you might want to do to child: Manipulate file descriptors, set environment variables, reduce privileges, ... - Yet fork requires no arguments at all 14 / 40

  15. Spawning a process without fork • Without fork, needs tons of different options for new process • Example: Windows CreateProcess system call - Also CreateProcessAsUser , CreateProcessWithLogonW , CreateProcessWithTokenW , ... BOOL WINAPI CreateProcess( _In_opt_ LPCTSTR lpApplicationName, _Inout_opt_ LPTSTR lpCommandLine, _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, _In_ BOOL bInheritHandles, _In_ DWORD dwCreationFlags, _In_opt_ LPVOID lpEnvironment, _In_opt_ LPCTSTR lpCurrentDirectory, _In_ LPSTARTUPINFO lpStartupInfo, _Out_ LPPROCESS_INFORMATION lpProcessInformation ); 15 / 40

  16. Outline (UNIX-centric) User view of processes 1 2 Kernel view of processes 3 Threads 4 Thread implementation details 16 / 40

  17. Implementing processes • Keep a data structure for each process - Process Control Block (PCB) - Called proc in Unix, task_struct in Linux, Process state and just struct thread in Pintos Process ID • Tracks state of the process User id, etc. Program counter - Running, ready (runnable), waiting, etc. Registers • Includes information necessary to run Address space - Registers, virtual memory mappings, etc. (VM data structs) - Open files (including memory mapped files) Open files • Various other data about the process PCB - Credentials (user/group ID), signal mask, controlling terminal, priority, accounting statistics, whether being debugged, which system call binary emulation in use, ... 17 / 40

  18. Process states scheduler new terminated admitted exit dispatch ready running interrupt I/O or event I/O or event wait completion waiting • Process can be in one of several states - new & terminated at beginning & end of life - running – currently executing (or will execute on kernel return) - ready – can run, but kernel has chosen different process to run - waiting – needs async event (e.g., disk operation) to proceed • Which process should kernel run? - if 0 runnable, run idle loop (or halt CPU), if 1 runnable, run it - if > 1 runnable, must make scheduling decision 18 / 40

  19. Scheduling • How to pick which process to run • Scan process table for first runnable? - Expensive. Weird priorities (small pids do better) - Divide into runnable and blocked processes • FIFO? - Put threads on back of list, pull them from front: t 1 t 2 t 3 t 4 head tail - Pintos does this—see ready_list in thread.c • Priority? - Give some threads a better shot at the CPU 19 / 40

  20. Scheduling policy • Want to balance multiple goals - Fairness – don’t starve processes - Priority – reflect relative importance of procs - Deadlines – must do X (play audio) by certain time - Throughput – want good overall performance - Efficiency – minimize overhead of scheduler itself • No universal policy - Many variables, can’t optimize for all - Conflicting goals (e.g., throughput or priority vs. fairness) • We will spend a whole lecture on this topic 20 / 40

  21. Preemption • Can preempt a process when kernel gets control • Running process can vector control to kernel - System call, page fault, illegal instruction, etc. - May put current process to sleep—e.g., read from disk - May make other process runnable—e.g., fork, write to pipe • Periodic timer interrupt - If running process used up quantum, schedule another • Device interrupt - Disk request completed, or packet arrived on network - Previously waiting process becomes runnable - Schedule if higher priority than current running proc. • Changing running process is called a context switch 21 / 40

  22. Context switch 22 / 40

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend