Operating Systems 2022F Lecture 7

From Soma-notes
Revision as of 16:21, 29 September 2022 by Soma (talk | contribs) (Created page with "==Video== Video from the lecture given on September 29, 2022 is now available: * [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec07-20220929.m4v video] * [https://homeostasis.scs.carleton.ca/~soma/os-2022f/lectures/comp3000-2022f-lec07-20220929.cc.vtt auto-generated captions] Video is also available through Brightspace (Resources->Zoom meeting->Cloud Recordings tab) ==Notes== <pre> Lecture 7 --------- Covered A1 solutions, walked throug...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Video

Video from the lecture given on September 29, 2022 is now available:

Video is also available through Brightspace (Resources->Zoom meeting->Cloud Recordings tab)

Notes

Lecture 7
---------

Covered A1 solutions, walked through gdb a bit

File descriptors
----------------

When you run "printf", what is that function doing?
 - it is writing to "standard out"
 - stdin, stdout, stderr are C concepts, supported on all platforms
   C runs on
 - but what is stdin, stdout, stderr on UNIX?
   (FILE * is not a thing on UNIX, it is a C thing)
 - but on UNIX, we have file descriptors
    - small numbers associated with open files

the system call open returns a file descriptor, and open, read, write, close
all take a file descriptor as an argument

So what is stdin, stdout, and stderr in terms of file descriptors?

0 = stdin
1 = stdout
2 = stderr

That's it

Note that a file descriptor is a reference to an OPEN file.  So someone has to open it.

When a process starts on UNIX, by convention file descriptors 0, 1, and 2 are valid.  0 can be read from, and 1 and 2 can be written to.  What they point to...can be any file!

(So what this means is that before you do an execve you make sure file descriptors 0, 1, and 2 point to where you want them for the program you're exec'ing)

If you don't do anything, the new program will just get the same file descriptors that you had (memory is wiped but files that were open stay open)

There is a maximum, it is defined by the kernel

Files are accessed through file descriptors
Most other I/O is also accessed through file descriptors, they just are
file descriptors referring to "special files" (i.e., device files)

/dev/tty, /dev/pts/0, /dev/null, etc are all special files

Normally open just opens the file on the next available file descriptor.  But you can use dup2 to copy it to another file descriptor (like 0, 1, or 2)