GNU Screen - Getting Started

Screen is an important tool especially for working remotely and running long tasks on the remote machine. In such scenarios, there is a possibility that the VPN connection could break in the middle of a task. GNU Screen allows you to start a window in your remote machine which is not tied to the life of your current terminal. This way even if you disconnect, your Screen will still continue to run on your remote machine and you can just re-attach to the same session once you re-connect.

Following are some basic commands to get you started :
  • Start screen - Just type "screen" into a Linux terminal. This will give you a window identical to a normal Linux terminal. You can send commands to the screen program by using CTRL + a. More on these commands below. Whatever you do now will be running inside screen.
  • Detach from Screen - CTRL + a + d. This will detach you from screen and take you to the terminal. Your screen session will continue to run in the background.
  • Re-attach to Screen - "screen -r". This will re-attach yourself to an existing screen session. In case, there are multiple screen sessions, then you need to pick the screen using its pid. The multiple screens will look as follows :
          There are several suitable screens on:
                            25779.pts-26.asterws0133 (10/16/2016 04:14:22 PM) (Detached)
                            20894.pts-27.asterws0133 (10/16/2016 01:19:11 PM) (Detached)
                            26605.pts-30.asterws0133 (10/15/2016 08:33:05 PM) (Detached)
          Type "screen [-d] -r [pid.]tty.host" to resume one of them.
          You can type "screen -r 25779" to attach to the first screen.
            • Open multiple windows in Screen - CTRL+a+c.
            • Close a window in Screen - "exit". If your screen only has one window, this will close it.
            • Switching between screen windows -
              • CTRL+a+n - next window
              • CTRL+a+p - previous window

            Vimdiff

            Although I have been straying more and more into the commandline realm the past two years " vim, cscope etc, etc, I was staying away from command line diffing tools until recently. I had been satisfied with melddiff viewer for Ubuntu. However, working on a remote putty does not make your life a hell of a lot easier. So, I discovered vimdiff, which once you get the hang of it is a simple but powerful tool and it also comes bundled with vim. So, the following are some of the commands that I found of paramount importance (ofcourse knowing vim is a pre-requisite) :

            [c - move to the change above you
            ]c - move to the change below you

            Move your cursor to a change and
            do - will get the changes from the other window to the current window
            dp - will put the changes in the current window to the other window

            Both are helpful and useful as you might have to merge a change where one window has extra lines.

            :diffu - which will recaculate diff and highlight the changes again. This is useful once you have already merged and undid your changes and the diffs are not highlighted anymore.

            Virtual Memory and My Misconceptions

            Segmentation Fault or bad alloc() why do they occur?? The former occurs for the so called stack allocation when the stack memory runs out for the process and the pointer points elsewhere. The latter occurs for heap allocation when there is not enough free pages available to be satisfy the  requested size. I know stack and heap are subjective in nature but lets call them that for the time being.

            However, are we not supposed to think that mechanisms like virtual memory and swapping allows us to essentially use the real memory + the disk as the effective memory that we can use?  The disk can be again be subjective as in Linux it can refer to a swap partition or a swap file, but lets hypothesize that we can allocate it so that it possibly can use the entire disk. Then again why do we run out of memory so easily when modern disks are so huge in size. And where exactly does virtual memory play a role?

            I had been baffled by all these questions sometime back when I was trying to research a new sort of memory allocator. This blog, “Out Of Memory” Does Not Refer to Physical Memory" by Eric Lippert gave me further insight into the matter at hand. He says that the entire memory for a process can be thought of as a file on disk and the RAM as a mere performance enhancement, a sort of cache. If you think about it this way a lot of problems goes away. But then again if memory is really a file on disk, then when do we run out of memory? Is it when we run out of disk space? That can't be possibly right, can it?

            We run out of memory when there is no more contiguous virtual memory address space to allocate to the requesting entity. In processors that provide disparate virtual address spaces to processes, the entity usually refers to threads. If that is in fact true, then running out of space is more of a misnomer, is it not?

            Should it not be called running out of virtual memory address space.
            Also the exception "Out of Memory" should be revised to be called "Out of Contiguous Virtual Address Space"

            Note: Whenever I talk about a language, it is almost always C++. Beg your pardon for any confusions

            sed - the powerful stream editor

            What I am putting down here is more for me rather than for anyone else. When you work with a file, it is often required to change the format of the data in the entire file or edit a certain pattern in there. And when the file size is large, this becomes a highly tedious task. And stream editors like sed are ideal for this purpose.
            Well, this post only highlights the use of the substitution command or the s command for sed. 
            The syntax is as follows:
            sed 's/pattern to be replaced/pattern to replace with/' filename
            The "filename" here refers to the input file. And the output works just like the linux command 'cat'. It will be displayed in the terminal.
            So, in order to write to a file, you can use
            sed 's/pattern to be replaced/pattern to replace with/' filename > outfilename
            There are also some extra additions to this command to obtain certain results:
            i) sed in the above shown form only works on the first pattern found in each line. To operate on all recurring patterns in a line the g command is used.
            sed 's/pattern to be replaced/pattern to replace with/g' filename > outfilename
            ii) In order to delete a pattern you can just leave the second delimiter blank
            sed 's/pattern to be replaced//' filename > outfilename
            iii) In order to look for a group of characters say a-z and replace each with a particular pattern then
            sed 's/[a-z]/pattern to replace with/' filename > outfilename
            iv) if you need to replace every other character than say a-z with a particular pattern then
            sed 's/[^a-z]/pattern to replace with/' filename > outfilename
            v) In order to operate on a particular pattern(say a tab) and the rest of the line 
            sed 's/[\t].*/pattern to replace with/' filename > outfilename
            I believe a combination of these are good enough to edit a basic text file. If you need a comprehensive tutorial it can be found here: