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

Study the code and explain the communication process. #include <sys/shm.h> #incl

ID: 3796160 • Letter: S

Question

 Study the code and explain the communication process.   #include <sys/shm.h> #include <sys/stat.h> #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>   #define MAX_SEQUENCE 10 // Max values to store in shared memory  #define MIN_SEQUENCE 2  // Min value the user can enter  //shared memory:  // 1) holds an array of numbers  // 2) holds how many numbers are in the array  typedef struct {  int fib_seq[MAX_SEQUENCE];  int sequence_size;  } shared_data;   //MAIN function  int main(int argc, char *argv[]) {   pid_t pid;    //process ID  int segment_id; //Shared Memory ID  shared_data *mem; //Shared Memory Pointer  //check to validate atleast two arguments  if(argc != 2) {  printf("USAGE ERROR: [0-9] ");  exit(0);  }  //validate the input is not larger then the MAX  if(atoi(argv[1]) > MAX_SEQUENCE) {  printf("Max Input Size: %d ", MAX_SEQUENCE);  exit(0);  }   //validate the input is not smaller then the MIN  if(atoi(argv[1]) < MIN_SEQUENCE) {  printf("Min Input Size: %d ", MIN_SEQUENCE);  exit(0);  }  // 1)  // 2)  // 3)   segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR|S_IWUSR);  //  mem = (shared_data *) shmat(segment_id,NULL,0);   //  mem->sequence_size = atoi(argv[1]);   // fork a child process  pid = fork();   if(pid < 0) { /* error occured */  fprintf(stderr, "Fork Failed ");  return 1;  }  else if(pid == 0) { /* child process */  int counter = 0;  printf("Child Fibonacci Sequence: ");   while(counter < mem->sequence_size) {  if(counter == 0){  //FIB of zero is always zero  mem->fib_seq[counter] = 0;  }  else if(counter == 1){  //FIB of one is always one  mem->fib_seq[counter] = 1;  }  else {  //The Fibonacci Sequence formula 'R = fib(n-1) + fib(n-2)'  //The first two numbers in the sequence are always 0 and 1.  //To get a value in the sequence you will want to take the previous  //two numbers and add them together. For example:  // b + a = c  // [fib(d-1) = c] + [fib(d-2) = b] = R  // fib(0) = 0  // fib(1) = 1  // fib(2): 1 + 0 = 1  // fib(3): 1 + 1 = 2  // fib(4): 2 + 1 = 3  // fib(5): 3 + 2 = 5  // The next Fibonacci number in the sequence will be '8'  mem->fib_seq[counter] = mem->fib_seq[counter - 1] + mem->fib_seq[counter - 2];  }  printf("%d ", mem->fib_seq[(counter)]);  counter++;  }  }  else { /* parent process */   //  wait(NULL);  //Print out shared memory  int count = 0;  printf(" Parent Fibonacci Sequence: ");  while(count < mem->sequence_size){  printf("%d ", mem->fib_seq[count]);  count++;  }   //  shmdt(mem);  //remove shared memory segment  shmctl(segment_id,IPC_RMID,NULL);  printf(" Complete ");  }  return 0;  } 

Explanation / Answer

A)


#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX_SEQUENCE 10
#define MIN_SEQUENCE 2
typedef struct {
int fib_seq[MAX_SEQUENCE];
int sequence_size;
} shared_data;
int main(int argc, char *argv[]) {
pid_t pid;
int segment_id;
shared_data *mem;
if(argc != 2) {
printf("USAGE ERROR: [0-9] ");
exit(0);
}
if(atoi(argv[1]) > MAX_SEQUENCE) {

printf("Max Input Size: %d ", MAX_SEQUENCE);

exit(0);

}
if(atoi(argv[1]) < MIN_SEQUENCE) {
printf("Min Input Size: %d ", MIN_SEQUENCE);
exit(0);
}
segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR|S_IWUSR);
mem = (shared_data *) shmat(segment_id,NULL,0);
mem->sequence_size = atoi(argv[1]);
pid = fork();
if(pid < 0) { /* error occured */
fprintf(stderr, "Fork Failed ");
return 1;
}
else if(pid == 0) { /* child process */
int counter = 0;
printf("Child Fibonacci Sequence: ");
while(counter < mem->sequence_size) {
if(counter == 0){
mem->fib_seq[counter] = 0;
}
else if(counter == 1){
mem->fib_seq[counter] = 1;
}
else {
mem->fib_seq[counter] = mem->fib_seq[counter - 1] + mem->fib_seq[counter - 2];
}
printf("%d ", mem->fib_seq[(counter)]);
counter++;
}
}
else { /* parent process */
wait(NULL);
int count = 0;
printf(" Parent Fibonacci Sequence: ");
while(count < mem->sequence_size){
printf("%d ", mem->fib_seq[count]);
count++;
}
shmdt(mem);
shmctl(segment_id,IPC_RMID,NULL);
printf(" Complete ");
}
return 0;
}