COMP 530: Operating Systems Basic OS Programming .

2y ago
9 Views
3 Downloads
3.54 MB
44 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Gideon Hoey
Transcription

COMP 530: Operating SystemsBasic OS ProgrammingAbstractions(and Lab 1 Overview)Don PorterPortions courtesy Kevin Jeffay1

COMP 530: Operating SystemsRecap We’ve introduced the idea of a process as acontainer for a running program This lecture: Introduce key OS APIs for a process– Some may be familiar from lab 0– Some will help with lab 1

COMP 530: Operating SystemsLab 1: A (Not So) Simple Shell Lab 0: Parsing for a shell– You will extend in lab 1 I’m giving you some boilerplate code that does basics My goal: Get some experience using process APIs– Most of what you will need discussed in this lecture You will incrementally improve the shell3

COMP 530: Operating SystemsTasks Turn input into commands; execute those commands– Support PATH variables Be able to change directoriesPrint the working directory at the command lineAdd debugging supportAdd scripting supportPipe indirection: , , and goheels – draw an ASCII art Tar HeelSignificant work – start early!4

COMP 530: Operating SystemsOutline Fork recapFiles and File HandlesInheritancePipesSocketsSignalsSynthesis Example: The Shell

COMP 530: Operating SystemsProcess Creation: fork/join in Linux The execution context for the child process is a copy ofthe parent’s context at the time of the callmain {int childPID;S1;fork()childPID fork();if(childPID 0) code for child process else { code for parent process wait();}S2;}CodechildPID 0DataStackParentCodeDatachildPID xxxStackChild

COMP 530: Operating SystemsProcess Creation: exec in Linux exec allows a process to replace itself with another program– (The contents of another binary file)foo:a.out:main {S’}main {S0exec(foo)S1S2}exec()CodeDataStackMemoryContext

COMP 530: Operating SystemsProcess Creation: Abstract fork in Linux Common case: fork followed by an execmain {int childPID;S1;fork()exec()childPID fork();if(childPID 0)exec(filename)else { code for parent process wait();}S2;}CodeCodeDataDataStackStackmain {S’}. /fooParentChild

COMP 530: Operating Systems2 Ways to Refer to a File Path, or hierarchical name, of the file– Absolute: “/home/porter/foo.txt” Starts at system root– Relative: “foo.txt” Assumes file is in the program’s current working directory Handle to an open file– Handle includes a cursor (offset into the file)

COMP 530: Operating SystemsPath-based calls Functions that operate on the directory tree– Rename, unlink (delete), chmod (change permissions), etc. Open – creates a handle to a file– int open (char *path, int flags, mode t mode); Flags include O RDONLY, O RDWR, O WRONLY Permissions are generally checked only at open– Opendir – variant for a directory

COMP 530: Operating SystemsHandle-based calls ssize t read (int fd, void *buf, size t count)– Fd is the handle– Buf is a user-provided buffer to receive count bytes of thefile– Returns how many bytes read ssize t write(int fd, void *buf, size t count)– Same idea, other direction int close (int fd)– Close an open file int lseek(int fd, size t offset, int flags)– Change the cursor position

COMP 530: Operating SystemsExamplePCchar buf[9];Awesome\0int fd open (“foo.txt”, O RDWR);bufssize t bytes read(fd, buf, 8);fd: 3if (bytes ! 8) // handle the errorbytes: 8lseek(3, 0, SEEK SET); //set cursormemcpy(buf, “Awesome”, 7);buf[7] ‘\0’;User-level stackbytes write(fd, buf, 8);KernelHandle 3if (bytes ! 8) // errorclose(fd);Contents\0AwesomeContentsfoo.txt

COMP 530: Operating SystemsWhy handles? Handles in Unix/Linux serve three purposes:1. Track the offset of last read/write–Alternative: Application explicitly passes offset2. Cache the access check from open()3. Hold a reference to a file–Unix idiom: Once a file is open, you can access thecontents as long as there is an open handle --- even if thefile is deleted from the directory13

COMP 530: Operating SystemsBut what is a handle? A reference to an open file or other OS object– For files, this includes a cursor into the file In the application, a handle is just an integer– This is an offset into an OS-managed table

andle indicesare processspecificDiskCOMP 530: Operating SystemsHandle TableProcess A PCB20Process B PCBHandleTableProcess C PCB

COMP 530: Operating SystemsHandle Recap Every process has a table of pointers to kernel handleobjects– E.g., a file handle includes the offset into the file and apointer to the kernel-internal file representation (inode) Applications can’t directly read these pointers– Kernel memory is protected– Instead, make system calls with the indices into this table– Index is commonly called a handle

COMP 530: Operating SystemsRearranging the table The OS picks which index to use for a new handle An application explicitly copy an entry to a specificindex with dup2(old, new)– Be careful if new is already in use

COMP 530: Operating SystemsOther useful handle APIs mmap() – can map part or all of a file into memory seek() – adjust the cursor position of a file– Like rewinding a cassette tape

COMP 530: Operating SystemsOutline Files and File HandlesInheritancePipesSocketsSignalsSynthesis Example: The Shell

COMP 530: Operating SystemsInheritance By default, a child process gets a reference to everyhandle the parent has open– Very convenient– Also a security issue: may accidentally pass something theprogram shouldn’t Between fork() and exec(), the parent has a chanceto clean up handles it doesn’t want to pass on– See also CLOSE ON EXEC flag

COMP 530: Operating SystemsStandard in, out, error Handles 0, 1, and 2 are special by convention– 0: standard input– 1: standard output– 2: standard error (output) Command-line programs use this convention– Parent program (shell) is responsible to useopen/close/dup2 to set these handles appropriatelybetween fork() and exec()

COMP 530: Operating SystemsExampleint pid fork();if (pid 0) {int input open (“in.txt”,O RDONLY);dup2(input, 0);exec(“grep”, “quack”);}//

COMP 530: Operating SystemsOutline Files and File HandlesInheritancePipesSocketsSignalsSynthesis Example: The Shell

COMP 530: Operating SystemsPipes FIFO stream of bytes between two processes Read and write like a file handle––––But not anywhere in the hierarchical file systemAnd not persistentAnd no cursor or seek()-ingActually, 2 handles: a read handle and a write handle Primarily used for parent/child communication– Parent creates a pipe, child inherits it

COMP 530: Operating SystemsExamplePCPCint pipe fd[2];int rv pipe(pipe fd);int pid fork();if (pid 0) {close(pipe fd[1]);dup2(pipe fd[0], 0);close(pipe fd[0]);exec(“grep”, “quack”);} else {close (pipe fd[0]);.WPCBHandle TableParentRChildGoal: Create a pipe; parent writes, child reads

COMP 530: Operating SystemsSockets Similar to pipes, except for network connections Setup and connection management is a bit trickier– A topic for another day (or class)

COMP 530: Operating SystemsSelect What if I want to block until one of several handleshas data ready to read? Read will block on one handle, but perhaps miss dataon a second Select will block a process until a handle has dataavailable– Useful for applications that use pipes, sockets, etc.

COMP 530: Operating SystemsOutline Files and File HandlesInheritancePipesSocketsSignalsSynthesis Example: The Shell

COMP 530: Operating SystemsSignals Similar concept to an application-level interrupt– Unix-specific (more on Windows later) Each signal has a number assigned by convention– Just like interrupts Application specifies a handler for each signal– OS provides default If a signal is received, control jumps to the handler– If process survives, control returns back to application

COMP 530: Operating SystemsSignals, cont. Can occur for:– Exceptions: divide by zero, null pointer, etc.– IPC: Application-defined signals (USR1, USR2)– Control process execution (KILL, STOP, CONT) Send a signal using kill(pid, signo)– Killing an errant program is common, but you can alsosend a non-lethal signal using kill() Use signal() or sigaction() to set the handler for asignal

COMP 530: Operating SystemsHow signals work Although signals appear to be deliveredimmediately – They are actually delivered lazily – Whenever the OS happens to be returning to the processfrom an interrupt, system call, etc. So if I signal another process, the other process maynot receive it until it is scheduled again Does this matter?

COMP 530: Operating SystemsMore details When a process receives a signal, it is added to apending mask of pending signals– Stored in PCB Just before scheduling a process, the kernel checks ifthere are any pending signals– If so, return to the appropriate handler– Save the original register state for later– When handler is done, call sigreturn() system call Then resume execution

COMP 530: Operating SystemsMeta-lesson Laziness rules!– Not on homework– But in system design Procrastinating on work in the system often reducesoverall effort– Signals: Why context switch immediately when it willhappen soon enough?

COMP 530: Operating SystemsLanguage Exceptions Signals are the underlying mechanism for Exceptionsand catch blocks JVM or other runtime system sets signal handlers– Signal handler causes execution to jump to the catch block

COMP 530: Operating SystemsWindows comparison Exceptions have specific upcalls from the kernel tontdll IPC is done using Events––––Shared between processesHandle in tableNo data, only 2 states: set and clearSeveral variants: e.g., auto-clear after checking the state

COMP 530: Operating SystemsOutline Files and File HandlesInheritancePipesSocketsSignalsSynthesis Example: The Shell

COMP 530: Operating SystemsShell Recap Almost all ‘commands’ are really binaries– /bin/ls Key abstraction: Redirection over pipes– ‘ ’, ‘ ‘, and ‘ ’implemented by the shell itself

COMP 530: Operating SystemsShell Example Ex: ls grep foo Shell pseudocde:thshfork()thshcshwhile(EOF ! read input) {parse input();// Sets up chain of pipes// Forks and exec’s ‘ls’ and ‘grep’ separately// Wait on output from ‘grep’, print to console// print console prompt}lsexec(ls)

COMP 530: Operating SystemsA note on Lab 1thshwait()fork()thshcshlsexec(ls) You’re going to be creating lots of processes in thisassignment If you fork a process and it never terminates You’ve just created a Z O M B I E P R O C E S S!!– Zombies will fill up the process table in the Linux kernel– Nobody can create a new process– This means no one can launch a shell to kill the zombies!

COMP 530: Operating SystemsA note on Lab 1thshwait()fork()thshcshlsexec(ls) Be safe! Limit the number of processes you can create– add the command “limit maxproc 10” to the file /.cshrc– (remember to delete this line at the end of the course!) Periodically check for and KILL! zombie processes– ps -ef egrep -e PID -e YOUR-LOGIN-NAME– kill pid-number Read the HW handout carefully for zombie-hunting details!

COMP 530: Operating SystemsWhat about Ctrl-Z? Shell really uses select() to listen for new keystrokes– (while also listening for output from subprocess) Special keystrokes are intercepted, generate signals– Shell needs to keep its own “scheduler” for backgroundprocesses– Assigned simple numbers like 1, 2, 3 ‘fg 3’ causes shell to send a SIGCONT to suspendedchild

COMP 530: Operating SystemsOther hints Splice(), tee(), and similar calls are useful forconnecting pipes together– Avoids copying data into and out-of application

COMP 530: Operating SystemsCollaboration Policy Reminder You can work alone or as part of a team– Must be the same as lab 0; may change starting in lab 2– Every line of code handed in must be written by one of thepair (or the boilerplate) No sharing code with other groups No code from Internet– Any other collaboration must be acknowledged in writing– High-level discussion is ok (no code) See written assignment and syllabus for more detailsNot following these rules is an Honor Code violation43

COMP 530: Operating SystemsSummary Understand how handle tables work– Survey basic APIs Understand signaling abstraction– Intuition of how signals are delivered Be prepared to start writing your shell in lab 1!

goheels–draw an ASCII art Tar Heel Significant work –start early! 4. COMP 530: Operating Systems Outline . Application specifies a handler for each signal –OS provides default If a signal is received, control jumps to the handler . Then resume execution. COMP 530: O

Related Documents:

The Invisible Man Two Feet Up, Two Feet Down Up, Up, and Away: The Story of Amelia Earhart We Like to Play! What Am I? . Bats and Other Animals with Amazing Ears Bigfoot False! Popular Myths Debunked First on the Moon Haunted Places Jane Goodall . 530 530 530 530 530 530 530 530 530 530 530 530

The Invisible Man The Very Big Potato Two Feet Up, Two Feet Down Up, Up, and Away: The Story of Amelia Earhart . The Spider and the Beehive Weird Science: How Freaky Animals Got That Way . 530 530 530 530 530 530 530 540 540 540 540 540 540 540 550 550 550 550 550 550 550 550 550 550 55

Company Script Read-Through 300-530 SEPT 28 Music Everyone 300-530 SEPT 29 Dance, Head-shots, Acting Class 300-530 SEPT 30 NO REHEARSAL 1 2 SIGN UP FOR REMIND 3 ACTING Principals Only pg. 37-50 300-530 4 EARLY DISMISSAL NO REHEARSAL 5 Music Everyone 300-530 6 Dance Team 300-530 7 NO REHEARSA

Song of St. Patrick – Haugen – G Comp II # 685 Taste and See – Haugen – G Comp II # 34 Taste and See – Moore – G Comp II # 827 The Love of The Lord – Joncas – G Comp II # 680 The Servant Song – Richard Gillard– – G Comp II # 661 We Have Been Told – Haas – G Comp II # 69

2016-17 HERI Faculty Survey Institutional Profile Report Full-time Undergraduate Faculty Total Men Women CIRP Construct Note: Significance * p .05, ** p .01, *** p .001 Page 1 of 76 1A. American University of Beirut Your Inst Comp 1 Comp 2 Your Inst Comp 1 Comp 2 Your Inst Comp 1 Comp 2

– If so, return to the appropriate handler – Save the original register state for later – When handler is done, call sigreturn() system call Then resume execution COMP 530: Operating Systems Meta-lesson Laziness rules! – Not on homework – But in system design Procrasti

12 530 02 38-77 1 fitting- fuel line 8 13 545 06 19-01 1 isolator filter housing 8 14 544 11 12-02 2 assy-oil/fuel cap w/ret. 8 15 530 01 59-06 2 screw m5.28x1.81x16mm plas 8 16 545 03 38-01 1 assy front isolator 8 17 577 54 06-01 1 rear isolator spring 8 18 530 09 56-46 1 assy- pick-up 19 530 02 11-06 1 line, fuel 8 20 530 01 58-14 1 screw .

WHAT IS MY SECOND GRADE STUDENT LEARNING IN MODULE 1? Wit & Wisdom is our English curriculum. It builds knowledge of key topics in history, science, and literature through the study of excellent texts. By reading and responding to stories and nonfiction texts, we will build knowledge of the following topics: Module 1: A Season of Change Module 2: American West Module 3: Civil Rights Heroes .