Fibonacci shared memory processes between two c files - c

Hello I have a single file in c that shares memory from the parent to child but I need my code separated into two separate files while still sharing the memory. I need the parent to create the shared memory and get the input of the fib number. Then the child process opens the share memory object to read the value n and overwrite the value n by the value of fib(n). and displays the fib series. This is what I have now
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
// So we could use other sizes without editing the source.
#ifndef MAX_SEQUENCE
# define MAX_SEQUENCE 10
#endif
// Check that MAX_SEQUENCE is large enough!
#if MAX_SEQUENCE < 2
#error MAX_SEQUENCE must be at least 2
#endif
typedef struct{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
} shared_data;
int main()
{
int a, b, m, n, i;
a = 0; b = 1;
printf("Enter the number of a Fibonacci Sequence:\n");
// Always check whether input conversion worked
if (scanf("%d", &m) != 1) {
printf("Invalid input, couldn't be converted.\n");
return EXIT_FAILURE;
}
if (m <= 0) {
printf("Please enter a positive integer\n");
return EXIT_FAILURE; // exit if input is invalid
} else if (m > MAX_SEQUENCE) {
printf("Please enter an integer less than %d\n", MAX_SEQUENCE);
return EXIT_FAILURE; // exit if input is invalid
}
/* the identifier for the shared memory segment */
int segment_id;
/* the size (in bytes) of the shared memory segment */
size_t segment_size = sizeof(shared_data);
/* allocate a shared memory segment */
segment_id = shmget(IPC_PRIVATE, segment_size, S_IRUSR | S_IWUSR);
// Check result of shmget
if (segment_id == -1) {
perror("shmget failed");
return EXIT_FAILURE;
}
/* attach the shared memory segment */
shared_data *shared_memory = shmat(segment_id, NULL, 0);
// Check whether attaching succeeded
if ((void*)shared_memory == (void*)-1) {
perror("shmat failed");
goto destroy; // clean up
}
printf("\nshared memory segment %d attached at address %p\n", segment_id, (void*)shared_memory);
shared_memory->sequence_size = m;
pid_t pid;
pid = fork();
if (pid == 0){
printf("Child is producing the Fibonacci Sequence...\n");
shared_memory->fib_sequence[0] = a;
shared_memory->fib_sequence[1] = b;
for (i = 2; i < shared_memory->sequence_size; i++){
n = a+b;
shared_memory->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_memory->sequence_size; i++) {
printf("%ld ", shared_memory->fib_sequence[i]);
}
printf("\n");
}
/* now detach the shared memory segment */
if (shmdt(shared_memory) == -1) {
fprintf(stderr, "Unable to detach\n");
}
destroy:
/* now remove the shared memory segment */
shmctl(segment_id, IPC_RMID, NULL);
return 0;
}

There are a couple of options for shared memories :
Datapools - Datapool is an allocated location that kernel provides to a process upon request. Then other processes use the name which was used to create the data pool to connect to it and read/write from it.
Pipelines - Pipeline is another form of sharing resources which again kernel provides upon request. The difference is pipeline is usually one-way whereas data pool can be read and written by all the processes. Also, reads from the pipeline are destructive.
Files - You can also use files which is the most basic and you are probably familiar with it.
These are basic explanations, you have to research on these topics to fully understand and use them. Also, each operating system has a specific way of using these concepts, but all of them provide it (in their own way).

Instead of attaching the shared memory in the parent and then inheriting it in the client, use ftok() to get a common shared memory key that's used by both processes.
Create a file fibonacci in your current directory, this will be used in the calls to ftok().
When the parent forks the child process, it calls execl() to execute the child program, rather than including the child code directly. The child program doesn't need any of the fork() code, it just needs to attach to the same shared memory segment and fill in the results.
fibonacci.h
#ifndef FIBONACCI_H
#define FIBONACCI_H
// So we could use other sizes without editing the source.
#ifndef MAX_SEQUENCE
# define MAX_SEQUENCE 10
#endif
// Check that MAX_SEQUENCE is large enough!
#if MAX_SEQUENCE < 2
#error MAX_SEQUENCE must be at least 2
#endif
#define TOKEN_PATH "fibonacci"
typedef struct{
long fib_sequence[MAX_SEQUENCE];
int sequence_size;
} shared_data;
#endif
testparent.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include "fibonacci.h"
int main()
{
int m, i;
printf("Enter the number of a Fibonacci Sequence:\n");
// Always check whether input conversion worked
if (scanf("%d", &m) != 1) {
printf("Invalid input, couldn't be converted.\n");
return EXIT_FAILURE;
}
if (m <= 0) {
printf("Please enter a positive integer\n");
return EXIT_FAILURE; // exit if input is invalid
} else if (m > MAX_SEQUENCE) {
printf("Please enter an integer less than %d\n", MAX_SEQUENCE);
return EXIT_FAILURE; // exit if input is invalid
}
/* the identifier for the shared memory segment */
int segment_id;
/* the size (in bytes) of the shared memory segment */
size_t segment_size = sizeof(shared_data);
/* Get shared memory token */
key_t token = ftok(TOKEN_PATH, 0);
if (token == -1) {
perror("ftok");
return EXIT_FAILURE;
}
/* allocate a shared memory segment */
segment_id = shmget(token, segment_size, S_IRUSR | S_IWUSR | IPC_CREAT);
// Check result of shmget
if (segment_id == -1) {
perror("shmget failed");
return EXIT_FAILURE;
}
/* attach the shared memory segment */
shared_data *shared_memory = shmat(segment_id, NULL, 0);
// Check whether attaching succeeded
if ((void*)shared_memory == (void*)-1) {
perror("shmat failed");
goto destroy; // clean up
}
printf("\nshared memory segment %d attached at address %p\n", segment_id, (void*)shared_memory);
shared_memory->sequence_size = m;
pid_t pid;
pid = fork();
if (pid == 0){
execl("./testchild", "./testchild", (char *)NULL);
perror("execl"); // If it returns it must have failed
return EXIT_FAILURE;
}
else{
printf("Parent is waiting for child to complete...\n");
wait(NULL);
printf("Parent ends\n");
for(i = 0; i < shared_memory->sequence_size; i++) {
printf("%ld ", shared_memory->fib_sequence[i]);
}
printf("\n");
}
/* now detach the shared memory segment */
if (shmdt(shared_memory) == -1) {
fprintf(stderr, "Unable to detach\n");
}
destroy:
/* now remove the shared memory segment */
shmctl(segment_id, IPC_RMID, NULL);
return 0;
}
testchild.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include "fibonacci.h"
int main()
{
int a, b, n, i;
a = 0; b = 1;
/* the identifier for the shared memory segment */
int segment_id;
/* the size (in bytes) of the shared memory segment */
size_t segment_size = sizeof(shared_data);
/* Get shared memory token */
key_t token = ftok(TOKEN_PATH, 0);
if (token == -1) {
perror("ftok");
return EXIT_FAILURE;
}
/* allocate a shared memory segment */
segment_id = shmget(token, segment_size, S_IRUSR | S_IWUSR);
// Check result of shmget
if (segment_id == -1) {
perror("shmget failed");
return EXIT_FAILURE;
}
/* attach the shared memory segment */
shared_data *shared_memory = shmat(segment_id, NULL, 0);
// Check whether attaching succeeded
if ((void*)shared_memory == (void*)-1) {
perror("shmat failed");
return EXIT_FAILURE;
}
printf("\nshared memory segment %d attached at address %p\n", segment_id, (void*)shared_memory);
printf("Child is producing the Fibonacci Sequence...\n");
shared_memory->fib_sequence[0] = a;
shared_memory->fib_sequence[1] = b;
for (i = 2; i < shared_memory->sequence_size; i++){
n = a+b;
shared_memory->fib_sequence[i] = n;
a = b;
b = n;
}
printf("\nChild ends\n");
/* now detach the shared memory segment */
if (shmdt(shared_memory) == -1) {
fprintf(stderr, "Unable to detach\n");
}
return 0;
}

Related

How do I print stored data from the shared memory?

I have the following program:
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 100
void ChildProcess(void);
void ParentProcess(void);
void main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}
I have to modify it in a way that both the parent and the child print stored data from the shared memory in the following way:
Create and initialize the shared memory in the parent.
Fill the shared memory with 5 integer numbers. (I should allocate enough shared memory to store the 5 ints.)
Fork from the parent to the child.
If fork is successful, then the child process must print the values stored in the shared memory as shown in the expected output where N1, N2, N3, N4, N5 are the numbers found in the shared memory.
Expected output
What I did in the ParentProcess function is the following:
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
int localVar = 0;
int* p = (int*) malloc(2);
pid_t childPID = fork();
*p = 0;
if (childPID >= 0)
{
printf("\nChild process has started\n");
if (childPID == 0)
{
localVar++;
globalVar++;
printf("Child process has found the following data %d,", *p);
*p = 70;
printf( " %d,", *p);
*p = 66;
printf(" %d,", *p);
*p = 51;
printf(" %d,", *p);
*p = 90;
printf(" %d in shared memory\n",*p);
printf("Child is existing\n\n");
}
}
}
And now I realize that I did it completely wrong but I have no idea how to fix that. I suppose I have to use shmget to create the shared memory, but then what? How do I store values in it?
If you find that you cannot help me with this or it is too long, please share sources where I can learn more about C programming in Linux, particularly regarding the usage of shared memory. Thank you in advance
It may be better to make it clear what you want to do first because as far as I read your code you call fork() twice in your code (once in main() function and once in ParentProcess() function)
So I write general solution for parent/child shared memory. There are several ways to achieve shared memory but this is one example which is modified version of the code here
How to use shared memory with Linux in C
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
void *create_shared_memory(size_t size)
{
int protection = PROT_READ | PROT_WRITE;
int visibility = MAP_SHARED | MAP_ANONYMOUS;
return mmap(NULL, size, protection, visibility, -1, 0);
}
int main()
{
// Allocate 4 ints
void *shmem = create_shared_memory(sizeof(int)*4);
if( shmem == NULL ){
fprintf(stderr, "Failed to create shared memory\n");
return -1;
}
// Initialize 4 ints
((int*)shmem)[0] = 10;
((int*)shmem)[1] = 100;
((int*)shmem)[2] = 1000;
((int*)shmem)[3] = 10000;
int pid = fork();
if (pid == 0)
{
// Print 4 ints in child
printf("Child reading int 0: %d\n", ((int*)shmem)[0]);
printf("Child reading int 1: %d\n", ((int*)shmem)[1]);
printf("Child reading int 2: %d\n", ((int*)shmem)[2]);
printf("Child reading int 3: %d\n", ((int*)shmem)[3]);
printf("Child end\n");
}
else
{
printf("Parent waiting for child ends...\n");
waitpid(pid, NULL, 0);
printf("Parent ends\n");
}
int ret = munmap(shmem, sizeof(int)*4);
if( ret != 0 ){
fprintf(stderr, "Failed to unmap shared memory\n");
return -1;
}
return 0;
}
I've written a small piece of c code which you might find helpful:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define NUM_INTS 5
int main(int argc, char *argv[])
{
key_t key = (key_t) 123456;
int shmgetrc, semgetrc;
struct shmid_ds ds;
int *shared_values;
int i;
struct sembuf sops[2];
int semid;
sops[0].sem_num = 0; /* Operate on semaphore 0 */
sops[0].sem_op = 0; /* Wait for value to equal 0 */
sops[0].sem_flg = 0;
sops[1].sem_num = 0; /* Operate on semaphore 0 */
sops[1].sem_op = 1; /* Increment value by one */
sops[1].sem_flg = 0;
/* create SHM segment */
shmgetrc = shmget(key, NUM_INTS * sizeof(int), IPC_CREAT | IPC_EXCL | 0x180);
if (shmgetrc < 0) {
perror("shmget failed...");
exit(1);
}
/* retrieve the address of the segment */
shared_values = (int *) shmat(shmgetrc, NULL, 0);
/* create a semaphore */
semgetrc = semget(key, 1, IPC_CREAT | IPC_EXCL | 0x180);
if (semgetrc < 0) {
perror("semget failed...");
exit(1);
}
/* lock the semaphore */
if (semop(semgetrc, sops, 2) == -1) {
perror("semop lock failed ...");
exit(1);
}
/* fill it with values */
for (i = 0; i < NUM_INTS; ++i) {
shared_values[i] = i;
}
/* unlock the semaphore */
sops[0].sem_op = -1;
if (semop(semgetrc, sops, 1) == -1) {
perror("semop release failed ...");
exit(1);
}
/* here something else could happen */
sleep(60);
/* lock the semaphore */
sops[0].sem_op = 0;
if (semop(semgetrc, sops, 2) == -1) {
perror("semop lock failed ...");
exit(1);
}
/* print values */
for (i = 0; i < NUM_INTS; ++i) {
printf("%d ", shared_values[i]);
}
printf("\n");
/* unlock the semaphore */
sops[0].sem_op = -1;
if (semop(semgetrc, sops, 1) == -1) {
perror("semop release failed ...");
exit(1);
}
/* remove the semaphore */
if (semctl(semgetrc, semgetrc, IPC_RMID) < 0) {
perror("semctl failed ...");
exit(1);
}
/* remove shm segment again */
if (shmctl(shmgetrc, IPC_RMID, &ds) < 0) {
perror("shmctl failed ...");
exit(1);
}
exit(0);
}
It was not my intention to write the most beautiful code ever written, just an example that shows:
how to create a shm segment
how to retrieve the address and to use it
how to remove it
Additionally, I've used a semaphore to protect the access.
Contrary to the other answer, I've used the ipc interface, not mmap().

