Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

220 Process Environment Chapter 7


#include <stdio.h>

FILE *
open_data(void)
{
FILE *fp;
char databuf[BUFSIZ]; /* setvbuf makes this the stdio buffer */

if ((fp = fopen("datafile", "r")) == NULL)
return(NULL);
if (setvbuf(fp, databuf, _IOLBF, BUFSIZ) != 0)
return(NULL);
return(fp); /* error */
}

Figure 7.14 Incorrect usage of an automatic variable

The problem is that whenopen_datareturns, the space it used on the stack will be
used by the stack frame for the next function that is called. But the standardI/O library
will still be using that portion of memory for its stream buffer.Chaos is sure to result.
To correct this problem, the arraydatabufneeds to be allocated from global memory,
either statically (staticorextern) or dynamically (one of theallocfunctions).

7.11 getrlimitandsetrlimit Functions


Every process has a set of resource limits, some of which can be queried and changed by
thegetrlimitandsetrlimitfunctions.

#include <sys/resource.h>

int getrlimit(intresource,struct rlimit *rlptr);

int setrlimit(intresource,const struct rlimit *rlptr);

Both return: 0 if OK,−1 on error

These two functions aredefined in the XSI option in the Single UNIX Specification. The
resource limits for a process arenormally established by process 0 when the system is
initialized and then inherited by each successive process. Each implementation has its own
way of tuning the various limits.

Each call to these two functions specifies a singleresourceand a pointer to the
following structure:
struct rlimit {
rlim_t rlim_cur; /* soft limit: current limit */
rlim_t rlim_max; /* hard limit: maximum value for rlim_cur */
};
Free download pdf