Recitation 9: Tshlab + VM

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213, Fall 06 Outline Shell Lab Processes Signals.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Recitation 8 (Nov. 1) Outline Lab 5 hints Virtual Memory Reminder Shell Lab: Due THIS Thursday TA: Kun Gao Modified from Minglong Shao’s Recitation, Fall.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Project 1. Process ID (pid) Synopsis #include pid_t getpid(void) – returns the pid of the currently running process. pid_t getppid(void) – returns the.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
CSE 451 Section 4 Project 2 Design Considerations.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Operating Systems Process Creation
Lecture 3 Introduction to Unix Systems Programming: Unix File I/O System Calls.
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Carnegie Mellon Introduction to Computer Systems / Spring 2009 March 23, 2009 Virtual Memory.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Overview Dr. Xiao Qin Auburn University
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Error handling I/O Man pages
Week 3 Redirection, Pipes, and Background
Today topics: File System Implementation
Operating Systems Moti Geva
Week 12 - Wednesday CS222.
UNIX PROCESSES.
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Week 4 - Monday CS222.
Nikhil, Kashish, Krishanu, Stan
Operating System Hebrew University Spring 2004
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
LINUX System Programming with Processes (additional)
תרגול 8 – ק/פ ותקשורת תהליכים ב-Linux
Recitation 8 Processes, Signals, Tshlab
CSE 333 – Section 3 POSIX I/O Functions.
Recitation 9: Processes, Signals, TSHLab
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Process Programming Interface
I/O and Process Management
CSE 333 – Section 3 POSIX I/O Functions.
IPC Prof. Ikjun Yeom TA – Hoyoun
Advanced UNIX progamming
CSE 333 – Section 3 POSIX I/O Functions.
CSCI 380: Operating Systems William Killian
FILE I/O File Descriptors I/O Efficiency File Sharing
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Week 12 - Wednesday CS222.
Recitation 9: Processes, Signals, TSHLab
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Recitation 9: Tshlab + VM Instructor: TAs 29 October 2018

Outline Labs Signals IO Virtual Memory mention status of regrade requests

tshlab and malloclab tshlab due Tuesday malloclab is released immediately after Start early Do the checkpoint first, don’t immediately go for the final Expect a recitation next week Working for several hours will improve the value significantly

Signals Parent process sends SIGINT to a child process. What is the behavior of the child? What is the default? What else could the child do? Child can block / ignore this signal. Keep this in mind for tshlab.

More Signals Parent process sends SIGKILL to a child process. What is the behavior of the child? What is the default? What else could the child do? SIGKILL cannot be blocked. Child is (eventually) terminated.

Sending Signals Parent sends SIGKILL to a child process. ... pid_t pid = ...; // child pid kill(pid, SIGKILL); // At this point, what could have // happened to the child process? Answer: we don’t know. Perhaps SIGKILL has been delivered, or it might not have been delivered.

Blocking Signals The shell is currently running its handler for SIGCHLD. What signals can it receive? What signals can it not receive (i.e., blocked)? When the handler for a particular signal is invoked, that signal is automatically blocked until the handler returns. Other signals can arrive and interupt the handler

Errno Included from <errno.h> Global integer variable – usually 0 When a system call fails (usually indicated by returning -1), it also will set errno to a value describing what went wrong Example: let’s assume there is no “foo.txt” in our path int fd = open(”foo.txt”, O_RDONLY); if(fd < 0) printf(“%d\n”, errno); The code above will print 2 – in the man pages, we can see that 2 is ENOENT “No such file or directory” In shell lab, your signal handlers must preserve errno Elaborate a little bit on what “preserve errno” means and tell the students your favorite error number to build rapport

IO functions Needed for tshlab Needed for life int open(const char *pathname, int flags); Some important flags: O_CREAT – creates file if needed, opens for read/write O_RDWR – opens for read/write O_RDONLY – opens for read only Various permission modes int close(int fd); int dup2(int oldfd, int newfd); Needed for life ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); off_t lseek(int fd, off_t offset, int whence);

More on open int open(const char *pathname, int flags, mode_t mode); For flags, you can pass a bitwise-OR of one or more flags Three kinds of flags (we only discuss the important ones) Access modes (one of them must be included): O_RDONLY, O_WRONLY, O_RDWR File creation flags: O_CREAT, O_TRUNC, etc. File status flags

Access mode flags and file creation flags O_RDONLY Open the file read-only. O_WRONLY Open the file write-only. O_RDWR Open the file read/write. O_CREAT If the provided pathname does not exist, create it as a regular file. O_TRUNC If the file already exists and if the access mode allows writing (i.e. is O_RDWR or O_WRONLY), then the file will be truncated to length 0.

More on open int open(const char *pathname, int flags, mode_t mode); For mode, you can pass a bitwise-OR of one or more constants Specifies, when creating a file, what permission the file will be created with Only useful when flags contain O_CREAT (or O_TMPFILE)

Linux permissions Every file and directory has permission information You’ve seen it before ls -l prints the permissions for each file/directory like: -rw-r--r-- ... drwxr-xr-x ... chmod changes the permissions for files/directories $ chmod -R 777 / There are read (R), write (W) and executable (X) permissions for user (USR), group (GRP) and other (OTH)

Specify permissions in open() Read (R) Write (W) Executable (X) All (RWX) User (USR) S_IRUSR S_IWUSR S_IXUSR S_IRWXU Group (GRP) S_IRGRP S_IWGRP S_IXGRP S_IRWXG Other (OTH) S_IROTH S_IWOTH S_IXOTH S_IRWXO These constants can be bitwise-OR’d and passed to the third argument of open() What does S_IRWXG | S_IXUSR | S_IXOTH mean? How to create a file which everyone can read from but only the user can write to it or execute it?

File descriptors fd 1 2 open file table Standard input Standard output Standard error stdin, stdout, stderr are opened automatically and closed by normal termination or exit()

open(“foo.txt”) fd 1 open file table 2 Standard input 3 1 2 3 open file table Standard input Standard output Standard error foo.txt

Each call to open() creates a new open file description open(“foo.txt”) inode table foo.txt fd 1 2 3 4 open file table Standard input Standard output Standard error foo.txt Each call to open() creates a new open file description

dup2(STDOUT_FILENO, 3) Closed silently fd 1 open file table 2 1 2 3 4 open file table Standard input Standard output Standard error foo.txt Closed silently

IO and Fork() File descriptor management can be tricky. How many file descriptors are open in the parent process at the indicated point? How many does each child have open at the call to execve? int main(int argc, char** argv) { int i; for (i = 0; i < 4; i++) int fd = open(“foo”, O_RDONLY); pid_t pid = fork(); if (pid == 0) int ofd = open(“bar”, O_RDONLY); execve(...); } // How many file descriptors are open in the parent?

Redirecting IO File descriptors can be directed to identify different open files. int main(int argc, char** argv) { int i; for (i = 0; i < 4; i++) { int fd = open(“foo”, O_RDONLY); pid_t pid = fork(); if (pid == 0) int ofd = open(“bar”, O_WRONLY); dup2(fd, STDIN_FILENO); dup2(ofd, STDOUT_FILENO); execve(...); } // How many file descriptors are open in the parent? Bonus question: how many fds are open in the last forked child?

Redirecting IO At the two points (A and B) in main, how many file descriptors are open? int main(int argc, char** argv) { int i, fd; fd = open(“foo”, O_WRONLY); dup2(fd, STDOUT_FILENO); // Point A close(fd); // Point B ...

Memory Access The processor tries to write to a memory address. List different steps that are required to complete this operation.

Memory Access The processor tries to write to a memory address. List different steps that are required to complete this operation. (non exhaustive list) Virtual to physical address conversion (TLB lookup) TLB miss Page fault, page loaded from disk TLB updated, check permissions L1 Cache miss (and L2 … and) Request sent to memory Memory sends data to processor Cache updated

Address Translation with TLB Translate 0x15213, given the contents of the TLB and the first 32 entries of the page table below. 1MB Virtual Memory 256KB Physical Memory 4KB page size VPN PPN Valid 00 17 1 10 26 01 28 11 02 14 12 0E 03 0B 13 04 05 15 18 06 0F 16 31 07 08 1C 23 09 25 19 0A 1A 0C 1B 2B 1E 0D 1D 3E 27 1F Index Tag PPN Valid 05 13 1 3F 15 10 0F 1E 2 1F 01 11 3 03 2B 1D 23 2-way set associative Make a joke about how despite the speech bubble TLBs can’t talk

If you get stuck on tshlab Read the writeup! Do manual unit testing before runtrace and sdriver! Post private questions on piazza! Read the man pages on the syscalls. Especially the error conditions What errors should terminate the shell? What errors should be reported?

man wait Taken from http://man7.org/linux/man-pages/man2/wait.2.html WAIT(2) Linux Programmer's Manual WAIT(2) NAME wait, waitpid, waitid - wait for process to change state SYNOPSIS #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *wstatus); pid_t waitpid(pid_t pid, int *wstatus, int options); int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options); /* This is the glibc and POSIX interface; see NOTES for information on the raw system call. */

man pages (probably) cover all you need What arguments does the function take? read SYNOPSIS What does the function do? read DESCRIPTION What does the function return? read RETURN VALUE What errors can the function fail with? read ERRORS Is there anything I should watch out for? read NOTES Different categories for man page entries with the same name Looking up man pages online is not an academic integrity violation

Function arguments Should I do dup2(old, new) or dup2(new, old)? Read the man page: $ man dup2 SYNOPSIS #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd);

Function behavior How should I write my format string when I need to print a long double in octals with precision 5 and zero-padded? Read the man page: $ man printf DESCRIPTION Flag characters The character % is followed by zero or more of the following flags: # The value should be converted... 0 The value should be zero padded... - The converted value is to be left adjusted... ' ' (a space) A blank should be left before... + A sign (+ or -) should always ...

Function return What does waitpid() return with and without WNOHANG? Read the man page: $ man waitpid RETURN VALUE waitpid(): on success, returns the process ID of the child whose state has changed; if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned. On error, -1 is returned. Each of these calls sets errno to an appropriate value in the case of an error.

Potential errors How should I check waitpid for errors? Read the man page: $ man waitpid ERRORS ECHILD (for waitpid() or waitid()) The process specified by pid (waitpid()) or idtype and id (waitid()) does not exist or is not a child of the calling process. (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.) EINTR WNOHANG was not set and an unblocked signal or a SIGCHLD was caught; see signal(7). EINVAL The options argument was invalid.

Get advice from the developers I sprintf from a string into itself, is this okay? Read the man page: $ man sprintf NOTES Some programs imprudently rely on code such as the following sprintf(buf, "%s some further text", buf); to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(), vsprintf(), and vsnprintf(). Depending on the version of gcc(1) used, and the compiler options employed, calls such as the above will not produce the expected results. The glibc implementation of the functions snprintf() and vsnprintf() conforms to the C99 standard, that is, behaves as described above, since glibc version 2.1. Until glibc 2.0.6, they would return -1 when the output was truncated.