3191932525 Distributed Software Programming Lecture 10 Week 14 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

3191932525 Distributed Software Programming Lecture 10 Week 14

Description:

... 14/09. 1. 31919/32525 Distributed Software Programming. Lecture 10 (Week 14) Posix Threads. 11/14/09. 2. Outline. Disadvantages of Using fork() Advantages of Using ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 11
Provided by: socs
Category:

less

Transcript and Presenter's Notes

Title: 3191932525 Distributed Software Programming Lecture 10 Week 14


1
31919/32525 Distributed Software
ProgrammingLecture 10 (Week 14)
  • Posix Threads

2
Outline
  • Disadvantages of Using fork()
  • Advantages of Using Threads
  • Basic Thread Functions

3
Problems with fork()
  • fork is expensive
  • Memory is copied to child
  • All fds are duplicated in the child.
  • Need IPC to pass information between the parent
    and child after the fork.

4
Features of Threads
  • Threads share
  • the same global variables
  • process instructions
  • most data
  • open files
  • signal handlers and signal dispositions
  • current working directory
  • user and group IDs

5
pthread_create Function
  • Initial or main thread is created by exec.
  • Additional threads are created by
  • int pthread_create(pthread_t tid, const
    pthread_attr_t attr, void (func)(void ), void
    arg)
  • Thead has attributes priority, stack size etc
  • Thread specifies a function to execute
  • arg is the single argument to the function
  • On success, pthread_create returns 0 and a thread
    is created. Otherwise a positive value is
    returned.

6
Start Function of a Thread
  • If multiple arguments are needed, the arguments
    are packaged into a data structure.
  • The function takes only a single argument.
  • The argument is a generic pointer pointing to the
    data structure.
  • The function returns a generic pointer.

7
pthread_join Function
  • One can wait for a given thread to terminate by
    calling
  • int pthread_join(pthread_t tid, void status)
  • tid specifies the thread to be waited for
  • If the status pointer is not NULL, the return
    value from the thread function is stored in the
    location pointed by status
  • pthread_join returns 0 on success, and a positive
    value on failure.

8
Joinable Thread and Detached Thread
  • Joinable threads ID and status are retained when
    the thread terminates until another thread calls
    pthread_join.
  • Detached threads resources are released when the
    thread terminates.
  • By default, a thread is joinable.
  • To change from joinable to detached thread, use
  • int pthread_detach (pthread_t tid)

9
Other Basic Functions
  • One way for a thread to terminate is to call
  • void pthread_exit (void status)
  • status must not point to an object that is local
    to the calling thread.
  • Another two ways for a thread to terminate
  • The thread function returns
  • The main() function calls exit().
  • A thread fetches its own ID by calling
  • pthread_t pthread_self (void)

10
Readings
  • Stevens, UNIX Network Programming, Volumn 1 (2nd
    Edition)
  • Chapter 23
Write a Comment
User Comments (0)
About PowerShow.com