Solution to Assignment 3.pdf

Hints tips 1 you must use a synchronization method to

This preview shows page 4 - 5 out of 5 pages.

Hints & Tips: 1) You must use a synchronization method to ensure that the "world" thread runs after the "hello" thread. 2) You must use a synchronization method to assure that the main thread does not execute until after the "world" thread. /* Include Files */ #include <stdio.h> /* External References */ extern int world( void ); extern int hello( void ); int main( int argc, char *argv[] ) { world(); hello(); printf( "\n" ); return( 0 ); } /* world - print the "world" part. */ int world( void ) { printf( "world" ); return( 0 ); } /* hello - print the "hello" part. */ int hello( void ) { printf( "hello " ); return( 0 ); } Answer: #include <stdio.h> #include <stdlib.h> #include <pthread.h> /* External References */
Image of page 4

Subscribe to view the full document.

void *world( void* ); void *hello( void* ); int main( int argc, char *argv[] ) { int rc1, rc2; pthread_t thread1, thread2; if((rc1 = pthread_create( &thread1, NULL, hello, NULL))){ printf("Thread1 is not created"); exit(EXIT_FAILURE); } pthread_yield(); if((rc2 = pthread_create( &thread2, NULL, world, NULL))){ printf("Thread2 is not created"); exit(EXIT_FAILURE); } //pthread_exit(NULL); pthread_join( thread1, NULL); pthread_join( thread2, NULL); printf( "\n" ); return 0 ; } /* world - print the "world" part. */ void *world( void*) { printf( "world" ); //return 0 ; } /* hello - print the "hello" part. */ void *hello( void* ) { printf( "hello " ); //return 0 ; }
Image of page 5
  • Fall '16
  • ZHANG

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