lgxqf  

Chapter 1

 

GCC

-E  Preprocess only; do not compile, assemble or link

     示当前CPP文件中包含的文件和宏 在当前CPP中被展开后的:g++ -E main.cpp

-I  By default, GCC looks in the current directory and in the directories where headers for the standard libraries are installed. If you need to include header files from somewhere else, you'll need the -I option

  指出文件所在的路:g++ -I headerfiledir main.cpp

-L    the linker looks for libraries in some standard places, including the /lib and /usr/lib directories that contain the standard system libraries. If you want the linker to search other directories as well, you should use the -L option, which is the parallel of the -I option

  文件所在的路径:g++ -o reciprocal main.o reciprocal.o -L/usr/local/lib/pam –lpam

       -D define the marco : gcc –D NDEBUG reciprocal.cpp

       -O2 optimize the code.

                                                                                                                                                     

Makefile:                                                                                                                                      

       Make CFLAG=-g

       Target: Dependence

              Rule

 

GDB:     run where

       break  next step

       up  print

 

Chapter 2

 

Stand I/O

       stdin 0  stdout 1 stderr 2  

       stdout stderr 的默认输出是屏幕  stdin的默认是键盘.可以通过 > 重定向输出, < 重定向输入

      ls 1>outfile (等价于 ls > outfile) ;     cat > catfile < somefile (somefile的内容复制到catfile)

      ls > outfile 2>1&    (把标准错误输出重定向到 标准输出 然后再把标准输出重定向到outfile 即相当于 把stdout stderr都输出到文件output)

 

            stdout 有缓冲区 缓冲区中的内容不会立即输出   除非缓冲区已满 程序正常退出 或stdout 关闭.也可通过显示调用 fflush输出缓冲区中的内容

      stderr没有缓冲区 它的内容会立即输出 见 APL/chapter/buffer.cpp

errno

      大部分系统调用在调用失败后都会设置errno  通过perrorsterror 可以打印出错误消息  头文件<errno.h>

      由于所有的程序在调用失败后都会重写errno 所以在用errno时要立刻将它的值记录在其它变量中 以防errno的值发生变化

Library:

      编译静态库 ar cr                               ar cr libtest.a test1.o test2.o -static  

      编译动态库 gcc -shared –fPIC                  gcc –c –fPIC test1.c   gcc -shared -fPIC -o libtest.so test1.o

       链接到动/静态库                    gcc –o app  -o app.o  -ltest

    强制链接到静态库(当静/动态库同时存在且重名时)        gcc –static –o app  app.o -ltest

   

LD_LIBRARY_PATH:

    设置程序运行时搜索动态库的路径.    LD_LIBRARY_PATH=/usr/lib:/opt/lib

 

Chapter 3

Process:

    Process ID 是由一个16位的二进值制组成,除了init进程 每个进程都有父进程. pid_t,  getpid(), getppid()

pstree 可以查看进程树.

僵尸进程:子进程在执行完毕后 若未进程未对该子进程执行wait()操作 那么该子进程就变成了僵尸进程.它会一直占用分配给它的pid的值

通过父进程在执行完毕后 其孤儿进程和僵尸子进程会被init收养 并由释init进程释放子进程的资源. 但若父进程是守护进程 一直在后台执行 那么它的僵尸子进程所占用的pid一直都不会释放.

 

fork & vfork & exec:

    fork 会复制当前进程 并产生一个子进程.vfork fork类似 但是它是写时复制.

    fork 在父进程中返回子进程ID, 在子进程中返回0

    exec函数族(execvp, execlp,execlp)用来一个新的程序来替换当前的进程 通常fork是为了让子进程去执行不同的任务,子进程可以通过调用exec函数来执行一个新的任务

    nice 可以用来设置进程的优先级 nice –n 8   设置越小进程的估先级越高. renice可以重新设置优先级

signal:

    如果一个进程安装了某信号处理程序, 那么当进程接收到该信号后会调用该信号的处理程序, 然后继续执行被中断处后面的代码. 

If a signal handler is used, the currently executing program is paused, the signal handler is executed, and, when the signal handler returns, the program resumes.

   

Chapter 线程

Create & exit

    进程结束:进程结束后 进程中所有的线程也立即结束.

       结束进程的几种方法:

       1.执行main函数中的return.  注意只有main中的return才会结束进程 线程中的return相当于pthread_exit()

       2.任意线程调用 exit()

       3.最后一个未束的线程调用 pthread_exit()  

    线程的结束:pthread_exit() 仅退出当前线程, 其它线程继续执行. 即使主线程调用该函数 其它线程也不会退出   

        若主线程在创建了其它线程之后 无其它事可做, 它就应该阴塞到所有线程都结束为止或调用pthread_exit()

让子线程总能完整执行(不会中途退出)的三种方法:一种方法是在主线程中调用pthread_join对其等待,即 pthread_create/pthread_join/pthread_exitreturn;一种方法是在主线程退出时使用 pthread_exit,这样子线程能继续执行,即pthread_create/pthread_detach/pthread_exit;还有一种是pthread_create/pthread_detach/return,这时就要保证主线程不能退出,至少是子线程完成前不能退出。现在的项目中用的就是第三种方法,主线程是一个死循环,子线程有的是死循环有的不是

常用函数: pthread_cancel pthread_self()  pthread_equal(pthread_self(), otherThread)    pthread_join(thread_id, thread_ret_val)

    释放线程所占的资源:pthread_detach pthread_join

    线程取消: pthread_cancel(tid) cancel a thread.

