Friday, July 4, 2014

Unix Shell: Temporary Files(2)

1. Temporary file storage
Most of temp files are stored in /tmp

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ df /tmp  
 Filesystem   1K-blocks  Used Available Use% Mounted on  
 /dev/sda1    29283420 4113216 23659652 15% /  

If the file system is swap, then it is memory resident file system, which means that files are stored in memory unless the memory is running low.

2. Use process id as part of temporary file name
Because temporary file is heavily used, it is very possible to have name in conflict. In order to remove the conflict, we recommend to use process id as part of the temporary file name.

script_1:
$$ represents the current process id
 #! /bin/bash  
   
 TMPDIR=/tmp  
 echo $TMPDIR/temp.$$  
 touch $TMPDIR/temp.$$  

terminal:
3650 and 3652 are process ids for each script_1's running.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 /tmp/temp.3650  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 /tmp/temp.3652  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls /tmp/temp.*  
 /tmp/temp.3650 /tmp/temp.3652  

3. "mktemp" to create temporary file with random names

The temporary files created by "touch" is not safe, since their names are easy to guess, we need one to create the temporary file with random name.

Last 5 "X" means, the name in this part should be random.

mktemp by default creates the temporary file at the local folder.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ tmpfile=`mktemp temp.XXXXX` || exit 1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $?  
 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls $tmpfile  
 temp.j4EGb  

-u option means, mktemp just generate one random file name instead of really creating the file.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ mktemp -u temp.XXXXX  
 temp.XA5bc  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls temp.XA5bc  
 ls: cannot access temp.XA5bc: No such file or directory  

-d option make "mktemp" create the temporary folder:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ mktemp -d temp.XXX  
 temp.FIM  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cd temp.FIM  

--tmpdir specify where to create the temporary file:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ tmpfile=`mktemp --tmpdir=/tmp temp.XXXXXX` || exit 1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls $tmpfile  
 /tmp/temp.2WUCYS  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls ./$tmpfile  
 ls: cannot access .//tmp/temp.2WUCYS: No such file or directory  

--suffix can add the suffix to the end file name:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ mktemp --suffix=ending temp.XXXX  
 temp.dyShending  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls temp.dyShending  
 temp.dyShending  

4. Binary Random Stream
time command: used to monitor the resources of a given command or script running.
terminal:
For ls command, system spend real time(wall clock time), user time(amount of CPU time spent in user mode within the process) and system time(amount of CPU time spent in system mode within the process).
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ time ls  
 script_1 script_1~ text  
   
 real    0m0.036s  
 user    0m0.004s  
 sys    0m0.008s  

dd command: copy bytes data from input to output.
terminal:
1) We have one text file, containing 3 lines of strings
2) count=3 means: read 3 blocks
    ibs=10 means: for each block, it will read 10 bytes
    if=./text means: input source is text file in local place
    This command means, read 3 blocks of data, each block is 10 bytes, from ./text file, by default, write to standard output.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat ./text  
 Hello world!  
 Amazing world!  
 Awesome world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ dd count=3 ibs=10 if=./text  
 Hello world!  
 Amazing world!  
 Aw3+0 records in  
 0+1 records out  
 30 bytes (30 B) copied, 0.00365285 s, 8.2 kB/s  

Copy data from random stream, and monitor resources system uses:
terminal:
1) dd command copy data from /dev/random stream, and it blocks for 14 seconds! Because /dev/random need to ensure that stream it generates until now are really "random". If randomness can't be satisfied, it will keep being blocked!
2) dd command copy data from /dev/urandom stream, the only difference is:  it never get blocked to check randomness. So random data generated by /dev/urandom is not as good as /dev/random, but it does spend much less time.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ time dd count=1 ibs=1024 if=/dev/random >/dev/null  
 0+1 records in  
 0+1 records out  
 6 bytes (6 B) copied, 14.7669 s, 0.0 kB/s  
   
 real    0m14.850s  
 user    0m0.004s  
 sys    0m0.028s  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ time dd count=1 ibs=1024 if=/dev/urandom >/dev/null  
 1+0 records in  
 2+0 records out  
 1024 bytes (1.0 kB) copied, 0.0351317 s, 29.1 kB/s  
   
 real    0m0.053s  
 user    0m0.004s  
 sys    0m0.016s  

No comments:

Post a Comment