AdministriviaProcessesSpeedProcesses in the real worldA process's view of the worldInter-Process CommunicationRest of lectureUNIX files I/OError returnsOperations on file descriptorsFile descriptor numbers exttt {type.c}Creating processesDeleting processesRunning programs exttt {minish.c} (simplified)Manipulating file descriptors exttt {redirsh.c}Pipes exttt {pipesh.c} (simplified)hypertarget {wfork}{Why fork?}Spawning process w/o forkImplementing processesProcess statesSchedulingScheduling policyPreemptionContext switchContext switch detailsThreadsWhy threads?Thread package APIKernel threadsLimitations of kernel-level threadsUser threadsImplementing user-level threadsBackground: calling conventionsBackground: procedure callsThreads vs. proceduresExample user threads implementationi386 thread_md_switchi386 thread_md_switchi386 thread_md_switchi386 thread_md_switchi386 thread_md_switchi386 thread_md_switchLimitations of user-level threadsUser threads on kernel threadsLimitations of $n:m$ threadingLessonsAdministrivia• Lab 1 is up, Due Thursday, Jan. 21- Due at start of lecture – 4:15pm- Free extension to midnight if you come to lecture- Or for SCPD students if y ou watch lecture that day• Ask cs140-staff for extension if you can’t finish- Tell u s where you are with the project- And how much longer you need to finish•No credit for late assignments w/o extension• Section tomorrow, 3:15pm (here)1/45Processes• A process is an instance of a program running• Modern OSes run multiple processes simultaneously• Examples (can all run simultaneously):- gcc fileA.c – compiler running on file A- gcc fileB.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- Higher throughput (better CPU utilization), lower latency2/45Speed• Multiple processes can increase CPU utilization- Overlap one process’s computation with another’s wait• Multiple processes can reduce latency- Running A then B requires 100 sec for B to complete- Running A and B concurrently makes B finish faster- A slightly slower, but less than 100 sec unless A and B bothcompletely CPU-bound3/45Processes in the real world• Processes, parallelism fact of life much longer thanOSes have been around- E.g., say takes 1 worker 10 months to make 1 widget- Company may hire 100 workers to make 10,000 widgets- Latency for first widget >> 1/10 month- Throughput may be < 10 widgets per month(if can’t perfectly parallelize task)- Or > 10 widgets per month if better utilization (e.g., 100workers on 10,000 widgets never idly waiting for paint to dry)• You will see this with Pintos- Don’t expect labs to take 1/3 time with three people4/45A 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 preemptivemultitasking)• *(char *)0xc000 different in P1& P2• Greatly 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.5/45Inter-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 alerts6/45Rest of lecture• User view of processes- Crash course in basic Unix/Linux system call interface- How to create, kill, and communicate between processes• Kernel view of processes- Implementing processes in the kernel• Threads• How to implement threads7/45UNIX files I/O• Applications “open” files (or devices) by name- I/O happens through open files• int open(char *path, int flags, /*mode*/...);- flags: ORDONLY, O WRONLY, O RDWR- OCREAT: create the file if non-existent- OEXCL: (w. O CREAT) create if file exists already- OTRUNC: Truncate the file- OAPPEND: Start writing from end of file- mode: final argument with OCREAT• Returns file descriptor—used for all I/O to file8/45Error returns• What if open fails? Returns -1 (invalid fd)• Most system calls return -1 on failure- Specific kind of error in global int errno• #include <sys/errno.h> for possible va lues- 2 = ENOENT “No such file or directory”- 13 = EACCES “Permission Denied”• perror function prints human-readable message- perror ("initfile");→ “initfile: No such file or directory”9/45Operations on file descriptors• int read (int fd, void *buf, int nbytes);- Returns number of bytes read- Returns 0 bytes at end of file, or -1 on error• int write (int fd, void *buf, int nbytes);- Returns number of bytes written, -1 on error• offt lseek (int fd, off t pos, int whence);- whence: 0 – start, 1 – current, 2 – end⊲ Returns previous file offset, or -1 on error• int close (int fd);10/45File descriptor numbers• File descriptors are inherited by processes- When one process spawns another, same fds by default• Descriptors 0, 1, and 2 have special meaning- 0 – “standard input” (stdin in ANSI C)- 1 – “standard output” (stdout, printf in ANSI C)- 2 – “standard error” (stderr, perror in ANSI C)- Normally all three attached to terminal• Example: type.c- Prints the contents of a file to stdout11/45type.cvoidtypefile (char *filename){int fd, nread;char buf[1024];fd = open (filename, O_RDONLY);if (fd == -1) {perror (filename);return;}while ((nread = read (fd, buf, sizeof (buf))) > 0)write (1, buf, nread);close (fd);}12/45Creating processes• 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 error13/45Deleting processes• void exit (int status);- Current process ceases to exist- status shows up in waitpid (shifted)- 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 always14/45Running programs• int execve (char *prog, char **argv, char **envp);- prog – full
View Full Document