Question

In: Computer Science

C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...

C Programming: POSIX: Producer / Consumer

Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence.

Producer.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <fcntl.h>

#include <sys/shm.h>

#include <sys/stat.h>

#include <sys/mman.h>

#include <zconf.h>

int main() {

/* The size (in bytes) of shared-memory object */

const int SIZE = 4096;

/* The name of shared-memory object */

const char *Obj = "Shm";

/* The shared-memory file descriptor */

int shm_fd;

/* The pointer to shared-memory object */

void *ptr;

/* Create the shared-memory object */

shm_fd = shm_open(Obj, O_CREAT | O_RDWR, 0666);

/* Configure the size of the shared-memory object */

ftruncate(shm_fd, SIZE);

/* Map the shared-memory object in the address space of the process */

ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

if (ptr == MAP_FAILED) {

printf("Map failed\n");

return -1;

}

printf("Input a positive number: \n");

/* code goes here */

fgets(ptr, SIZE, stdin);

printf("Producer: Writing the message to the shared memory is done! \n");

return 0;

}

Consumer.c

#include <stdio.h>

#include <stdlib.h>

#include <fcntl.h>

#include <sys/shm.h>

#include <sys/stat.h>

#include <sys/mman.h>

int main() {

/* The size (in bytes) of shared-memory object */

const int SIZE = 4096;

/* The name of shared-memory object */

const char *Obj = "Shm";

/* The shared-memory file descriptor */

int shm_fd;

/* The pointer to shared-memory object */

void *ptr;

/* Open the shared-memory object */

shm_fd = shm_open(Obj, O_RDONLY, 0666);

if (shm_fd == -1) {

printf("Shared memory failed\n");

exit(-1);

}

/* Map the shared-memory object in the address space of the process */

ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);

if (ptr == MAP_FAILED) {

printf("Map failed\n");

exit(-1);

}

/* Read from the shared-memory object */

printf("Consumer: The output sequence is: %s", (char *)ptr);

/* Remove the shared-memory object */

if (shm_unlink(Obj) == -1) {

printf("Error removing %s\n", Obj);

exit(-1);

}

return 0;

}

Solutions

Expert Solution

code for producer.c file that calculates the Fibonacci sequence and writes the sequence to the shared-memory object.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<zconf.h>
int main()
{
/* The size (in bytes) of shared-memory object */
const int SIZE = 4096;
/* The name of shared-memory object */
const char *Obj = "Shm";
/* The shared-memory file descriptor */
int shm_fd;
/* The pointer to shared-memory object */
void *ptr;
/* Create the shared-memory object */
shm_fd = shm_open(Obj, O_CREAT | O_RDWR, 0666);
/* Configure the size of the shared-memory object */
ftruncate(shm_fd, SIZE);
/* Map the shared-memory object in the address space of the process */
ptr = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED)
{
printf("Map failed\n");
return -1;
}
printf("Input a positive number: \n");
/* code goes here */
int a,b,m,n,i,j;
a=0; b=1;
printf("Enter the number of a Fibonacci Sequence:\n");
scanf("%d", &m);
if (m < 0)
printf("Please enter a non-negative integer\n");
else if (m> MAX_SEQUENCE)
printf("Please enter an integer less than 10\n");
int segment_id; //the identifier for the shared memory segment
int segment_size = sizeof(shared_data); //the size (in bytes) of the shared memory segment
segment_id = shmget(IPC_PRIVATE, segment_size, S_IRUSR | S_IWUSR); // allocate a shared memory segment
shared_data *shared_memory = shmat(segment_id, NULL, 0);

