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

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,...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
•Modify p4.c so that the output file p4.output is created but also displayed to standard output...
•Modify p4.c so that the output file p4.output is created but also displayed to standard output ( the screen ). This should be done by another instance of exec(). •Implement the pipe() command to do the following: $> grep –o else p4.c | wc –l p4.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <sys/wait.h> int main(int argc, char *argv[]) { int rc = fork(); if (rc < 0) {     // fork failed     fprintf(stderr, "fork...
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. This is a good reference for how throwing exceptions works in java. I would encourage you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT