Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS450: Introduction to Parallel Programming

Assignment 2: Pthreads

2022

Question 1

Below is code that uses Pthreads. The program prints to the screen 4 lines. Assume these print statements are unbuffered. Answer the following regarding the program output:

Question (true/false): The code below has one possible output. If false, list 3 possible program outputs.

1   void*  do_work1(void*  arg);                                                                                                                            2   void*  do_work2(void*  arg);

3

4   int main(int  argc,  char*  argv[])  {                                                                                                             5          int  array[100];                                                                                                                                              6          int  sum;                                                                                                                                                            7         pthread_t  worker_thread1;                                                                                                                           8         pthread_t  worker_thread2;                                                                                                                           9          struct  arguments*  arg;

10

11

12          //  Create  thread1

13          if  (pthread_create(&worker_thread1,  nullptr,

14                                                                          do_work1,  (void*)  arg))  {

15                 fprintf(stderr,  "Error  while  creating  thread\n");

16                return  1; 17          }

18

19          //  Create  thread2

20          if  (pthread_create(&worker_thread2,  nullptr,

21                                                                          do_work2,  (void*)  arg))  {

22                 fprintf(stderr,  "Error  while  creating  thread\n");

23                return  1; 24          }

25

26          //  Join  with  thread

27          if  (pthread_join(worker_thread1,  nullptr))  {

28                 fprintf(stderr,  "Error  while  joining  with  child  thread  1\n");

29                return  1; 30          }

31

32          if  (pthread_join(worker_thread2,  nullptr))  {

33                 fprintf(stderr,  "Error  while  joining  with  child  thread  2\n");

34                return  1; 35          }

36

37          return  0;

38   }

39

40   void*  do_work1(void*  arg)  {                                                                                                                          41         printf("Starting  1\n");                                                                                                                               42         printf("Exiting  1\n");

43

44         return  nullptr;                                                                                                                                              45   }

46

47   void*  do_work2(void*  arg)  {                                                                                                                          48         printf("Starting  2\n");                                                                                                                               49         printf("Exiting  2\n");

50

51         return  nullptr;                                                                                                                                              52   }

Question 2

Below is code that uses Pthreads. The program prints to the screen 2 lines. Assume these print statements are unbuffered. Answer the following regarding the program output:

Question (true/false): The code below has one possible output. If false, what are the possible program outputs?

1   struct  arguments  {                                                                                                                                            2          int  value;                                                                                                                                                        3   };

4

5   void*  do_work(void*  arg);

6

7   int main(int  argc,  char*  argv[])  {

8          pthread_t  worker_thread1;

9          pthread_t  worker_thread2;

10          struct  arguments  *arg[2];

11

12          //  Build  argument  to  threads

13          arg[0]  =  (struct  arguments  *)  calloc(1,  sizeof(struct  arguments));

14          arg[0]->value  =  5;

15

16          arg[1]  =  (struct  arguments  *)  calloc(1,  sizeof(struct  arguments));

17          arg[1]->value  =  12;

18

19

20          //  Create  thread1

21          if  (pthread_create(&worker_thread1,  nullptr,

22                                                                          do_work,  (void*  )  arg[0]))  {

23                 fprintf(stderr,  "Error  while  creating  thread  1\n");

24                return  1; 25          }

26

27          //  Create  thread2

28          if  (pthread_create(&worker_thread2,  nullptr,

29                                                                          do_work,  (void*  )  arg[1]))  {

30                 fprintf(stderr,  "Error  while  creating  thread  2\n");

31                return  1; 32          }

33

34          //  Join  with  thread

35          if  (pthread_join(worker_thread1,  nullptr))  {

36                 fprintf(stderr,  "Error  while  joining  with  child  thread  1\n");

37                return  1; 38          }

39

40          if  (pthread_join(worker_thread2,  nullptr))  {

41                 fprintf(stderr,  "Error  while  joining  with  child  thread  2\n");

42                return  1; 43          }

44

45          return  0;

46   }

47

48   void*  do_work(void*  arg)  {

49          struct  arguments  *argument;

50          argument  =  (struct  arguments*)  arg;

51

52          int  val  =  argument->value;

53          printf("%d\n",  val);

54

Question 3

Below is code that uses Pthreads. The program prints to the screen 1 line. Assume these print statements are unbuffered. Answer the following regarding the program output:

Question (true/false): The code below has one possible output. If false, what are the possible program outputs?

2          int*  sum;

3          int  value; 4   };

5

7

12

13          int  sum=0;

14

19

23

30

37

42          }

43

44          if  (pthread_join(worker_thread2,  nullptr))  {

45                 fprintf(stderr,  "Error  while  joining  with  child  thread  2\n");

46                return  1; 47          }

48

49          printf("Sum:  %d\n",  sum);

50

51          return  0;

52   }

53

57

60

61          *sum  +=  val;

62

63          return  nullptr;

64   }

Question 4

Below is code that uses Pthreads. The program prints to the screen 2 lines. Assume these print statements are unbuffered. Answer the following regarding the program output:

Question (true/false): The code below has one possible output. If false, what are the possible program outputs?

3          int  val;

4          int*  count; 5   };

6

8


14

16          int  count  =  0;

17

18

21          arg[0]->mutex  =  &lock;

22          arg[0]->val  =  2;

23          arg[0]->count  =  &count;

24

25          arg[1]  =  (struct  arguments*)  calloc(1,  sizeof(struct  arguments));

26          arg[1]->mutex  =  &lock;

27          arg[1]->val  =  3;

28          arg[1]->count  =  &count;

29

30

31          //  Create  thread1

32          if  (pthread_create(&worker_thread1,  nullptr,

33                                                                          do_work,  (void*)  arg[0]))  {

34                 fprintf(stderr,  "Error  while  creating  thread  1\n");

35                return  1; 36          }

37

38          //  Create  thread2

39          if  (pthread_create(&worker_thread2,  nullptr,

40                                                                          do_work,  (void*)  arg[1]))  {

41                 fprintf(stderr,  "Error  while  creating  thread  2\n");

42                return  1; 43          }

44

45          //  Join  with  thread

46          if  (pthread_join(worker_thread1,  nullptr))  {

47                 fprintf(stderr,  "Error  while  joining  with  child  thread  1\n");

48                return  1; 49          }

50

51          if  (pthread_join(worker_thread2,  nullptr))  {

52                 fprintf(stderr,  "Error  while  joining  with  child  thread  2\n");

53                return  1; 54          }

55

56          return  0;

57   }

58

59   void*  do_work(void*  arg)  {

60          struct  arguments*  argument;

61          argument  =  (struct  arguments*)  arg;

62

63          pthread_mutex_t* mutex  =  argument->mutex;

64          int  val  =  argument->val;

65          int*  count  =  argument->count;

66

67          pthread_mutex_lock(mutex); 68          *count  +=  val;

69          printf("%d\n",*count);

70          pthread_mutex_unlock(mutex);

71