Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

After reviewing some material on POSIX threads (pthreads) : A) Describe the majo

ID: 3815682 • Letter: A

Question

After reviewing some material on POSIX threads (pthreads) :

A) Describe the major functions one would use in the pthreads library to write a multithreaded application.

B)How to override the default thread scheduling mechanism.

C) Can pthreads be assigned to specific processors on the alphaserver? If yes, how do you do it and display a screenshot of a thread and its related process?

D)What is the default scheduling mechanism for pthreads on the alphaserver?

E)If pthreads are used on a single core computer (e.g., Beaglebone) is the OS smart enough to know to not to block the process on an I/O request when another thread is available to be scheduled.

Explanation / Answer

A Ans]

Tries to lock a MuTex.

The function pthread_mutex_trylock is identifierentical to pthread_mutex_lock except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call returns immediately.

Unlocks a MuTex.

The pthread_mutex_unlock function releases the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex's type attribute. If there are threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock is called, resulting in the mutex becoming available, the scheduling policy is used to determine which thread shall acquire the mutex.

The pthread_mutex_unlock function releases the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex's type attribute. If there are threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock is called, resulting in the mutex becoming available, the scheduling policy is used to determine which thread shall acquire the mutex.

B Ans]

  #include <assert.h>  
  #include <pthread.h>  
       
  void* PosixThreadMainRoutine(void* data)  
  {  
      // Do some work here.  
       
      return NULL;  
  }  
       
  void LaunchThread()  
  {  
      // Create the thread using POSIX routines.  
      pthread_attr_t  attr;  
      pthread_t       posixThreadID;  
      int             returnVal;  
       
      returnVal = pthread_attr_init(&attr);  
      assert(!returnVal);  
      returnVal = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);  
      assert(!returnVal);  
       
      int     threadError = pthread_create(&posixThreadID, &attr, &PosixThreadMainRoutine, NULL);  
       
      returnVal = pthread_attr_destroy(&attr);  
      assert(!returnVal);  
      if (threadError != 0)  
      {  
           // Report an error.  
      }  
  }