正在加载图片...
Linux操作系统下c语言编程入门 int status printf( This will demostrate how to get child status\n) if(child=forko)==-1) printf( Fork Error: %s\n" strerror(errno); else if(child==O) int i; printf( I am the child: %ld\n", getpido): for(=0;i<1000000H++)sin(); printf('I exit with %d\n i while(((child=wait(&status))==-1)&(errno==EINTR)) if(child==-1) printf( Wait Error: %s\n strerror(erno)); else if(lstatus) printf("Child %ld terminated normally return status is zero\n else if(WIFEXITED(status) printf( Child %ld terminated normally return status is d\n child, WEXITSTATUS(status)); else if(WIFSIGNALED(status)) printf( Child %ld terminated due to signal %d znot caught \n child, WTERMSIG(status)) strerror函数会返回一个指定的错误号的错误信息的字符串 4。守护进程的创建 如果你在DOS时代编写过程序,那么你也许知道在DOS下为了编写一个常驻内存的程序 我们要编写多少代码了相反如果在Lnux下编写一个”常驻内存”的程序却是很容易的我 们只要几行代码就可以做到.实际上由于 Linux是多任务操作系统我们就是不编写代码 也可以把一个程序放到后台去执行的我们只要在命令后面加上&符号 SHELL就会把我们的 程序放到后台去运行的.这里我们”开发”一个后台检查邮件的程序.这个程序每个一个指 定的时间回去检查我们的邮箱,如果发现我们有邮件了,会不断的报警(通过机箱上的小喇 叭来发出声音)后面有这个函数的加强版本加强版本 后台进程的创建思想:首先父进程创建一个子进程然后子进程杀死父进程(是不是很无 情?).信号处理所有的工作由子进程来处理 #indlude <unistd.h> #include <sys/stat h #include <stdio. h> 第9页共84页Linux 操作系统下 c 语言编程入门 int status; printf(“This will demostrate how to get child status\n”); if((child=fork())==-1) { printf(“Fork Error :%s\n”,strerror(errno)); exit(1); } else if(child==0) { int i; printf(“I am the child:%ld\n”,getpid()); for(i=0;i<1000000;i++) sin(i); i=5; printf(“I exit with %d\n”,i); exit(i); } while(((child=wait(&status))==-1)&(errno==EINTR)); if(child==-1) printf(“Wait Error:%s\n”,strerror(errno)); else if(!status) printf(“Child %ld terminated normally return status is zero\n”, child); else if(WIFEXITED(status)) printf(“Child %ld terminated normally return status is %d\n”, child,WEXITSTATUS(status)); else if(WIFSIGNALED(status)) printf(“Child %ld terminated due to signal %d znot caught\n”, child,WTERMSIG(status)); } strerror 函数会返回一个指定的错误号的错误信息的字符串. 4。守护进程的创建 如果你在 DOS 时代编写过程序,那么你也许知道在 DOS 下为了编写一个常驻内存的程序 我们要编写多少代码了.相反如果在 Linux 下编写一个”常驻内存”的程序却是很容易的.我 们只要几行代码就可以做到. 实际上由于 Linux 是多任务操作系统,我们就是不编写代码 也可以把一个程序放到后台去执行的.我们只要在命令后面加上&符号 SHELL 就会把我们的 程序放到后台去运行的. 这里我们”开发”一个后台检查邮件的程序.这个程序每个一个指 定的时间回去检查我们的邮箱,如果发现我们有邮件了,会不断的报警(通过机箱上的小喇 叭来发出声音). 后面有这个函数的加强版本加强版本 后台进程的创建思想: 首先父进程创建一个子进程.然后子进程杀死父进程(是不是很无 情?). 信号处理所有的工作由子进程来处理. #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> 第 9 页 共 84 页
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有