一支小小的 semaphore 程式

sem1.cpp

#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <semaphore.h>


#define SNAME "mysemx"

int main()
{
    sem_t *sem = sem_open(SNAME, O_CREAT, 0644, 3); /* Initial value is 3. */
    sleep(10) ;
    sem_post(sem) ;
    sleep(3) ;
    sem_unlink(SNAME) ;
}

sem2.cpp

#define SNAME "mysemx"

int main()
{
    sem_t *sem = sem_open(SNAME, 0); /* Open a preexisting semaphore. */
    while(1)
    {
        sem_wait(sem) ;
        printf("doing .....\n") ;
    }
    printf(" done \n") ;
}

 

全站熱搜

hedgezzz 發表在 痞客邦 留言(0) 人氣()