UNIX环境高级编程之进程环境

1.atexit函数,命令行参数,环境变量

练习程序:
#include
void myexit(void)
{
    printf("myexit function exit\n");
}

int  main(int argc,char * argv [])
{
        int i = 0;
        for (i = 0; i < argc; i++)
        {
                printf("argv %d is %s\n", i + 1, argv[i]);
        }
        printf("it comes into main function\n");
        if (0 != atexit(myexit))
        {
                printf("atexit err");
        }
        if (0 != atexit(myexit))
        {
                printf("atexit err");
        }


        if ( 0  != putenv("TMOUT=9999999"))
        {
                printf("set env TMOUT failed\n");
        }

        printf("set env TMOUT succesfully\n");

        char *s = NULL;
        s=(char *)getenv("TMOUT");
        printf("TMOUT = %s\n", s);
        return 0;
}
输出结果:

gai@ubuntu:~/unixexecise$ ./a.out 1 2 3
argv 1 is ./a.out
argv 2 is 1
argv 3 is 2
argv 4 is 3
it comes into main function
set env TMOUT succesfully
TMOUT = 9999999
myexit function exit
myexit function exit


保留的疑问:

 echo $TMOUT
0

2.资源限制

练习代码:

int pr_limits(char * name, int resource)
{
        struct rlimit limit;
        //int getrlimit(int resource, struct rlimit *rlptr)、
        printf("we are search info about %s \n", name);
        int iRet = getrlimit(resource, &limit);
        if (0 != iRet)
        {
                printf("we get %s resource failed, iRet= %d \n", name, iRet);
                return 1;
        }

        if (RLIM_INFINITY == limit.rlim_cur)
        {
                printf("%s rlim_cur is not limited\n", name);
        }
        else
        {

                printf("%s rlim_cur is %10lldd \n", name, (long long int)limit.rlim_cur);
        }

        if (RLIM_INFINITY == limit.rlim_max)
        {
                printf("%s rlim_max is not limited\n", name);
        }
        else
        {

                printf("%s rlim_max is %10lld\n", name, (long long int)limit.rlim_max);
        }
        return 0;
}
void main(void)
{
        #ifdef RLIMIT_CPU
                doit(RLIMIT_CPU);
        #endif
}
输出结果:

we are search info about RLIMIT_CPU
RLIMIT_CPU rlim_cur is not limited
RLIMIT_CPU rlim_max is not limited

相关知识点:

define中的特殊用法:

用法总结:http://blog.chinaunix.net/uid-17188120-id-4026378.html   

可变参数用法:http://blog.csdn.net/zhezhelin/article/details/4789355

3.共享库

gai@ubuntu:~/unixexecise/no7$ gcc -static a.c
gai@ubuntu:~/unixexecise/no7$ size a.out
   text    data     bss     dec     hex filename
 659090    4096    5020  668206   a322e a.out
gai@ubuntu:~/unixexecise/no7$ gcc a.c -o b.out
gai@ubuntu:~/unixexecise/no7$ size b.out
   text    data     bss     dec     hex filename
   1878     296       4    2178     882 b.out

4.用atexit函数打印调用栈

//#include
#include
#include
#include

void printback(void)
{
        void * buff[20];
        char ** strings;
        int  st;
        int i;

        st = backtrace(buff, 20);
        strings = backtrace_symbols(buff, st);
        printf("st =%d\n", (int)st);
        for (i = 0; i < st; i++)
        {
                printf("%s\n",strings[i]);
        }


}
void second(void)
{
        printback();
}
void first(void)
{
        second();
}

void main(void)
{

        first();
        atexit(printback);


}
编译:

gcc -rdynamic a.c

执行结果:

[root@localhost unixexcise]# ./a.out
st =6
./a.out(printback+0x19) [0x80486cd]
./a.out(second+0xb) [0x804872e]
./a.out(first+0xb) [0x804873b]
./a.out(main+0xe) [0x804874b]
/lib/libc.so.6(__libc_start_main+0xe6) [0x1d6d36]
./a.out() [0x8048621]
st =4
./a.out(printback+0x19) [0x80486cd]
/lib/libc.so.6(exit+0xdf) [0x1edeaf]
/lib/libc.so.6(__libc_start_main+0xee) [0x1d6d3e]
./a.out() [0x8048621]




你可能感兴趣的:(UNIX环境高级编程学习)