/* -*- C++ -*- */ /************************************************************************* * Copyright(c) 1995~2005 Masaharu Goto (root-cint@cern.ch) * * For the licensing terms see the file COPYING * ************************************************************************/ // common.cxx #include #include #include #include #include #include #include #include #include #include const char *keyfile = "./keyfile"; const char projid = 'Q'; const char projid2 = 'q'; const int semsize = 5; key_t mykey; key_t mykey2; // shared memory int myshmid; void *shmbuffer; // semaphore int mysemid; struct sembuf lock[1] = { { 1, -1, 0 } }; struct sembuf unlock[1] = { { 1, 1, 0 } }; // message int mymsg; int mymsg2; ///////////////////////////////////////////////////////////////////////// // Initialize IPC ///////////////////////////////////////////////////////////////////////// void initsend() { // Prepare keys mykey=ftok(keyfile,projid); printf("mykey=%x\n",mykey); mykey2=ftok(keyfile,projid2); printf("mykey2=%x\n",mykey2); // Shared Memory myshmid = shmget(mykey,semsize*sizeof(int),SHM_R|SHM_W|IPC_CREAT); printf("myshmid=%x\n",myshmid); if(-1 == myshmid) { fprintf(stderr,"shmget failed\n"); exit(1); } shmbuffer = shmat(myshmid,0,0); printf("shmbuffer=%x\n",shmbuffer); if((void*)0xffffffff==shmbuffer) { fprintf(stderr,"shmat failed\n"); exit(1); } #ifndef NOSEM // Semaphoe mysemid = semget(mykey,2,SHM_R|SHM_W|IPC_CREAT); printf("semid=%x\n",mysemid); if(-1 == mysemid) { fprintf(stderr,"semget failed\n"); } //int stat = semctl(mysemid,0,SETALL,(void*)1); //printf("semctl = %d\n",stat); // set semval = 1 , same operation as unlock int stat = semop(mysemid,unlock,1); printf("semstat = %d\n",stat); #endif // Message mymsg = msgget(mykey,SHM_R|SHM_W|IPC_CREAT); printf("mymsg = %d\n",mymsg); mymsg2 = msgget(mykey2,SHM_R|SHM_W|IPC_CREAT); printf("mymsg2 = %d\n",mymsg2); } //////////////////////////////////////////////////////////////////////// void initrecv() { mykey=ftok(keyfile,projid); printf("mykey=%x\n",mykey); mykey2=ftok(keyfile,projid2); printf("mykey2=%x\n",mykey2); // Shared Memory while(-1==(myshmid=shmget(mykey,semsize*sizeof(int),SHM_R|SHM_W))) { printf("myshmid=%x\n",myshmid); sleep(1); } shmbuffer = shmat(myshmid,0,0); printf("shmbuffer=%x\n",shmbuffer); if((void*)0xffffffff==shmbuffer) { fprintf(stderr,"shmat failed\n"); exit(1); } #ifndef NOSEM // Semaphoe mysemid = semget(mykey,1,SHM_R|SHM_W); printf("mysemid=%x\n",mysemid); if(-1 == mysemid) { fprintf(stderr,"semget failed\n"); } #endif // Message mymsg = msgget(mykey,SHM_R|SHM_W); printf("mymsg = %d\n",mymsg); mymsg2 = msgget(mykey2,SHM_R|SHM_W); printf("mymsg2 = %d\n",mymsg2); } ///////////////////////////////////////////////////////////////////////// // Send and Receive information vir shared memory ///////////////////////////////////////////////////////////////////////// void send(int x=2) { #ifndef NOSEM // semaphore lock int stat = semop(mysemid,lock,1); printf("semstat = %d\n",stat); #endif // shared memory int *ary = (int*)shmbuffer; for(int i=0;i