Daemon Processes - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Daemon Processes

Description:

typically, system services that need to be continuously ... grep, sort, etc. a process invokes a filter as a coprocess by opening two pipes and forking ... – PowerPoint PPT presentation

Number of Views:768
Avg rating:5.0/5.0
Slides: 17
Provided by: richarde78
Category:
Tags: daemon | grep | processes

less

Transcript and Presenter's Notes

Title: Daemon Processes


1
Daemon Processes
  • processes that run in the background
  • have no controlling terminal
  • typically, system services that need to be
    continuously available are run as daemons
  • typically started at boot, and run continuously
  • can be any process that we wish to disassociate
    from a session

2
Daemon Characteristics
  • background process having no controlling terminal
  • typically, daemons are process and group leaders,
    with no other processes in their group
  • system daemons are typically started by, or
    inherited by init
  • conventionally, programs intended to run as
    daemons have the character d appended to their
    names
  • syslogd, initd, ftpd, etc.

3
Daemon Coding Rules
  • fork and exit
  • guarantees not process group leader
  • setsid()
  • new session leader, process group leader, no
    controlling terminal
  • CWD to local filesystem
  • prevents system hang on attempted unmount
  • set file mode mask to 0
  • avoid inherited mask limitations
  • close unnecessary descriptors
  • prevents holding open inherited file descriptors

4
Error Logging
  • daemons cant write to stderr
  • no controlling terminal
  • shouldnt write to console unless message urgent
  • console may be running windowing server
  • can make text console unusable
  • can write to specific log file, but makes system
    daemons difficult to monitor
  • typically write to system log facility

5
Streams log Driver
  • SVR4 uses streams log system
  • log messages can go to any of three loggers
  • error (for unremarkable errors)
  • trace (for debugging)
  • console (for urgent messages or those intended
    for a syslog daemon)

6
BSD Syslog system
  • used by the vast majority of system daemons
  • syslogd handles logging requests via syslog
    functions
  • syslogd is itself a daemon
  • messages can be generated in three ways
  • kernel
  • local process
  • remote process or local process using network
    port
  • syslogd configured from a file on startup
  • controls destination file of log messages

7
syslog functions
  • three functions for interfacing to syslogd
  • openlog()
  • if not specifically called, will be called
    automatically when syslog() called for first time
  • syslog()
  • takes log message and priority
  • closelog()
  • closes the file descriptor associated with the
    log message (optional)

8
Client-Server Model
  • server processes wait for client processes to
    make requests
  • one way communication with client
  • syslogd, etc.
  • two way communication with client
  • ftpd, httpd, etc.
  • typically, servers run as daemons

9
Interprocess Communication
  • methodologies for passing information between
    processes
  • implementations and support vary widely across
    systems
  • half-duplex pipes are universal
  • LINUX and most modern Unices support SVR4 IPC
  • messages
  • semaphores
  • shared memory

10
Pipes
  • oldest common form of UNIX IPC
  • half-duplex
  • data flows in only one direction
  • can only be used by processes with a common
    ancestor
  • usually used between parent and child
  • created using the pipe() function
  • pipe() returns a two element array of file
    descriptors
  • FD in element 0 used for reading
  • FD in element 1 used for writing

11
using pipes
  • pipes can be identified using the fstat function
    against the FD
  • since pipes are half-duplex, two pipes must be
    opened for two-way communication between
    processes
  • if piping from parent to child, parent closes
    read FD and child closes write FD
  • if piping from child to parent, parent closes
    write FD and child closes read FD

12
rules for pipes
  • when write end of a pipe is closed
  • after reading data, read returns 0 to indicate
    end of file
  • when read end of a pipe is closed
  • write generates SIGPIPE signal
  • if signal ignored or caught, write returns error

13
popen() and pclose() Functions
  • used for common task of creating a pipe to
    another program
  • creates a pipe
  • forks a child
  • execs a program from the child
  • pipe connects to STDIN of output command if type
    is w, STDOUT of output command if type is r

14
Coprocesses
  • a coprocess is a filter used by a program which
    both supplies input and accepts output
  • filters read from STDIN and write to STDOUT
  • typically used to perform data transformations
  • grep, sort, etc.
  • a process invokes a filter as a coprocess by
    opening two pipes and forking
  • the child dups one pipe to STDIN, the other to
    STDOUT then execs the filter

15
FIFOs
  • sometimes referred to as named pipes
  • used to pipe between unrelated processes
  • FIFOs have pathnames, and behave as ordinary
    files
  • mkfifo() is used to create a FIFO
  • FIFOs can be accessed by multiple processes

16
rules for FIFOs
  • when opened for reading or writing, a FIFO will
    block until some process opens it for writing or
    reading respectively, unless opened non-blocking
  • if opened non-blocking, an open for reading will
    return immediately, an open for writing will
    return an error if no process has the FIFO open
    for reading
  • writing to a FIFO that no process has open for
    reading generates a SIGPIPE signal
  • multiple processes may read or write to a FIFO so
    the rules for atomic I/O apply
Write a Comment
User Comments (0)
About PowerShow.com