In: Computer Science
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;
}
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;
}