From 6d9d48a77e76851362ade3e441e1e993c83ef3f8 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Sat, 10 Nov 2018 14:28:40 +0800
Subject: [PATCH] Add rlimit.c sample code

---
 ch3_fork/rlimit.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/ch3_fork/rlimit.c b/ch3_fork/rlimit.c
new file mode 100644
index 0000000..367adb7
--- /dev/null
+++ b/ch3_fork/rlimit.c
@@ -0,0 +1,52 @@
+#include <stdio.h>  
+#include <string.h>
+#include <errno.h>
+#include <sys/resource.h>  
+  
+void print_limits(char* name, int resource)
+{ 
+       	struct rlimit limit;  
+	if(getrlimit(resource, &limit) <0)
+	{  
+		printf("getrlimit for %s failure: %s\n", strerror(errno));  
+		return ;
+	}  
+	
+	printf("%-15s ",name);  
+	if(limit.rlim_cur == RLIM_INFINITY)
+	{  
+		printf("(infinite)     ");  
+	}
+	else
+	{  
+		printf("%-15ld",limit.rlim_cur);  
+	} 
+
+	if(limit.rlim_max == RLIM_INFINITY)
+	{  
+		printf("(infinite)     ");  
+	}else
+	{  
+		printf("%-15ld",limit.rlim_max);  
+	}  
+	printf("\n");  
+}  
+
+int main(void)
+{  
+	struct rlimit limit = {0};
+
+	print_limits("RLIMIT_NPROC", RLIMIT_NPROC);
+	print_limits("RLIMIT_DATA", RLIMIT_DATA);
+	print_limits("RLIMIT_STACK", RLIMIT_STACK);
+	print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);
+
+	printf("\nAfter set RLIMIT_NOFILE:\n");
+	getrlimit(RLIMIT_NOFILE, &limit );
+	limit.rlim_cur  = limit.rlim_max;
+	setrlimit(RLIMIT_NOFILE, &limit );
+
+	print_limits("RLIMIT_NOFILE", RLIMIT_NOFILE);
+
+	return 0;
+}

--
Gitblit v1.9.1