Question

In: Computer Science

Giva details on explanation too (I.e. NOT just basic knowledge of os) Can you please build...

Giva details on explanation too (I.e. NOT just basic knowledge of os)

Can you please build a code for part1 to part8?

- After child is forked and process is created, both parent and child are attached with shared memory segment on their address in part1, part2.
- Each will go on for 10 iterations to access the shared memory as follows-> on i-th iteration (0<=i<10), the parent will first update the call of a[i]= i.
- And let child read the updated value by reading b[I].
- Use SIGSTOP and SIGCONT signals to make sure whenever the parent updates one entry in the shared memory the child should read the updated value afterwards before parent and child move on to next iteration.

Functions to add:
1. Creating Shared memory
2. Attaching shared memory
3. De-attach shared memory

System call&library allowed : kill(), getpud(), wait(), exit(), shmctl()

-------------------Sample code here------------------
int main(){
   pid_t pid;
   int shmid, status;
   int *a, *b, i;
  
/*part1: Initialization*/
  
   pid = fork();
   if (pid==0){ //child
      
       /*part2: Child process before entering for loop*/
      
       for (I=0;i<10;i++){
  
           /*part3: Implement correct code leading right output*/
          
           printf("\t Child iterating %d time, reading b[%d] = %d.\n", I, I, b[I]);
       }
       /*part4: Implement code here*/
   }
   else{ //parent
       /*part5: Parent's process before for-loop*/
      
       sleep(1);
       for (I=0;i<10;i++){
           /*part6: implement your code here*/
           a[I] = I;
           printf("Parent iteratin %d time, writing a[%d] = %d.\n", I, I, a[I]);
          
          /*part7: Implement code here*/
       }
   /*part8: Add code before the end of the program*/
}

-------------------------------------------------------------------------

Expected output:
When you run ./main

Parent iterating 0 time, writing a[0] = 0
   Child iterating 0 time, reading b[0] = 0
Parent iterating 0 time, writing a[1] = 1
   Child iterating 0 time, reading b[1] = 1
Parent iterating 0 time, writing a[2] = 2
   Child iterating 0 time, reading b[2] = 2
Parent iterating 0 time, writing a[3] = 3
   Child iterating 0 time, reading b[3] = 3
Parent iterating 0 time, writing a[4] = 4
   Child iterating 0 time, reading b[4] = 4
Parent iterating 0 time, writing a[5] = 5
   Child iterating 0 time, reading b[5] = 5
Parent iterating 0 time, writing a[6] = 6
   Child iterating 0 time, reading b[6] = 6
Parent iterating 0 time, writing a[7] = 7
   Child iterating 0 time, reading b[7] = 7
Parent iterating 0 time, writing a[8] = 8
   Child iterating 0 time, reading b[8] = 8
Parent iterating 0 time, writing a[9] = 9
   Child iterating 0 time, reading b[9] = 9

(I.e. Direct explanations not basic knowledge of os)

Can you please build a code for part1 to part8?

After child is forked and process is created, both parent and child are attached with shared memory segment on their address in part1, part2.
Each will go on for 10 iterations to access the shared memory as follows-> on i-th iteration (0<=i<10), the parent will first update the call of a[i]= i.
And let child read the updated value by reading b[I].
Use SIGSTOP and SIGCONT signals to make sure whenever the parent updates one entry in the shared memory the child should read the updated value afterwards before parent and child move on to next iteration.

Functions to add:
1. Creating Shared memory
2. Attaching shared memory
3. De-attach shared memory

System call&library allowed : kill(), getpud(), wait(), exit(), shmctl()

-------------------Sample code here------------------
int main(){
   pid_t pid;
   int shmid, status;
   int *a, *b, i;
  
/*part1: Initialization*/
  
   pid = fork();
   if (pid==0){ //child
      
       /*part2: Child process before entering for loop*/
      
       for (I=0;i<10;i++){
  
           /*part3: Implement correct code leading right output*/
          
           printf("\t Child iterating %d time, reading b[%d] = %d.\n", I, I, b[I]);
       }
       /*part4: Implement code here*/
   }
   else{ //parent
       /*part5: Parent's process before for-loop*/
      
       sleep(1);
       for (I=0;i<10;i++){
           /*part6: implement your code here*/
           a[I] = I;
           printf("Parent iteratin %d time, writing a[%d] = %d.\n", I, I, a[I]);
          
          /*part7: Implement code here*/
       }
   /*part8: Add code before the end of the program*/
}

Expected output:
When you run ./main

Parent iterating 0 time, writing a[0] = 0
   Child iterating 0 time, reading b[0] = 0
Parent iterating 0 time, writing a[1] = 1
   Child iterating 0 time, reading b[1] = 1
Parent iterating 0 time, writing a[2] = 2
   Child iterating 0 time, reading b[2] = 2
Parent iterating 0 time, writing a[3] = 3
   Child iterating 0 time, reading b[3] = 3
Parent iterating 0 time, writing a[4] = 4
   Child iterating 0 time, reading b[4] = 4
Parent iterating 0 time, writing a[5] = 5
   Child iterating 0 time, reading b[5] = 5
Parent iterating 0 time, writing a[6] = 6
   Child iterating 0 time, reading b[6] = 6
Parent iterating 0 time, writing a[7] = 7
   Child iterating 0 time, reading b[7] = 7
Parent iterating 0 time, writing a[8] = 8
   Child iterating 0 time, reading b[8] = 8
Parent iterating 0 time, writing a[9] = 9
   Child iterating 0 time, reading b[9] = 9

Solutions

Expert Solution


#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>



int main(){
   pid_t pid;
   int shmid, status;
   int *a, *b, i;
    
   shmid = shmget(IPC_PRIVATE,10*sizeof(int),0777|IPC_CREAT);
/*part1: initialization*/
  
   pid = fork();
   if (pid==0){ //child
        b = (int *) shmat(shmid, 0, 0);
       /*part2: Child process before entering for loop*/
      
       for (i=0;i<10;i++){
            kill(getpid(),SIGSTOP);
           /*part3: implement correct code leading right output*/
          
           printf("\t Child iterating %d time, reading b[%d] = %d.\n", i, i, b[i]);
           kill(getppid(),SIGCONT);
       }
       shmdt(b);
       /*part4: implement code here*/
   }
   else{ //parent
       /*part5: Parent's process before for-loop*/
      
//        sleep(1);
        a = (int *) shmat(shmid, 0, 0);
       for (i=0;i<10;i++){
           /*part6: implement your code here*/
           if(i>0)
               kill(getpid(),SIGSTOP);
           a[i] = i;
           printf(" \tParent iteratin %d time, writing a[%d] = %d.\n", i, i, a[i]);
           kill(pid,SIGCONT) ;         
          /*part7: implement code here*/
       }
       shmdt(a);
   /*part8: Add code before the end of the program*/
    }
}


Related Solutions

Please show all details and you can use excel to build some tables. Your goal is...
Please show all details and you can use excel to build some tables. Your goal is to figure out whether starting a microbrewery in Nova Scotia is a good idea or not. You need to perform net present value analysis. In addition, you need to estimate payback period, discounted payback, internal rate of return and profitability index. If you cannot estimate a certain parameter, indicate why. The inputs for your analysis are as follows: you will run the business for...
long explanation: Can fundamental analysis be profitable, or market just too efficient to justify the effort?...
long explanation: Can fundamental analysis be profitable, or market just too efficient to justify the effort? Being familiar with P/E ratios by now, what has happened to the values for the other relative valuation ratios – that is, the P/BV, P/CF, and P/S ratios?
Can someone please give details on how you solved them please! Your firm just received an...
Can someone please give details on how you solved them please! Your firm just received an order from a customer for $50,000 of widgets. You have been tasked with evaluating the effect of time value of money on your firm’s cash flows (on a transaction-by-transaction basis). To fulfill this order your firm will need to produce and transport the widgets. This will take 50 days. Assume that the widgets will cost $30,000 in COGS; your firm is given 20 days...
Please respond too this statement in 1-2 paragraphs (Explanation please) on on why you agree with...
Please respond too this statement in 1-2 paragraphs (Explanation please) on on why you agree with it and what other additionaly information you can add too help improve it. , will give positive rating. anything will do please The public good identified in the video is fisheries. This has a characteristic of a public good because it tends to get treated as a common property resource. Theres a race too fish and who's going to get to the fish first...
Can you please tell me just the right answer for each question. Do not need explanation....
Can you please tell me just the right answer for each question. Do not need explanation. Thank you! QUESTION 1 The ____ strategies assume that people are rational and follow their own self-interest. ? Empirical-rational? Normative re-educative Power-coercive? Normative-educative 10 points    QUESTION 2 In the ____ change outcomes image, some, but not all, change intentions are achievable. Intended? Partially intended? Unintended? Partially unintended 10 points    QUESTION 3 The ____ image is based on an image of management as...
You may build some tables for this assignment in Excel and please show all details. If...
You may build some tables for this assignment in Excel and please show all details. If you use excel, please just copy and paste. Your goal is to figure out whether starting a microbrewery in Nova Scotia is a good idea or not. You need to perform net present value analysis. In addition, you need to estimate payback period, discounted payback, internal rate of return and profitability index. If you cannot estimate a certain parameter, indicate why. The inputs for...
How can you build conceptual knowledge related to the 10 themes in social studies for your...
How can you build conceptual knowledge related to the 10 themes in social studies for your students? Share two examples.
Desktop and Server OS Vulnerabilities a long and comprehensive explanation by typing please 1.IDENTIFY SPECIFIC VULNERABILITIES...
Desktop and Server OS Vulnerabilities a long and comprehensive explanation by typing please 1.IDENTIFY SPECIFIC VULNERABILITIES AND EXPLAIN WAYS TO FIX THEM 2.EXPLAIN WHAT RED HAT IS OS. TO LINUX WHAT DOES RED HAT DO?
can you please summrize the prolmes in short paragraph please? Is it too late for Tesla?...
can you please summrize the prolmes in short paragraph please? Is it too late for Tesla? What may have started as an ill-advised bit of summer whimsy — Elon Musk’s tweet on Aug. 7 suggesting that, as Tesla’s chief executive officer and its largest shareholder, he was going to take the company private and had the “funding secured” to do so — has turned into a full-blown crisis. There’s the unwanted scrutiny: Thanks to Mr. Musk’s tweet, Tesla is the...
Assignment Details Please use your own experiences and the knowledge you have gained from this week’s...
Assignment Details Please use your own experiences and the knowledge you have gained from this week’s readings to answer the following topics and questions. You may also use information that you find in the textbook, AIU’s library or the Internet to support your discussion. Make sure you use economic concepts in your main contribution.   Think of items that you have purchased in a market: Big ticket items like a house, or an apartment rental, or a car; Or ordinary items...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT