linux中的信号量机制

来源:互联网 发布:中国农业大学网络管理 编辑:程序博客网 时间:2024/05/17 23:49
  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
原创粉丝点击