Homework chapter 4

docx

School

Jackson State University *

*We aren’t endorsed by this school

Course

OPERATING

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

2

Uploaded by idrisatajuddin

Report
Homework chapter 4 1. Which of the following components of program state are shared across threads in a multithreaded process? a. Register values b. Heap memory c. Global variables d. Stack memory In multithreaded process, the heap memory and global variables are shared whereas each thread has its own register values and stack memory. 2. The program show in Figure 4.23 uses the Pthreads API. What would be the output from the program at LINE C and LINE P? #include <pthread.h> #include <stdio.h> int value = 0; void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) { int pid; pthread_t tid; pthread_attr_t attr; pid = fork(); if (pid == 0){ /* child process*/ pthread_attr_init(&attr); pthread_create(&tid,&attr,runner,NULL); pthread_join(tid,NULL); printf(“CHILD: value = %d” , value); /* LINE C */ } else if (pid > 0) { /* parent process */ wait(NULL); printf(“PARENT: value = %d” , value); /* LINE P */ } } void *runner(void *param) { value = 5; pthread_exit(0); } Ans: At C= CHILD:value=5
At p=PARENT:value= 0 3. In Chapter 3, we discussed Google’s Chrome browser and its practice of opening each new tab in a separate process. Would the same benefits have been achieved if, instead, Chrome had been designed to open each new tab in a separate thread? Explain. No, the same benefits cannot be achieved by using separate thread for new tab because threads are allocated with shared memory spaces; thus, if we use separate thread for each page, if one webpage crashes, the entire application is affected. 4. Can a multithreaded solution using multiple user-level threads achieve better performance on a multiprocessor system than on a single-processor system? Explain. No, multiple processor system does not give any benefit in performance because the multiple thread system comprising of multiple user-level threads use only one processor as the operating system sees only a single process at a time regardless to how many processors they are. 5. Consider the following code segment: pid t pid; pid = fork(); if (pid == 0) { /* child process */ fork(); thread create( . . .); } fork(); a. How many unique processes are created? There are 6 unique processes created b. How many unique threads are created? There are 2 unique threads created using fork() in the if block
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help