Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Sunday, January 20, 2013

Linux FILE SYSTEM Structure


More file system layout

3.1.3.1. Visual

For convenience, the Linux file system is usually thought of in a tree structure. On a standard Linux system you will find the layout generally follows the scheme presented below.
Figure 3-1. Linux file system layout
This is a layout from a RedHat system. Depending on the system admin, the operating system and the mission of the UNIX machine, the structure may vary, and directories may be left out or added at will. The names are not even required; they are only a convention.
The tree of the file system starts at the trunk or slash, indicated by a forward slash (/). This directory, containing all underlying directories and files, is also called the root directory or "the root" of the file system.
Directories that are only one level below the root directory are often preceded by a slash, to indicate their position and prevent confusion with other directories that could have the same name. When starting with a new system, it is always a good idea to take a look in the root directory. Let's see what you could run into:

emmy:~> cd /
emmy:/> ls
bin/   dev/  home/    lib/         misc/  opt/     root/  tmp/  var/
boot/  etc/  initrd/  lost+found/  mnt/   proc/    sbin/  usr/
Table 3-2. Subdirectories of the root directory
DirectoryContent
/binCommon programs, shared by the system, the system administrator and the users.
/bootThe startup files and the kernel, vmlinuz. In some recent distributions also grub data. Grub is the GRand Unified Boot loader and is an attempt to get rid of the many different boot-loaders we know today.
/devContains references to all the CPU peripheral hardware, which are represented as files with special properties.
/etcMost important system configuration files are in /etc, this directory contains data similar to those in the Control Panel in Windows
/homeHome directories of the common users.
/initrd(on some distributions) Information for booting. Do not remove!
/libLibrary files, includes files for all kinds of programs needed by the system and the users.
/lost+foundEvery partition has a lost+found in its upper directory. Files that were saved during failures are here.
/miscFor miscellaneous purposes.
/mntStandard mount point for external file systems, e.g. a CD-ROM or a digital camera.
/netStandard mount point for entire remote file systems
/optTypically contains extra and third party software.
/procA virtual file system containing information about system resources. More information about the meaning of the files in proc is obtained by entering the command man proc in a terminal window. The file proc.txt discusses the virtual file system in detail.
/rootThe administrative user's home directory. Mind the difference between /, the root directory and /root, the home directory of the root user.
/sbinPrograms for use by the system and the system administrator.
/tmpTemporary space for use by the system, cleaned upon reboot, so don't use this for saving any work!
/usrPrograms, libraries, documentation etc. for all user-related programs.
/varStorage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the Internet, or to keep an image of a CD before burning it.
How can you find out which partition a directory is on? Using the df command with a dot (.) as an option shows the partition the current directory belongs to, and informs about the amount of space used on this partition:

sandra:/lib> df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda7             980M  163M  767M  18% /
As a general rule, every directory under the root directory is on the root partition, unless it has a separate entry in the full listing from df (or df -h with no other options).
Read more in man hier.

Saturday, December 8, 2012

The fork function


UNIX / Linux Processes: C fork() Function

by HIMANSHU ARORA on MAY 18, 2012
Every running instance of a program is known as a process. The concept of processes is fundamental to the UNIX / Linux operating systems. A process has its own identity in form of a PID or a process ID. This PID for each process is unique across the whole operating system. Also, each process has its own process address space where memory segments like code segment, data segment, stack segment etc are placed. The concept of process is very vast and can be broadly classified into process creation, process execution and process termination.

Linux Processes Series: part 1, part 2, part 3, part 4 (this article).
In this article we will concentrate on the process creation aspect from programming point of view. We will focus on the fork() function and understand how it works.

The fork() Function

The fork() function is used to create a new process by duplicating the existing process from which it is called. The existing process from which this function is called becomes the parent process and the newly created process becomes the child process. As already stated that child is a duplicate copy of the parent but there are some exceptions to it.
  • The child has a unique PID like any other process running in the operating system.
  • The child has a parent process ID which is same as the PID of the process that created it.
  • Resource utilization and CPU time counters are reset to zero in child process.
  • Set of pending signals in child is empty.
  • Child does not inherit any timers from its parent
Note that the above list is not exhaustive. There are a whole lot of points mentioned in the man page of fork(). I’d strongly recommend readers of this article to go through those points in the man page of fork() function.

The Return Type

Fork() has an interesting behavior while returning to the calling method. If the fork() function is successful then it returns twice. Once it returns in the child process with return value ’0′ and then it returns in the parent process with child’s PID as return value. This behavior is because of the fact that once the fork is called, child process is  created and since the child process  shares the text segment with parent process and continues execution from the next statement in the same text segment so fork returns twice (once in parent and once in child).

C Fork Example

Lets take an example to illustrate the use of fork function.
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>

int var_glb; /* A global variable*/

int main(void)
{
    pid_t childPID;
    int var_lcl = 0;

    childPID = fork();

    if(childPID >= 0) // fork was successful
    {
        if(childPID == 0) // child process
        {
            var_lcl++;
            var_glb++;
            printf("\n Child Process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb);
        }
        else //Parent process
        {
            var_lcl = 10;
            var_glb = 20;
            printf("\n Parent process :: var_lcl = [%d], var_glb[%d]\n", var_lcl, var_glb);
        }
    }
    else // fork failed
    {
        printf("\n Fork failed, quitting!!!!!!\n");
        return 1;
    }

    return 0;
}
In the code above :
  • A local and a global variable  (var_lcl and var_glb ) declared and initialized with value ’0′
  • Next the fork() function is called and its return value is saved in variable childPID.
  • Now, the value of childPID is checked to make sure that the fork() function passed.
  • Next on the basis of the value of childPID, the code for parent and child is executed.
  • One thing to note here is why the variables var_lcl and var_glb are used?
  • They are used to show that both child and parent process work on separate copies of these variables.
  • Separate values of these variables are used to show the above stated fact.
  • In linux, a copy-on-write mechanism is used where-in both the child and the parent keep working on the same copy of variable until one of them tries to change its value.
  • After fork, whether the child will run first or parent depends upon the scheduler.
Now, when the above code is compiled and run :
$ ./fork

Parent process :: var_lcl = [10], var_glb[20]

Child Process :: var_lcl = [1], var_glb[1]
We see that in the output above, both the child and parent process got executed and logs show separate values of var_lcl and var_glb. This concludes that both parent and child had their own copy of var_lcl and var_glb.
NOTE: A function getpid() can be used to fetch the process ID of the calling process and the function getppid() can be used to fetch the PID of the parent process.

Thursday, November 15, 2012

IPC - InterProcess Communication

进程间通信英文缩写为IPC。
Linux系统中进程间通信的方式有:socket, named pipe,message queque, signal,share memory.
Java系统中的进程间通信方式有socket, named pipe等.
 A named pipe is system-persistent and exists beyond the life of the process and must be deleted once it is no longer being used. Processes generally attach to the named pipes (usually appearing as a file) to perform inter-process communication (IPC).

Sunday, November 11, 2012

vi && vim

VI 代码自动对齐: command mode [ 注意不是(last line mode)]下输入gg=G, 连回车都不用

vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令。由于对Unix及Linux系统的任何版本,vi编辑器是完全相同的,因此您可以在其他任何介绍vi的地方进一步了解它。Vi也是Linux中最基本的文本编辑器,学会它后,您将在Linux的世界里畅行无阻。

1、vi的基本概念
  基本上vi可以分为三种状态,分别是命令模式(command mode)、插入模式(Insert mode)和底行模式(last line mode),各模式的功能区分如下:

1) 命令行模式command mode)

  控制屏幕光标的移动,字符、字或行的删除,移动复制某区段及进入Insert mode下,或者到 last line mode。

2) 插入模式(Insert mode)

  只有在Insert mode下,才可以做文字输入,按「ESC」键可回到命令行模式。

3) 底行模式(last line mode)

  将文件保存或退出vi,也可以设置编辑环境,如寻找字符串、列出行号……等。

不过一般我们在使用时把vi简化成两个模式,就是将底行模式(last line mode)也算入命令行模式command mode)。

2、vi的基本操作
a) 进入vi

  在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面:

   $ vi myfile


  不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(command mode)」,您要切换到「插入模式(Insert mode)」才能够输入文字。初次使用vi的人都会想先用上下左右键移动光标,结果电脑一直哔哔叫,把自己气个半死,所以进入vi后,先不要乱动,转换到「插入模式(Insert mode)」再说吧!

b) 切换至插入模式(Insert mode)编辑文件

  在「命令行模式(command mode)」下按一下字母「i」就可以进入「插入模式(Insert mode)」,这时候你就可以开始输入文字了。

c) Insert 的切换

  您目前处于「插入模式(Insert mode)」,您就只能一直输入文字,如果您发现输错了字!想用光标键往回移动,将该字删除,就要先按一下「ESC」键转到「命令行模式(command mode)」再删除文字。

d) 退出vi及保存文件

  在「命令行模式(command mode)」下,按一下「:」冒号键进入「Last line mode」,例如:

: w filename (输入 「w filename」将文章以指定的文件名filename保存)

: wq (输入「wq」,存盘并退出vi)

: q! (输入q!, 不存盘强制退出vi)


3、命令行模式(command mode)功能键
1). 插入模式

  按「i」切换进入插入模式「insert mode」,按“i”进入插入模式后是从光标当前位置开始输入文件;

  按「a」进入插入模式后,是从目前光标所在位置的下一个位置开始输入文字;

  按「o」进入插入模式后,是插入新的一行,从行首开始输入文字。

2). 从插入模式切换为命令行模式

  按「ESC」键。

3). 移动光标

  vi可以直接用键盘上的光标来上下左右移动,但正规的vi是用小写英文字母「h」、「j」、「k」、「l」,分别控制光标左、下、上、右移一格。

  按「ctrl」+「b」:屏幕往“后”移动一页。

  按「ctrl」+「f」:屏幕往“前”移动一页。

  按「ctrl」+「u」:屏幕往“后”移动半页。

  按「ctrl」+「d」:屏幕往“前”移动半页。

  按数字「0」:移到文章的开头。

  按「G」:移动到文章的最后。

  按「$」:移动到光标所在行的“行尾”。

  按「^」:移动到光标所在行的“行首”

  按「w」:光标跳到下个字的开头

  按「e」:光标跳到下个字的字尾

  按「b」:光标回到上个字的开头

  按「#l」:光标移到该行的第#个位置,如:5l,56l。

4). 删除文字

  「x」:每按一次,删除光标所在位置的“后面”一个字符。

  「#x」:例如,「6x」表示删除光标所在位置的“后面”6个字符。

  「X」:大写的X,每按一次,删除光标所在位置的“前面”一个字符。

  「#X」:例如,「20X」表示删除光标所在位置的“前面”20个字符。

  「dd」:删除光标所在行。

  「#dd」:从光标所在行开始删除#行

5). 复制

  「yw」:将光标所在之处到字尾的字符复制到缓冲区中。

  「#yw」:复制#个字到缓冲区

  「yy」:复制光标所在行到缓冲区。

  「#yy」:例如,「6yy」表示拷贝从光标所在的该行“往下数”6行文字。

  「p」:将缓冲区内的字符贴到光标所在位置。注意:所有与“y”有关的复制命令都必须与“p”配合才能完成复制与粘贴功能。

6). 替换

  「r」:替换光标所在处的字符。

  「R」:替换光标所到之处的字符,直到按下「ESC」键为止。

7). 回复上一次操作

  「u」:如果您误执行一个命令,可以马上按下「u」,回到上一个操作。按多次“u”可以执行多次回复。

