Linux程序设计入门 - fork, pthread, and signals 

  在UNIX程序设计中,学会fork及signal的运用,算是相当基本的功夫。  

  fork()及signal经常运用在daemon守护神这一类常驻程序,另外像 

  a4c.tty/yact/chdrv这些中文终端机程序也有用到,一般如 

  Mozilla/Apache/Squid等大程序几乎都一定会用到。  

  虽然在UNIX下的程序写作,对thread的功能需求并非很大,但thread在现代的 

  作业系统中,几乎都已经存在了。pthread是Linux上的thread函数库,如果您 

  要在Linux下撰写多线程序,例如MP3播放程序,熟悉pthread的用法是必要的。 

  pthread及signal都可以用一大章来讨论。在这里,我只谈及最简单及常用的技 

  巧,当您熟悉这些基本技巧的运用後,再找一些专门深入探讨pthread及signal 

  程序写作的书籍来研究。这些进阶的写法,用到的机会较少,将层次分明,学 

  习速度应该会比较快。  

  程序分歧fork() 

  fork()会产生一个与父程序相同的子程序,唯一不同之处在於其process 

  id(pid)。  

  如果我们要撰写守护神程序,或是例如网路伺服器,需要多个行程来同时提供 

  多个连线,可以利用fork()来产生多个相同的行程。  

  函数宣告 

  pid_t fork(void);  

  pid_t vfork(void);  

  返回值: 

  -1 : 失败。  

    0 : 子程序。  

  >0 : 将子程序的process id传回给父程序。  

  在Linux下fork()及vfork()是相同的东西。  

  范例一: fork.c 

  在这个范例中,我们示范fork()的标准用法。  

  #include <stdio.h>  

  #include <stdlib.h>  

  #include <unistd.h>  

  void main(void)  

  {  

    pid_t pid;  

    printf("hello\n");  

    pid = fork();  

    switch (pid) {  

      case -1: printf("failure!\n"); break;  

      case  0: printf("I am child!\n"); break;  

      default: printf("my child is %d\n",pid); break;  

    }  

    for (;;) { /* do something here */ }  

  }  

  编译: 

  gcc -o ex1 fork.c  

  执行结果: 

  ./ex1 &  

  hello  

  my child is 8650  

  I am child!  

  我们可以见到,使用fork(),可将一个程序分岐成两个。在分歧之前的程序码 

  只执行一次。  

  检验行程: 

  ps | grep ex1  

   8649  p0 R    0:40 ./ex1  

   8650  p0 R    0:40 ./ex1  

  8649是父程序的pid,8650则为子程序的pid。  

  您会需要用到"killall ex1"来杀掉两个行程。  

  范例二: daemon.c 

  在UNIX中,我们一般都利用fork(),来实作所谓的"守护神程序",也就是DOS中 

  所谓的"常驻程序"。一般的技巧是将父程序结束,而子程序便成为"守护神"。  

  这个范例中,示范一般标准的daemon写法。  

  #include <stdio.h>  

  #include <stdlib.h>  

  #include <unistd.h>  

  void main(void)  

  {  

    pid_t pid;  

    pid = fork();  

    if (pid>0) {  

      printf("daemon on duty!\n");  

      exit(0);  

    } else  

    if (pid<0) {  

      printf("Can't fork!\n");  

      exit(-1);  

    }  

    for (;;) {  

      printf("I am the daemon!\n");  

      sleep(3);  

      /* do something your own here */  

    }  

  }  

  编译: 

  gcc -o ex2 daemon.c  

  执行结果: 

  ./ex2  

  daemon on duty!  

  I am the daemon!  

  接下来每三秒钟,都会出现一个"I am the daemon!"的讯息,这表示您的程序 

  已经"长驻"在系统中了。  

  检验行程: 

  ps | grep ex2  

  8753  p0 S    0:00 ./ex2  

  注意到在范例一中,我们下的指令为"./ex1 &",而在范例二中为"./ex2",没 

  有"&"符号。  

  范例三: lock.c 

  许多的时候,我们希望"守护神"在系统中只有一个,这时候会需要用到pid 

  lock的技巧。如果您注意到/var/run目录中的内容,您会发现到有许多的*.pid 

  档,观看其内容都是一些数字,这些数字其实就是该行程的pid。  

  #include <stdio.h>  

  #include <stdlib.h>  

  #include <unistd.h>  

  void main(void)  

  {  

    FILE *fp;  

    pid_t pid;  

      exit(-1);  

    }  

    act.sa_handler = quit;  

    act.sa_flags   = 0;  

    sigemptyset(&act.sa_mask);  

    sigaction(SIGTERM,&act,NULL);  

    sigaction(SIGHUP,&act,NULL);  

    sigaction(SIGINT,&act,NULL);  

    sigaction(SIGQUIT,&act,NULL);  

    sigaction(SIGUSR1,&act,NULL);  

    sigaction(SIGUSR2,&act,NULL);  

    for (;;) {  

      sleep(3);  

    }  

  }  

  编译: 

  gcc -o ex1 lock.c  

  执行 

  ./ex1  

  daemon on duty!  

  送信号 

  我们先找出该守护神程序的pid  

  PID=`cat /var/run/lock.pid`  

  接下来利用kill来送信号  

  kill $PID  

  Receive signal 15  

  程序将会结束,并且/var/run/lock.pid将会被删除掉,以便下一次daemon再启 

  动。注意到如果quit函数内,没有放exit(),程序 将永远杀不掉。  

  接下来送一些其它的信号试试看。  

  ./ex1  

  PID=`cat /var/run/lock.pid`  

  kill -HUP $PID  

  Receive signal 1  

  您可以自行试试  

  kill -INT $PID  

  kill -QUIT $PID  

  kill -ILL $PID  

  .  

  .  

  .  

  等等这些信号,看看他们的结果如何。  

  信号的定义 

  在/usr/include/signum.h中有各种信号的定义  

  #define SIGHUP          1       /* Hangup (POSIX).  */  

  #define SIGINT          2       /* Interrupt (ANSI).  */  

  #define SIGQUIT         3       /* Quit (POSIX).  */  

  #define SIGILL          4       /* Illegal instruction (ANSI).  */  

  #define SIGTRAP         5       /* Trace trap (POSIX).  */  

  #define SIGABRT         6       /* Abort (ANSI).  */  

  #define SIGIOT          6       /* IOT trap (4.2 BSD).  */  

  #define SIGBUS          7       /* BUS error (4.2 BSD).  */  

  #define SIGFPE          8       /* Floating-point exception (ANSI).  

  */  

  #define SIGKILL         9       /* Kill, unblockable (POSIX).  */  

  #define SIGUSR1         10      /* User-defined signal 1 (POSIX).  */ 

  #define SIGSEGV         11      /* Segmentation violation (ANSI).  */ 

  #define SIGUSR2         12      /* User-defined signal 2 (POSIX).  */ 

  #define SIGPIPE         13      /* Broken pipe (POSIX).  */  

  #define SIGALRM         14      /* Alarm clock (POSIX).  */  

  #define SIGTERM         15      /* Termination (ANSI).  */  

  #define SIGSTKFLT       16      /* ??? */  

  #define SIGCLD          SIGCHLD /* Same as SIGCHLD (System V).  */  

  #define SIGCHLD         17      /* Child status has changed (POSIX).  

  */  

  #define SIGCONT         18      /* Continue (POSIX).  */  

  #define SIGSTOP         19      /* Stop, unblockable (POSIX).  */  

  #define SIGTSTP         20      /* Keyboard stop (POSIX).  */  

  #define SIGTTIN         21      /* Background read from tty (POSIX).  

  */  

  #define SIGTTOU         22      /* Background write to tty (POSIX).  

  */  

  #define SIGURG          23      /* Urgent condition on socket (4.2 

  BSD).  */  

  #define SIGXCPU         24      /* CPU limit exceeded (4.2 BSD).  */  

  #define SIGXFSZ         25      /* File size limit exceeded (4.2 

  BSD).  */  

  #define SIGVTALRM       26      /* Virtual alarm clock (4.2 BSD).  */ 

  #define SIGPROF         27      /* Profiling alarm clock (4.2 BSD).  

  */  

  #define SIGWINCH        28      /* Window size change (4.3 BSD, Sun). 

    */  

  #define SIGPOLL         SIGIO   /* Pollable event occurred (System 

  V).  */  

  #define SIGIO           29      /* I/O now possible (4.2 BSD).  */  

  #define SIGPWR          30      /* Power failure restart (System V).  

  */  

  #define SIGUNUSED       31  

  函数宣告: 

  Signal Operators  

       int sigemptyset(sigset_t *set);  

       int sigfillset(sigset_t *set);  

       int sigaddset(sigset_t *set, int signum);  

       int sigdelset(sigset_t *set, int signum);  

       int sigismember(const sigset_t *set, int signum);  

  Signal Handling Functions  

       int sigaction(int signum,  const  struct  sigaction  *act,struct 

       sigaction *oldact);  

       int  sigprocmask(int  how,  const  sigset_t *set, sigset_t 

       *oldset);  

       int sigpending(sigset_t *set);  

       int sigsuspend(const sigset_t *mask);  

  Structure Signal Action  

  struct sigaction {  

                   void (*sa_handler)(int);  

                   sigset_t sa_mask;  

                   int sa_flags;  

                   void (*sa_restorer)(void);  

               }  

  OK STATION, Webmaster, Brian Lin  

GNU FORK PTHREAD SIGNALS的更多相关文章

  1. Cross-platform Tools

    MinGW和Cygwin都解决了C++跨平台交叉编译的问题,使Linux下的程序在Windows上编译运行成为可能. MinGW给习惯在Linux上开发的人员在windows上提供了一套类似的工具集: ...

  2. cygwin-介绍-安装

    初学linux时,最头疼的是,因为windows和linux各有优点,各有用途,所以只能麻烦的在两者之间切换,不断的重启.开机时也麻烦,因为初学者大多数使用windows,装了linux后,开机会自动 ...

  3. [cross compile]cygwin和mingw

    转自:http://blog.csdn.net/embededvc/article/details/6829010 1. MinGW和CygWin/gcc概念 Unix下编译通过的C代码,在win32 ...

  4. mingw64环境搭建

    转自:http://www.cr173.com/soft/132367.html MinGW64位版,默认编译出来是64位的,需要编译32位请使用-m32 参数!mingw是一款gnu工具集合是Min ...

  5. Android学习之一:Cygwin简介

    为了能够一窥Android底层的东东,还是要搭建编译Android的环境.虽有Ubuntu和Suse系统,无奈总感觉在不同的系统下切来切去很是不便.在Windows工作学习,要编译Android,就不 ...

  6. win7 下使用cygwin

    http://cygwin.com/index.html      还是看官网! 很多用windows的朋友不习惯于用linux的开发环境.虽然很乐意尝试一下,但是往往怕 linux系统打乱了自己的正 ...

  7. cygwin,在win中开发linux程序

    cygwin,在win中开发linux程序 http://www.cygwin.cn/site/info/show.php?IID=1001  很多用windows的朋友不习惯于用linux的开发环境 ...

  8. Cygwin、MinG、MSys区别与联系(转)

    转自:https://www.biaodianfu.com/cygwin-ming-msys.html 什么是Cygwin? Cygwin,原Cygnus出品(已被红帽收购),目前是RedHat名下的 ...

  9. Cygwin使用2-心得

    引用:http://www.jb51.net/article/6236.htm 1.在cygwin里访问Windows盘 cd /cygdrive/c cd c: 2.整合cygwin命令到Windo ...

