3191932525 Distributed Software Programming Lecture 2 Week 2 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

3191932525 Distributed Software Programming Lecture 2 Week 2

Description:

argv[0] - pointer to the first argument. A program may include error checking ... getopt sets optind to the index of the next argument in argv ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 18
Provided by: socs
Category:

less

Transcript and Presenter's Notes

Title: 3191932525 Distributed Software Programming Lecture 2 Week 2


1
31919/32525 Distributed Software
ProgrammingLecture 2 (Week 2)
  • UNIX Programming (Part 2)

2
Writing UNIX Programs
  • There are two categories of UNIX programs
  • User Commands programs users run from shells,
    e.g. cc, ls, vi
  • Daemons their execution is usually invisible to
    users

3
Arguments from Command Line
  • Main function contains two arguments
  • argc - count of arguments
  • argv0 - pointer to the first argument
  • A program may include error checking
  • error message is printed on stderr by fprintf
  • On successful exit, return 0 otherwise a nonzero
    value (e.g. 1)

4
getopt Routine
  • int getopt(int argc char const argv, const char
    opts)
  • extern char optarg
  • extern int optind, opterr, optopt
  • opts points to a string containing the option
    letters. Each option that has an argument is
    followed by a colon ()

5
getopt Routine (contd)
  • When getopt returns a character in opts, optarg
    points to the argument of the option (if there is
    one)
  • getopt sets optind to the index of the next
    argument in argv
  • set opterr to 0 to suppress the error to be
    printed by getopt

6
Error Handling
  • perror prints a string on stderr, followed by a
    description of the error
  • Two other public routines for error handling
  • error(const char fmt, )
  • for nonfatal error report
  • fatal(const char fmt, )
  • for fatal error report and calls to exit at the
    end

7
Inputs and Outputs (I/O)
  • FILE fopen(const char fname, const char type)
  • int fclose(FILE fp)
  • int open(const char path, int oflag, / mode_t
    mode/)
  • returns fd (file descriptor)
  • ssize_t read(int fd, void buf, size_t nbyte)
  • returns number of bytes read and -1 on error
  • ssize_t write(int fd, const void buf, size_t
    nbyte)
  • returns number of bytes written and -1 on error
  • int close(int fd)
  • returns 0 on success and -1 on failure

8
Creating a Log File
  • Use open to open a log file
  • int fcnt1(int fd, int cmd, / void arg /) gets
    the close-on-exec flag
  • int fstat(int fd, struc stat sbuf) reads the
    file stat and check its user and group IDs
  • int fchown(int fd, uid_t owner, gid_t group)
    changes the user and group IDs to the proper
    values

9
Signaling
  • Signals are the software equivalent of hardware
    interrupts.
  • When a signal is sent, it is posted or generated.
    When received, it is delivered. Signal between
    posted and delivered is called pending.
  • Way to control signals is called signal
    disposition.

10
Signal Interfaces
  • void (sigset(int sig, void (disp)(int)))(int)
  • sig is the signal number
  • disp is address of a signal-handling or a
    constant such as SIG_IGN, SIG_DEF and SIG_HOLD
  • On success
  • if sig was held, returns SIG_HOLD
  • otherwise, returns previous disposition
  • On error, returns SIG_ERR and sets errno

11
Signal Interfaces (contd)
  • int sighold(int sig) adds sig to signal mask
  • int sigrelse(int sig) removes sig from mask
  • int sigignore(int sig) ignores the signal
  • int sigpause(int sig) removes sig from mask and
    suspends process till next signal
  • They all return 0 on success and -1 on failure

12
Creating Child Processes
  • Reasons
  • Multi processes can work together to accomplish
    the task
  • Child process does not destroy parent process
    context
  • Use fork to create another process
  • Use exec to replace text and data of parent
    process

13
fork System Call
  • pid_t fork(void) returns 0 to the child and
    nonzero values to the parent
  • If fork fails, it returns -1 to the parent and
    sets errno to EAGAIN
  • Child gets its own copies of fds
  • When a child terminates, SIGCHLD is sent to the
    parent

14
wait and waitpid System Calls
  • pid_t wait(int status) and
  • pid_t waitpid(pid_t pid, int status, int
    options) suspend the calling process until one of
    its children terminates or stops
  • If the status of a child is available, they
    return the process ID of the child
  • If the parent is uninterested in the status,
    status can be set to NULL

15
exec family
  • exec family has functions such as execl, execle,
    execlp, execv, execve and execvp
  • They all replace the text and data of current
    process with that of the program specified by a
    path of file
  • The first argument is the basename of the command
    and the last one is NULL

16
Creating Daemons
  • Change the file creation mask
  • Create a log file to record errors
  • Obtain max number of open files
  • Create new session and process group and ignore
    SIGHUP in the child process
  • Close all files except the log file
  • Reserve fds 0, 1 2

17
Readings
  • Rago, UNIX System V Network Programming,
  • Chapter 2
Write a Comment
User Comments (0)
About PowerShow.com