8). 更改

  「cw」:更改光标所在处的字到字尾处

  「c#w」:例如,「c3w」表示更改3个字

9). 跳至指定的行

  「ctrl」+「g」列出光标所在行的行号。

  「#G」:例如,「15G」,表示移动光标至文章的第15行行首。

4、Last line mode下命令简介
  在使用「last line mode」之前,请记住先按「ESC」键确定您已经处于「command mode」下后,再按「:」冒号即可进入「last line mode」。

A) 列出行号

 「set nu」:输入「set nu」后,会在文件中的每一行前面列出行号。


B) 跳到文件中的某一行

 「#」:「#」号表示一个数字,在冒号后输入一个数字,再按回车键就会跳到该行了,如输入数字15,再回车,就会跳到文章的第15行。

C) 查找字符

 「/关键字」:先按「/」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往后寻找到您要的关键字为止。

 「?关键字」:先按「?」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往前寻找到您要的关键字为止。

D) 保存文件

 「w」:在冒号输入字母「w」就可以将文件保存起来。

E) 离开vi

 「q」:按「q」就是退出,如果无法离开vi,可以在「q」后跟一个「!」强制离开vi。

 「qw」:一般建议离开时,搭配「w」一起使用,这样在退出的时候还可以保存文件。

5、vi命令列表
1、下表列出命令模式下的一些键的功能:

h
左移光标一个字符

l
右移光标一个字符

k
光标上移一行

j
光标下移一行

^
光标移动至行首

0
数字“0”,光标移至文章的开头

G
光标移至文章的最后

$
光标移动至行尾

Ctrl+f
向前翻屏

Ctrl+b
向后翻屏

Ctrl+d
向前翻半屏

Ctrl+u
向后翻半屏

i
在光标位置前插入字符

a
在光标所在位置的后一个字符开始增加

o
插入新的一行,从行首开始输入

ESC
从输入状态退至命令状态

x
删除光标后面的字符

#x
删除光标后的#个字符

X
(大写X),删除光标前面的字符

#X
删除光标前面的#个字符

dd
删除光标所在的行

#dd
删除从光标所在行数的#行

yw
复制光标所在位置的一个字

#yw
复制光标所在位置的#个字

yy
复制光标所在位置的一行

#yy
复制从光标所在行数的#行

p
粘贴

u
取消操作

cw
更改光标所在位置的一个字

#cw
更改光标所在位置的#个字


2、下表列出行命令模式下的一些指令
w filename
储存正在编辑的文件为filename

wq filename
储存正在编辑的文件为filename,并退出vi

q!
放弃所有修改,退出vi

set nu
显示行号

/或?
查找,在/后输入要查找的内容

n
与/或?一起使用,如果查找的内容不是想要找的关键字,按n或向后(与/联用)或向前(与?联用)继续查找,直到找到为止。

对于第一次用vi,有几点注意要提醒一下:
1、用vi打开文件后,是处于「命令行模式(command mode)」,您要切换到「插入模式(Insert mode)」才能够输入文字。切换方法:在「命令行模式(command mode)」下按一下字母「i」就可以进入「插入模式(Insert mode)」,这时候你就可以开始输入文字了。
2、编辑好后,需从插入模式切换为命令行模式才能对文件进行保存,切换方法:按「ESC」键。
3、保存并退出文件:在命令模式下输入:wq即可!(别忘了wq前面的:)

Linux Command


Copy & Paste in Unix Shell
 use CtrlShiftC and CtrlShiftV.

Install Package in shell
sudo apt-get install <selected package>
sudo 获取superuser权限(通常是5分钟)
apt-get: The apt-get command is a powerful command-line tool used to work with Ubuntu's Advanced Packaging Tool (APT) performing such functions as installation of new software packages, upgrade of existing software packages, updating of the package list index, and even upgrading the entire Ubuntu system.



Linux Command ORG

Java and the Linux Command Line

Bash Shell Reference Manual