DOC PREVIEW
Stanford CS 140 - Lecture Notes

This preview shows page 1-2-3-21-22-23-43-44-45 out of 45 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 45 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

AdministriviaProcessesA process's view of the worldSpeedProcesses in the real 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)Why fork?Spawning process w/o forkImplementing processesProcess statesSchedulingScheduling policyPreemptionContext switchContext switch detailsThreadsWhy threads?Thread package APIKernel threadsWhy kernel threads suckUser threadsImplementing user-level threadsBackground: calling conventionsBackground: procedure callsThreads vs. proceduresExample user threads implementationi386 thread_md_switchWhy user threads suckUthreads on kthreadsProblemsLessonsAdministrivia• Lab 1 will be up by tomorrow, Due Oct. 11- Due at start of lecture – 4:15pm- Free extension to midnight if you come to lecture- Or for SCPD students only if you w atch lecture live•No credit for late assignments w/o extension- Ask course staff for an extension if you can’t finish on time- We are nice people- . . . but can’t help you if you miss deadlines and don’t talk to us• Section tomorrow, 4:15pm, this room• Pass/fail allowed, but not advisable- You must tell your team members if you are enrolled pass/fail- You will probably work almost as hard anyway– p.1/45Process e s• A process is an instance of a program running• Modern OSes run multiple processes simultaneously• Examples (can all run simultaneously):-g file_A.– compiler running on file A-g file_B.– compiler running on file B-emas– 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 latency– p.2/45A process’s view of the world• Each proc. Pihas own view of machine- Its own address space- Its own open files- Its own virtual CPU (through preeptivemultitasking)•*(har *)0x000different in P1& P2• Greatly simplifies programming model-gdoes not care thatfirefoxis running• Sometimes want interaction between processes- Simplest is through files:emasedits file,gcompiles it- More complicated: Shell/command, Window manager/app.– p.3/45Speed• Multiple processes can increase CPU utilization- Overlap one process’s computation with another’s w ait• 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-bound– p.4/45Process e s in the re al 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 w idget >> 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 people– p.5/45Inter-Process Communication• How can processes interact in real tim e?(a) By passing messages through the kernel(b) By sharing a region of physical memory(c) Through asynchronous sig nals or alerts– p.6/45Rest o f lecture• User view of processes- Crash course in basic Unix/Linux system call interface- How to create, kill, and communicate between processes• Kernel v ie w of process e s- Implementing processes in the kernel• Threads• How to implement threads– p.7/45UNIX files I/O• Applications “open” files (or devices) by name- I/O happens through open files•int open(har *path, int flags, /*mode*/...);-flags:O_RDONLY,O_WRONLY,O_RDWR-O_CREAT: create the file if non-existent-O_EXCL: (w.O_CREAT) create if file exists already-O_TRUNC: Truncate the file-O_APPEND: Start writing from end of file-mode: final argument withO_CREAT• Returns file descriptor—used for a ll I/O to file– p.8/45Error returns• What ifopenfails? Returns -1 (invalid fd)• Most s y s te m ca lls return -1 on failure- Specific kind of error in global interrno•#inlude <sys/errno.h>for possible values- 2 =ENOENT“No such file or directory”- 13 =EACCES“Permission Denied”•perrorfunction prints human-readable message-perror ("initfile");→ “initfile: No suh file or diretory”– p.9/45Operations o n 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•off_t lseek (int fd, off_t pos, int whene);-whene: 0 – start, 1 – current, 2 – end- Returns previous file offset, or -1 on error•int lose (int fd);– p.10/45File des criptor numbers• File descriptors a re inherited by proces s e s- When one process spawns another, same fds by default• Descriptors 0, 1, and 2 have special meaning- 0 – “standard input” (stdinin ANSI C)- 1 – “standard output” (stdout, printfin ANSI C)- 2 – “standard error” (stderr, perrorin ANSI C)- Normally all three attached to terminal• Example:type.- Prints the contents of a file tostdout– p.11/45type.voidtypefile (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);}– p.12/45Creating processes•int fork (void);- Create new process that is exact copy of current one- Returns process ID of new proc. 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 orWNOHANG- Returns process ID or -1 on error– p.13/45Deleting proc esses•void exit (int status);- Current process ceases to exist-statusshows up inwaitpid(shifted)- By convention,statusof 0 is success, non-zero error•int kill (int pid, int sig);- Sends signalsigto processpid-SIGTERMmost common value, kills process by default(but application can catch it for “cleanup”)-SIGKILLstronger, kills process always–


View Full Document

Stanford CS 140 - Lecture Notes

Documents in this Course
Homework

Homework

25 pages

Notes

Notes

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?