Unix system calls
This presentation is the property of its rightful owner.
Sponsored Links
1 / 62

http:// codeschool.org / PowerPoint PPT Presentation


  • 80 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

http:// codeschool.org /

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


Codeschool

Unix system calls

(part 2)

http://codeschool.org/

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


Codeschool

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

(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


Codeschool

Process

syscallwrite

HD

OS

write buffer


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

f = open(‘/alice/tim’)

write(f, ‘blabla’)

write(f, ‘blablabla’)

close(f)


Codeschool

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)

close(f)


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

f = open(‘/alice/tim’)

data = read(f)

whilelen(data) != 0:

print(data)

data = read(f)

close(f)


Codeschool

marker

byte 0

last byte


Codeschool

marker

byte 0

last byte


Codeschool

byte 0

last byte

move with truncate


Codeschool

move with lseek

marker

byte 0

last byte

move with truncate


Codeschool

descriptor

description


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

f = open(‘/alice/tim’)

f2 = open(‘/alice/tim’)

write(f, ‘blabla’)

data = read(f2)


Codeschool

descriptor

description

buffer

file on disk


Pid 75 write

pid442read

byte 0

last byte

pid75write

pid580write

pid46write


Pid 75 write1

‘herpderp’

byte 0

last byte

pid75write

pid580write

‘blabla’


Pid 75 write2

‘herpderp’

‘her blap’

‘blrpderp’

‘hlapbderp’

byte 0

last byte

pid75write

pid580write

‘blabla’


Oldmask umask newmask f open alice tim write f bla bla

umask

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

oldmask = umask(newmask)

f = open(‘/alice/tim’)

write(f, ‘blabla’)


Chmod alice tim mask

chmod

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

chmod(‘/alice/tim’, mask)


Chmod alice tim mask chown alice tim user group

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)


Codeschool

HD

HD

flash drive

CD-ROM

partition 1

partition 4

partition 5

partition 6

partition 2

partition 7

partition 3


Codeschool

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

mkdir

(‘make directory’)

rmdir

(‘remove a file or directory’)

mkdir(‘/alice/tim’)

rmdir(‘/alice/tim’)


Link alice ian ben jill

link

(add directory entry)

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


Link alice ian ben jill unlink alice ian

link

(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

(‘get directory entries’)

f = open(‘/alice’)

entries = getdents(f)

whilelen(entries) != 0:

print(entries)

entries = getdents(f)

close(f)


Codeschool

partition 1

/alice/tim

partition 2

/

HD

partition 3

/jessica/vincent

/jessica

partition 4

flash drive


Mount partition1 alice tim umount alice tim

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

absolute path:/alice/tim

relative path:alice/tim


Chdir ben ian f open alice tim f2 open alice tim becomes ben ian 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’


Codeschool

  • ‘regular’ file

  • directory

  • symbolic link

  • character device file

  • block device file

  • pipe

  • socket


Codeschool

symlink

(create a symbolic link)

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

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


Codeschool

device

registers

read/write 

CPU


Codeschool

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


Codeschool

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


Codeschool

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


Codeschool

block device file

byte 0

block 0

block 1

block 2

block 3

block 4

block 5

block 6

last byte


Codeschool

character device file

read

input

process

device

input buffer

write

output

output buffer


Codeschool

input/output buffers are FIFO (first in, first out)

data appended here

data taken starting here


Codeschool

input/output buffers are FIFO (first in, first out)

device appends data here

input buffer

read takes data starting here


Codeschool

input/output buffers are FIFO (first in, first out)

write appends data here

output buffer

device takes data starting here


Codeschool

block device buffers backed by storage

character device buffers not backed by storage


Bf open dev sda1 cf open dev lp0

/dev

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

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

/dev/zeroreturns zeroed bytes/dev/randomreturns random data

/dev/nulldiscards data; returns no bytes at all


Codeschool

pipe (a FIFO as a file)

write appends data here

read takes data starting here


Codeschool

process A

pipe

process B

read

write

FIFO


Codeschool

pipe

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

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

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

fs = pipe()

f1 = fs[0]

f2 = fs[1]


Memory mapped files

memory-mapped files

kernel code

stack

heap

byte n

file

mmap’d file

byte 0

code


Codeschool

memory-mapped files

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…

  • 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…

  • 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…

  • 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

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


Codeschool

http://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

(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

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

status = stat(‘/alice/tim’)


Codeschool

partition 1

/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

(change root directory)

chroot(‘/ben/ian’)

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


  • Login