I have the follow code, that i want to communicate between two processes via a shared memory segment. My problem is that i take an error at attaching memory segment and i don't know why.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "process.h"
int main(int argc, char** argv)
{
/*- Check the command-line arguments -*/
if(argc != 2)
{
printf("\n--- Wrong input at command-line arguments ---\n\n");
printf("--- The program terminate --- \n");
return 1;
}
int N = atoi(argv[1]); // The amount of total threads to be created
int status; // for the wait() function
char* data; // a string to use for shared memory segment
/*- Variables for the shared memory segment -*/
key_t key = 1003; // for the shmget() key argument
int shmid; // to store the returned value from shmget()
int size = 1024; // for the shmget() size argument
/* ************************************************************************************/
pid_t pid = fork(); // create second process
if(pid < 0) // if something going wrong
{
printf("\n\n---- Error in function fork!----\n");
exit(1);
}
else if(pid == 0) // the child process (P)
{
// create the shared memory segment for the P process
shmid = CreateShmSegment(key, size, IPC_CREAT);
if(shmid == -1) // check if creating shared memory return an error
{
printf("\n---Error at creating the memory segment! ---\n");
exit(1);
}
// attach the shared memory segment
data = AttachShmSegment(shmid);
if(data == (char*)-1) // check if attached the shared memory return an error
{
printf("\n---Error at attaching the memory segment! ---\n");
exit(1);
}
char* str = data;
sprintf(str, "testing");
// ChildProcess();
printf("\n** Child process! ** \n");
printf("N = %d\n", N);
printf("write: %s\n",str);
// detach the shared memory segment
if(shmdt(data) == -1) //check for error
{
printf("\n---Error at detaching memory segment! ---\n");
exit(1);
}
}
else // the parent process (C)
{
// create the shared memory segment for the C process
shmid = CreateShmSegment(key, size, IPC_CREAT);
if(shmid == -1) // check if creating shared memory return an error
{
printf("\n---Error at creating the memory segment! ---\n");
exit(1);
}
// attach the shared memory segment
data = AttachShmSegment(shmid);
if(data == (char*)-1) // check if attached the shared memory return an error
{
printf("\n---Error at attaching the memory segment! ---\n");
exit(1);
}
// ParentProcess();
wait(&status);
printf("\n** Parent process! **\n");
printf("N = %d\n",N);
printf("read from segment: %s\n", data);
// detach the shared memory segment
if(shmdt(data) == -1) //check for error
{
printf("\n---Error at detaching memory segment! ---\n");
exit(1);
}
// deallocate the shared memory segment
if(DeallocateShmSegment(shmid) == -1) //check for error
{
printf("\n---Error at destroy memory segment! ---\n");
exit(1);
}
}
return 0;
}
I quote and the other two files for the compile.
process.h:
#ifndef __Processes_H_
#define __Processes_H_
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
void ChildProcess();
void ParentProcess();
int CreateShmSegment(key_t, size_t, int);
void* AttachShmSegment(int);
int DetachShmSegment(const void*);
int DeallocateShmSegment(int);
#endif
And process.c:
#include "process.h"
void ChildProcess()
{
}
/***************************************************************/
void ParentProcess()
{
}
/****************************************************************/
int CreateShmSegment(key_t key, size_t size, int flag)
{
int id;
id = shmget(key, size, flag);
return id;
}
/*****************************************************************/
void* AttachShmSegment(int id)
{
char* data = NULL;
data = (char*) shmat(id, NULL, 0);
return data;
}
/*****************************************************************/
int DetachShmSegment(const void* addr)
{
return shmdt(addr);
}
/*****************************************************************/
int DeallocateShmSegment(int id)
{
return shmctl(id, IPC_RMID, NULL);
}
I don't know what is going wrong about attaching the memory. I have search the web and make some changes at arguments of shmat(), but i couldn't solve it.