I’m reading APUE these days, and just found an issue.
One common way to use pthread_attr_setstack is to
malloca chuck of memory- set the
addrandlengthby the method
Then, here comes my question, what if I want to use the guard-size to protect my data? Say I need A bytes of memory, and B bytes for guard-size.
Shall I malloc A+B bytes, or malloc A bytes?
Actually pthread library itself provides an API to set the guard size
But note that if you are setting the stack location or size, either using functions like
pthread_attr_setstack(3)orpthread_attr_setstackaddr(3), which you are, then the guard size attribute is ignored (i.e., no guard area is created by the system): it is the your responsibility to handle stack overflow (perhaps by usingmprotect(2)to manually define a guard area at the end of the stack that you have allocated.So, coming to your specific question, yes, you need to malloc
A+Bbytes to include the guard area yourself if you wish to usepthread_attr_setstack. Else, if you are OK with the default stack size, then you use thepthread_attr_setstackfunction.Read about
pthreadshere. Also read specifically about thread stack management here under the sectionStack Management