Forum > Linux

Changing maximum number of open files for your running process

(1/1)

Molochnik:
Is it possible to change the maximum number of open files for your currently running process?
I tried different approaches but failed

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---   if RunCommand('/bin/bash',['-c','ps -C testapp -o pid='],s) then     writeln(s);// finding your currently running pid  if RunCommand('/bin/bash',['-c','prlimit --pid ' + s.trim + ' --nofile=1024:1024'],s) then     writeln(s);// changing max number of the open files to 1024 via prlimit, RunCommand fails  if RunCommand('/bin/bash',['-c','ulimit -Sn 1024'],s) then     writeln(s);// changing max number of the open files to 1024 via ulimit, no error and no result   if RunCommand('/bin/bash',['-c','ulimit -Sn'],s) then     writeln(s);// shows the maximum number of open files, works fine, the return value is always the same 

Kays:

--- Quote from: Molochnik on January 01, 2024, 11:55:32 pm ---Is it possible to change the maximum number of open files for your currently running process?
--- End quote ---
Yes.
--- Quote from: Molochnik on January 01, 2024, 11:55:32 pm ---I tried different approaches but failed
--- End quote ---
The resource limits apply to the current process. RunCommand spawns a new process so any changes do not propagate to the parent process. You will need to utilize the setrlimit system call directly.
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program fileHoarder(input, output, stdErr);{$typedAddress on}uses        errors, math, baseUnix;const        minimumMaximumNumberOfFiles = 19479;var        limit: tRLimit;begin           { What limits does the current process have? }        case fpGetRLimit(rLimit_noFile, @limit) of                { failure }                -1:                begin                        pError('Could not obtain maximum open files resource limits',                                fpGetErrno);                        halt;                end;                { success }                 0: ;        end;                { Current soft limit below the minimum maximum? }        if limit.rLim_cur < minimumMaximumNumberOfFiles then        begin                { Raise soft limit. The hard limit requires extra privileges. }                limit.rLim_cur := minimumMaximumNumberOfFiles;                { Soft limit argument may not exceed the hard limit argument. }                limit.rLim_max := max(limit.rLim_max, limit.rLim_cur);                                case fpSetRLimit(rLimit_noFile, @limit) of                        { failure }                        -1:                        begin                                writeLn(stdErr, 'Error: Could not raise maximum open files resource limit.');                                case fpGetErrno of                                        eSYsEInval:                                        begin                                                writeLn(stdErr, 'A programming mistake caused the invalid parameter',                                                        'tRLimit[rLim_cur: ', limit.rLim_cur:1,                                                        '; rLim_max: ', limit.rLim_max:1, '].');                                        end;                                        eSysEPerm:                                        begin                                                writeLn(stdErr, 'The raised limits exceed the system’s hard limits.');                                        end;                                end;                                halt;                        end;                        { success }                         0: ;                end;        end;                { Optional: Let’s verify our result. }        case fpGetRLimit(rLimit_noFile, @limit) of                { success }                 0:                begin                        writeLn('I can have up to ', limit.rLim_cur:1, ' open files.');                end;        end;end.

Molochnik:
Kays
That works perfect! Thank you very much!

Navigation

[0] Message Index

Go to full version