How to create shared memory after fork?

My code is
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
int main() {
char parent[] = "parent";
char child[] = "child";
char *shmem = (char*)mmap(NULL, 1000, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
char *shmem_child = "NOT CHANGE";
memcpy(shmem, parent, sizeof(parent));
int pid = fork();
if (pid == 0) {
char child_new[] = "new child";
printf("Child read: %s\n", shmem);
memcpy(shmem, child, sizeof(child));
printf("Child wrote: %s\n", shmem);
shmem_child = (char*)mmap(NULL, 1000, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
memcpy(shmem_child, child_new, sizeof(child_new));
printf("Child create: %s\n", shmem_child);
} else {
printf("Parent read: %s\n", shmem);
sleep(1);
printf("After 1s, parent read: %s\n", shmem);
printf("After 1s, parent read shmem_child: %s\n", shmem_child);
}
}
And the output is
Parent read: parent
Child read: parent
Child wrote: child
Child create: new child
After 1s, parent read: child
After 1s, parent read shmem_child: NOT CHANGE
As you can see, the shared memory(shmem) created before fork works, but the shared memory(shmem_child) created inside child does not work.
Am I doing something wrong? How can I create shared memory inside child so that parent and even brothers(other children of same parent) can access?
Anonymous shared memory stays shared across a fork().
So, both the parent and the child(ren) should access the same memory area, shmem.
You cannot create anonymous shared memory in a child process, and have it magically appear in the parent process. Anonymous shared memory must be created in the parent; then it will be accessible to all children.
You can create non-anonymous shared memory, via e.g. shm_open(). The creator ftruncate()s it to appropriate length, and all processes memory-map the descriptor. You do need to remember to remove the shared memory when no longer needed, via shm_unlink().
Here is a simple (tested and verified) example of anonymous shared memory between a parent and a child:
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
/* SPDX-License-Identifier: CC0-1.0 */
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
typedef struct {
char message[256];
} shared_mem;
static size_t page_aligned(const size_t size)
{
const size_t page = sysconf(_SC_PAGESIZE);
if (size <= page)
return page;
else
return page * (size_t)(size / page + !!(size % page));
/* !!(size % page) is 0 if size is a multiple of page, 1 otherwise. */
}
int child_process(shared_mem *shared)
{
printf("Child: shared memory contains \"%s\".\n", shared->message);
fflush(stdout);
snprintf(shared->message, sizeof shared->message, "Child");
printf("Child: changed shared memory to \"%s\".\n", shared->message);
fflush(stdout);
return EXIT_SUCCESS;
}
int main(void)
{
const size_t size = page_aligned(sizeof (shared_mem));
shared_mem *shared;
pid_t child, p;
shared = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, (off_t)0);
if (shared == MAP_FAILED) {
fprintf(stderr, "Cannot map %zu bytes of shared memory: %s.\n", size, strerror(errno));
return EXIT_FAILURE;
}
snprintf(shared->message, sizeof shared->message, "Parent");
printf("Parent: set shared memory to \"%s\".\n", shared->message);
fflush(stdout);
/* Create the child process. */
child = fork();
if (!child) {
/* This is the child process. */
return child_process(shared);
} else
if (child == -1) {
fprintf(stderr, "Cannot create a child process: %s.\n", strerror(errno));
return EXIT_FAILURE;
}
/* This is the parent process. */
/* Wait for the child to exit. */
do {
p = waitpid(child, NULL, 0);
} while (p == -1 && errno == EINTR);
if (p == -1) {
fprintf(stderr, "Cannot reap child process: %s.\n", strerror(errno));
return EXIT_FAILURE;
}
/* Describe the shared memory, */
printf("Parent: shared memory contains \"%s\".\n", shared->message);
fflush(stdout);
/* and tear it down. Done. */
munmap(shared, size);
return EXIT_SUCCESS;
}
Save it as e.g. example.c, then compile and run it via e.g.
gcc -Wall -Wextra -O2 example1.c -o ex1
./ex1
It will output
Parent: set shared memory to "Parent".
Child: shared memory contains "Parent".
Child: changed shared memory to "Child".
Parent: shared memory contains "Child".
showing that this indeed works.
To create shared memory after fork(), or between unrelated processes, all processes have to agree on the name. For POSIX shared memory objects (that you obtain a descriptor to using shm_open(), the name must start with a slash.
Note that I used mode 0600, which corresponds to (decimal 384) -rw-------, i.e. only accessible to processes running as the same user.
Consider the following example:
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
/* SPDX-License-Identifier: CC0-1.0 */
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
typedef struct {
pid_t changer;
time_t when;
char message[256];
} shared_mem;
static size_t page_aligned(const size_t size)
{
const size_t page = sysconf(_SC_PAGESIZE);
if (size <= page)
return page;
else
return page * (size_t)(size / page + !!(size % page));
/* !!(size % page) is 0 if size is a multiple of page, 1 otherwise. */
}
enum {
ACTION_NONE = 0,
ACTION_CREATE = (1<<0),
ACTION_REMOVE = (1<<1),
ACTION_MODIFY = (1<<2),
};
int main(int argc, char *argv[])
{
const size_t size = page_aligned(sizeof (shared_mem));
shared_mem *shared;
const char *name;
time_t now;
const char *message = NULL;
int action = ACTION_NONE;
int arg, shm_fd;
if (argc < 2 || argc > 4 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
const char *argv0 = (argc > 0 && argv && argv[0]) ? argv[0] : "(this)";
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv0);
fprintf(stderr, " %s /NAME [ +CREATE ] [ MESSAGE ] [ -REMOVE ]\n", argv0);
fprintf(stderr, "\n");
return EXIT_FAILURE;
}
/* Grab and check name */
name = argv[1];
if (name[0] != '/' || name[1] == '\0') {
fprintf(stderr, "%s: Shared memory name must begin with a slash.\n", argv[1]);
return EXIT_FAILURE;
}
/* Check other command-line parameters. */
for (arg = 2; arg < argc; arg++) {
if (argv[arg][0] == '+') {
action |= ACTION_CREATE;
if (argv[arg][1] != '\0') {
message = argv[arg] + 1;
action |= ACTION_MODIFY;
}
} else
if (argv[arg][0] == '-') {
action |= ACTION_REMOVE;
if (argv[arg][1] != '\0') {
message = argv[arg] + 1;
action |= ACTION_MODIFY;
}
} else
if (argv[arg][0] != '\0') {
if (message) {
fprintf(stderr, "%s: Can only set one message (already setting '%s').\n", argv[arg], message);
return EXIT_FAILURE;
}
message = argv[arg];
action |= ACTION_MODIFY;
}
}
if (action & ACTION_CREATE) {
/* Create the shared memory object. */
shm_fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
if (shm_fd == -1) {
fprintf(stderr, "%s: Cannot create shared memory object: %s.\n", name, strerror(errno));
return EXIT_FAILURE;
}
/* Resize it to desired size. */
if (ftruncate(shm_fd, (off_t)size) == -1) {
fprintf(stderr, "%s: Cannot resize shared memory object to %zu bytes: %s.\n", name, size, strerror(errno));
close(shm_fd);
shm_unlink(name);
return EXIT_FAILURE;
}
} else {
/* Open an existing shared memory object. */
shm_fd = shm_open(name, O_RDWR, 0600);
if (shm_fd == -1) {
fprintf(stderr, "%s: Cannot open shared memory object: %s.\n", name, strerror(errno));
return EXIT_FAILURE;
}
}
/* Map the shared memory object. */
shared = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_NORESERVE, shm_fd, (off_t)0);
if (shared == MAP_FAILED) {
fprintf(stderr, "%s: Cannot map %zu bytes of shared memory object: %s.\n", name, size, strerror(errno));
close(shm_fd);
if (action & (ACTION_CREATE | ACTION_REMOVE))
shm_unlink(name);
return EXIT_FAILURE;
}
/* The shared memory object descriptor is no longer needed. */
if (close(shm_fd) == -1) {
fprintf(stderr, "Warning: Error closing shared memory object: %s.\n", strerror(errno));
}
/* Current time in UTC */
now = time(NULL);
/* If we created it, we need to initialize it too. */
if (action & ACTION_CREATE) {
shared->changer = getpid();
shared->when = now;
snprintf(shared->message, sizeof shared->message, "Initialized");
}
/* Show contents. */
printf("Shared memory was last changed %ld seconds ago by process %ld to '%s'.\n",
(long)(now - shared->when), (long)(shared->changer), shared->message);
fflush(stdout);
/* Modify contents. */
if (action & ACTION_MODIFY) {
printf("Changing shared memory contents into '%s'.\n", message);
fflush(stdout);
shared->changer = getpid();
shared->when = now;
snprintf(shared->message, sizeof shared->message, "%s", message);
}
/* Unmap shared memory object. */
munmap(shared, size);
/* Remove shared memory? */
if (action & ACTION_REMOVE) {
if (shm_unlink(name) == -1) {
fprintf(stderr, "Warning: %s: Cannot remove shared memory object: %s.\n", name, strerror(errno));
return EXIT_FAILURE;
} else {
printf("%s: Shared memory object removed successfully.\n", name);
fflush(stdout);
}
}
/* All done. */
return EXIT_SUCCESS;
}
Save it as e.g. example2.c, and compile it using e.g.
gcc -Wall -Wextra -O2 example2.c -lrt -o ex2
Open up multiple windows. In one, run
./ex2 /myshared +
to create the shared memory; and in others, run
./ex2 /myshared newmessage
When you are done, remember to remove the shared memory object using
./ex2 /myshared -

Shared Memory - Process communication

What I have to do:
-Create a shared memory segment.
-fill an array with a fixed number of integers e.g 100
-pass these created numbers to a child process in blocks of 10 each.
-in the child process: output these numbers
So this is basically a communication between two processes via a shared memory segment.
My question: So far I can pass the first 10 numbers to the child process .. but I don't really know how to pass the next 10 numbers to the very same child process to output it without creating more child processes .. I thought maybe I could simply wrap everything in a while loop but then everytime a new child process is created which is not what I want.
Right now I have the following Code:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define N_SHARED 10
#define SIZE 100
int main(int argc, char **argv){
int shID;
int *myPtr;
int i;
int counter = 0;
int array[SIZE];
for(i=0;i<SIZE;i++){
array[i] = lrand48();
}
/* Shared Memory Creation */
shID = shmget(223520, N_SHARED, IPC_CREAT | 0666);
if (shID >= 0) {
/* get memory */
myPtr = shmat(shID, 0, 0);
if (myPtr==(int *)-1) {
perror("shmat");
} else {
/* memory is accessible */
for (i=0; i<N_SHARED; i++) {
myPtr[i] = array[counter];
counter += 1;
}
// create Child Process
pid_t child = fork();
if(child == 0){
sleep(5);
shID = shmget(223520, N_SHARED, 0666);
if (shID >= 0) {
myPtr = shmat(shID, 0, 0);
if (myPtr==(int *)-1) {
perror("shmat");
} else {
for (i=0; i<N_SHARED; i++) {
printf("%d\n", myPtr[i]);
}
shmdt(myPtr);
}
}else {
/* shmget went wrong */
perror("shmget");
}
}
/* give up memory */
shmdt(myPtr);
}
} else {
/* shmget went wrong */
perror("shmget");
}
}