pthread_setcanceltype 设置取消类型 同步PTHREAD_CANCEL_DEFERRED 或异步 PTHREAD_CANCEL_ASYNCHRONOUS. 同步取消 与 异步取消的区别:同步取消会把"取消请求"放入一个队列 直到执行到某(取消点)点时才执行取消操作 异步取消:可能会随时执行取消操作.

            pthread_testcancel  设置函数的取消点.

            pthread_setcancelstate  设置该线程是否可以被取消

线程私有全局数据: pthread_key_create(pthread_key_t &, void (cleanupFunction )(void*))

The first argument is a pointer to a pthread_key_t variable. That key value can be used by each thread to access its own copy of the corresponding data item. The second argument to pthread_key_t is a cleanup function. If you pass a function pointer here, GNU/Linux automatically calls that function when each thread exits, passing the thread-specific value corresponding to that key.

 

Semaphores 信号量

    阴塞操作 sem_wait()  -1  非阴塞操作 sem_trywait() -1 sem_post +1

条件变量

    pthread_cond_wait() 将当前线程阻塞 挂起  pthread_cond_signal 唤醒进程

条件变量要与mutex 一起使用  因为在发singal时若没有wait的线程 则singal 被丢失

线程实现:

    GNU/Linux 线程相当于进程 有自已的pid. 所以pthread_kill可以给某个线程发信号  Typically, signals sent from outside the program are sent to the process corresponding to the main thread of the program.

 

Chapter 5 进程间通讯 IPC

共享内存

    效率高 最快的进程间通信方式

    没有共同祖先的进程 以共享内存的方式通信时 需要进程就键值达成一致.进程可以直接访协商或通过ftok和路径名来协商

信号量集:    

Semaphores continue to exist even after all processes using them have terminated

信号量集与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机制。相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时,进程也可以修改该标志。除了用于访问控制外,还可用于进程同步。

管道:

无名管道只适合有共同父进程的进程用

命名管道 适用于不相干 即无共同父进程的进程间通信

 

Chapter 6 Device

/dev

    设备文件都放在/dev目录, 设备文件是设备的入口 通过读写设备文件 可以读/写数据到设备;  cat 1.mp3 > /dev/audio

/dev/null

    linux会丢弃所有写入/dev/null的内容. 我们可以利用这一技巧 如果我们不想保存程序的输出可以直接把程序的输出重定向到/dev/null

/dev/random  /dev/urandom

    这两个文件会把随机数输出到屏幕 随机数的产生是根据敲击键盘和移动鼠标的时间间隔产生的,这两个文件不同之处在于:

    /dev/random 会阻塞 直到用户按键或移动鼠标

    /dev/urandom 会一直输出 如果用户无任何操作它会根据一特定的算法输出随机数

      od –t x1 /dev/random     

 

Chapter 7 Proc File System

/proc

      /proc 可以提供对进程 硬件 内核相关信息的访问.:cat /proc/cpuinfo  cat /proc/meminfo

      /proc 为每个正在运行的进程建立了一个目录 目录名为进程的ID号 目录里面的文件存储的是进程的相关信息. 但这些信息都是在读取这些文件时由内核动态产生的.

/proc/self

      /proc/self 是个链接 它指向当前进程在/proc中的目录

 

Chapter 8 Linux system call

系统调用

      应包含的头文件 /usr/include/asm/unistd.h

strace

      把进程在执行时的所有系统调用(:公系统调用  不会列出一般函数调用 )和它接到到的    信号列出来.  strace ./test  strace hostname

fcntl

      给文件加锁, 不同的进程可以在读/写文件时先调用fcntl获取/释放 锁操作来同步对文件   的读写.

fsync/fdatasync

      在写文件时系统把写到文件的内容先(暂存)缓冲到内存而不是立刻写入文件 等缓冲满时再   写入文件,  这样可以提高写文件的效率. 但如果不想缓冲 而是立刻写入文件则需显示调  fsync/fdatasync.这两个函数的区别:fsync在写文件时会去更新文件的修改时间而   fdatasync却不会 所以fdatasync会比fsync少写一次文件 效率更高.

sendfile

      高效的拷贝文件.(不用建立缓冲区)直接把一个文件的内容写到另一个文件中.

 

Chapter 10 Security

id

      查看当前用户的ID信息  一个用户可以有多个用户名但只能对应一个user ID

      每个进程有两个User ID:有效ID 和 实际ID. 一般情况下  内核只检查有效ID

实际ID与有效ID

进程的有效/实际UID 是从当前shell中继承过来. 在运行程序时 程序的有效/实际UIDshellID相同. 若可执行文件设置了"设置用户ID' 那么执行该可执行文件时生成的程序的有效ID变成该可执行文件的有效ID 而不是Shell中的有效ID. 但实际ID没变

      chmod +s 命令可以设置"设置用户"

setreuid()

      只有有效ID0的程序(即以root 身份创建的可执行的程序) 才能调用setreuid()来改变程序的有效和实际ID.

      Any other process(not root), however, can do only one of the following things:

      ·         Set its effective user ID to be the same as its real user ID

      ·         Set its real user ID to be the same as its effective user ID

      ·         Swap the two user IDs

 

 

posted on 2009-06-18 20:05  Justin_Ma  阅读(416)  评论(0)    收藏  举报