How can you "avoid" a SIGSEGV?
I'm writing a client-server app, in which the client has a determined memory address from the server side.
If something goes wrong and the server needs to be reestarted the address the client has is not valid a开发者_Go百科nymore. When using a function using that invalid info a SIGSEGV will be sent to the server as the address may not be its anymore.
How can the server protect itself from a SIGSEGV and continue accepting connections and operating normally? Is there any way not to crash the server when this happens?
Thank you very much.
The client should not send a memory address to the server, period. If the client needs a reference to server resources, the server should provide it with some sort of handle that the server can translate to an address, but which isn't directly dereferenced.
In your case, the server restart has probably rendered the client's handle invalid. The server should notice this and return a well-understood error code to the client telling it to get a new resource handle.
Manage a table of connection, where the pointer is stored. If ever the server restarts, it rebuild a fresh table. The client only knows the index (or key or whatever) of its connection.
The context is not specified, but sending a pointer to the client can be a big security hole as the client can crash the server very easily.
Instead of using a pointer, use an index of an array or map. This may result in indexes being reused, but it's easy enough to detect and ignore grossly invalid indexes.
精彩评论