Bonus 5 points Implement a shared memory to enable inter process communication

Bonus 5 points implement a shared memory to enable

This preview shows page 7 - 10 out of 10 pages.

4. (Bonus 5 points) Implement a shared memory to enable inter-process communication between the parent process and process P. The shared memory should be able to hold 3 int items. The code of either a consumer or a producer, along with clear comment, are enough for full credit. Either one of the following three implementation is correct (Only the consumer code is shown. Proper comments and declarations of variables are needed for full credit)
Background image
Implementation 3 Problem 4 (12+5 points). Simply putting a final result will not earn full credit. You need to show your work to obtain the result.Understand the following code and answer the questions below. #include <pthread.h> #include <stdio.h> #include <types.h> int value = 0; void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) { pid_t 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 */ }
Background image
1. What would be the output from the program at LINE C and LINE P? 2. If this program is run at a single-core machine, why is creating a thread better than creating a process to handle the computation from LINE A to LINE B? 3. If this program is run at a multi-core machine, why would multi-threading usually outperform single-threading? 4. (Bonus 5 points) If the target computation (LINE A to LINE B) is to calculate the average of a set of numbers, would multi-threading help the performance? Why?
Background image
Image of page 10

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture