Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1064120
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: C/C++

2009-07-07 11:43:31

#include
#include
#include
#include
#include
#include
#include
void main(int argc,char **argv[])
{
   int i;
   sem_t *sem;
   int pid;
   pid = fork();
   if(pid == 0) {
      sleep(1);
      sem = sem_open("/tmp", 0);
      for(i=0;i<3;i++){
         sem_wait(sem);
         printf("child do\n");
      }
      sem_close(sem);
   }else {
      sem = sem_open("/tmp", O_RDWR|O_CREAT, S_IWUSR|S_IRUSR, 0);
      for(i=0;i<3;i++){
         sem_post(sem);
         printf("par do\n");
         sleep(1);
      }
      sem_close(sem);
      sem_unlink("/tmp");
   }
}
注意编译需要-lrt参数
阅读(1260) | 评论(0) | 转发(0) |
0

上一篇:多线程练习

下一篇:posix共享内存练习

给主人留下些什么吧!~~