// attach the shared memory segment
printf("\nshared memory segment %d attached at address %p\n", segment_id, shared_memory);
shared_data->sequence_size = m;
pid_t pid;
pid = fork();
if (pid == 0)
{
printf("Child is producing the Fibonacci Sequence...\n");
shared_data->fib_sequence[0] = a;
shared_data->fib_sequence[1] = b;
for (i=2;i<shared_data->sequence_size;i++)
{
n=a+b;
shared_data->fib_sequence[i] = n;
a=b;
b=n;
}
printf("\nChild ends\n");
}
else
{
printf("Parent is waiting for child to complete...\n");
wait(NULL);
printf("Parent ends\n");
for(i=0;i<= shared_data->sequence_size;i++)
printf("%ld ", shared_data->fib_sequence[i]);
}

//now detach the shared memory segment
if ( shmdt(shared_memory) == -1)
{
fprintf(stderr, "Unable to detach\n");
}

fgets(ptr, SIZE, stdin);
printf("Producer: Writing the message to the shared memory is done! \n");
return 0;

}

code for consumer.c for output the sequence.

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<conio.h>

int main()
{

/* The size (in bytes) of shared-memory object */

const int SIZE = 4096;

/* The name of shared-memory object */

const char *Obj = "Shm";

/* The shared-memory file descriptor */

int shm_fd;

/* The pointer to shared-memory object */

void *ptr;

/* Open the shared-memory object */

shm_fd = shm_open(Obj, O_RDONLY, 0666);

if (shm_fd == -1)
{

printf("Shared memory failed\n");

exit(-1);

}

/* Map the shared-memory object in the address space of the process */

ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);

if (ptr == MAP_FAILED)
{

printf("Map failed\n");

exit(-1);

}

/* Read from the shared-memory object */

printf("Consumer: The output sequence is: %s", (char *)ptr);

/* Remove the shared-memory object */

if (shm_unlink(Obj) == -1)
{

printf("Error removing %s\n", Obj);

exit(-1);

}

return 0;

}


Related Solutions

**Keil uVision5 - ARM Cortex M0+ - Embedded C programming Modify the program below so that...
**Keil uVision5 - ARM Cortex M0+ - Embedded C programming Modify the program below so that the Red, Green, and Blue LEDs are switched ON and OFF in a sequence with one second delay for Red and Green LEDs, and 0.5 second delay for the Blue LED. #include "MKL25Z4.h" void delay(int n); int main (void) { SIM_SCGC5 |= SIM_SCGC5_PORTB(1); /* enable clock to Port B */ PORTB_PCR18 |=PORT_PCR_MUX(1); /* Configure PORTB ,pin 18 as GPIO ; set MUX*/ GPIOB_PDDR=(1UL <<...
In Java, Modify “Producer and Consumer Problem” from lecture note so that it can use all...
In Java, Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffer_size – 1” as in the lecture note. This program should work as follows: 1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffer size and counter limit. 2. The main program will then create two separate threads, producer and consumer thread. 3. Producer thread generates a random...
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
JAVA programming language Modify the following code. Make changes so that Movies can be sorted by...
JAVA programming language Modify the following code. Make changes so that Movies can be sorted by title ----------------------------------------------------------------- package Model; import java.time.Duration; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; // TODO - Modify the movie class so that Java knows how to compare two Movies for sorting by title public class Movie extends DataStoreObj { private String title; private String description; private LocalDate releaseDate; private Duration runningTime; private Rating rating; private List<Genre> genres = new ArrayList<>(); private List<Actor> actors = new...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the selection to the web page, the selection is also placed in the browser’s local storage. Use ‘select’ as the local-storage key. The value will be the name of the category that was selected or the empty string is no selection was made. (d) Add a button called ‘retrieve’. When it is clicked the local storage is read and the prior selection is shown on...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read. 2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
How can i modify my c code so that each number stored in the array is...
How can i modify my c code so that each number stored in the array is not the array index but the value of the array index converted to radians. I have already made a function for this converion above main(). Below is the code: #include <stdio.h> #include <stdlib.h> #include <math.h> float Deg2Rad (float degrees) { // Calculate & return value float Result; Result = ((M_PI*degrees)/180.0); return (Result); } int main(void) { // Declare variables int Array[90]; int i; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT