System V 信号量操作,进程间通信(…

来源:互联网 发布:男士服装搭配软件 编辑:程序博客网 时间:2024/06/11 15:31
//实现semWait, semPost封装
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/sem.h>

union semun {
    int val;                
    struct semid_ds *buf;     
    ushort *array;           
};

//Allocate resources
int semWait(int semid, int idx)
{
       if ( semid < 0 )
              return -1;

       struct sembuf op;
       int ret;       //"return"

       op.sem_num = idx;
       op.sem_op  = -1;
       op.sem_flg = SEM_UNDO;

       while ((ret = semop(semid, &op,1)) == -1 && errno == EINTR)
              ;
       return ret;
}

//Release resources
int semPost(int semid, int idx)
{
       if ( semid < 0 )
              return -1;

       struct sembuf op;
       int ret;

       op.sem_num = idx;
       op.sem_op  = 1;
       op.sem_flg = SEM_UNDO;

       while ((ret = semop(semid, &op,1)) == -1 && errno == EINTR)
              ;
       return ret;
}

void displaySem(int semid, int idx)
{
       fprintf(stdout, "sem val: %d\n", semctl(semid,idx, GETVAL));
}

#if 01
int main()
{
       //__nsems = 10, the number of semaphores in thissemaphore set.
       int semid = semget(321034, 10, IPC_CREAT |IPC_EXCL | 0666);
       if(semid < 0){
              perror("semget : ");
              return -1;
       }

       int limit = 5;
       pid_t pid;
       union semun arg;
//     arg.val = 1;

       ushort array[10] = {1,1,1,1,1,1,1,1,1,1};
       arg.array = array;

       // Set the values of all the semaphores in theset to the values in the array pointed to
       // by the array member of the passed-in unionsemun
       semctl(semid, 0, SETALL, arg); //Here, Thesemnum parameter to semctl() isn't used.

       / * Allocate resources. Block the callingprocess until the value of the semaphore isgreater than or equal to the absolute value ofsem_op. (That is, wait until enough resourceshave been freed by other processes for this one toallocate.) Then add (effectively subtract,since its negative) the value of sem_op to thesemaphore's value. * /

       if((pid = fork()) < 0){
              semctl(semid, 2,IPC_RMID);
              return -1;
       }

       if(0 == pid){
             srand(time(NULL)^getpid());
              int i = 0;

              while(i++ <limit){
                    semWait(semid, 2);displaySem(semid, 2);

                     int sec =rand() & 255;

                    fprintf(stdout, "[semWait]child doing work...\nafter %dmsfinish...\n\n", sec);
                    usleep(sec);

                    semPost(semid, 2);
              }

       }else{
             srand(time(NULL)^getpid());
              int i = 0;

              while(i++ <limit){
                    semWait(semid, 2);

                     int sec =rand() & 255;

                    fprintf(stdout, "[semWait]parent doing work...\nafter %dmsfinish...\n\n", sec);
                    usleep(sec);

                    semPost(semid, 2);
              }

       }

       usleep(1000);

       semctl(semid, 2, IPC_RMID);
       return 0;
}

#endif
原创粉丝点击