620 likes | 773 Views
Unix system calls (part 2). http:// codeschool.org /. 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. http:// codeschool.org /.
E N D
Unix system calls (part 2) http://codeschool.org/ 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. http://codeschool.org/ This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
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 syscallwrite HD OS write buffer
f = open(‘/alice/tim’) write(f, ‘blabla’) write(f, ‘blablabla’) close(f)
Process syscallread HD OS data and code read buffer
f = open(‘/alice/tim’) data = read(f) close(f)
f = open(‘/alice/tim’) data = read(f) whilelen(data) != 0: print(data) data = read(f) close(f)
marker byte 0 last byte
marker byte 0 last byte
byte 0 last byte move with truncate
move with lseek marker byte 0 last byte move with truncate
descriptor description
f = open(‘/alice/tim’) f2 = open(‘/alice/tim’) write(f, ‘blabla’) data = read(f2)
descriptor description buffer file on disk
pid442read byte 0 last byte pid75write pid580write pid46write
‘herpderp’ byte 0 last byte pid75write pid580write ‘blabla’
‘herpderp’ ‘her blap’ ‘blrpderp’ ‘hlapbderp’ byte 0 last byte pid75write pid580write ‘blabla’
umask (get/set default permissions for new files/directories) oldmask = umask(newmask) f = open(‘/alice/tim’) write(f, ‘blabla’)
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 HD flash drive CD-ROM partition 1 partition 4 partition 5 partition 6 partition 2 partition 7 partition 3
file 35 each file and directory in a partition is known by a unique inode number directory 21 file 4 directory 86 file 7 root directory always has inode 2 root directory 2 file 61
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) whilelen(entries) != 0: print(entries) entries = getdents(f) close(f)
partition 1 /alice/tim partition 2 / HD partition 3 /jessica/vincent /jessica partition 4 flash drive
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’
device registers read/write CPU
block 0 block 1 partition composed of blocks block 2 block 3 block 4 block 5 a block may only be read/written as a whole block 6 block 7 block 8 block 9
block 0 block 1 inode 86 partition composed of blocks block 2 block 3 block 4 inode 86 block 5 block 6 inode 86 block 7 block 8 block 9
block 0 read/write block 1 inode 86 block 2 block 3 block 4 inode 86 block 4 buffer block 5 block 6 inode 86 block 7 block 8 block 9
block device file byte 0 block 0 block 1 block 2 block 3 block 4 block 5 block 6 last byte
character device file read input process device input buffer write output output buffer
input/output buffers are FIFO (first in, first out) data appended here data taken starting here
input/output buffers are FIFO (first in, first out) device appends data here input buffer read takes data starting here
input/output buffers are FIFO (first in, first out) write appends data here output buffer device takes data starting here
block device buffers backed by storage character device buffers not backed by storage
/dev directory of device files bf = open(‘/dev/sda1’) cf = open(‘/dev/lp0’)
/dev directory of device files bf = open(‘/dev/sda1’) cf = open(‘/dev/lp0’) lseek(bf, 100) bdata = read(bf) cdata = read(cf)
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) write appends data here read takes data starting here
process A pipe process B read write FIFO
pipe read write FIFO process A process B write read FIFO 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)