You wake up, segfaults
Post up, segfaults
Ride round in it, segfaults
Flossin on that, segfaults
– Stefan Dierauf
struct Pair {
int x, y;
};
void foo(LinkedList list, struct Pair p) {
list->payload = &p;
}
p
is passed by value; its lifetime ends when foo
exits
list->payload
becomes a dangling pointer
valgrind is your friend
acknowledge those with whom you discuss problems
We never learned anything about how to use typedef, makefiles, or several other things that were then required as part of the exercises. I’ve had to do extensive google-ing for nearly every exercise just to learn how to do the things that were asked. If you expect us to know information, it should be covered in lecture/section, or there should at least be a comprehensive tutorial attached to the exercise.
FILE
Amazon Cloud Goes Down Friday Night, Taking Netflix, Instagram And Pinterest With It
– Forbes, June 30, 2012 (see also Amazon’s summary)
/home/auser/ex0.c
examples | octal | description |
---|---|---|
-r--r--r-- |
0444 | anyone can read |
-rwxrwxrwx |
0777 | anyone can read/write/execute |
-rw-r--r-- |
0644 | owner can read/write; anyone can read |
--w------- |
0200 | owner can write |
see also: setuid/setgid/sticky, access control lists
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
int fd = open("foo.txt", O_RDONLY);
if (fd == -1)
err(EXIT_FAILURE, "open");
/* ... */
close(fd);
}
open
: pass in the filename and access mode
close
: pass in an opened file descriptorOS abstraction for accessing a file
an integer int
used in many syscalls
name | integer value | |
---|---|---|
standard input | STDIN_FILENO |
0 |
standard output | STDOUT_FILENO |
1 |
standard error | STDERR_FILENO |
2 |
example:
write(STDOUT_FILENO, "hello world!\n", 13);
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
count
bytes for fd
errno
)q: how to safely update a file? see also Alice’s notes questions
q: is the content on disk if the machine crashes after close
?
int fd = open("foo.txt", O_WRONLY | O_CREAT, 0600);
write(fd, "hello world!\n", 13);
close(fd);
maybe, maybe not
you need to flush cache onto storage device to ensure durability
int fsync(int fd);
read glibc’s Low-Level Input/Output/manpages/google
defined in stdio.h
: fopen
, fread
, fwrite
, fprintf
, fclose
, …
implemented on top of POSIX I/O syscalls
FILE stream can be either text or binary
buffered by default: use fflush
to flush
predefined streams: stdin
, stdout
, stderr
see glibc’s Input/Output on Streams/manpages/google
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
int main(int argc, char **argv) {
if (argc != 2) return EXIT_FAILURE;
FILE *f = fopen(argv[1], "rb"); // read only, binary mode
if (!f)
err(EXIT_FAILURE, "fopen");
for (;;) { // read from file & write to stdout
char buf[256];
size_t count = fread(buf, 1, sizeof(buf), f);
if (count == 0)
break;
fwrite(buf, 1, count, stdout);
}
fclose(f);
return EXIT_SUCCESS;
}
opendir
, readdir
, closedir
, …
read glibc’s Accessing Directories
or read manpage section 3: man 3 opendir
FILE
, …$ cat in.txt
1213
3231
000005
52
$ ./ex1 in.txt
5
52
1213
3231
input file: each line contains an integer
your program: parse input into uint32_t
’s & sort & print
see soln
bug case study: Bitcoin
alternative interface: mmap