Advanced%20UNIX%20programming - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced%20UNIX%20programming

Description:

Memory, instruction, program counter, stack pointer, registers, open file descriptor, etc. ... files, signal handlers and signal dispositions, current working ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 9
Provided by: asri9
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Advanced%20UNIX%20programming


1
Advanced UNIX programming
  • Fall 2002
  • Instructor Ashok Srinivasan
  • Lecture 29

Acknowledgements The syllabus and power point
presentations are modified versions of those by
T. Baker and X. Yuan
2
Announcements
  • Reading assignment
  • Chapter 23
  • Project Outline due Monday
  • Midterm 2
  • Bonus points test
  • 20 Nov 2002
  • HW 4 will be announced Friday

3
Week 11 Topics
  • Threads
  • Basic thread functions
  • Some more thread functions
  • Thread-specific data
  • Mutual exclusion
  • Conditional variables

4
Threads
  • A thread is an independent sequence of execution
    of program code inside a process
  • A thread is often called a lightweight process
  • But it is NOT a process
  • It is something smaller than a process
  • The process context includes
  • Memory, instruction, program counter, stack
    pointer, registers, open file descriptor, etc.
  • A thread has less
  • Program counter, a stack, and a set of
    registers, etc.

5
Threads introduction (continued)
  • Threads share
  • Process instructions, most data, open files,
    signal handlers and signal dispositions, current
    working directory, user and group ID
  • Threads do not share
  • Thread ID, set of registers (including pc
    program counter -- and sp stack pointer),
    stack, errno, signal mask, priority
  • POSIX standard pthreads
  • Specifies the API and the way other POSIX
    interfaces deal with threads

6
Threads vs Processes
  • Advantages of threads
  • It is less expensive to create a thread
  • Inter-thread communication is much simpler and
    cheaper than inter-process communication
  • Thread programming can be dangerous
  • It is easy to make mistakes
  • Some system functions may break with threads

7
Basic thread functions
  • Thread creation
  • int pthread_create(pthread_t thread, const
    pthread_attr_t attr, void (start_routine)(void
    ), void arg)
  • Thread attributes (stack size, scheduling policy)
    can be modified
  • Use NULL for default
  • Returns 0 if successful, and an error number if
    it fails
  • EAGAIN System lacks resources
  • EINVAL Invalid attribute
  • EPERM Caller does not have appropriate
    permission
  • Pthread functions return error values
  • They do not set errno
  • New thread executes the function given by
    start_routine
  • arg is the argument to this function
  • The thread ID is stored in thread

8
Basic thread functions (continued)
  • Wait for a thread to terminate
  • int pthread_join(pthread_t thread, void
    value_ptr)
  • Note that the thread ID, and not a pointer to it,
    is given as the first argument
  • The return value of the function executed by the
    thread is stored in value_ptr
  • example1.c and example2.c
  • Link the thread program with lpthread
Write a Comment
User Comments (0)
About PowerShow.com