Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please provide answers and explanation. Suppose a friend has a library function gethost(const char *name): hostaddr * gethost(const char *name); available for you. If the
Please provide answers and explanation.
Suppose a friend has a "library" function gethost(const char *name): hostaddr * gethost(const char *name); available for you. If the input is a hostname (like "condor.depaul.edu"), it returns a pointer to a struct: typedef struct \{ char host 4[16]; unsigned int addr; \} hostaddr; Where host4 member is the dotted ip adress (e.g. "140.192.1.66") and the addr member is the corresponding 4 byte integer (140 * 2563+1922562+1256+66) However, the struct pointed to is declared in the library function static and so this function is not thread safe. You want to use this gethost library routine in a a thread function (so multiple threads may be calling gethost at the same time. Assume a semaphore, mutex is accessible to the threads (i.e. declared globally) and was initialized with value 1. The thread can declare a local variable of type hostaddr and use a lock-copy technique so that multiple threads can use the gethost library routine. Rewrite the thread function using the lock-copy technique so that gethost can be used in a thread-safe way. void * thrfnc(void *argp) \{ char *name =( char *) argp; hostaddr h; host *hptr = \&h; hostaddr *hp; / Lock-Copy Solution here * / / No change to remaining code */ if (hptr == NULL) { pthread_exit(1); } /* Rest of the function continues to use hptr and the hostaddr it points to */ \}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started