memory and pass values in a Pointer in shared memory

I'm trying to pass some values between a parent and 10 child processes with shared memory and active wait.
Some values in the struct are pointers to allocate outside dynamic memory
The error is showing when i'm trying to write a number in a string to pass a path file with numbers, but I can't since the memory is not there, I can only do it without a number.
typedef struct {
char *path[10];
char *word[10];
int number_ocurrency[10];
int flag[10];
} shared_data_type;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
int fd;
int data_size = sizeof(shared_data_type),i;
pid_t pid;
shared_data_type *shared_data;
if((fd = shm_open("/ex06_searchWord", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR))==-1)
{
perror("Error at shared memory allocation!\n");
return 0;
}
ftruncate (fd, data_size);
shared_data = (shared_data_type *)mmap(NULL,data_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
for(i=0; i<10;i++){
shared_data->flag[i]=0;
pid=fork();
if (pid == -1)
{
printf("Error at fork!\n");
return -1;
}
if (pid == 0) { /* reader */
while(!shared_data->flag[i]);
FILE *file;
file = fopen(shared_data->path[i], "r");
if (file == NULL)
{
printf("Could not open/find the specified file.\n");
return -1;
}
int size = 0;
char readChar=NULL;
char *msg = NULL;
while((readChar = fgetc(file)) != EOF) {
msg = (char *) realloc(msg, size+1);
*(msg + size) = readChar;
size++;
}
*(msg + size) = '\0';
int count = 0;
while(msg = strstr(msg, shared_data->word[i]))
{
count++;
msg++;
}
shared_data->number_ocurrency[i]=count;
exit(0);
}
if(pid>0){
shared_data->word[i]="SCOMP";
char path[16]="files/file1.txt";
shared_data->path[i]=malloc(sizeof(path)+1);
sprintf(shared_data->path[i],"files/file%d.txt",i);
//shared_data->path[i]= "files/file.txt";
shared_data->number_ocurrency[i]=0;
shared_data->flag[i]=1;
}
}
for(i=0; i<10;i++){
wait(NULL);
}
for(i=0; i<10;i++){
printf("The word %s in the son %d appeared: %d times\n",shared_data->word[i],i,shared_data->number_ocurrency[i]);
}
if (munmap(shared_data, data_size) == -1)
{
perror("Error at unmap!\n");
return 0;
}
if(shm_unlink("/ex06_searchWord")==-1){
perror("Error at unlink!\n");
return 0;
}
return 0;
}
When you share bytes between processes, you need to make sure those bytes contain something that is meaningful and understandable to all the processes that are going to use it. Putting pointers to memory that isn't shared in shared memory makes no sense. And unless all the processes can be guaranteed to map the shared memory at the same address, even putting absolute pointers to shared memory in shared memory makes no sense.
You can divide the shared memory into "slots" if you want and get the effect of having pointers in shared memory by placing the slot number in shared memory. The slot number will have to be translated to and from an absolute address in each process, taking into account the base address of the mapping.

