
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
![Problem 2. In addition to POSIX semaphores, the POSIX threads standard provides another
useful synchronization primitives: the muter (type pthread_mutex_t) A mutex is used to wait for
it implement mutual exclusion. A thread locks a mutex variable by calling pthread_mutex_lock(),
and unlocks it by calling pthread_mutex_unlock(). When a thread calls pthread_mutex_lock()
while another thread has previously locked the same mutex but not yet unlocked it, the former
thread blocks until the latter thread calls pthread_mutex_unlock() on it.
a. Is there any difference between a binary semaphore and a pthread_mutex? Justify your answer.
b. Consider a program with multiple threads running this function:
struct {...} shared_data;
pthread_mutex_t m;
void *thread_func(void *arg) {
[update shared_data]; // critical section!
return NULL;
}
Assume the mutex is initialized before the threads are created. Add the necessary pthread_mutex_*
calls to the above code to ensure that only one thread is in the critical section at any time.
c. Consider the program below, where "..." represents code that updates a shared data structure.](https://content.bartleby.com/qna-images/question/2b2b0246-74c5-427f-bbae-6a253749f017/70482a6e-2a10-4ff4-8c6f-be3da7e1385b/1iesqip_thumbnail.png)
Transcribed Image Text:Problem 2. In addition to POSIX semaphores, the POSIX threads standard provides another
useful synchronization primitives: the muter (type pthread_mutex_t) A mutex is used to wait for
it implement mutual exclusion. A thread locks a mutex variable by calling pthread_mutex_lock(),
and unlocks it by calling pthread_mutex_unlock(). When a thread calls pthread_mutex_lock()
while another thread has previously locked the same mutex but not yet unlocked it, the former
thread blocks until the latter thread calls pthread_mutex_unlock() on it.
a. Is there any difference between a binary semaphore and a pthread_mutex? Justify your answer.
b. Consider a program with multiple threads running this function:
struct {...} shared_data;
pthread_mutex_t m;
void *thread_func(void *arg) {
[update shared_data]; // critical section!
return NULL;
}
Assume the mutex is initialized before the threads are created. Add the necessary pthread_mutex_*
calls to the above code to ensure that only one thread is in the critical section at any time.
c. Consider the program below, where "..." represents code that updates a shared data structure.

Transcribed Image Text:void *thread_func (void *arg) {
pthread_mutex_t mymutex;
int rv = pthread_mutex_init(&mymutex,NULL); // default attributes
}
rv = pthread_mutex_lock (&mymutex);
assert (!rv);
... // critical section
rv = pthread_mutex_unlock (&mymutex);
assert (!rv);
return NULL;
The main program creates and executes 4 threads running thread_func(). What is wrong
with the above code? (Hint: it has nothing to do with the variable rv or the call to assert.)
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- I have code in C++ that won't attach, but I continue to get this excpetion thrown, and am completly lost.arrow_forwardConsider the following data that are shared between two threads and are initialised before the threads are executed, as follows: boolean flag = true; int x = 50; Thread 1 is defined as follows: void run_thread1() { while (flag) { } print x; } Thread 2 is defined as follows: void run_thread2() { x = 60; flag = false; } Let us assume that, as there are no data dependencies between the variables flag and x, it is possible that a processor may reorder the instructions for Thread 2. Which of the following statements regarding the possible output of Thread 1 is TRUE? Thread 1 could enter an infinite loop The output could only be 50 The output could either be 50 or 60 None of the mentioned The output could only be 60arrow_forwardComplete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the screenshot. You must complete the missing parts as shown in the screenshot as well as complete the missing codes of producer and consumer. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> #include <fcntl.h> #define MAX_LENGTH_CAP 100 #define INIT -127 #define UNDERFLOW (0x80 + 0x02) #define OVERFLOW 0x80 + 0x01 #define BADPTR (0x80 + 0x03) #define CONSUMER_TERMINATION_PROBABILITY 40 #define PRODUCER_TERMINATION_PROBABILITY 30 // ============= LOCKED…arrow_forward
- Complete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the Figure below. You must complete the missing parts between lines 226-261 as shown in the screenshot.arrow_forwardWrite java code to create a thread by (extending), theprogram create 3 thread that displaying “fatmah” and thenumber of thread that is running.Rewrite the above program by implementing the RunnableInterfacearrow_forwardMulti-tasking can not be achieved with a single processor machine. True False 2. Async/Await is best for network bound operations while multi-threading and parallel programming is best for CPU-bound operations. True False 3. The following is a characteristic of an async method:The name of an async method, by convention, ends with an "Async" suffix. True False 4. Using asynchronous code for network bound operations can speed up the time needed to contact the server and get the data back. True False 5. Asynchronous programming has been there for a long time but has tremendously been improved by the simplified approach of async programming in C# 5 through the introduction of: The Task class True Falsearrow_forward
- Subject Name: Advanced Object-Oriented Programming 1. Complete the below given code (1) class NyThread Thread t: Runnable ( MyThread (String n, int pl t (2) (3) : / creates teh Thread t with nane n : // changen the priority to p Syster.out.printin ("Thread "+n+* created with priority "+p): t. (4) : // exexcute the thread t. I // The only abatarct aethed in Runnable public for (int 1=10: 1>=1: 1--) { Syaten.out.prántin ("Thread *t.getName ()+"\e i - "a) : (5) try { (6) :/ ma ke the Thead t eleep to 2 seconds } catch (Exception e) Syater.out.printin ("Exception in Child"): System.out.printin ("Thread "+t. getName ()+" 1s Exiting"): public statie void main (Stringt) arga) { MyThread t1, 12; ti - new MyThread ("R1", 7): t2 = new MyThread ("R1", 3): try ( :// 2 atatements to aake the Threads ti a v2 exit at same time (8) ) catoh (Exception e) ( Syster.out.printin("Exception in main"): System.out.printin ("Thread Main is Exiting"): 1. 2. 3. 4. 6. 7. 8. 5.arrow_forwardFor this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma omp parallel num_threads(no of threads) The program should take n and the number of threads to use as command line arguments: ./parallel_vector_addition Where n is the length of the vectors and threads is the number of threads to be created. Pseudocode for Assignment mystart = myid*n/p; // starting index for the individual thread myend = mystart+n/p; // ending index for the individual thread for (i = mystart; i < myend; i++) // each thread computes local sum do vector addition // and later all local sums combined. As an input vector A,…arrow_forwardin java fx Write a program that launches 1,000 threads. Each thread adds 1 to a variable sum that initially is 0. You need to pass sum by reference to each thread. In order to pass it by reference, define an Integer wrapper object to hold sum. Run the program with and without synchronization to see its effect.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education