APUE函数笔记五: 进程环境

58 篇文章 0 订阅
49 篇文章 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值