Working with semaphores and shared memory under Linux

I need to write a program that is creating a N amount of sub processes and every single one of them adds one to a shared memory variable. My idea is to use semaphores and shared memory, but the processes are not waiting for each other and the shared memory variable is also not working as I want it.
mydefs.h
#ifndef __MYDEFS__H__
#define __MYDEFS__H__
// Includes
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <memory.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/shm.h>
#endif // __MYDEFS__H__
main.c
#include "mydefs.h"
#define PROC_COUNT 3
#define INITAL_MARKER_VALUE 0
#define PID_LEN 32
char mypid[PID_LEN];
int main()
{
int i, shm_id;
sem_t mutex;
if(sem_init(&mutex,1,1) < 0)
{
perror("semaphore initilization");
exit(0);
}
shm_id = shmget(IPC_PRIVATE, 4*sizeof(int), IPC_CREAT | 0666);
if (shm_id < 0) {
printf("shmget error\n");
}
int *shmpointer = shmat(shm_id,0,0);
memset(mypid, 0, sizeof(mypid));
sprintf(mypid, "%06d", getpid());
for(i = 0; i < PROC_COUNT; i++)
{
if (fork() == 0)
{
while(sem_wait(&mutex)!=0);
execl("slaveproc", "slaveproc", mypid, (char *)0);
shmpointer += 1;
sem_post(&mutex);
perror("\n Can't exec slave program. Cause ");
exit(1);
}
}
sleep(1);
printf("%d\n", *shmpointer);
return 0;
}
slaveproc.c
#include "mydefs.h"
int marker; // Marker value
int main(int argc, char *argv[])
{
master_pid = atoi(argv[1]);
printf("\n --------------------------------------");
printf("\n I'm the slave proc!");
printf("\n My pid: %d", getpid());
printf("\n My master's pid: %d", master_pid);
printf("\n --------------------------------------");
for(;;) pause();
return 0;
}
The problem (or at least "a problem") is that mutex is not in shared memory: it's allocated on the stack. When you fork(), the new process will have a completely separate copy from the old process, so calling sem_wait(&mutex) on one process will not affect the other process's mutex at all.
You should put mutex in the shared memory:
int main()
{
int i, shm_id;
shm_id = shmget(IPC_PRIVATE, sizeof(sem_t) + 4*sizeof(int), IPC_CREAT | 0666);
if (shm_id < 0) {
printf("shmget error\n");
}
int *shmpointer = shmat(shm_id,0,0);
sem_t *mutex = shmpointer;
shmpointer = (void*)shmpointer + sizeof(sem_t);
if(sem_init(mutex,1,1) < 0)
{
perror("semaphore initilization");
exit(0);
}
memset(mypid, 0, sizeof(mypid));
sprintf(mypid, "%06d", getpid());
for(i = 0; i < PROC_COUNT; i++)
{
if (fork() == 0)
{
while(sem_wait(mutex)!=0);
execl("slaveproc", "slaveproc", mypid, (char *)0);
shmpointer += 1;
sem_post(mutex);
perror("\n Can't exec slave program. Cause ");
exit(1);
}
}
sleep(1);
printf("%d\n", *shmpointer);
return 0;
}
You're also never writing to the memory in shmpointer (perhaps you meant (*shmpointer) += 1?), but I'll let you figure that out on your own.

Resources