文档详情

CH07 Linux进程和信号.ppt

发布:2017-06-17约1.72万字共89页下载文档
文本预览下载声明
第7章 进程和信号 主要内容 进程介绍 进程结构 进程调度 进程控制相关的系统调用 fork, exec,wait… 信号 PID(进程标志符) 4. 进程的控制 进程创建 进程撤销 进程睡眠 进程唤醒 “exec” 函数族 把当前进程替换为一个新进程. 执行新程序的进程保持原进程的一系列特征: pid, ppid, uid, gid, working directory, … 打开文件描述符 “exec” 函数族 #include unistd.h extern char **environ; int execl(const char *path, const char *arg, ...); int execlp(const char *file, const char *arg, ...); int execle(const char *path, const char *arg, ..., char * const envp[]); int execv(const char *path, char * const argv[]); int execvp(const char *file, char * const argv[]); int execve(const char *filename, char * const argv[], char * const envp[]); A simple example “fork”的典型用法 pid=fork(); if ( pid 0) { /* error handling */ } else if (pid == 0) { /* child */ } else { /* parent */ }; #include stdlib.h # include unistd.h # include stdio.h int main() { pid_t pid; char *message; int n; printf(“fork program starting \n”); pid=fork(); switch(pid){ case -1: exit(EXIT_FAILURE); # include sys/types.h # include sys/wait.h # include err_exit.h int main() { pid_t pid; if ((pid=fork()0) err_exit(“fork error”); else if(pid==0) { if((pid=fork())0) err_exit(“fork error”); else if(pid0) exit(EXIT_SUCCESS); sleep(2); printf(“second child ,parent pid=%d\n”,getppid()); exit(EXIT_SUCCESS); } if (waitpid(pid,NULL,0)!=pid) err_exit(“watpid error”); printf(“parent exit \n”); exit(EXIT_SUCCESS); } upper.c #include stdio.h #include ctype.h int main() { int ch; while((ch = getchar()) != EOF) { putchar(toupper(ch)); } exit(0); } useupper.c #include unistd.h #include stdio.h int main(int argc, char *argv[]) { char *filename; if(argc != 2) { fprintf(stderr, usage: useupper file\n); exit(1); } filename = argv
显示全部
相似文档