
Working With Unix Processes

Every Unix process comes with three open resources. These are your standard input (STDIN), standard output (STDOUT), and standard error (STDERR) resources. These standard resources exist for a very important reason that we take for granted today. STDIN provides a generic way to read input from keyboard devices or pipes, STDOUT and STDERR provide
... See moreJesse Storimer • Working With Unix Processes
Environment variables are often used as a generic way to accept input into a command-line program. Any terminal (on Unix or Windows) already supports them and most programmers are familiar with them. Using environment variables is often less overhead than explicitly parsing command line options.
Jesse Storimer • Working With Unix Processes
Set the maximum number of open files to 3. We know this # will be maxed out because the standard streams occupy # the first three file descriptors. Process.setrlimit(:NOFILE, 3) File.open('/dev/null') outputs: Errno::EMFILE: Too many open files - /dev/null
Jesse Storimer • Working With Unix Processes
We'll continue on the subject of file descriptors. Using Ruby we can ask directly for the maximum number of allowed file descriptors: p Process.getrlimit(:NOFILE) On my machine this snippet outputs: [2560, 9223372036854775807] We used a method called Process.getrlimit and asked for the maximum number of open files using the symbol :NOFILE. It
... See moreJesse Storimer • Working With Unix Processes
Just knowing the pid isn't all that useful in itself. So where is it used? A common place you'll find pids in the real world is in log files.
Jesse Storimer • Working With Unix Processes
Unix processes have very few inherent ways of communicating about their state. Programmers have worked around this and invented things like logfiles. Logfiles allow processes to communicate anything they want about their state by writing to the filesystem, but this operates at the level of the filesystem rather than being inherent to the process
... See moreJesse Storimer • Working With Unix Processes
A part of the Unix philosophy: in the land of Unix 'everything is a file'. This means that devices are treated as files, sockets and pipes are treated as files, and files are treated as files. Since all of these things are treated as files I'm going to use the word 'resource' when I'm talking about files in a general sense (including devices,
... See moreJesse Storimer • Working With Unix Processes
The kernel of your Unix system sits atop the hardware of your computer. It's a middleman for any interactions that need to happen with the hardware. This includes things like writing/reading from the filesystem, sending data over the network, allocating memory, or playing audio over the speakers. Given its power, programs are not allowed direct
... See moreJesse Storimer • Working With Unix Processes
In much the same way as pids represent running processes, file descriptors represent open files.