两个版本的system 第二个版本用sigaction设置了信号处理
来源:互联网 发布:淘宝卖东西赚钱吗 编辑:程序博客网 时间:2024/05/01 18:43
书中实现了两个版本的system函数,第二个版本中队信号进行了处理。
忽略了SIGINT和SIGQUIT,阻塞了SIGCHLD。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
int
system1(const char *cmdstring) /* version without signal handling */
{
pid_t pid;
int status;
if (cmdstring == NULL)
return(1); /* always a command processor with UNIX */
if ((pid = fork()) < 0) {
status = -1; /* probably out of processes */
} else if (pid == 0) { /* child */
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
_exit(127); /* execl error */
} else { /* parent */
while (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) {
status = -1; /* error other than EINTR from waitpid() */
break;
}
}
}
return(status);
}
int
system2(const char *cmdstring) /* with appropriate signal handling */
{
pid_t pid;
int status;
struct sigaction ignore, saveintr, savequit;
sigset_t chldmask, savemask;
if (cmdstring == NULL)
return(1); /* always a command processor with UNIX */
ignore.sa_handler = SIG_IGN; /* ignore SIGINT and SIGQUIT */
sigemptyset(&ignore.sa_mask);
ignore.sa_flags = 0;
if (sigaction(SIGINT, &ignore, &saveintr) < 0)
return(-1);
if (sigaction(SIGQUIT, &ignore, &savequit) < 0)
return(-1);
sigemptyset(&chldmask); /* now block SIGCHLD */
sigaddset(&chldmask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0)
return(-1);
if ((pid = fork()) < 0) {
status = -1; /* probably out of processes */
} else if (pid == 0) { /* child */
/* restore previous signal actions & reset signal mask */
sigaction(SIGINT, &saveintr, NULL);
sigaction(SIGQUIT, &savequit, NULL);
sigprocmask(SIG_SETMASK, &savemask, NULL);
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
_exit(127); /* exec error */
} else { /* parent */
while (waitpid(pid, &status, 0) < 0)
if (errno != EINTR) {
status = -1; /* error other than EINTR from waitpid() */
break;
}
}
/* restore previous signal actions & reset signal mask */
if (sigaction(SIGINT, &saveintr, NULL) < 0)
return(-1);
if (sigaction(SIGQUIT, &savequit, NULL) < 0)
return(-1);
if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0)
return(-1);
return(status);
}
static void
sig_int(int signo)
{
printf("caught SIGINT/n");
}
static void
sig_chld(int signo)
{
printf("caught SIGCHLD/n");
}
int
main(void)
{
if (signal(SIGINT, sig_int) == SIG_ERR)
perror("signal(SIGINT) error");
if (signal(SIGCHLD, sig_chld) == SIG_ERR)
perror("signal(SIGCHLD) error");
if (system2("/bin/ed") < 0)
perror("system() error");
exit(0);
}
- 两个版本的system 第二个版本用sigaction设置了信号处理
- 用sigaction( )处理信号
- c的信号处理sigaction
- 双卡版本不插卡的情况下状态栏中只显示了一个信号图标,而不是两个信号图标,如何修改为2个图标
- OIPF出了第二个版本
- linux 0.11版本内核中信号signal()和sigaction()的区别
- sigaction函数及信号的处理方式
- 信号处理:signal和sigaction的区别
- sigaction 信号处理
- 信号处理函数sigaction()
- sigaction信号处理
- sigaction(查询或设置信号处理方式)
- 个人库的第二个版本,将Zepto封装了进去
- 信号处理——sigaction
- struct sigaction 和信号处理函数的一些说明--1
- struct sigaction 和信号处理函数的一些说明
- linux c编程信号处理的一些实例signal sigaction
- linux c编程信号处理的一些实例signal sigaction
- 网站流量怎么做起来
- Venue Hire - What You Need to Know_817
- 从追MM谈Java的23种设计模式
- 黄山印象
- 错误的结果2......VC/bin/cl.exe返回的最终解决方案
- 两个版本的system 第二个版本用sigaction设置了信号处理
- Asp.net与SQL一起打包部署安装
- javamial 发送邮件
- BI publisher- 在超链接里引入变量
- Sql Server 2005 数据库维护计划
- SQL SERVER 2005字符串转换为数字
- 设置表格边框
- SQLite入门与分析(二)---设计与概念(续)
- IceFaces代码,回去研究