Zajmavosti ze systmovho programovn Pavel imerda pavlix@pavlix.net - - PowerPoint PPT Presentation

zaj mavosti ze syst mov ho programov n
SMART_READER_LITE
LIVE PREVIEW

Zajmavosti ze systmovho programovn Pavel imerda pavlix@pavlix.net - - PowerPoint PPT Presentation

Zajmavosti ze systmovho programovn Pavel imerda pavlix@pavlix.net C API File I/O Lets start with shell Use cat to read a file Use strace to trace the syscalls Use ltrace to trace the library calls Call ANSI


slide-1
SLIDE 1

Zajímavosti ze systémového programování

Pavel Šimerda pavlix@pavlix.net

slide-2
SLIDE 2

C API

slide-3
SLIDE 3

File I/O

◮ Let’s start with shell ◮ Use cat to read a file ◮ Use strace to trace the syscalls ◮ Use ltrace to trace the library calls ◮ Call ANSI fread from a binary ◮ Call POSIX read from a binary

slide-4
SLIDE 4

fopen + fgets

◮ ANSI file I/O ◮ Returns pointer to buffer on success

◮ No use for the pointer, though.

◮ Returns NULL on error ◮ Returns NULL on end of file ◮ Stops reading at end of line ◮ Supplies the cstring NUL character ◮ Doesn’t work with binary data

slide-5
SLIDE 5
  • pen + read

◮ POSIX file I/O ◮ Almost 1:1 with Linux syscalls ◮ Returns positive number on success

◮ Number of bytes actually written

◮ Returns zero on end of file ◮ Returns minus one on error

◮ Check errno for details

◮ Doesn’t care about new lines ◮ Works with text and binary

slide-6
SLIDE 6

Memory Allocation and Reallocation

◮ Static global allocation ◮ Stack local allocation ◮ Dynamic allocation and reallocation ◮ Reallocation strategy

slide-7
SLIDE 7

Process Management

◮ Kernel tasks

◮ Processes ◮ Threads ◮ Namespaces

slide-8
SLIDE 8

fork

◮ Library function

◮ Uses clone syscall

◮ Returns 0 in child process ◮ Returns child pid in parent process ◮ Returns -1 on error

◮ Check errno

slide-9
SLIDE 9

exec

◮ Set of library functions

◮ execlp() for example ◮ Using execve() and PATH

◮ Doesn’t return on success ◮ Returns -1 on failure

◮ Check errno

◮ File descriptor handling

◮ O_CLOEXEC ◮ What if std streams were closed?

slide-10
SLIDE 10

Process Creation Strategy

◮ Master process and forked processes

◮ Easy to create shared resources

◮ Thread creation

◮ Many more resources shared by default

◮ Separately forked processes

◮ Identifying shared resources via name or path ◮ Passing shared resources via IPC

◮ Service management and environment

◮ initscripts ◮ systemd and other service managers

slide-11
SLIDE 11

Pipes and Sockets

◮ Communication ◮ Forked pipe and socketpair ◮ Named pipes and unix sockets

◮ Run mkfifo binary ◮ mkfifo() function ◮ mknod syscall

slide-12
SLIDE 12

Shared Memory and Mutual Exclusion

◮ Consumer-producer example ◮ Just mmap() ◮ Path based shared memory ◮ Path based semaphores

slide-13
SLIDE 13

Asynchronous Signal Handling

◮ Classic async handlers ◮ Returning control ◮ Interrupted syscalls ◮ Event based handling

◮ signalfd

slide-14
SLIDE 14

Bonus: Debugging and Tracing

◮ strace the strace! ◮ Example debugging and tracing sessions ◮ Tracing those to see how they use the operating system

slide-15
SLIDE 15

Bonus: Network Sockets

◮ Using TCP/IP sockets ◮ Dual stack socket abstraction

◮ multisock

◮ Name resolution

◮ getaddrinfo call ◮ netresolve project

slide-16
SLIDE 16

Bonus: I/O Multiplexing

◮ select, poll, epoll ◮ User space implementations

◮ userspacefd project