Slide 1: Lecture 01: Welcome to CS110: Principles of Computer Systems

Principles of Computer Systems

Winter 2020

Stanford University

Computer Science Department

Instructors:

Chris Gregg and Nick Troccoli

PDF of this presentation

 

Slide 2: Lecture 01: Welcome to CS110: Principles of Computer Systems

Slide 3: Lecture 01: Welcome to CS110: Principles of Computer Systems

cgregg@myth55:/usr/class/archive/cs/cs110/cs110.1202$ ls -1
ARCHIVE.README
cgi-bin
final-tests
include
lecture-examples
lib
local
main.cgi
private_data
repos
samples
staff
tools
WWW

Slide 4: Lecture 01: Welcome to CS110: Principles of Computer Systems

cgregg@myth55:/usr/class/archive/cs/cs110/cs110.1202$ ls -1
ARCHIVE.README
cgi-bin
final-tests
include
lecture-examples
lib
local
main.cgi
private_data
repos
samples
staff
tools
WWW

Slide 5: Lecture 01: Welcome to CS110: Principles of Computer Systems

ls -1 -i
504030014 ARCHIVE.README
503231001 cgi-bin
503723839 final-tests
503186329 include
503185617 lecture-examples
503186393 lib
503186405 local
504453014 main.cgi
503231019 private_data
503192313 repos
503216939 samples
503216981 staff
503230523 tools
503185411 WWW

Slide 6: Lecture 01: Welcome to CS110: Principles of Computer Systems

ls -1 -i
504030014 ARCHIVE.README
503231001 cgi-bin
503723839 final-tests
503186329 include
503185617 lecture-examples
503186393 lib
503186405 local
504453014 main.cgi
503231019 private_data
503192313 repos
503216939 samples
503216981 staff
503230523 tools
503185411 WWW

Slide 7: Lecture 01: Welcome to CS110: Principles of Computer Systems: Instructors

Slide 8: Lecture 01: Welcome to CS110: Principles of Computer Systems: Instructors

Slide 9: Lecture 01: Welcome to CS110: Principles of Computer Systems

Slide 10: Lecture 01: Welcome to CS110: Principles of Computer Systems

Slide 11: Lecture 01: Welcome to CS110: Principles of Computer Systems

Slide 12: Lecture 01: Welcome to CS110: Principles of Computer Systems

Slide 13: Lecture 01: Welcome to CS110: Principles of Computer Systems

 

Slide 14: Lecture 01: Welcome to CS110: Principles of Computer Systems

Slide 15: Course Syllabus

Slide 16: Course Syllabus

Slide 17: Course Syllabus

Slide 18: Course Expectations

Slide 19: Course Expectations

Slide 20: Course Expectations

Slide 21: Course Expectations

Slide 22: Honor Code

Slide 23: Introduction to UNIX Filesystems

cgregg@myth58:~/cs110/spring-2019/lecture-examples/filesystems$ ls
alphabet.txt  contains.c  copy.c  list.c  Makefile  search.c  t.c  vowels.txt
ls -al
total 23
drwx------  2 cgregg operator 2048 Mar 29 12:33 .
drwx------ 10 cgregg operator 2048 Mar 29 12:33 ..
-rw-------  1 cgregg operator   27 Mar 29 12:33 alphabet.txt
-rw-------  1 cgregg operator 2633 Mar 29 12:33 contains.c
-rw-------  1 cgregg operator 1882 Mar 29 12:33 copy.c
-rw-------  1 cgregg operator 5795 Mar 29 12:33 list.c
-rw-------  1 cgregg operator  628 Mar 29 12:33 Makefile
-rw-------  1 cgregg operator 2302 Mar 29 12:33 search.c
-rw-------  1 cgregg operator 1321 Mar 29 12:33 t.c
-rw-------  1 cgregg operator    6 Mar 29 12:33 vowels.txt

Slide 24: Introduction to UNIX Filesystems

$ ls -l list
-rwxr-xr-x 1 cgregg operator 19824 Mar 29 12:47 list

 owner

               group 

                             other

In this case, the owner has read, write, and execute permissions, the group has only read and execute permissions, and the user also has only read and execute permissions.

Slide 25: Introduction to UNIX Filesystems

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);


We will generally only care about the following other flags when creating a file:

