threads2

threads2 - Programming with POSIX Threads II CS 167 IV1...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 167 IV–1 Copyright © 2008 Thomas W. Doeppner. All rights reserved. Programming with POSIX Threads II
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 167 IV–2 Copyright © 2008 Thomas W. Doeppner. All rights reserved. Global Variables int IOfunc( ) { extern int errno; ... if (write (fd, buffer, size) == –1) { if (errno == EIO) fprintf (stderr, "IO problems ...\n"); ... return (0); } ... } Unix was not designed with multithreaded programming in mind. A good example of the implications of this is the manner in which error codes for failed system calls are made available to a program: if a system call fails, it returns –1 and the error code is stored in the global variable errno . Though this is not all that bad for single-threaded programs, it is plain wrong for multithreaded programs.
Background image of page 2
CS 167 IV–3 Copyright © 2008 Thomas W. Doeppner. All rights reserved. Coping Fix Unix’s C/system-call interface Make errno refer to a different location in each thread e.g. # define errno __errno(thread_ID) The ideal way to solve the “errno problem” would be to redesign the C/system-call interface: system calls should return only an error code. Anything else to be returned should be returned via result parameters. (This is how things are done in Windows NT.) Unfortunately, this is not possible (it would break pretty much every Unix program in existence). So we are stuck with errno . What can we do to make errno coexist with multithreaded programming? What would help would be to arrange, somehow, that each thread has its own private copy of errno . I.e., whenever a thread refers to errno , it refers to a different location from any other thread when it refers to errno . What can we do? As shown in the slide, we might use the C preprocessor to redefine errno as something a bit more complicated—references to errno result in accessing a function that retrieves this thread’s private errno value. This is how things are actually done in Linux (and other implementations of POSIX threads). Please see the textbook for information on how this approach is generalized.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 167 IV–4 Copyright © 2008 Thomas W. Doeppner. All rights reserved. Shared Data Thread 1: printf ("goto statement reached"); Thread 2: printf ("Hello World\n"); Printed on display: go to Hell Yet another problem that arises when using libraries that were not designed for multithreaded programs concerns synchronization. The slide shows what might happen if one relied on the single-threaded versions of the standard I/O routines.
Background image of page 4
CS 167 IV–5 Copyright © 2008 Thomas W. Doeppner. All rights reserved. Coping Wrap library calls with synchronization constructs Fix the libraries To deal with this printf problem, we must somehow add synchronization to printf (and all of the other standard I/O routines). A simple way to do this would be to supply wrappers for all of the standard I/O routines ensuring that only one thread is operating on any particular stream at a time. A better way would be to do the same sort of thing by fixing the
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/03/2010 for the course CS 168 taught by Professor Jj during the Spring '10 term at Brown.

Page1 / 36

threads2 - Programming with POSIX Threads II CS 167 IV1...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online