读书时间 05/28/2011 -2

14 篇文章 0 订阅

《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

共享内存

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值