Slide 26: Introduction to UNIX Filesystems

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
    mode_t old_mask = umask(0); // set to 0, but get old mask as return value
    umask(old_mask); // restore to original
    printf("umask is set to %03o\n",old_mask);
    return 0;
}
$ gcc show_umask.c -o show_umask
$ ./show_umask
umask is set to 077

Slide 27: Introduction to UNIX Filesystems

Slide 28: Introduction to UNIX Filesystems

#include <fcntl.h>    // for open
#include <unistd.h>   // for read, write, close
#include <stdio.h>
#include <sys/types.h> // for umask
#include <sys/stat.h>  // for umask
#include <errno.h>

const char *kFilename = "my_file";
const int kFileExistsErr = 17;
int main() {
    umask(0); // set to 0 to enable all permissions to be set
    int file_descriptor = open(kFilename, O_WRONLY | O_CREAT | O_EXCL, 0644);
    if (file_descriptor == -1) {
        printf("There was a problem creating '%s'!\n",kFilename);
        if (errno == kFileExistsErr) {
            printf("The file already exists.\n");
        } else {
            printf("Unknown errorno: %d\n",errno);
        }
        return -1;
    }
    close(file_descriptor);
    return 0;
}
$ make open_ex
cc     open_ex.c   -o open_ex
$ ./open_ex
$ ls -l my_file
-rw-r--r-- 1 cgregg operator 0 Mar 31 13:29 my_file

Slide 29: UNIX Filesystem APIs

Slide 30: Implementing copy to emulate cp

Slide 31: Implementing copy to emulate cp

int main(int argc, char *argv[]) {
  int fdin = open(argv[1], O_RDONLY);
  int fdout = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0644);
  char buffer[1024];
  while (true) {
    ssize_t bytesRead = read(fdin, buffer, sizeof(buffer));
    if (bytesRead == 0) break;
    size_t bytesWritten = 0;
    while (bytesWritten < bytesRead) {
      bytesWritten += write(fdout, buffer + bytesWritten, bytesRead - bytesWritten);
    }
  }
  close(fdin); 
  close(fdout);
  return 0;
}
ssize_t read(int fd, void *buf, size_t count);

ssize_t write(int fd, const void *buf, size_t count);

Slide 32: Implementing copy to emulate cp

int main(int argc, char *argv[]) {
  int fdin = open(argv[1], O_RDONLY);
  int fdout = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0644);
  char buffer[1024];
  while (true) {
    ssize_t bytesRead = read(fdin, buffer, sizeof(buffer));
    if (bytesRead == 0) break;
    size_t bytesWritten = 0;
    while (bytesWritten < bytesRead) {
      bytesWritten += write(fdout, buffer + bytesWritten, bytesRead - bytesWritten);
    }
  }
  close(fdin); 
  close(fdout)
  return 0;
}

Slide 33: Pros and cons of file descriptors over FILE pointers and C++ iostreams

Slide 34: Implementing t to emulate tee

$ cat alphabet.txt | ./tee one.txt two.txt three.txt
abcdefghijklmnopqrstuvwxyz
$  cat one.txt 
abcdefghijklmnopqrstuvwxyz
$  cat two.txt
abcdefghijklmnopqrstuvwxyz
$  diff one.txt two.txt
$  diff one.txt three.txt
$
$ cat vowels.txt | ./tee one.txt
aeiou
$  cat one.txt 
aeiou

Source: https://commons.wikimedia.org/wiki/File:Tee.svg

Slide 35: Implementing t to emulate tee

int main(int argc, char *argv[]) {
  int fds[argc];
  fds[0] = STDOUT_FILENO;
  for (size_t i = 1; i < argc; i++)
    fds[i] = open(argv[i], O_WRONLY | O_CREAT | O_TRUNC, 0644);

  char buffer[2048];
  while (true) {
    ssize_t numRead = read(STDIN_FILENO, buffer, sizeof(buffer));
    if (numRead == 0) break;
    for (size_t i = 0; i < argc; i++) writeall(fds[i], buffer, numRead);
  }

  for (size_t i = 1; i < argc; i++) close(fds[i]);
  return 0;
}

static void writeall(int fd, const char buffer[], size_t len) {
  size_t numWritten = 0;
  while (numWritten < len) {
    numWritten += write(fd, buffer + numWritten, len - numWritten);
  }
}