Lecture 23 Threads POSIX - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Lecture 23 Threads POSIX

Description:

Lecture 23. Threads (POSIX) Topics. POSIX. Threads. Pthread Library. November 10, 2005 ... CSCE 510 Fall 2005. Last Time. Sockets. Finished Record Locks. Two ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 17
Provided by: mantonm5
Category:
Tags: posix | lecture | posix | threads

less

Transcript and Presenter's Notes

Title: Lecture 23 Threads POSIX


1
Lecture 23Threads (POSIX)
CSCE 510 Systems Programming
  • Topics
  • POSIX
  • Threads
  • Pthread Library

November 10, 2005
2
  • Last Time
  • Sockets
  • Finished Record Locks
  • Two-way, three-way deadlock
  • Daemons
  • (almost) Unix Domain Sockets
  • Today
  • Unix Domain Sockets
  • Threads
  • ftp//ftp.uu.net/published/oreilly/nutshell/pthrea
    ds/examples.tar.gz

3
Cliconn.c
  • include "apue.h"
  • include ltsys/socket.hgt
  • include ltsys/un.hgt
  • include lterrno.hgt
  • define CLI_PATH "/var/tmp/"
    / 5 for pid 14 chars /
  • define CLI_PERM S_IRWXU
    / rwx for user only /
  • / Create a client endpoint and connect to a
    server.
  • Returns fd if all OK, lt0 on error. /
  • int
  • cli_conn(const char name)
  • int fd, len,
    err, rval
  • struct sockaddr_un un

4
  • / create a UNIX domain stream socket /
  • if ((fd socket(AF_UNIX, SOCK_STREAM,
    0)) lt 0)
  • return(-1)
  • / fill socket address structure with our address
    /
  • memset(un, 0, sizeof(un))
  • un.sun_family AF_UNIX
  • sprintf(un.sun_path, "s05d", CLI_PATH,
    getpid())
  • len offsetof(struct sockaddr_un,
    sun_path) strlen(un.sun_path)
  • unlink(un.sun_path) / in
    case it already exists /
  • if (bind(fd, (struct sockaddr )un, len)
    lt 0)
  • rval -2
  • goto errout
  • if (chmod(un.sun_path, CLI_PERM) lt 0)
  • rval -3
  • goto errout

5
  • / fill socket address structure with server's
    address /
  • memset(un, 0, sizeof(un))
  • un.sun_family AF_UNIX
  • strcpy(un.sun_path, name)
  • len offsetof(struct sockaddr_un,
    sun_path) strlen(name)
  • if (connect(fd, (struct sockaddr )un,
    len) lt 0)
  • rval -4
  • goto errout
  • return(fd)
  • errout
  • err errno
  • close(fd)
  • errno err
  • return(rval)

6
Servlisten.c
  • include "apue.h"
  • include ltsys/socket.hgt
  • include ltsys/un.hgt
  • include lterrno.hgt
  • define QLEN 10
  • /
  • Create a server endpoint of a connection.
  • Returns fd if all OK, lt0 on error.
  • /
  • int
  • serv_listen(const char name)
  • int
    fd, len, err, rval
  • struct sockaddr_un un

7
  • / create a UNIX domain stream socket /
  • if ((fd socket(AF_UNIX, SOCK_STREAM,
    0)) lt 0)
  • return(-1)
  • unlink(name) / in case it already
    exists /
  • / fill in socket address structure /
  • memset(un, 0, sizeof(un))
  • un.sun_family AF_UNIX
  • strcpy(un.sun_path, name)
  • len offsetof(struct sockaddr_un,
    sun_path) strlen(name)
  • / bind the name to the descriptor /
  • if (bind(fd, (struct sockaddr )un, len)
    lt 0)
  • rval -2
  • goto errout

8
  • / create a UNIX domain stream socket /
  • if ((fd socket(AF_UNIX, SOCK_STREAM,
    0)) lt 0)
  • return(-1)
  • unlink(name) / in case it already
    exists /
  • / fill in socket address structure /
  • memset(un, 0, sizeof(un))
  • un.sun_family AF_UNIX
  • strcpy(un.sun_path, name)
  • len offsetof(struct sockaddr_un,
    sun_path) strlen(name)
  • / bind the name to the descriptor /
  • if (bind(fd, (struct sockaddr )un, len)
    lt 0)
  • rval -2
  • goto errout

9
  • if (listen(fd, QLEN) lt 0) / tell kernel
    we're a server /
  • rval -3
  • goto errout
  • return(fd)
  • errout
  • err errno
  • close(fd)
  • errno err
  • return(rval)

10
serv_accept.c
  • include "apue.h"
  • include ltsys/socket.hgt
  • include ltsys/un.hgt
  • include lttime.hgt
  • include lterrno.hgt
  • define STALE 30 / client's name can't be
    older than this (sec) /
  • /
  • Wait for a client connection to arrive, and
    accept it.
  • We also obtain the client's user ID from the
    pathname
  • that it must bind before calling us.
  • Returns new fd if all OK, lt0 on error
  • /
  • int
  • serv_accept(int listenfd, uid_t uidptr)

11
  • serv_accept(int listenfd, uid_t uidptr)
  • int
    clifd, len, err, rval
  • time_t
    staletime
  • struct sockaddr_un un
  • struct stat statbuf
  • len sizeof(un)
  • if ((clifd accept(listenfd, (struct
    sockaddr )un, len)) lt 0)
  • return(-1) / often
    errnoEINTR, if signal caught /
  • / obtain the client's uid from its
    calling address /
  • len - offsetof(struct sockaddr_un,
    sun_path) / len of pathname /
  • un.sun_pathlen 0
    / null terminate /
  • if (stat(un.sun_path, statbuf) lt 0)
  • rval -2
  • goto errout

12
  • ifdef S_ISSOCK / not defined for SVR4
    /
  • if (S_ISSOCK(statbuf.st_mode) 0)
  • rval -3 / not a
    socket /
  • goto errout
  • endif
  • if ((statbuf.st_mode (S_IRWXG
    S_IRWXO))
  • (statbuf.st_mode S_IRWXU) !
    S_IRWXU)
  • rval -4 / is not
    rwx------ /
  • goto errout

13
  • staletime time(NULL) - STALE
  • if (statbuf.st_atime lt staletime
  • statbuf.st_ctime lt staletime
  • statbuf.st_mtime lt staletime)
  • rval -5 / i-node is too
    old /
  • goto errout
  • if (uidptr ! NULL)
  • uidptr statbuf.st_uid /
    return uid of caller /
  • unlink(un.sun_path) / we're
    done with path /
  • return(clifd)

14
  • errout
  • err errno
  • close(clifd)
  • errno err
  • return(rval)

15
Putting it together client.c
16
Putting it together server
Write a Comment
User Comments (0)
About PowerShow.com