17
KUAS – High Performance KUAS – High Performance Distributed System Distributed System Hig h Performanc e Distribu ted System Linux Programming – Process & Signal Reporter: Po-Sen Wang

Linux Programming – Process & Signal

  • Upload
    afya

  • View
    54

  • Download
    0

Embed Size (px)

DESCRIPTION

Linux Programming – Process & Signal. Reporter: Po-Sen Wang. 起始新程序 (1/2). #include int system(const char * string ); Example. 起始新程序 (2/2). 使用 system 函數在背景執行 Example. 程序的替換 (1/3). #include extern char **environ; - PowerPoint PPT Presentation

Citation preview

Page 1: Linux Programming – Process & Signal

KUAS – High Performance KUAS – High Performance Distributed SystemDistributed System

High

Performance

Distributed

System

Linux Programming – Process & Signal

Reporter: Po-Sen Wang

Page 2: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 2

HPDSHPDS 起始新程序 (1/2)

#include <stdlib.h> int system(const char *string); Example

Page 3: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 3

HPDSHPDS 起始新程序 (2/2)

使用 system 函數在背景執行 Example

Page 4: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 4

HPDSHPDS 程序的替換 (1/3)

#include <unistd.h> extern char **environ; int execl(const char *path, const char *arg0, ..., (char *) 0); int execlp(const char *file, const char *arg0, ..., (char *) 0); int execle(const char *path, const char *arg0, ..., (char *) 0, const char *envp[]); int execv(const char *path, const char *argv[]); int execvp(const char *file, const char *argv[]); int execve(const char *path, const char *argv[], const char *envp[]);

Page 5: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 5

HPDSHPDS 程序的替換 (2/3)

使用 exec 函數來起始 ps 程式: const char *ps_argv[]={“ps”, “-ax”, 0}; const char *ps_envp[]=

{“PATH=/bin:/usr/bin”, “TERM=console”, 0};

execl(“/bin/ps”, “ps”, “-ax”, 0); execlp(“ps”, “ps”, “-ax”, 0); execle(“/bin/ps”, “ps”, “-ax”, 0, ps_envp); execv(“/bin/ps”, ps_argv); execvp(“ps”, ps_argv); execve(“/bin/ps”, ps_argv, ps_evnp);

Page 6: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 6

HPDSHPDS 程序的替換 (3/3)

Example

Page 7: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 7

HPDSHPDS 複製程序 (1/2)

#include <sys/types.h> #include <unistd.h> pid_t fork(void);

起始程序

父始程序繼續執行 子程序

fork()

傳回 0傳回子程序 PID

Page 8: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 8

HPDSHPDS 複製程序 (2/2)

Example

Page 9: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 9

HPDSHPDS 等待程序 (1/2)

#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_val); 巨集

WIFEXITED(stat_val) 子程序正常終止,傳回非零值

WEXITSTATUS(stat_val) WIFEXITED 非零,傳回子程序離開碼

WIFSIGNALED(stat_val) 子程序因漏失訊號而終止,傳回非零值

WTERMSIG(stat_val) WIFSIGNALED 非零,傳回訊號碼

WIFSTOPPED(stat_val) 子程序因為收到訊號而停止,傳回非零值

WSTOPSIG(stat_val) WIFSTOPPED 非零,傳回訊號碼

Page 10: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 10

HPDSHPDS 等待程序 (2/2)

Page 11: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 11

HPDSHPDS Signal (1/2)

一些會造成程序立即終止的訊號:SIGABORT 程序停止SIGALRM 警示SIGFPE 浮點數例外SIGHUP 掛斷SIGILL 非法指令SIGINT 終端機插斷SIGKILL Kill( 無法被捕捉或忽略 )

SIGPIPE 寫入一無收取端的 pipe

SIGQUIT 終端機終止SIGSEGV 存取無效的記憶體區段

Page 12: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 12

HPDSHPDS Signal (2/2)

#include <signal.h> void (*signal(int sig, void (*func)(int)))(int);

func可以自定,或是 SIG_IGN( 忽略訊息 ) 、 SIG_DFL(恢復預設的行為 )

Example

Page 13: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 13

HPDSHPDS 傳送訊息 (1/2)

#include <sys/types.h> #include <signal.h> int kill(pid_t pid, int sig);

#include <unistd.h> unsigned int alarm(unsigned int seconds);

Page 14: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 14

HPDSHPDS 傳送訊息 (2/2)

Page 15: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 15

HPDSHPDS Sigaction (1/2)

#include <signal.h> int sigaction(int sig, const struct sigaction *act, struct si

gaction *oact) sigaction struct 的部分成員:

void (*sa_handler)(int); //function, SIG_DFL, SIG_IGN

sigset_t sa_mask; //signals to block in sa_handler

int sa_flags; //signal action modifiers sa_flags :

SA_NOCLDSTOP 當子程序停止時則不要產生 SIGCHLD

SA_RESETHAND 將訊號接收動作重設為 SIG_DEL

SA_RESTART 重新啟動可插斷函數,而非 EINTR 錯誤SA_NODEFER 捕捉時不要將訊號加到 mask 中

Page 16: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 16

HPDSHPDS Sigaction (2/2)

Page 17: Linux Programming – Process & Signal

2007/7/27 Po-Sen Wang 17

HPDSHPDS 作業

請撰寫一程式產生一子程序。 父程序分別印出父程序及子程序的 id ,並等待子程序傳送 SIGALRM 訊號。接收到 SIGALRM 訊號後,在營幕上印出” Child process finish.” 訊息。

子程序去執行另一個 1 加到 100 的程式,並傳送 SIGALRM 訊號給父程序。