head – output the first part of files

commandline session

$cat bash.txt
hack
hacker
$head bash.txt
hack
hacker
$head -n 1 bash.txt
hack
$head -n 2 bash.txt
hack
hacker
$head -n 3 bash.txt
hack
hacker
$head -v -n 1 bash.txt
==> bash.txt  bash.txt <==
hack
hacker
$

http://www.gnu.org/software/coreutils/manual/html_node/head-invocation.html

$ head -n 1 qsort.c
#include
$ head -n 2 qsort.c
#include

$ head -n 3 qsort.c
#include

void quick_sort (int *a, int n) {
$ head -n 4 qsort.c
#include

void quick_sort (int *a, int n) {
    if (n  qsort.c <==
#include

void quick_sort (int *a, int n) {
    if (n  source.c <==
 #include 

int
main(int argc, char *argv[])
$ head -n 1 qsort.c source.c
==> qsort.c <==
#include

==> source.c <==
 #include 
$ head -n 1 qsort.c -n 2 source.c
==> qsort.c <==
#include


==> source.c <==
 #include 

$ head -n 1 qsort.c -n 5 source.c
==> qsort.c <==
#include

void quick_sort (int *a, int n) {
    if (n  source.c <==
 #include 

int
main(int argc, char *argv[])
    {
$ head -n 1 qsort.c  source.c
==> qsort.c <==
#include

==> source.c <==
 #include 
$

SOURCE CODE
http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/head.c

hung_task_timeout_secs( topic related to linux kernel hangup )

ABOUT hung_task_timeout_secs

 
if a task(process) is hung then hung_task_timeout_secs value
decides if the hung task needs no reboot or reboot after
n seconds

LINUX KERNEL RELATED PARAMETER
$cat /proc/sys/kernel/hung_task_timeout_secs
120
$

$echo 0 | sudo tee –append /proc/sys/kernel/ hung_task_timeout_secs
0
$sudo cat /proc/sys/kernel/hung_task_timeout_secs
0
$

When a task in D state did not get scheduled for more than
this value report a warning.This file shows up if CONFIG_
DETECT_HUNG_TASK is enabled. 0: means infinite timeout - no checking done. Possible values
to set are in range {0..LONG_MAX/HZ}.

PARAMETER RELATED

TEST-MAIL1 ~ #dmesg

“echo 0 > /proc/sys/kernel/hung_task_timeout_secs” disables this message.
rm D ffff88107f472c40 0 16705 22512 0x00000000
ffff881014693810 0000000000000086 ffff881000000000 ffff88102013b040
0000000000012c40 ffff880471855fd8 0000000000012c40 ffff880471854010
ffff880471855fd8 0000000000012c40 ffff881017ff8e40 0000000100000000
Call Trace:
[<ffffffff8148d45d>] ? schedule_timeout+0x1ed/0x2d0
[<ffffffffa0b7d1ea>] ? dlmlock+0x8a/0xda0 [ocfs2_dlm]
[<ffffffff8148ce5c>] ? wait_for_common+0x12c/0x1a0
[<ffffffff81052230>] ? try_to_wake_up+0x280/0x280
[<ffffffffa0a3b9c0>] ? __ocfs2_cluster_lock+0x1f0/0x780 [ocfs2]
[<ffffffff8148ce80>] ? wait_for_common+0x150/0x1a0
[<ffffffffa0a9c6bc>] ? ocfs2_buffer_cached+0x8c/0x180 [ocfs2]
[<ffffffffa0a40bc6>] ? ocfs2_inode_lock_full_nested+0x126/0x540 [ocfs2]
[<ffffffffa0a5922e>] ? ocfs2_lookup_lock_orphan_dir+0x6e/0x1b0 [ocfs2]
[<ffffffffa0a5922e>] ? ocfs2_lookup_lock_orphan_dir+0x6e/0x1b0 [ocfs2]
[<ffffffffa0a5ba1a>] ? ocfs2_prepare_orphan_dir+0x4a/0x290 [ocfs2]
[<ffffffffa0a5e621>] ? ocfs2_unlink+0x6e1/0xbb0 [ocfs2]
[<ffffffff811bcfea>] ? may_link+0xda/0x170
[<ffffffff81141c8e>] ? vfs_unlink+0x9e/0x100
[<ffffffff81145881>] ? do_unlinkat+0x1a1/0x1d0
[<ffffffff81147b00>] ? vfs_readdir+0xa0/0xe0
[<ffffffff8116fedb>] ? fsnotify_find_inode_mark+0x2b/0x40
[<ffffffff81170c24>] ? dnotify_flush+0x54/0x110
[<ffffffff81133eec>] ? filp_close+0x5c/0x90
[<ffffffff81496912>] ? system_call_fastpath+0x16/0x1b

CLASSROOM

While waiting for read() or write() to/from a file
descriptor return, the process will be put in aspecial
kind of sleep, known as "D" or "Disk Sleep". This is
special, because the process can not be killed or
interrupted while in such a state. A process waiting
for a return from ioctl() would also be put to sleep in this manner.

RELATED SOURCE CODE EXPOSURE

/*
* Ok, the task did not get scheduled for more than 2 minutes,
* complain:
*/
if (sysctl_hung_task_warnings) {
if (sysctl_hung_task_warnings > 0)
sysctl_hung_task_warnings–;
pr_err(“INFO: task %s:%d blocked for more than %ld seconds.\n”,
t->comm, t->pid, timeout);
pr_err(” %s %s %.*s%s\n”,
print_tainted(), init_utsname()->release,
(int)strcspn(init_utsname()->version, ” “),
init_utsname()->version,
LINUX_PACKAGE_ID);
pr_err(“\”echo 0 > /proc/sys/kernel/hung_task_timeout_secs\””
” disables this message.\n”);
sched_show_task(t);
hung_task_show_lock = true;
}
[/c]

[c light=”true”]
/*
* Process updating of timeout sysctl
*/
int proc_dohung_task_timeout_secs(struct ctl_table * styletable, int write,
void __user *buffer,
size_t *lenp, loff_t *ppos)
{
int ret;

ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);

if (ret || !write)
goto out;

wake_up_process(watchdog_task);

out:
return ret;
}
[/c]
SOURCE CODE TAKEN FROM OFFICIAL LINUX KERNEL

RELATED FROM RESEARCH PAPER

Kernel  data collection  tools. Several  monitoring  facilities are
provided by  the Linux  kernel, which have  been exploited  in this
work. In  particular, we use  KProbes which inserts  breakpoints in
arbitrary binary code locations in charge of triggering user-defined
handler  functions. Handlers  can  be used  to collect  information
about internal kernel  variables; subsequently, kernel execution is
restored. Kdump is a tool  for failure data collection based on the
execution of  a secondary kernel,  namely capture kernel,  which is
preliminarily  loaded  into  a  reserved memory  region.  When  the
primary kernel fails, the capture  kernel is executed; then, it can
collect failure  data by reading  the main memory  state.  Built-in
hang  detection mechanisms. Several  hang detection  mechanisms are
available in the Linux OS,  which can be enabled by recompiling the
kernel.  In particular, the  following facilities  can be  used for
hang  detection: Soft  lockup detection,  i.e., the  kernel detects
whether a  "canary" task  is not scheduled  within a  timeout; Hard
lockup detection, i.e.,  if any CPU in the  system does not handles
local    timer    interrupt   for    longer    than   a    timeout;
Sleep-inside-spinlock   checking,  i.e.,  assertions   that  verify
whether there are spinlocks  that have been acquired before calling
a  sleeping function  (i.e., a  function during  which  the current
thread may block and be preempted by the scheduler); Checks on lock
API  usage, that  is: missing  lock initialization,  release  of an
already freed lock, release of a  lock by a thread or CPU different
from the lock holder, lock data structure corruption.

 

source : http://tinyurl.com/7pt5j9a

Assessment and Improvement of Hang Detection in the Linux Operating System
2009 28th IEEE International Symposium on Reliable Distributed System

LINKS
https://access.redhat.com/solutions/60572
https://www.linuxquestions.org/questions/linux-software-2/kernel-panic-echo-0-proc-sys-kernel-4175629199/
https://www.kernel.org/doc/Documentation/sysctl/kernel.txt
https://stackoverflow.com/questions/84882/sudo-echo-something-etc-privilegedfile-doesnt-work-is-there-an-alterna
https://www.tldp.org/LDP/tlk/kernel/processes.html
https://www.nico.schottelius.org/blog/reboot-linux-if-task-blocked-for-more-than-n-seconds/
http://stackoverflow.com/questions/1475683/linux-process-states

GET THE HANG bundle install

$sudo bundle install
Fetching gem metadata from https://rubygems.org/........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (0.9.2.2)
Installing RedCloth (4.2.9)
Installing chunky_png (1.2.5)
Installing fast-stemmer (1.0.1)
Installing classifier (1.3.3)
Installing fssm (0.2.9)
Installing sass (3.2.9)
Installing compass (0.12.2)
Installing directory_watcher (1.4.1)
Installing haml (3.1.7)
Installing kramdown (0.13.8)
Installing liquid (2.3.0)
Installing syntax (1.0.0)
Installing maruku (0.6.1)
Installing posix-spawn (0.3.6)
Installing yajl-ruby (1.1.0)
Installing pygments.rb (0.3.4)
Installing jekyll (0.12.0)
Installing rack (1.5.2)
Installing rack-protection (1.5.0)
Installing rb-fsevent (0.9.1)
Installing rdiscount (2.0.7.3)
Installing rubypants (0.2.0)
Installing sass-globbing (1.0.0)
Installing tilt (1.3.7)
Installing sinatra (1.4.2)
Installing stringex (1.4.0)
Using bundler (1.3.5)
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
$

getopt . more on parse command options

commandline session

$getopt hello
 --
$getopt helloo
 --
$getopt hellooo
 --
$getopt hellooo how
 -- how
$getopt hellooo how are
 -- how are
$getopt h how are
 -- how are
$getopt h h h
 -- h h
$getopt ab -a
 -a --
$getopt ab -ab
 -a -b --
$getopt ab -ab -R
getopt: invalid option -- 'R'
 -a -b --
$getopt ab -ab -l
getopt: invalid option -- 'l'
 -a -b --
$getopt ab -abl
getopt: invalid option -- 'l'
 -a -b --
$getopt ab -abn
getopt: invalid option -- 'n'
 -a -b --
$getopt ab -abc
getopt: invalid option -- 'c'
 -a -b --
$getopt nl -nl
 -n -l --
$getopt R -nl
getopt: invalid option -- 'n'
getopt: invalid option -- 'l'
 --
$getopt R -R
 -R --
$

vmstat . more on virtual memory statistics

commandline session

$vmstat -s
      1995396 K total memory
      1197836 K used memory
       599976 K active memory
       483048 K inactive memory
       797560 K free memory
        49620 K buffer memory
       648028 K swap cache
      5843964 K total swap
            0 K used swap
      5843964 K free swap
       261891 non-nice user cpu ticks
          534 nice user cpu ticks
        68515 system cpu ticks
      1118198 idle cpu ticks
        17369 IO-wait cpu ticks
            0 IRQ cpu ticks
         2940 softirq cpu ticks
            0 stolen cpu ticks
       629227 pages paged in
       700676 pages paged out
            0 pages swapped in
            0 pages swapped out
      5625600 interrupts
     14181960 CPU context switches
   1372693030 boot time
         5917 forks
$

http://superuser.com/questions/101183/what-is-a-cpu-tick

stat . more from stat command

commandline session

$ls
store  store1
$pwd
/home/jeffrin/Test
$stat store
  File: ‘store’
  Size: 19        	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 7036935     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ jeffrin)   Gid: ( 1000/ jeffrin)
Access: 2013-06-29 18:37:33.000000000 +0530
Modify: 2013-06-29 18:37:30.000000000 +0530
Change: 2013-06-29 18:37:30.000000000 +0530
 Birth: -
$stat -f store
  File: "store"
    ID: f7da53a314c76a3d Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 581002890   Free: 23134751   Available: 20155347
Inodes: Total: 14901248   Free: 134100922
$stat -t store
store 19 8 81a4 1000 1000 801 7036935 1 0 0 1372511253 1372511250 1372511250 0 4096
$stat -f -t store
store f7da53a314c76a3d 255 ef53 4096 4096 581002890 23134516 20155112 14901248 134100919
$stat -f -t -c %A store
?
$stat  -c %A store
-rw-r--r--
$stat  -c %m store
/
$stat  -c %G store
jeffrin
$stat  -c %i store
7036935
$stat  -c %n store
store
$

nc netcat

commandline session

$nc 127.0.0.1 25
220 debian.Jeff ESMTP Exim 4.80 Thu, 27 Jun 2013 00:05:20 +0530
^C
$nc gnu.org
no port[s] to connect to
$nc gnu.org 25
^C
$nc -g gnu.org 25
no port[s] to connect to
$nc -g gnu.org gnu.org 25
^C
$nc -g 127.0.0.1 127.0.0.1  25
^C
$nc www.youtube.com
no port[s] to connect to
$nc www.youtube.com 80
^C
$
$nc -z -vv www.gnu.org  80
wildebeest.gnu.org [208.118.235.148] 80 (http) open
 sent 0, rcvd 0
$nc -z -vv 127.0.0.1  80
localhost.localdomain [127.0.0.1] 80 (http) : Connection refused
 sent 0, rcvd 0
$nc -z -vv 127.0.0.1  25
localhost.localdomain [127.0.0.1] 25 (smtp) open
 sent 0, rcvd 0
$