Unix system calls
Download
1 / 62

http:// codeschool.org / - PowerPoint PPT Presentation


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

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

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


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/zeroreturns zeroed bytes/dev/randomreturns random data

/dev/nulldiscards 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)


pipe

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

fs = pipe()

f1 = fs[0]

f2 = fs[1]


memory-mapped files

kernel code

stack

heap

byte n

file

mmap’d file

byte 0

code


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 are sent by…

  • the kernel

signals

the receiving process…

  • performs a default action

  • or invokes a handler function

  • or blocks it

  • or ignores it


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


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

(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://codeschool.org/

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


partition 1

/alice/tim

2

partition 2

/

1

HD

partition 3

/jessica

2

partition 4

flash drive

/jessica/vincent

3


chroot

(change root directory)

chroot(‘/ben/ian’)

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


ad
  • Login