Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7705646
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2010-11-01 20:53:33

/****************

创建一个信号量集

*****************/

#include

#include

#include

#include

#include

#include

int main()

{

      int semid;

      key_t key;

      key = ftok("/home",'a');

      if(key<0)

      {

           perror("ftok error");

           exit(1);

      }

      semid = semget(key,1,IPC_CREAT|0666);

      if(semid<0)

      {

           perror("semget error");

           exit(1);

      }

      printf("Done\n");

      return 0;

}

 

 

/***********

进程间同步

************/

#include

#include

#include

#include

#include

#include

#include

#include

int main()

{

      int semid,i,ret,status;  

      struct sembuf lock ={0,-1,SEM_UNDO};

      struct sembuf unlock ={0,1,SEM_UNDO|IPC_NOWAIT};

      pid_t pid;

      key_t key;

      key = ftok("/home",'a');

      if(key<0)

      {

           perror("ftok error");

           exit(1);

      }

      semid = semget(key,1,IPC_CREAT|0666);

      if(semid<0)

      {

           perror("semget error");

           exit(1);

      }

      ret=semctl(semid,0,SETVAL,1);

      if(ret==-1)

      {

           perror("semctl error");

           exit(1);

      }

      pid = fork();

      if(pid<0)

      {

           perror("fork error");

           exit(1);

      }

      if(pid==0)

      {

           for(i=0;i<3;i++)

           {

                 sleep(2);

                 ret=semop(semid,&lock,1);

                 if(ret==-1)

                 {

                      perror("semop lock error");

                      exit(1);

                 }

                 printf("Child proxdss access the resource\n");

                 sleep(2);              

                 printf("Complete\n");

                 ret=semop(semid,&unlock,1);

                 if(ret==-1)

                 {

                      perror("semop unlock error");

                      exit(1);

                 }              

           }

      }

      else

      {

           for(i=0;i<3;i++)

           {

                 sleep(2);

                 ret=semop(semid,&lock,1);

                 if(ret==-1)

                 {

                      perror("semop lock error");

                      exit(1);

                 }

                 printf("Parent proxdss access the resource\n");

                 sleep(2);              

                 printf("Complete\n");

                 ret=semop(semid,&unlock,1);

                 if(ret==-1)

                 {

                      perror("semop unlock error");

                      exit(1);

                 }              

           }

            if(pid!=wait(&status))

           {

                 printf("wait error");

                 exit(1);

           }

           ret=semctl(semid,0,IPC_RMID,0);

           if(ret==-1)

           {

                 perror("semctl error");

                 exit(1);

           }        

      }   

      printf("Done\n");

      return 0;

}

 

 

/***********

创建共享内存

************/

#include

#include

#include

#include

#include

#define SIZE 1024

 

int main()

{

      int shmid;       

      key_t key;

      key = ftok("/home",'a');

      if(key<0)

      {

           perror("ftok error");

           exit(1);

      }

      shmid = shmget(key,SIZE,IPC_CREAT|0666);

      if(shmid<0)

      {

           perror("semget error");

           exit(1);

      }

      else

           printf("Done\n");

      return 0;

}

 

 

/***********

共享内存

************/

#include

#include

#include

#include

#include

#define SIZE 1024

 

int main(int argc, char **argv)

{

      int shmid,psm;

      struct shmid_ds dsbuf;    

      pid_t pid;

      key_t key;

      if(argc!=2)

      {

           printf("aruments error");

           exit(1);

      }

      key = ftok("/home",'a');

      if(key<0)

      {

           perror("ftok error");

           exit(1);

      }

      shmid = shmget(key,SIZE,IPC_CREAT|0666);

      if(shmid<0)

      {

           perror("semget error");

           exit(1);

      }   

      pid = fork();

      if(pid<0)

      {

           perror("fork error");

           exit(1);

      }

      if(pid==0)

      {

           printf("Child process\n");

           printf("PID:%d\n",getpid());

           psm=shmat(shmid,NULL,0);

           if(psm==-1)

           {

                 perror("shmat error");

                 exit(1);

           }

           else

           {

                 strcpy((char *)psm,argv[1]);

                 printf("Send message:%s\n",(char *)psm);

                 if(shmdt((char *)psm)<0)

                      perror("shmdt error");

                 sleep(2);

           }

      }              

      else

      {

           sleep(2);

           printf("Parent process\n");

           printf("PID:%d\n",getpid());

           if(shmctl(shmid,IPC_STAT,&dsbuf)<0)            

           {

                 perror("shmctl error");

                 exit(1);

           }

           else

           {

                 printf("Share Memory Information\n");

                 printf("\tCreator PID:%d\n",dsbuf.shm_cpid);

                 printf("\tSize :%d\n",dsbuf.shm_segsz);

                 printf("\tLast Opeetor PID:%d\n",dsbuf.shm_lpid);

                 psm=shmat(shmid,NULL,0);

                 if(psm==-1)

                 {

                      perror("shmat error");

                      exit(1);

                 }

                 else

                 {

                      printf("Received message:%s\n",(char *)psm);

                      if(shmdt((char *)psm)<0)

                      {

                            perror("shmdt error");

                            exit(1);

                      }

                 }                         

                

           }

           if(shmctl(shmid,IPC_RMID,NULL)<0)

           {

                 perror("shmctl error");

                 exit(1);

           }

      }   

      printf("Done\n");

      return 0;

}

 

 

阅读(3368) | 评论(0) | 转发(3) |
0

上一篇:C 信号编程

下一篇:C 消息列队编程

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