linux中的信号量机制

  1 #include<stdio.h>
  2 #include<sys/wait.h>
  3 #include<sys/types.h>
  4 #include<unistd.h>
  5 #include<errno.h>
  6 #include<signal.h>
  7 int main()
  8 {
  9         pid_t pid=fork();
 10         int ret;
 11         if(pid<0)
 12                 perror("fork");
 13         if(pid==0)
 14         {
 15                 puts("in chile process");
 16                 printf("111111111111");
 17                 printf("222222222222");
 18                 printf("333333333333");
 19                raise(SIGSTOP);
 20                 return(0);
 21         }
 22         else
 23         {
24                 printf("create pid=%d",pid);
 25 
 26                 if(waitpid(pid,NULL,WNOHANG)==0)
 27                 {
 28                         if(ret=kill(pid,SIGKILL)==0)
 29                                 printf("kill's return is:%d,pid =%d\n",ret,p    id);
 30                         else
 31                                 perror("kill failed");
 32 
 33                 }
 34         }
 35         return 0;

 36 }

这个代码在开始时

 15                 puts("in chile process");
 16                 printf("111111111111");
 17                 printf("222222222222");
 18                 printf("333333333333");

完全无法输出

后发现是因为这些函数还没有被调用,就已经被父进程杀死了。

接着我在

26                 if(waitpid(pid,NULL,WNOHANG)==0)

前加入了sleep(3);


此时,可以实现 puts("in chile process");

但printf函数功能仍未实现。

后来通过网上查找资料http://www.360doc.com/content/09/0315/10/26398_2812414.shtml

发现内核创建的用户进程不能printf不能输出
          

                                                             
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值