Shared memory for fork

I want to create shared memory between two processes. I used fork (). The child is trying to change this shared memory, and the mother is creating another child, so a new child is trying to change the same memory. here is my code in C programming. (Ubunt)

mylist ch=NUL; f=fork(); if(!f){ pba=shmget(KEYSHM,sizeof(char),0); /*created shared memory*/ ch=(mylist *) shmat(pba,0,0); ch->name=ugur; ch->surname=cedric; ...do something... } else{ if(ch) printf("this is top of mylist %s"ch->name); .......do something } 

He never writes ch-> name. What for? I created shared memory. Why can't the parent process read?

+2
source share
6 answers

In order for the memory to be shared, the parent and child must have access to the same shared memory.

You have two options: simpler and harder:

  • Create and attach to shared memory before forking. Both parents and the child automatically have access to the same shared memory.

  • First, select the plug, and then the parent and child should be connected to shared memory separately. As soon as the processes branch, they no longer share memory, and, in particular, everything that is allocated in the child is not available in the parent.

You need to allocate more than 1 character of shared memory to store useful strings, such as names.

+5
source

Do you have a race condition, what if the parent runs in front of the child and you are trying to access ch-> before the child has posted anything there?

You also allocate only 1 byte (sizeof (char)), but you are trying to treat this as a pointer to a structure - you need to allocate enough space for mylist - if you haven't done this before. You will also need the IPC_CREAT flag for shmget.

+3
source

if the first start of the parent process cannot access ch->name , because ch->name=ugur; not yet running. I recommend looking at the shared memory tutorial and pdf file .
one solution that I think gets shared memory before fork() and uses it in child and parent.

+1
source

As you write your code, ch will point to some kind of shared memory, but ch itself (i.e. a pointer) will not be shared. Since the assignment of ch occurs only in the child, the parent will continue to see ch as NULL.

You have two ways to fix this:

  • Set up shared memory before plug.
  • Let the parent and child languages ​​open the same shared memory.

When working with records in shared memory, you need to make sure that all data for this record exists in shared memory. For example, in this code:

  ch->name=ugur; ch->surname=cedric; 

It looks like mylist :: name is a char *. If ch points to an entry in shared memory, this will only result in pointers to names in shared memory. Unless you take specific steps to transfer these lines to shared memory, they will be in normal memory and probably will not be available to other programs.

+1
source

If I create and attach shared memory before forcing, then I cannot check if the shared memory is NULL or not. It is initiated upon joining.

0
source

You also used 0 for your flags, which define rights such as access rights, and if you want to create a shared memory segment. In other words, you did not give yourself the right to read or write and did not indicate which segment will be created if it does not already exist.

0
source

Source: https://habr.com/ru/post/893793/


All Articles