随机推荐

  1. 基于JAX-WS的Web Service服务端/客户端 ;JAX-WS + Spring 开发webservice

    一.基于JAX-WS的Web Service服务端/客户端 下面描述的是在main函数中使用JAX-WS的Web Service的方法,不是在web工程里访问,在web工程里访问,参加第二节. JAX ...

  2. WCF服务部署到IIS上,然后通过web服务引用方式出现错误的解决办法

    本文转载:http://www.cnblogs.com/shenba/archive/2012/01/06/2313932.html 昨天在用IIS部署一个WCF服务时,碰到了如下错误: 理解了文档内 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码]

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码] Unity 2.x依赖注入(控制反转)IOC,对 ...

  4. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

  5. TOJ3744(Transportation Costs)

    Transportation Costs   Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte Total Submi ...

  6. MVCC

    http://blog.chinaunix.net/xmlrpc.php?id=3886838&r=blog/article&uid=26664667

  7. cflow察看工程函数调用关系+Linux 0.11 内核实验环境

    http://savannah.gnu.org/projects/cflow http://tinylab.org/linux-0.11-lab/ http://ftp.gnu.org/gnu/cfl ...

  8. ZYKeyboardUtil 全自动处理键盘遮挡事件

    键盘遮盖输入控件或按钮在日常app开发中避之不及,考虑各种情况下UI各种嵌套,最后还要注册监听再获取指定键盘信息.我们可以通过键盘处理工具类ZYKeyboardUtil避繁就简,利用Block的方式处 ...

  9. 关于a标签的链接的表现形式

    target属性:self在自身标签页里打开 blank在新标签页中打开 1.  链接到其它地址位置.html文档等 <a href="1.html"></a&g ...

  10. oracle过滤分割字符串自定义函数

    该函数实现过滤前后的指定的字符串,诸如过滤分隔符等.可用于过滤字符串中的逗号分割符.特别说明:substr()函数支持从字符串倒数开始读取,例如: dbms_output.put_line( subs ...