This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 2)
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. This is one part of a larger series. You may need to view previous parts to understand this material.
open (open/create a file and return a file descriptor) close (release file descriptor) read (copy bytes from a file to memory)* write (copy bytes of memory to a file)* *blocks
Process OS syscall write write buffer HD
f = open(‘/alice/tim’) write(f, ‘bla bla’) write(f, ‘bla bla bla’) close(f)
Process OS data and code syscall read read buffer HD
f = open(‘/alice/tim’) data = read(f) close(f)
f = open(‘/alice/tim’) data = read(f) while len(data) != 0: print(data) data = read(f) close(f)
byte 0last byte marker
byte 0last byte marker
byte 0last byte move with truncate
byte 0last byte move with truncate move with lseek marker
descriptor description
f = open(‘/alice/tim’) f2 = open(‘/alice/tim’) write(f, ‘bla bla’) data = read(f2)
descriptor description buffer file on disk
pid 75 write byte 0last byte pid 580 write pid 442 read pid 46 write
pid 75 write byte 0last byte pid 580 write ‘bla bla’ ‘herp derp’
pid 75 write byte 0last byte pid 580 write ‘her blap’ ‘blrp derp’ ‘hlapbder p’ ‘herp derp’ ‘bla bla’
umask (get/set default permissions for new files/directories) oldmask = umask(newmask) f = open(‘/alice/tim’) write(f, ‘bla bla’)
chmod (‘change mode’: set permissions of an existing file/directory) chmod(‘/alice/tim’, mask)
chmod (‘change mode’: set permissions of an existing file/directory) chown (‘change owner’: set owner of an existing file/directory) chmod(‘/alice/tim’, mask) chown(‘/alice/tim’, user, group)
HD partition 1 partition 2 partition 3 HD partition 4 flash drive partition 5 CD-ROM partition 6 partition 7
file 35 file 7 file 61 file 4 directory 21 directory 86 root directory 2 root directory always has inode 2 each file and directory in a partition is known by a unique inode number
mkdir (‘make directory’) rmdir (‘remove a file or directory’) mkdir(‘/alice/tim’) rmdir(‘/alice/tim’)
link (add directory entry) link(‘/alice/ian’, ‘/ben/jill’)
link (add directory entry) unlink (remove directory entry) link(‘/alice/ian’, ‘/ben/jill’) unlink(‘/alice/ian’)
getdents (‘get directory entries’) f = open(‘/alice’) entries = getdents(f) while len(entries) != 0: print(entries) entries = getdents(f) close(f)
HD partition 1 partition 2 partition 3 flash drive partition 4 / /alice/tim /jessica/vincent /jessica
mount (attach partition to the unified file system) umount (‘unmount’: detach partition from the unified file system) mount(partition1, ‘/alice/tim’) umount(‘/alice/tim’)
absolute path: /alice/tim relative path: alice/tim
chdir (change cwd, the ‘current working directory’) chdir(‘/ben/ian’) f = open(‘/alice/tim’) f2 = open(‘alice/tim’) becomes: ‘/ben/ian/alice/tim’
‘regular’ file directory symbolic link character device file block device file pipe socket
symlink (create a symbolic link) symlink(‘/alice/tim’, ‘/jill/ken’) f = open(‘/jill/ken’) follows link to: ‘/alice/tim’
CPU registers device read/write
a block may only be read/written as a whole partition composed of blocks block 0 block 1 block 2 block 3 block 4 block 5 block 6 block 7 block 8 block 9
partition composed of blocks block 0 block 1 inode 86 block 2 block 3 block 4 inode 86 block 5 block 6 inode 86 block 7 block 8 block 9
read/write block 0 block 1 inode 86 block 2 block 3 block 4 inode 86 block 5 block 6 inode 86 block 7 block 8 block 9 block 4 buffer
block 0 block 1 block 2 block 3 block 4 block 5 block 6 block device file byte 0 last byte
device output buffer input buffer process character device file read write input output
data taken starting here data appended here input/output buffers are FIFO (first in, first out)
read takes data starting here device appends data here input buffer
input/output buffers are FIFO (first in, first out) device takes data starting here write appends data here output buffer
block device buffers backed by storage character device buffers not backed by storage
/dev bf = open(‘/dev/sda1’) cf = open(‘/dev/lp0’) directory of device files
/dev bf = open(‘/dev/sda1’) cf = open(‘/dev/lp0’) lseek(bf, 100) bdata = read(bf) cdata = read(cf) directory of device files
pseudo-device files /dev/zero returns zeroed bytes /dev/random returns random data /dev/null discards data; returns no bytes at all
pipe (a FIFO as a file) read takes data starting here write appends data here
FIFO process A pipe readwrite process B
FIFO process A pipe readwrite process B FIFO readwrite pipe
mknod (‘make node’: create a regular file, device file, or named pipe) mknod(‘/ryan/kim’, BLOCK, deviceNum) mknod(‘/ryan/erin’, CHR, deviceNum) mknod(‘/ryan/tina’, FIFO)
pipe (creates a new, anonymous pipe and returns two file descriptors) fs = pipe() f1 = fs[0] f2 = fs[1]
memory-mapped files file stack code mmap’d file heap kernel code byte 0 byte n
f = open(‘/brad/mike’) address = mmap(500, f, 200) … # reading/writing the allocated memory reads/writes the file munmap(address) close(f) memory-mapped files
signals signals are sent by… the kernel the receiving process… performs a default action or invokes a handler function or blocks it or ignores it
signals signals are sent by… the kernel SIGSEGV SIGFPE SIGSTOP SIGCONT the receiving process… performs a default action or invokes a handler function or blocks it or ignores it
signals signals are sent by… the kernel or the kill system call SIGSEGV SIGFPE SIGSTOP SIGCONT the receiving process… performs a default action or invokes a handler function or blocks it or ignores it
kill (send a signal to a process) signal (set a signal to be handled, ignored, or trigger its default action) kill(35, SIGSTOP) signal(func, SIGFPE)
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
lockf (obtains/releases an “advisory” lock on a file) f = open(‘/alice/tim’) lockf(f, F_LOCK) … # do stuff to file lockf(f, F_ULOCK) close(f)
stat (get file/directory information: size, last modification date/time, owner, etc.) status = stat(‘/alice/tim’)
HD partition 1 partition 2 partition 3 flash drive partition 4 / /alice/tim /jessica/vincent /jessica
chroot (change root directory) chroot(‘/ben/ian’) open(‘/alice/tim’) absolute path: ‘/ben/ian/alice/tim’