Ch4 Linux系统程序设计 进程控制和进程间通信.ppt
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《Ch4 Linux系统程序设计 进程控制和进程间通信.ppt》由会员分享,可在线阅读,更多相关《Ch4 Linux系统程序设计 进程控制和进程间通信.ppt(137页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Ch4 Linux System Programming Process & IPC,Jianjian SONGSoftware Institute, Nanjing UniversityOct, 2004,Content,Process & process environmentProcess ControlProcess identifier, fork, execProcess relationshipSignalInter-process communication (IPC)Pipe, FIFOsemaphore, shared memory, message queueDaemon
2、Thread,1. Process & Process Environment,What is a process?Program and processProcess: an address space with one or more threads executing within that address space, and the required system resources for those threads. (SUSv3),The startup of a process,System call “fork” Process resourcesstruct task_s
3、tructSystem space stackSystem call “exec”The entry of C programs,System stack,The entry of C programs,crt0.occ/ldmain functionfunction prototype:int main(int argc, char *argv);,The termination of a process,Five ways of terminating a processNormal terminationReturn from “main” functionCall “exit” fun
4、ctionCall “_exit” functionAbnormal terminationCall “abort” functionTerminated by a signal,exit & _exit functions,Function prototype:#include void exit(int status);#include void _exit(int status);Exit statusDifference_exit is corresponding to a system call, while “exit” is a library function._exit te
5、rminate a process immediately.,Exit handler,atexit functionRegister a function to be called at normal program termination.Prototype:#include int atexit(void (*function)(void);on_exit functionExample,The memory map of a C program,Text segment (code segment)Data segmentInitialized dataUninitialized da
6、taHeapStack,Command line arguments,main functionint main(int argc, char *argv);Example:The implementation of echo(1)Command line optionStandard usagegetopt function,getopt function,Prototype:int getopt(int argc, char *const argv, const char *optstring);extern char *optarg;extern int optind, opterr,
7、optopt;Question:getopt(argc, argv, “if:lr”);Program example (P104, in BLP),Environment variables,Environment tableEnvironment pointerextern char *environ;,putenv & getenv functions,Get, set or unset an environment variable#include char *getenv(const char *name);int putenv(char *string);int setenv(co
8、nst char *name, const char *value, int overwirte);void unsetenv(const char *name);,Shared object,Shared objectDynamic linkAdvantages and disadvantagesExampleHow to create a static and shared library?How to use (link) a static or shared library?,Memory allocation,Allocate and free dynamic memory.#inc
9、lude void *malloc(size_t size);void *calloc(size_t nmemb, size_t size);void free(void *ptr);void realloc(void *ptr, size_t size);,2. Process control,Process identifierSystem call “fork”The simple synchronization between parent and child processThe “exec” function familyExample: The implementation of
10、 a simple shell,Process identifier,fork,fork: create a child process#include #include pid_t fork(void);returned value: pid of child (in the current (parent) process), 0 (in child process), -1 (failure),A simple example,#include #include #include /* fork系统调用的第一个例子(不含错误检查)*/void main()printf(“Hello, w
11、orld!n”);fork();printf(“byen”);,The usage of “fork”,Code exampleif ( (pid=fork() zombie,wait & waitpid functions,Wait for process terminationPrototype#include #include pid_t wait(int *status);pid_t waitpid(pid_t pid, int *status, int options);Example,Race condition,int main(void) pid_tpid; if ( (pid
12、 = fork() 0) err_sys(fork error); else if (pid = 0) charatatime(output cccccccccccc from childn); else charatatime(output pppppppppp from parentn); exit(0);,An improvement,int main(void) pid_tpid; TELL_WAIT(); if ( (pid = fork() 0) err_sys(fork error); else if (pid = 0) WAIT_PARENT();/* parent goes
13、first */ charatatime(output cccccccccccc from childn); else charatatime(output pppppppppp from parentn); TELL_CHILD(pid); exit(0);,The “exec” family of functions,Replace the current process image with a new process image.执行新程序的进程保持原进程的一系列特征:pid, ppid, uid, gid, working directory, root directory euid
14、/egid?打开文件描述符?,Functions prototypes,#include 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
15、*file, char * const argv);#include int execve(const char *filename, char * const argv, char * const envp);,exec and opened file descriptor,close-on-exec bit of a file descriptorSet by “fcntl” functionfcntl(fd, F_SETFD, 0); /*系统默认, 打开文件描述符在 exec 时不关闭 */fcntl(fd, F_SETFD, 1); /*打开文件描述符在 exec 时关闭 */,Us
16、ing fork and exec together,Two ways of using forkThe parent process duplicates itself, and then two different pieces of codes are executed in parent process and child process.A process want to execute another program.Example:The implementation of “system” functionint system(const char*cmdstring);,Ex
17、ample: a simple shell,printf(% );/* print prompt */while (fgets(buf, MAXLINE, stdin) != NULL) bufstrlen(buf) - 1 = 0; /* replace newline with null */ if ( (pid = fork() 0 ) err_sys(“fork error”); else if ( pid = 0 ) /* child */ execlp(buf, buf, (char *) 0); fprintf(stderr, couldnt execute: %s, buf);
18、 exit(127); if ( (pid = waitpid(pid, ,3. Process relationship,父进程和子进程进程树ps, pstree命令,Startup & login (1),Login on serial terminal,Startup & login (2),Network login,Process group & session,Process groupThe set of one or more process(es).getpgrp/setpgid functionsSessionThe set of one or more process g
19、roup(s).setsid functionControlling terminalWhy are they introduced?Job control,Process group & session (contd),Process group & session (contd),4. Signal,The concept of signalsThe “signal” functionSend a signalkill, raiseThe “alarm” and “pause” functionsReliable signal mechanism,The concept of signal
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ch4 linux 系统 程序设计 进程 过程 控制 节制 以及 通信 通讯
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内