关闭

APUE函数笔记五: 进程环境

328人阅读 评论(0) 收藏 举报

第七章  进程环境:

#include <stdlib.h>
void exit(int status);
void _Exit(int status);

#include <unistd.h>
void _exit(int status);

#include <stdlib.h>
int atexit(void (*func)(void));
    ret is 0 means success, log-func-total-counts <= 32 in one process

extern char ** environ;

    argc, argv[], envp[]/environ
              stack
               ||
               \/
               /\
               ||
              heap
uninit-data(bss: block started by symbol)
            init-data
              text

#include <stdlib.h>
void * malloc(size_t size);
void * calloc(size_t nobj, size_t size);
    alloc >= nobj * size, each-block >= size
    will init with 0, not means 0.0 of float, NULL of pointer
void * realloc(void * ptr, size_t newsize);
    if ptr is NULL, same as malloc
void free(void * ptr);

void * alloca(size_t size);
    alloc from stack, will free itself, but not surpport by some OS

#include <stdlib.h>
char * getenv(const char * name);

#include <stdlib.h>
int putenv(char * str);
    add or rewrite an environ item to env-table, bit-copy, may be not safe
    ret is 0 means success
int setenv(char * name, char * value, int rewrite);
    add or rewrite an environ item to env-table, 
    function will generic string: "name=value" itself, so it is safe
    rewrite is 0, if name exist, do nothing, return 0
    ret is 0 means success
int unsetenv(char * name);
    delete an environ item
    if name not exist, will return 0
    ret is 0 means success

#include <setjmp.h>
int setjmp(jmp_buf env);
    if call it directly, return 0, 
    callback after longjmp will return non-zero value
void longjmp(jmp_buf env, int val);

#include <sys/resource.h>
int getrlimit(int resource, struct rlimit * rlptr);
int setrlimit(int resource, const struct rlimit * rlptr);
    ret is 0 means success
    struct rlimit {
        rlim_t rlim_cur; /* soft limit: current limit */
        rlim_t rlim_max; /* hard limit: maximum value for rlim_cur */
    };
    RLIM_INFNITY: no limit

示例:

#include <stdio.h>
#include <stdlib.h>

void 
my_exit1(void)
{
    printf("first exit handler\n");
}

void 
my_exit2(void)
{
    printf("second exit handler\n");
}

int 
main(void)
{
    if (atexit(my_exit1) != 0) {
        printf("cannot register my_exit1\n");
        return -1;
    }

    if (atexit(my_exit2) != 0) {
        printf("cannot register my_exit2\n");
        return -1;
    }

    if (atexit(my_exit2) != 0) {
        printf("cannot register my_exit2\n");
        return -1;
    }

    printf("main is done\n");
    return 0;
}

#include <stdio.h>

void 
show1(int argc, char * argv[])
{
    int i;

    for (i = 0; i < argc; ++i) {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
}

void 
show2(char * argv[])
{
    int i;

    for (i = 0; argv[i] != NULL; ++i) {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
}

int 
main(int argc, char * argv[])
{
    show1(argc, argv);
    show2(argv);

    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

static jmp_buf jmpbuffer;
static int     globval;

void 
jump(void)
{
    longjmp(jmpbuffer, 1);
}

void 
doit(int i, int j, int k, int l)
{
    printf("in doit():\n");
    printf("globval = %d, autoval = %d, regival = %d, "
           "volaval = %d, statval = %d\n", globval, i, j, k, l);
    jump();
}

int 
main(void)
{
    int            val;
    int            autoval;
    register int   regival;
    volatile int   volaval;
    static   int   statval;

    globval = 1;
    autoval = 2;
    regival = 3;
    volaval = 4;
    statval = 5;

    if ((val = setjmp(jmpbuffer)) != 0) {
        printf("after longjmp: %d\n", val);
        printf("globval = %d, autoval = %d, regival = %d, "
               "volaval = %d, statval = %d\n", 
               globval, autoval, regival, volaval, statval);
        exit(-1);
    }

    globval = 95;
    autoval = 96;
    regival = 97;
    volaval = 98;
    statval = 99;

    doit(autoval, regival, volaval, statval);
    exit(0);
}

#include <stdio.h>
#include <stdlib.h>
#if defined(BSD) || defined(MACOS)
#include <sys/time.h>
#define  FMT  "%10lld  "
#else
#define  FMT  "%10ld  "
#endif
#include <sys/resource.h>

void 
pr_limits(char * name, int resource)
{
    struct rlimit limit;

    if (getrlimit(resource, &limit) < 0) {
        printf("getrlimit error for %s\n", name);
        return;
    }
    printf("%-14s  ", name);
    if (limit.rlim_cur == RLIM_INFINITY) {
        printf("(infinity)  ");
    }
    else {
        printf(FMT, limit.rlim_cur);
    }
    if (limit.rlim_max == RLIM_INFINITY) {
        printf("(infinity)");
    }
    else {
        printf(FMT, limit.rlim_max);
    }
    printf("\n");
}

#define   doit(resource)  pr_limits(#resource, resource)

int 
main(void)
{
#ifdef   RLIMIT_AS
    doit(RLIMIT_AS);
#endif
    doit(RLIMIT_CORE);
    doit(RLIMIT_CPU);
    doit(RLIMIT_DATA);
    doit(RLIMIT_FSIZE);
#ifdef   RLIMIT_LOCKS
    doit(RLIMIT_LOCKS);
#endif
#ifdef   RLIMIT_MEMLOCK
    doit(RLIMIT_MEMLOCK);
#endif
    doit(RLIMIT_NOFILE);
#ifdef   RLIMIT_NPROC
    doit(RLIMIT_NPROC);
#endif
#ifdef   RLIMIT_RSS
    doit(RLIMIT_RSS);
#endif
#ifdef   RLIMIT_SBSIZE
    doit(RLIMIT_SBSIZE);
#endif
    doit(RLIMIT_STACK);
#ifdef   RLIMIT_VMEM
    doit(RLIMIT_VMEM);
#endif

    exit(0);
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:155284次
    • 积分:3259
    • 等级:
    • 排名:第10429名
    • 原创:167篇
    • 转载:10篇
    • 译文:0篇
    • 评论:41条
    文章分类
    最新评论