Unix system calls
Download
1 / 62

Codeschool - PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on

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 /.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Codeschool' - quasar


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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.


Blocks

open previous parts to understand this material.

(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 previous parts to understand this material.

syscallwrite

HD

OS

write buffer


F open alice tim write f bla bla write f bla bla bla close f

f previous parts to understand this material. = open(‘/alice/tim’)

write(f, ‘blabla’)

write(f, ‘blablabla’)

close(f)


Process previous parts to understand this material.

syscallread

HD

OS data and code

read buffer


F open alice tim data read f close f

f = previous parts to understand this material.open(‘/alice/tim’)

data = read(f)

close(f)


F open alice tim data read f while len data 0 prin t data data read f close f

f = previous parts to understand this material.open(‘/alice/tim’)

data = read(f)

whilelen(data) != 0:

print(data)

data = read(f)

close(f)


marker previous parts to understand this material.

byte 0

last byte


marker previous parts to understand this material.

byte 0

last byte


byte 0 previous parts to understand this material.

last byte

move with truncate


move with previous parts to understand this material.lseek

marker

byte 0

last byte

move with truncate


descriptor previous parts to understand this material.

description


F open alice tim f2 open alice tim write f bla bla data read f2

f = previous parts to understand this material.open(‘/alice/tim’)

f2 = open(‘/alice/tim’)

write(f, ‘blabla’)

data = read(f2)


descriptor previous parts to understand this material.

description

buffer

file on disk


Pid 75 write

pid previous parts to understand this material.442read

byte 0

last byte

pid75write

pid580write

pid46write


Pid 75 write1

previous parts to understand this material.herpderp’

byte 0

last byte

pid75write

pid580write

‘blabla’


Pid 75 write2

previous parts to understand this material.herpderp’

‘her blap’

‘blrpderp’

‘hlapbderp’

byte 0

last byte

pid75write

pid580write

‘blabla’


Oldmask umask newmask f open alice tim write f bla bla

umask previous parts to understand this material.

(get/set default permissions for new files/directories)

oldmask = umask(newmask)

f = open(‘/alice/tim’)

write(f, ‘blabla’)


Chmod alice tim mask

chmod previous parts to understand this material.

(‘change mode’: set permissions of an existing file/directory)

chmod(‘/alice/tim’, mask)


Chmod alice tim mask chown alice tim user group

chmod previous parts to understand this material.

(‘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 previous parts to understand this material.

HD

flash drive

CD-ROM

partition 1

partition 4

partition 5

partition 6

partition 2

partition 7

partition 3


file 35 previous parts to understand this material.

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 alice tim rmdir alice tim

mkdir previous parts to understand this material.

(‘make directory’)

rmdir

(‘remove a file or directory’)

mkdir(‘/alice/tim’)

rmdir(‘/alice/tim’)


Link alice ian ben jill

link previous parts to understand this material.

(add directory entry)

link(‘/alice/ian’, ‘/ben/jill’)


Link alice ian ben jill unlink alice ian

link previous parts to understand this material.

(add directory entry)

unlink

(remove directory entry)

link(‘/alice/ian’, ‘/ben/jill’)

unlink(‘/alice/ian’)


F open alice entries getdents f while len entries 0 prin t entries entries getdents f close f

getdents previous parts to understand this material.

(‘get directory entries’)

f = open(‘/alice’)

entries = getdents(f)

whilelen(entries) != 0:

print(entries)

entries = getdents(f)

close(f)


partition 1 previous parts to understand this material.

/alice/tim

partition 2

/

HD

partition 3

/jessica/vincent

/jessica

partition 4

flash drive


Mount partition1 alice tim umount alice tim

mount previous parts to understand this material.

(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

absolute path: previous parts to understand this material./alice/tim

relative path:alice/tim


Chdir ben ian f open alice tim f2 open alice tim becomes ben ian alice tim

chdir previous parts to understand this material.

(change cwd, the ‘current working directory’)

chdir(‘/ben/ian’)

f = open(‘/alice/tim’)

f2 = open(‘alice/tim’) becomes:‘/ben/ian/alice/tim’


  • ‘regular’ file previous parts to understand this material.

  • directory

  • symbolic link

  • character device file

  • block device file

  • pipe

  • socket


symlink previous parts to understand this material.

(create a symbolic link)

symlink(‘/alice/tim’, ‘/jill/ken’)

f = open(‘/jill/ken’) follows link to:‘/alice/tim’


device previous parts to understand this material.

registers

read/write 

CPU


block 0 previous parts to understand this material.

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 previous parts to understand this material.

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 previous parts to understand this material.

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 previous parts to understand this material.

byte 0

block 0

block 1

block 2

block 3

block 4

block 5

block 6

last byte


character device file previous parts to understand this material.

read

input

process

device

input buffer

write

output

output buffer


input/output buffers are FIFO (first in, first out) previous parts to understand this material.

data appended here

data taken starting here


input/output buffers are FIFO (first in, first out) previous parts to understand this material.

device appends data here

input buffer

read takes data starting here


input/output buffers are FIFO (first in, first out) previous parts to understand this material.

write appends data here

output buffer

device takes data starting here


block device buffers backed by storage previous parts to understand this material.

character device buffers not backed by storage


Bf open dev sda1 cf open dev lp0

/dev previous parts to understand this material.

directory of device files

bf = open(‘/dev/sda1’)

cf = open(‘/dev/lp0’)


Bf open dev sda1 cf open dev lp0 lseek b f 100 bdata read b f cdata read cf

/dev previous parts to understand this material.

directory of device files

bf = open(‘/dev/sda1’)

cf = open(‘/dev/lp0’)

lseek(bf, 100)

bdata = read(bf)

cdata = read(cf)


Pseudo device files

pseudo-device files previous parts to understand this material.

/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) previous parts to understand this material.

write appends data here

read takes data starting here


process A previous parts to understand this material.

pipe

process B

read

write

FIFO


pipe previous parts to understand this material.

read

write

FIFO

process A

process B

write

read

FIFO

pipe


Mknod ryan kim block devicenum mknod ryan erin chr devicenum mknod ryan tina fifo

mknod previous parts to understand this material.

(‘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)


Fs pipe f1 fs 0 f2 fs 1

pipe previous parts to understand this material.

(creates a new, anonymous pipe and returns two file descriptors)

fs = pipe()

f1 = fs[0]

f2 = fs[1]


Memory mapped files

memory-mapped files previous parts to understand this material.

kernel code

stack

heap

byte n

file

mmap’d file

byte 0

code


memory-mapped files previous parts to understand this material.

f = open(‘/brad/mike’)

address = mmap(500, f, 200)

…# reading/writing the allocated memory reads/writes the file

munmap(address)

close(f)


Signals

signals are sent by… previous parts to understand this material.

  • the kernel

signals

the receiving process…

  • performs a default action

  • or invokes a handler function

  • or blocks it

  • or ignores it


Signals1

signals are sent by… previous parts to understand this material.

  • the kernel

signals

SIGSEGV

SIGFPE

SIGSTOP

SIGCONT

the receiving process…

  • performs a default action

  • or invokes a handler function

  • or blocks it

  • or ignores it


Signals2

signals are sent by… previous parts to understand this material.

  • the kernel

  • or the kill system call

signals

SIGSEGV

SIGFPE

SIGSTOP

SIGCONT

the receiving process…

  • performs a default action

  • or invokes a handler function

  • or blocks it

  • or ignores it


Kill 35 sigstop signal func sigfpe

kill previous parts to understand this material.

(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)


http:// previous parts to understand this material.codeschool.org/

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.


F open alice tim lockf f f lock do stuff to file lockf f f ulock close f

lockf previous parts to understand this material.

(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)


Status stat alice tim

stat previous parts to understand this material.

(get file/directory information: size, last modification date/time, owner, etc.)

status = stat(‘/alice/tim’)


partition 1 previous parts to understand this material.

/alice/tim

2

partition 2

/

1

HD

partition 3

/jessica

2

partition 4

flash drive

/jessica/vincent

3


Chroot ben ian open alice tim absolute path ben ian alice tim

chroot previous parts to understand this material.

(change root directory)

chroot(‘/ben/ian’)

open(‘/alice/tim’) absolute path:‘/ben/ian/alice/tim’


ad