Chinaunix首页 | 论坛 | 博客
  • 博客访问: 837166
  • 博文数量: 489
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 3087
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 16:28
文章分类

全部博文(489)

文章存档

2013年(7)

2012年(301)

2011年(181)

分类:

2011-12-22 20:54:11

原文地址:C 共享内存编程 作者:luozhiyong131

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

创建一个信号量集

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

#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;

}

 

 

阅读(342) | 评论(0) | 转发(0) |
0

上一篇:C 消息列队编程

下一篇:C 信号编程

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