1
votes

I' using shared memory (with semaphore) for communicating between two processes: Fist, I open shared memory object using the call:

int fd = shm_open("name") [http://linux.die.net/man/3/shm_open]

Second, I map this shared mem object into my adress space using call:

void* ptr = mmap(..fd..) [http://linux.die.net/man/2/mmap2]

However, I want to use EPOLL in conjunction with shared memory file descriptor==> I don't use mmap anymore, and instead, using EPOLL for monitoring, and then add, write function for direct access to shared memory using fd (shared memmory file descriptor)

My question is that: how is the speed of direct reading and writing on shared memory object in comparison with memcpy on pointer returned by mmap?

read(fd, buffer) vs memcpy(des, source, size) //???

Hope to see your answer! Thanks!

1
Why not do some profiling and find out for yourself?John3136

1 Answers

1
votes

read is a syscall and implies a privilege transition which implies address space manipulation (MMU) and then the kernel will call memcpy from your provided buffer to the destination address. It basically does the same thing you would do (call memcpy) but adding 2 expensive operations (privilege transitions) and a cheap one (finding the destination address).

We can conclude that the read/write is very likely to be slower.