下載/瀏覽Download

Download Report

Transcript 下載/瀏覽Download

Killing other processes
EOS STUT
1
EOS STUT
2
signal(訊號)
Signals are a techniques used to
notify a process that some condition
has occurred
Three ways to handle signals
 Ignore
the signal
 Let the default action occur
 Provide a function that is called when
the signal occurs.
EOS STUT
3
signal
Asynchronous message mechanism
void sighup_handler()
{
printf("Receive SIGHUP signal.\n");
}
int main()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &sighup_handler;
sigaction(SIGHUP, &sa, NULL);
while (1);
return 0;
}
EOS STUT
4
Signal number
 Signal defined:
EOS STUT
5
Daemon program - 1
 Parent terminates after child run

The parent of the child becomes the init process
Main
Process
fork()
Parent
Process
Child
Process
Process
Terminate
Daemon
Services
EOS STUT
6
Daemon program - 2
int main()
{ int pid;
if( pid=fork() < 0){
perror("fork");
exit(1);
}
/*
/*fork 後,子程序就不是 process group leader,
*可以確保成功的執行 setsid(),脫離 controling terminal
*/
if( pid==0 ){
/* * 執行setsid() 建立新的 session
*/
setsid();
/* daemon 不需要 stdin stdout stderr */
fclose(stdin);
fclose(stdout);
fclose(stderr);
/* * 改變目錄到根目錄,以免要 unmount 目錄時,
* 因為有 daemon 在存取目錄無法 unmount
*/
chdir("\");
while(1){ /* 要做的函式放這裡 */
do();
}
}
EOS STUT
return 0;}
7
Race condition (競爭情況)
When multiple processes share a
resource or resources, it happens
there are errors due to running
sequences, (race condition)
Hard to predict the running sequence
of processes when forking processes.
Mechanisms used to synchronize
processes (IPC).
EOS STUT
8
Executing a program(1/2)
EOS STUT
9
Executing a program(2/2)
EOS STUT
10