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

3. (15 points) Consider the code example for allocating and releasing processes

ID: 3790653 • Letter: 3

Question

3. (15 points) Consider the code example for allocating and releasing processes shown below. Note that the functions are not complete—comments show where detailed code would be required to allocate and release process resources. This code would be part of the kernel. #define MAX_PROCESSES 255 int number_of_processes = 0; /* An implementation of fork() would call this function */ int allocate_process() { int new_pid; if (number_of_processes == MAX_PROCESSES) return -1; else { // Code to allocate process resources and assign new_pid ++number_of_processes; return new_pid; } } /* An implementation of exit() would call this function */ void release_process() { // Code to release process resources --number_of_processes; } a. (7 points) Identify the race condition(s) in these two functions, assuming each function could be called by one (or more) kernel threads.. (In other words, determine what operation(s) involving shared data depend on the timing or ordering of different threads.) Give an example of the race condition causing incorrect results to support your answer. b. (8 points) Given a lock, mutex, with operations lock() and unlock(), indicate where these operations would have to be used to prevent the race condition(s).

Explanation / Answer

When you call into a function that expects a BSTR argument, you must allocate the memory for the BSTR before the call and release it afterwards. For example:

C++
HRESULT CMyWebBrowser::put_StatusText(BSTR bstr)C++
// shows using the Win32 function // to allocate memory for the string: BSTR bstrStatus = ::SysAllocString(L"Some text"); if (bstrStatus != NULL) { pBrowser->put_StatusText(bstrStatus); // Free the string: ::SysFreeString(bstrStatus); }

When you call into a function that returns a BSTR, you must free the string yourself. For example:

C++
HRESULT CMyWebBrowser::get_StatusText(BSTR* pbstr)C++
BSTR bstrStatus; pBrowser->get_StatusText(&bstrStatus); // shows using the Win32 function // to free the memory for the string: ::SysFreeString(bstrStatus);

When you implement a function that returns a BSTR, allocate the string but do not free it. The receiving the function releases the memory. For example:

C++
HRESULT CMyClass::get_StatusText(BSTR* pbstr) { try { //m_str is a CString in your class *pbstr = m_str.AllocSysString(); } catch (...) { return E_OUTOFMEMORY; } // The client is now responsible for freeing pbstr. return(S_OK); }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote