




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
写给linux编程的初学者,进程编程是linux编程首先要学习的东西,往后要学习进程之间通讯的一些编程。下面的是进程编程的一些基本函数。1 用户标识(UID)和有效用户标识(EUID)使用getuid函数和geteuid函数来获取当前进程的用户标识和有效用户标识#include #include #include int main(void) printf(Current process UID : %ldn,(long)getuid(); printf(Current porcess EUID : %ldn,(long)geteuid(); return 0; 运行结果:Current process UID:500Current process EUID:500 2. fork函数通过fork函数并判断函数返回值,根据返回值来判断是在父进程还是子进程。#include #include #include int main(void) pid_t pid;/fork函数如果返回值小于0,表示调用失败/如果返回值为0,表示处于进程中,如果大于0,表示在父进程中 if(pid=fork()0) perror(Cannot create the new process); return 1; else if(pid=0) printf(In the child process!n); return 0; else printf(In the parent process!n); return 0; 运行结果:In the child processs!In the parent process!调用fork函数后,其后代码会被父子进程分别执行。#include #include #include int main(void) fork(); printf(Will be executed twicen); return 0; 运行结果:Will be excuted twiceWill be excuted twice 3. vfork函数首先定义g_var为全局变量, var为局部变量,然后调用fork函数创建子进程,并在子进程对g_var变量和var变量进行修改。子进程在输出这两个变脸值后退出,输出父进程中的这两个变量的取值情况。#include #include #include int g_var=0;int main(void) pid_t pid; int var=1; printf(process id:%ldn,(long)getpid(); printf(before execute the fork system call, g_var=%d var=%dn,g_var,var); if(pid=fork()0) perror(Cannot create a new process); return 1; else if(pid=0) g_var+; var+; printf(process id : %ld, g_var=%d var=%dn,(long)getpid(),g_var,var); _exit(0); printf(process id : %ld, g_var=%d var=%dn,(long)getpid(),g_var,var); return 0; 运行结果:Process id :24437Before excute the fork system call,g_var=0 var=1Process id :24438, g_var=1 var=2Process id :24437, g_var=0 var=1修改上面的程序,将fork函数的语句进行替换,使用vfork函数,将代码保存并运行#include #include #include int g_var=0;int main(void) pid_t pid; int var=1; printf(process id:%ldn,(long)getpid(); printf(before execute the fork system call, g_var=%d var=%dn,g_var,var); if(pid=vfork()0) perror(Cannot create a new process); return 1; else if(pid=0) g_var+; var+; printf(process id : %ld, g_var=%d var=%dn,(long)getpid(),g_var,var); _exit(0); printf(process id : %ld, g_var=%d var=%dn,(long)getpid(),g_var,var); return 0; 运行结果:Process id :24574Before excute the fork system call,g_var=0 var=1Process id :24575, g_var=1 var=2Process id :24574, g_var=1 var=2 4 exec 函数族execvp函数支持参数列表,使用参数列表将使程序获得更大的灵活性,程序通过读取agrv中的参数,实现对输入的shell命令的执行。#include #include int main(int argc,char* argv) if(argc2) printf(Usage: %s pathn,argv0); return 1; execlp(/bin/ls,ls,argv1,(char*)NULL); return 0; 5 exit函数使用atexit注册了一个在进程退出时的处理函数,该处理函数只是显示一段文字信息。Main函数退出时将调用exit函数,这样进程就会在退出时自动调用atexit注册的处理函数。#include #include #include void do_at_exit(void) printf(You can see the output when the program terminatesn);int main() int flag; flag=atexit(do_at_exit); if (flag != 0) printf(Cannot set exit functionn); return EXIT_FAILURE; exit(EXIT_SUCCESS); 6 _exit函数下面的程序和上面的除了退出函数不同,其他都一样。不同的是使用_exit函数退出时,不会执行atexit中注册的处理函数 #include #include #include void do_at_exit(void) printf(You can see the output when the program terminatesn);int main() int flag; flag=atexit(do_at_exit); if (flag != 0) printf(Cannot set exit functionn); return EXIT_FAILURE; _exit(EXIT_SUCCESS); 7 kill 函数发送信号直接用kill函数给进程发送结束信号或是让程序自动退出。#include #include #include #include int main(int argc,char* argv) pid_t pid; int exit_code; pid=getpid(); srand(unsigned)pid); exit_code=(int)(rand() % 256); sleep(10); if(atoi(*(argv+1) % 2) printf(the child process id:%d receive signal SIGKILLn,pid); kill(pid,9); else printf(the child process id:%d normally exit,exit_code is %0xn,pid,exit_code); exit(exit_code); 下面程序通过fork函数创建子进程,并调用execl函数执行上面的程序。为了方便了解程序运行情况,在父进程中显示了创建的子进程的进程号。在while循环中调用wait函数,跳出条件是wait函数放回值小于0,或wait_pid为-1。这种情况下,所有的子进程都已经完全退出。#include #include #include #include int main(int argc,char* argv) pid_t pid,wait_pid; int status; int i; if(argc4) printf(Usage: %s para1 para2 para3n,argv0); return 1; for(i=1;i4;i+) if(pid=fork()=0) execl(./p7.14,p7.14,argvi,NULL); else printf(create the child process id: %dn,pid); while(wait_pid=wait(&status) & wait_pid!=-1) printf(process id:%d exit,exit_code is %0xn,wait_pid,status); return 0; 8 waitpid函数使用waitpid等待SIGSTOP,SIGCONT和SIGKILL这3种信号.#include #include #include #include int main(void) pid_t pid,wait_pid; int status; pid = fork(); if (pid=-1) perror(Cannot create new process); exit(1); else if (pid=0) printf(child process id: %ldn, (long) getpid(); pause(); _exit(0); else do wait_pid=waitpid(pid, &status, WUNTRACED | WCONTINUED); if (wait_pid = -1) perror(cannot using waitpid function); exit(1); if (WIFEXITED(status) printf(child process exites, status=%dn, WEXITSTATUS(status); if(WIFSIGNALED(status) printf(child process is killed by signal %dn, WTERMSIG(status); if (WIFSTOPPED(status) printf(child process is stopped by signal %dn, WSTOPSIG(status);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 琵琶行集体备课课件
- 琵琶行并序课件
- 服装扶贫工程方案范文(3篇)
- 扶贫希望工程方案(3篇)
- 洞库工程临时伪装方案(3篇)
- 电梯工程的安装方案(3篇)
- 农业电商新业态:2025年乡村特色农产品直播基地风险管理报告
- 广西灵山县大步江水闸除险加固工程环评报告
- 玲玲的画课件
- 风机更换工程方案(3篇)
- 输液反应应急预案课件
- 2025年德惠市公开招聘社区工作者(194人)备考练习题库及答案解析
- 三同时培训课件
- 2025国家网络安全宣传周
- 预算评审课件
- 银行双录专区课件
- 单位与个人劳务合同范本
- 毕节法院辅警面试题目及答案
- GB/T 31586.2-2015防护涂料体系对钢结构的防腐蚀保护涂层附着力/内聚力(破坏强度)的评定和验收准则第2部分:划格试验和划叉试验
- 特钢锻件项目商业计划书范文参考
- 《体育基本理论教程》教学大纲
评论
0/150
提交评论