《Linux编程技术详解》 杜华 -- 续
1. 带日志的Daemon
int main(int argc, char **argv) {
int i=0;
daemon(0,0);
openlog(argv[0], LOG_CONS | LOG_PID, LOG_USER);
for(i=0;i<100;i++) {
sleep(1);
syslog(LOG_INFO, "%d, log info test", i);
}
closelog();
return 0;
}
2. alarm
[root@localhost ch08]# cat p8.5.alarm.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int pid, wait_pid, status, i;
for(i=1;i<5;i++) {
pid=fork();
if(pid<0) {
perror("fork!");
return -1;
} else if(pid==0) {
printf("Child process(pid:%d), the process will be terminated in %d seconds./n", getpid(), i);
alarm(i);
pause();
}
}
while((wait_pid=wait(&status)) && wait_pid!=-1) {
if(WIFSIGNALED(status)) {
printf("process id: %d Received SIG : %d exit/n", pid, WTERMSIG(status));
}
if(WIFEXITED(status)) {
printf("process id: %d exit code : %d/n", pid, WEXITSTATUS(status));
}
}
return 0;
}
3. pipe
* 管道仅存在于父子或兄弟之间
* 命名管道可用于其它process之间
4. POSIX IPC
也就是System V IPC
包含:
消息队列
信号量Sephamore
共享内存