Question

Debugging and testing multithreaded programs is made more difficult compared to dealing with single-threaded programs by...

Debugging and testing multithreaded programs is made more difficult compared to dealing with single-threaded programs by:

1. the necessity to divide activities into separate and concurrent tasks

2. the existence of many more different execution paths made possible by parallel thread execution

3. multithreading library APIs with confusing semantics

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Consider the following code that creates N threads using the POSIX threading library. The thread function threadFun receives as parameter a thread index (from 0 to N-1).

#define NTHREADS 4 // number of test threads
void* threadFun(void* p) {
    LINE A
     // remainder of the function is irrelevant for this question
}
void main() {
  int i;
  pthread_t tid[N]; 
  pthread_attr attr;
  pthread_attr_init(&attr);
  for (i=0; i

What is the right set of statements for LINE A and LINE B that correctly initialize the thread index variable from 0 to N-1, regardless of the timing of each thread's scheduling? HINT: watch for memory that can be modified inadvertently by other threads.

1.

LINE A: index = *(int)p;

LINE B: pthread_create(&tid[i], &attr, threadFun, &i);

2.

LINE A: index = (int)*p;

LINE B: pthread_create(&tid[i], &attr, threadFun, (void*)i);

3.

LINE A: index = (int)p;

LINE B: pthread_create(&tid[i], &attr, threadFun, (void*)i);

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What kind of thread cancellation is demonstrated in the threadFun() thread function? int done = 0; // global variable // the thread function void* threadFun(void* p) { ... while (! done) { ... // do some work } pthread_exit(0); }

1. deferred cancellation

2. loop cancellation

3. forceful cancellation

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What does NOT belong specifically to a thread, but is shared by all threads of a process (pick one or more):

1. data section

2. stack

3. program counter

4. contents of the general purpose registers

5. heap

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What is NOT an advantage of using a thread pool to implement a multithreaded application?

1. The thread pool size can be adjusted based on demand from concurrent requests.

2. Using an existing thread to service a request takes less time than waiting to create a new thread.

3. Threads in the pool share a common request queue that requires synchronized access.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. = Option 2

Debugging and testing multithreaded programs is made more difficult compared to dealing with single-threaded programs by the existence of many more different execution paths made possible by parallel thread execution.

2. = option A

LINE A: index = *(int)p;

LINE B: pthread_create(&tid[i], &attr, threadFun, &i); is the correct answer.

3 = Loop Cancellation

Loop Cancellation is the type of  thread cancellation is demonstrated in the threadFun() thread function? int done = 0; // global variable // the thread function void* threadFun(void* p) { ... while (! done) { ... // do some work } pthread_exit(0); }

4. Option A = Data Section and Program Counter

Options A and D

These are the options that NOT belong specifically to a thread, but is shared by all threads of a process

5.Option C

Threads in the pool share a common request queue that requires synchronized access is one of the not an advantages.

Add a comment
Know the answer?
Add Answer to:
Debugging and testing multithreaded programs is made more difficult compared to dealing with single-threaded programs by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n)...

    so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...

  • i want to fix the solution and provide an explanation why the pthread_join is not working...

    i want to fix the solution and provide an explanation why the pthread_join is not working as intended? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ void* PrintHello(void* data) { pthread_t tid = (pthread_t)data; /* data received by thread */ pthread_join(tid, NULL); /* wait for thread tid */ printf("Hello from new thread %u - got %u\n", pthread_self(), data); pthread_exit(NULL); /* terminate the thread */ } /* like any C program, program's execution...

  • In C using the following 2 files to create a 3rd file that uses multiple threads...

    In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems....

    a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class {    struct class *next;    int id;    int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...

  • Help with a Parallel and Distributed Programming assignment. In this assignment, you will be expl...

    Help with a Parallel and Distributed Programming assignment. In this assignment, you will be exploring different methods of counting the prime numbers between 1 and N. You will use 8 threads, and each will be given a range in which to count primes. The question is: where do you store your counter? Do you use a local variable? Do you use a global variable? Please use the following function to determine whether an integer number is a prime. // Return...

  • How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses...

    How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex).      Replace the codes (for mutex) by the codes (for reader-writer lock).            To initialize reader-writer lock, pthread_rwlock_initializer.            At the end,...

  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...

    Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std; void *PrintHello(void *arg) { int actual_arg = *((int*) arg); cout << "Hello World from thread with arg: " << actual_arg << "!\n"; return 0; } int main() { pthread_t id; int rc; cout << "In main: creating thread \n"; int t = 23; rc = pthread_create(&id, NULL, PrintHello, (void*) &t); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc <<...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT