2012年6月16日 星期六

Interprocess Communications (IPC)

> messages (msgget msgctl msgop)
    communication is in the form of data stored in a buffer. The buffer can be either sent or reveived.


> semaphores (semget semctl semop)
    communication is in the form of positive integers with a value between 0 and 32,767. Semaphores may be contained in an array the size of which is determined by the system administrator. The default maximun size for the array is 25.

    semid : used to identify or reference a particular data structure and semaphore set(array)

    int semget(key_t key, int nsems, int semflg)
        * create a new semaphore
        * get an existing semaphore
        key is equal to IPC_PRIVATE; key is passed a unique hexadecimal integer
        semflg ANDed with IPC_CREATE is TRUE; accesss permissions, execution, control fields(commands)

        semid = semget(key, nsems, (IPC_CREATE | 0400));
        semid = semget(key, nsems, (IPC_CREATE | IPC_EXCL | 0400));
        semid = semget(IPC_PRIVATE, nsems, semflg);
        semid = semget(0, nsems, semflg);

    semaphore internal values
        sem_perm.cuid
        sem_perm.uid
        sem_perm.cgid
        sem_perm.gid
        sem_perm.mode
        sem_nsems
        sem_otime
        sem_ctime

    int semop(int semid, struct sembuf *sops, unsigned nsops);
        * acquiring or release a semaphore

        struct sembuf {
            unsigned short sem_num;
            short sem_op;
            short sem_flg;
        }

        semaphore flag options (sembuf.sem_flg)
            SEM_UNDO // Undo the semaphore operation if the process exits.
            IPC_NOWAIT // Return immediately if the ssemaphore operation cannot be performed.

    int semctl(int semid, int semnum, int cmd, ...);
        * get info about a semaphore
        * set info about a semaphore
        * remove a semaphore

        operations that can be performed using semctl
            GETVAL
            SETVAL        int
            GETPID
            GETNCNTL    int
            GETZCNTL    int
            GETAL        u_short*
            SETALL        u_short*
            IPC_STAT    struct semid_ds*
            IPC_SET        struct semid_ds*
            IPC_RMID
    User utilities
        // provide information on the ipc faclities
        $ ipcs
        // provide semaphore informations
        $ ipcs -s
        $ ipcs -s -i semid
        // remove semaphore of the semid
        $ ipcrm -s semid

    $ starce -o strace_output excutable_file

> shared memory (shmget shmctl shmop)
    communication takes place through a common area of main memory. One or more processes can attach a segment of memory and as a consequence can share whatever data is placed there.

沒有留言:

張貼留言