{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs552-s10-pthreads

cs552-s10-pthreads - POSIX Threads(Pthreads Operating...

Info icon This preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style 1/20/11 POSIX Threads (Pthreads) Operating Systems CS 552 Spring 2010 Kenneth Chiu
Image of page 1

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

View Full Document Right Arrow Icon
1/20/11 Compiling Include pthread.h . Solaris cc –mt … -lpthread Linux gcc –pthread … Do not manually define _REENTRANT , or link against any libraries! It’s very commonly done, but is actually
Image of page 2
1/20/11 ALWAYS CHECK YOUR RETURN CODES WITH AT It will save you much, much time! ec = pthread_some_function(...); assert(ec == 0); ec = pthread_some_function(...); assert(ec == 0); I have learned this from painful experience!
Image of page 3

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

View Full Document Right Arrow Icon
1/20/11 Thread-Safety What does thread-safety mean? What about signals? Can I call any thread-safe function from a signal handler? Is this function thread safe? void foo() { lock(&mutex); // Do something. unlock(&mutex); } Can it be called from a signal
Image of page 4
1/20/11 A function that can always be re- entered, even when currently executing it, is reentrant. Is every reentrant function thread-safe? Is every thread-safe function re-entrant? Sometimes a different definition of thread-safe is used: A function is thread- safe if protected access to it will work. Sources of problems: Returning static data: const char *ctime(const time_t *clock);
Image of page 5

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

View Full Document Right Arrow Icon
1/20/11 Creation
Image of page 6
1/20/11 Creation int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); Example: extern "C" void *run(void *data) { … } // Use default attributes, pass 1234 as void *. int ec = pthread_create(&tid, NULL, run, (void *) 1234); assert(ec == 0);
Image of page 7

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

View Full Document Right Arrow Icon
1/20/11 Thread Attributes Stack characteristics: Size: int pthread_attr_setstacksize(&attr, size_t); Can provide the stack itself: int pthread_attr_setstackaddr(&attr, void *stack); User level or kernel threads: pthread_attr_setscope(&attr,
Image of page 8
1/20/11 Passing Data Passing data: pthread_create(&tid, NULL, run, (void *) 123); void *run(void *vp) { int i = (int) vp; // int i = (int) (long) vp; ... } How do we pass more than one variable? Suppose we want to pass a string and an integer?
Image of page 9

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

View Full Document Right Arrow Icon
1/20/11 We can pass a pointer to a struct. struct Data { char name[100]; int count; }; void *thread_func(void *vp) { fprintf(stderr, "%s\n", ((Data *) vp)->name); ... } int main(int argc, char *argv[] { ... Data data = {“”, 0}; for (int i = 0; i < n_threads; i++) { sprintf(data.name, “Thread:%04d”, i); pthread_create(&tid[i], 0, thread_func, &data); data.count++; } ... } [Show passing_data_thread.cpp .] Cannot pass on stack, unless we make sure that the variable persists on the
Image of page 10
1/20/11 Could be done using a semaphore. struct Data { char name[100]; int count; }; void *thread_func(void *vp) { char name[100]; strcpy(name, ((Data *) vp)->name); up(sync_sem); fprintf(stderr, "%d\n", name); ... } int main(int argc, char *argv[] { ... Data data = {“”, 0}; for (int i = 0; i < n_threads; i++) { sprintf(data.name, “Thread:%04d”, i); pthread_create(&tid[i], 0, thread_func, &data); down(sync_sem); data.count++; } ... } Other ways?
Image of page 11

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

View Full Document Right Arrow Icon
1/20/11 Simple solution is to allocate memory.
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern