diff --git a/C13-Linux系统编程/C13-Linux系统编程学习笔记.md b/C13-Linux系统编程/C13-Linux系统编程学习笔记.md index f575d79..f22b763 100644 --- a/C13-Linux系统编程/C13-Linux系统编程学习笔记.md +++ b/C13-Linux系统编程/C13-Linux系统编程学习笔记.md @@ -895,11 +895,77 @@ int getopt_long(int argc, char *const argv[], const char *optstring, ``` ### 环境变量 +**KEY = VALVE** +可以通过`export`命令查看 + +```c +char *getenv(const char *name); + +/* + * change or add + * + * @prarm: overwrite 是否覆盖 + * + * 覆盖时是释放原来的空间,重新分配 + */ +int setenv(const char *name, const char *value, int overwrite); +int unsetenv(const char *name); + +/* + * 和getenv一样的作用,change or add + * 用法不一样,且没有const修饰 + */ +int putenv(char *string); +``` ### C程序的存储空间布局 +`pmap`命令,查看进程空间布局 ### 库 +- 动态库 +- 静态库 +- 手工装载库 + ```c + void *dlopen(const char *filename, int flag); + char *dlerror(void); + int dlclose(void *handle); + void *dlsym(void *handle, const char *symbol); + // Link with -ldl + ``` ### 函数之间正常的跳转 +`goto`无法跨函数跳转。 + +```c +/* + * 设置跳转点 + * + * @return 0 说明是在设置跳转点 + * @return 非0 说明是通过 longjmp 返回 + */ +int setjmp(jmp_buf env); + +/* + * 跳转到跳转点 + * + * @prarm: env 跳转点 + * @prarm: val 传递给 setjmp 的值 + */ +void longjmp(jmp_buf env, int val); +``` ### 资源的获取及控制 +`ulimit -a` + +```c +// get/set resource limits +int getrlimit(int resource, struct rlimit *rlim); +int setrlimit(int resource, const struct rlimit *rlim); + +// 普通用户不能设置超过硬限制 +// root 用户可以 升高/降低 硬限制 +struct rlimit { + rlim_t rlim_cur; /* soft limit */ + rlim_t rlim_max; /* hard limit */ +}; +``` \ No newline at end of file diff --git a/C13-Linux系统编程/fs/getenv.c b/C13-Linux系统编程/fs/getenv.c new file mode 100644 index 0000000..29bb521 --- /dev/null +++ b/C13-Linux系统编程/fs/getenv.c @@ -0,0 +1,17 @@ +#include +#include + +int main(int argc, char **argv) +{ + + puts(getenv("PATH")); + + getchar(); + /** + * ./getenv + * ps axf | grep getenv + * pmap 4938 + */ + + exit(0); +} \ No newline at end of file diff --git a/C13-Linux系统编程/fs/jmp.c b/C13-Linux系统编程/fs/jmp.c new file mode 100644 index 0000000..acb420b --- /dev/null +++ b/C13-Linux系统编程/fs/jmp.c @@ -0,0 +1,99 @@ +#include +#include +#include + +static jmp_buf save; + +static void d(void) +{ + printf("%s():Begin.\n", __FUNCTION__); + + printf("%s():Jump now.\n", __FUNCTION__); + longjmp(save, 6); + // longjmp(save, 0); // !!! 如果返回0,实际会返回1,避免与另一种情况冲突 + + printf("%s():End.\n", __FUNCTION__); +} + +static void c(void) +{ + printf("%s():Begin.\n", __FUNCTION__); + printf("%s():Call d().\n", __FUNCTION__); + + d(); + + printf("%s():d() returned.\n", __FUNCTION__); + printf("%s():End.\n", __FUNCTION__); +} + +static void b(void) +{ + printf("%s():Begin.\n", __FUNCTION__); + printf("%s():Call c().\n", __FUNCTION__); + + c(); + + printf("%s():c() returned.\n", __FUNCTION__); + printf("%s():End.\n", __FUNCTION__); +} + +static void a(void) +{ + int ret; + + printf("%s():Begin.\n", __FUNCTION__); + + ret = setjmp(save); + if (0 == ret) + { + printf("%s():Call b().\n", __FUNCTION__); + b(); + printf("%s():b() returned.\n", __FUNCTION__); + } + else + { + printf("%s():Jumped back here with code %d.\n", __FUNCTION__, ret); + } + + printf("%s():End.\n", __FUNCTION__); +} + + +/** + * @brief jmp + * @details + * d跳回a + * + * @param argc + * @param argv + * @return int + */ +int main(int argc, char **argv) +{ + printf("%s():Begin.\n", __FUNCTION__); + printf("%s():Call a().\n", __FUNCTION__); + + a(); + + printf("%s():a() returned.\n", __FUNCTION__); + printf("%s():End.\n", __FUNCTION__); + + exit(0); +} + + +// ! out +// main():Begin. +// main():Call a(). +// a():Begin. +// a():Call b(). +// b():Begin. +// b():Call c(). +// c():Begin. +// c():Call d(). +// d():Begin. +// d():Jump now. +// a():Jumped back here with code 6. +// a():End. +// main():a() returned. +// main():End. \ No newline at end of file diff --git a/C13-Linux系统编程/fs/myenv.c b/C13-Linux系统编程/fs/myenv.c new file mode 100644 index 0000000..1e556e8 --- /dev/null +++ b/C13-Linux系统编程/fs/myenv.c @@ -0,0 +1,13 @@ +#include +#include + +extern char **environ; + +int main(int argc, char **argv) +{ + + for (int i = 0; environ[i] != NULL; i++) + puts(environ[i]); + + exit(0); +} \ No newline at end of file