Skip to content

Instantly share code, notes, and snippets.

@gnaggnoyil
Created November 14, 2022 10:55
Show Gist options
  • Save gnaggnoyil/cda2ff5748beab3c519467eba66d80da to your computer and use it in GitHub Desktop.
Save gnaggnoyil/cda2ff5748beab3c519467eba66d80da to your computer and use it in GitHub Desktop.
Sample program showind differences of behavior on real linux and WSL1
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <unistd.h>
int main(void){
int return_value = 0;
struct rlimit rl;
int ret;
char path_buf[6 + sizeof(uintmax_t) + 7];
size_t size_ret;
pid_t current_pid;
FILE *fp;
unsigned char buffer[1024];
size_t write_size_ret;
if(freopen(NULL, "wb", stdout) == NULL){
return 3;
}
current_pid = getpid();
memset(path_buf, 0, sizeof(path_buf));
size_ret = snprintf(path_buf, sizeof(path_buf), "/proc/%ju/limits", (uintmax_t)current_pid);
if(size_ret >= sizeof(path_buf)){
return 4;
}
fp = fopen(path_buf, "rb");
if(fp == NULL){
return 5;
}
ret = getrlimit(RLIMIT_AS, &rl);
if(ret != 0){
return_value = 1;
goto close_file;
}
rl.rlim_cur = 0;
ret = setrlimit(RLIMIT_AS, &rl);
if(ret != 0){
return_value = 2;
goto close_file;
}
memset(buffer, 0, sizeof(buffer));
while(true){
size_ret = fread(buffer, sizeof(unsigned char), 1024, fp);
if(size_ret < (sizeof(unsigned char) * 1024)){
if(ferror(fp)){
return_value = 7;
goto close_file;
}
}
write_size_ret = fwrite(buffer, sizeof(unsigned char), size_ret, stdout);
if(write_size_ret < (sizeof(unsigned char) * size_ret)){
if(ferror(stdout)){
return_value = 8;
goto close_file;
}
}
if(size_ret < (sizeof(unsigned char) * 1024)){
break;
}
}
close_file:
ret = fclose(fp);
if(ret == EOF){
return 6;
}
return return_value;
}
/*
On a real linux environment, with linux distribution being Archlinux, gcc
version being 12.2.0, and glibc version being 2.36, compiled with
`gcc -o setrlimit -std=gnu11 setrlimit.c`, the executed output was:
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size unlimited unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 62783 62783 processes
Max open files 1024 524288 files
Max locked memory 8388608 8388608 bytes
Max address space 0 unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 62783 62783 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
On a WSL1 linux environment, with linux distribution being Archlinux, gcc
version begin 12.2.0, and aur/glibc-linux4 version being 2.36, compiled with
`gcc -o setrlimit -std=gnu11 setrlimit.c`, the executed output was:
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 8041 8041 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 8041 8041 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 40 40
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment