linux 进程间通信 信号量
2015-08-12 14:02
686 查看
实例中首先使用fork() 创建一个子进程, 在父进程调用kill() 之前, 在子进程中使用raise() 向自身发送SIGSTOP信号,是子进程暂停。接下来使用kill()向子进程发送信号
代码:
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat kill_raise.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int ret;
if ((pid = fork()) < 0)
{
printf("fork error! \n");
exit(1);
}
if (pid == 0)
{
printf("child pid: %d is waiting for any signal\n", getpid());
raise(SIGSTOP);
exit(0);
}
else
{
if ((waitpid(pid, NULL, WNOHANG)) == 0)
{
if ((ret = kill(pid, SIGKILL)) == 0)
{
printf("parent kill %d \n", pid);
}
}
waitpid(pid, NULL, 0);
exit(0);
}
}
编译运行:
ngnsvr9 [** NONE **]/home/xionghailong/example $ gcc kill_raise.c -o kill_raise
ngnsvr9 [** NONE **]/home/xionghailong/example $ ./kill_raise
child pid: 546 is waiting for any signal
parent kill 546
代码:
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat kill_raise.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int ret;
if ((pid = fork()) < 0)
{
printf("fork error! \n");
exit(1);
}
if (pid == 0)
{
printf("child pid: %d is waiting for any signal\n", getpid());
raise(SIGSTOP);
exit(0);
}
else
{
if ((waitpid(pid, NULL, WNOHANG)) == 0)
{
if ((ret = kill(pid, SIGKILL)) == 0)
{
printf("parent kill %d \n", pid);
}
}
waitpid(pid, NULL, 0);
exit(0);
}
}
编译运行:
ngnsvr9 [** NONE **]/home/xionghailong/example $ gcc kill_raise.c -o kill_raise
ngnsvr9 [** NONE **]/home/xionghailong/example $ ./kill_raise
child pid: 546 is waiting for any signal
parent kill 546
相关文章推荐
- Linux命令-测试数据库连接
- linux web php 安全相关设置
- CentOS---NFS安装、配置、使用
- CentOS---tftp(安装、配置、使用)
- 编译安装mod_jk on centOS
- VMWare虚拟机下 centos network is unreachable 问题的解决
- centos 中串口打印 ---工具 minicom
- Linux磁盘挂载
- Linux-JDK安装说明
- Kali Linux 2.0安装光盘分享
- linux查看CPU及内存信息
- CentOS上安装比较习惯的代码编辑器
- rsync+sersync2文件同步(基于centos6.5)
- openssl升级(基于centos6.5)
- mysql主从配置(基于centos6.5)
- ntp内网时间同步(基于centos6.5)
- Linux Command Line 详细解析
- ubuntu 14.04系统 安装搜狗输入法
- Centos 6.5 启动DHCP报错 Can't chown new lease file: Operation not permitted
- linux下提示一些基本的命令找不到