Linux Problem Set
1 Linux environment variable setting and storage location
1. Display Environment Variables Home
$ echo $HOME
/home/terry
2. Set a new environment variable welcome
$ exportwelcome= "hello!"
$ echo$welcome
Hello!
3. Use the ENV command to display all environment variables
$ env
hostname=terry.mykms.org
Pvm_rsh=/usr/bin/rsh
Shell=/bin/bash
Term=xterm
histsize=1000
...
4. Use the SET command to display all locally defined shell variables
$ set
Bash=/bin/bash
bash_versinfo= ([0]= "2" [1]= "05b" [2]= "0" [3]= "1" [4]= "Release" [5]= "I386-redhat-linux-gnu")
Bash_version= ' 2.05b.0 (1)-release '
Colors=/etc/dir_colors.xterm
Columns=80
Dirstack= ()
display=:0.0
...
5. Use the unset command to clear environment variables
Set sets the value of an environment variable. Clear the value of the environment variable with the unset command. If no value is specified, the value of the variable is set to NULL. Examples are as follows:
$ exporttest= "Test ..." #增加一个环境变量TEST
$ env|greptest #此命令有输入 to prove that the environment variable test already exists
Test=test ...
$ unset $TEST # Delete environment variable TEST
$ env|greptest #此命令没有输出 to prove that the environment variable test already exists
6. Set read-only variables using the readonly command
If the readonly command is used, the variable cannot be modified or erased. Examples are as follows:
$ exporttest= "Test ..." #增加一个环境变量TEST
$ readonlytest #将环境变量TEST设为只读
$ unset test# will find that this variable cannot be deleted
-bash:unset:test:cannot unset:readonly Variable
$TEST = "New" #会发现此也变量不能被修改
-bash:test:readonly variable
The global settings for environment variables are located in the/etc/profile file, and you can add a subordinate row if you need to increase the new environment variable
Exportpath= $PATH:/path1:/path2:/pahtn
2 Ubuntu Install the Delete command
Apt says it is a advancedpacakge Tool.
Apt-cachesearch Package Search
Apt-cacheshow PackageName Gets the package information, such as version and size.
Apt-getinstall PackageName installation package.
Apt-getinstall pacekage--reinstall Reinstall
Apt-get-finstall PackageName Forced installation
Apt-getremove PackageName Delete a program
Apt-getremove packagename--pure clean off.
Apt-getautoremove automatically remove the packets that are not
Apt-getupdate
Apt-getupgrade updating a package that has already been installed
Apt-getdist-upgrade upgrade system, no use
Apt-cachedepends Pacakgename understand the use of dependency, rarely used
Apt-cacherdepends PackageName understand the specific dependencies.
APT-GETBUILD-DEP PackageName Install the relevant compilation environment. This sometimes needs to be used.
Apt-getsource package Download and install the source code, sometimes need to look at programming
Apt-get Clean
Apt-getautoclean clean up the download file archive.
Apt-getcheck Check for damaged dependencies, rarely used.
A more detailed explanation can be obtained through the man Apt.
3 How to make Linux automatically load scripts at startup
1 Write the script first, such as the name a.sh
2 save path is:/etc/rc.d/a.sh
3 Modifying/etc/rc.d/rc.local
At the end of the file, add the a.sh script's storage path:/etc/rc.d/a.sh
Save, exit, OK.
4 Mount and the Umount Command
Mount Syntax:
Mount [option]<-t type > [-o mount Options] < device > < mount point >
The-t option is used to specify the type of file system on the partition. The file system you encounter most often should be EXT2FS (gnu/linux file system) or EXT3FS (improved log performance ext2fs), VFAT (for all dos/windows? Partitions: FAT12, + or 32) and ISO9660 ( CD-ROM file system). If you do not specify any type, mount will try to read the chunks in the partition to guess the file system.
The-o option specifies one or more mount options. The options you choose vary depending on the file system type. See the details in the Mount (8) manual page. Typically, a partition is mounted on a/mnt or other mount point (the mount point must already exist).
Example:
$ mount-text3/dev/hdb1/mnt
To remove a partition, you can use the Umount command, Syntax:
Umount < mount points | equipment >
Example:
$ UMOUNT/MNT OR:
$ umount/dev/hdb1
Tip: Some devices (usually CD-ROM) may be busy or unresponsive at some point. At this point, the workaround for most users is to restart the computer. We don't have to do this. For example, if UMOUNT/DEV/HDC fails, you can try "lazy" umount. The syntax is simple:
umount-l< mount point | equipment >
This command disconnects the device and turns off all handles that open the device. Typically, you can use the eject < mount point | device > Command to eject the disc. So, if the eject command fails and you don't want to restart, use the lazy umount.
If we want the system to mount certain partitions automatically at boot time, you should edit the/etc/fstab file. Each line of the file corresponds to a set of mount relationships, including the file system, mount point, and other options. Here is an example of this file:
/DEV/HDA1/EXT2 Defaults 1 1
/dev/hda5/home ext2 Defaults 1 2
/dev/hda6 swap swap defaults 0 0
None/mnt/cdrom supermount Dev=/dev/scd0,fs=udf:iso9660,ro,--0 0
None/mnt/floppy supermountdev=/dev/fd0,fs=ext2:vfat,--, sync,umask=0 0 0
NONE/PROC proc Defaults 0 0
None/dev/pts devpts mode=0622 0 0
Each row is made up of the following:
The device on which the file system resides;
mount point;
File system type;
Mount option;
Dump tool backup flag ;
The check order of fsck (file system check) FileSystem.
There is always a row explaining the root file system. The swap partition is a special partition because you cannot find its corresponding location in the tree structure, and the mount point field for those partitions is only the SWAP keyword. As for the/proc file system, we will describe it in more detail in the 10th Chapter/proc File System . Another special file system is/dev/pts.
For example, if you want to automatically mount the/dev/hdb1 to/usr, you have to add the following line to the/etc/fstab file:
/DEV/HDB1/USR ext2 Defaults 1 2
This will automatically mount the partition on each boot and check for errors if necessary.
Now let's introduce two special options: Noauto and user. The Noauto option specifies that the file system should not be mounted at startup, but only when you want to mount it. The user option specifies that the file system can be mounted or removed by anyone. These two options are typically used for CD-ROM and floppy disk drives. For more information about the other options in/etc/fstab, please see the fstab (5) manual page.
The advantage of using/etc/fstab is that it greatly simplifies the syntax of the Mount command. To mount the file system described in the file, you only need to refer to the mount point or device. To mount a floppy disk, you can enter only:
$ mount/mnt/floppy
Or:
$ mount/dev/fd0
5 file Lock Problem
Q: With Fcntl to file lock, in a process to add a file multiple times the same type of lock, no error?
A: If a process has a lock on a file interval, and then the process attempts to add a lock to the same file interval, the new lock replaces the old one.
6 framebuffer Introduction
FrameBuffer is a driver interface that appears in the 2.2.xx kernel. This interface will display the device abstract as a frame buffer. The user can view it as an image of the display memory, and after mapping it to the process address space, it can read and write directly, and the write operation can be immediately reflected on the screen. The driver's device files are generally/dev/fb0,/DEV/FB1, and so on. For example, assuming that the current display mode is a 1024x768-8 bit color, you can clear the screen with the following command:
$ dd If=/dev/zero of=/dev/fb0 bs=1024 count=768
In the application, it is generally used by mapping the FrameBuffer device to the process address space, such as the following program to open the/DEV/FB0 device and address mapping through MMAP system calls, followed by a memset to empty the screen (assuming that the display mode is 1024x768-8 bit color mode, linear memory mode):
int FB;
unsigned char* fb_mem;
FB = open ("/dev/fb0", O_RDWR);
Fb_mem = Mmap (NULL, 1024*768,prot_read| prot_write,map_shared,fb,0);
memset (fb_mem, 0, 1024x768);
The FrameBuffer device also provides several IOCTL commands that allow you to obtain some fixed information about the display device (such as display memory size), variable information related to the display mode (such as resolution, pixel structure, byte width per scan line), and color palette information in pseudo-color mode, and so on.
With the FrameBuffer device, you can also get the type of accelerated display card supported by the current kernel (via fixed information), which is usually associated with a particular display chip. For example, the current kernel (2.4.9) includes accelerated support for S3, Matrox, NVidia, 3Dfx, and other popular display chips. After the accelerated chip type has been obtained, the application can map the memory I/O (Memio) of the PCI device to the address space of the process. These Memio are generally used to control the registers of the display card, and by manipulating these registers, the application can control the acceleration of a particular video card.
PCI devices can map their control registers to physical memory space, and then access to these control registers becomes access to physical memory. Therefore, these registers are also called "Memio". Once mapped to physical memory, the normal process of Linux can map these memory I/O to the process address space through mmap, so that the registers can be accessed directly.
Of course, because different display chips have different acceleration capabilities, the use and definition of Memio are different, at this point, you need to speed up the different types of chip to achieve different acceleration functions. For example, most chips provide hardware acceleration support for rectangular fills, but different implementations of the chip are different, and it is necessary to write different functions to complete the fill rectangle for different chip types.
In this case, the reader may have realized that FrameBuffer is just a device that provides display memory and display chip registers that are mapped from physical memory to the process address space. So, for an application, there's a lot more work to do if you want to do graphical programming on top of framebuffer. For example, FrameBuffer is like a canvas, using what kind of brush, how to draw, and you need to do it yourself.
7 Strings role of the command
Some files in binary format. You want to know what constant strings are in there. is very useful. For example, you generate such a program, A.C:
int main (void)
{
return printf ("Hello world\n");
}
#cc-o A.OUTA.C
#strings./a.out
/lib64/ld-linux-x86-64.so.2
_jv_registerclasses_
_gmon_start_
_
Libc.so.6
Printf
__libc_start_main
glibc_2.2.5
Hello World
8 What is A tty?
Terminal equipment is a type of character device, it has a variety of types, usually using TTY to collectively. TTY is the abbreviation for teletype. Teletype is one of the first terminal equipment, much like a telex typewriter (or ","), is produced by Teletype company. In the Linux system equipment special files directory/dev/, the terminal equipment files generally have the following kinds:
1. End-of-string terminal (/DEV/TTYSN)
The serial port terminal (Serial Port Terminal) is a terminal device connected using a computer serial port. The computer sees each serial port as a character device. For a while these serial port devices are often referred to as end-device, because it is used to connect the terminal at that time. The serial ports corresponding to the device name is/dev/tts/0 (or/DEV/TTYS0),/DEV/TTS/1 (or/dev/ttys1), etc., the device number is (4,0), (4,1), respectively, corresponding to the DOS system COM1, COM2 and so on. To send data to a port, you can redirect standard output to these special file names on the command line. For example, at a command-line prompt, type: echotest >/dev/ttys1 will send the word "test" to the device connected to the TtyS1 (COM2) port.
2. Pseudo-terminal (/dev/pty/)
Pseudo-terminal (Pseudo Terminal) is a paired logical terminal device, such as/DEV/PTYP3 and/DEV/TTYP3 (or/dev/pty/m3 and/DEV/PTY/S3 in the device file system, respectively). They are not directly related to the actual physical device. If a program considers TTYP3 as a serial port device, its read/write operation on the port is reflected in the other upper (PTYP3) of the logical terminal pair. The PTYP3 is a logical device that another program uses for read and write operations. In this way, two programs can communicate with each other through this logical device, and one of the programs that uses TTYP3 thinks that it is communicating with a serial port. This is like a pipe operation between logical device pairs.
For TTYP3 (S3), any program designed to use a serial port device can use the logical device. However, for programs that use PTYP3, you need to specifically design a ptyp3 (m3) logical device.
For example, if someone uses the Telnet program to connect to your computer on the Internet, the Telnet program may start connecting to the device Ptyp2 (m2) (on a pseudo-terminal port). At this point a Getty program should run on the corresponding TTYP2 (S2) port. When Telnet obtains a character from the far end, the character is passed to the Getty program via M2, S2, and the Getty program returns the "login:" String information to the network via S2, M2, and Telnet programs. In this way, the login program communicates with the Telnet program through a "pseudo-terminal". By using the appropriate software, two or more pseudo-terminal devices can be connected to the same physical serial port.
In order to obtain a large number of pseudo-terminal equipment special files before using device filesystem, a more complex file name naming method is used. Because there are only 16 Ttyp (TTYP0-TTYPF) of the device files, in order to get more logical device pairs, like Q, R, S and other characters are used instead of p. For example, TTYS8 and Ptys8 are a pseudo-terminal device pair. However, this naming method is still used in Linux systems such as Redhat.
However, Unix98 on a Linux system does not use the method described above and uses the "Pty Master" approach, such as/DEV/PTM3. The corresponding end is automatically created as a/DEV/PTS/3. This makes it possible to provide a Pty pseudo-terminal when needed. The directory/dev/pts is a file system of type devpts and can be seen in the list of loaded file systems. Although the file/DEV/PTS/3 appears to be an item in the device file system, it is a completely different file system.
3. Control Terminal (/dev/tty)
If the current process has a control terminal (controlling Terminal), then/dev/tty is the current Process Control terminal device special files. You can use the command "Ps–ax" to see which control terminal the process is connected to. For your login Shell,/dev/tty is the terminal you use, the device number is (5,0). Use the command "TTY" to see which actual terminal device it corresponds to. /dev/tty somewhat resembles a join or alias to a terminal device that is actually used.
4. Console (/dev/ttyn,/dev/console)
In a Linux system, a computer monitor is often referred to as a console terminal or console. It simulates a type of Linux terminal (Term=linux), and there are some device special files associated with it: Tty0, Tty1, Tty2, and so on. When you log in on the console, you are using Tty1. When using ALT+[F1-F6], we can switch to Tty2, Tty3, and so on. Tty1–tty6 is called a virtual terminal, and Tty0 is an alias of the virtual terminal that is currently used, and the information generated by the Linux system is sent to that terminal. So regardless of which virtual terminal we are currently using, the system information will be sent to our screen. C} $k]\
You can log in to different virtual terminals, so you can have several different sessions in the system at the same time. However, only the system or Superuser root can write to/dev/tty0, and sometimes the/dev/console is connected to/dev/tty0 or/dev/tty1.
5. Other types
The Linux system also has many other kinds of terminal equipment special files for many different character devices. For example, for ISDN device/dev/ttyin terminal equipment, and so on, here no longer repeat.
9 NM command and Strip command
The NM command displays the symbol information in the object file, the executable file, and the object file library.
The Strip command reduces the size of the Xcoff object file. The Strip command selectively removes line number information, relocation information, debug segments, Typchk segments, comment segments, file headers, and all or part of the symbol table from the XCOFF object file. Once you use this command, it is difficult to debug the symbols of the file, so you should usually use the strip command only on build modules that have been debugged and tested. Use the Strip command to reduce the amount of storage overhead required for an object file.
Common problems in embedded Linux