#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<math.h>
#include<sys/uio.h>
#include<time.h>
#include<string.h>
#include<sys/timeb.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <sys/time.h>
#include <dirent.h>

struct SemStruct{
    sem_t * Sem[128]  ;
    char    SemFile[128][80] ;
    int     iSenNum ;
    const char *getFileName(int idx){ return SemFile[idx] ; }
} ;

struct SemStruct GlbSemStruct ;

int main(int argc, char **argv)
{
    DIR *d ;
    struct dirent *dir;
    d = opendir("/home/informix/test/semaphore");
    if (d)
    {
        while ((dir = readdir(d)) != NULL){
            if( strncmp(dir->d_name,"SEM",3) == 0 ){
                strncpy(GlbSemStruct.SemFile[GlbSemStruct.iSenNum],dir->d_name+3,strlen(dir->d_name)-3) ;
                GlbSemStruct.Sem[GlbSemStruct.iSenNum] = sem_open(GlbSemStruct.SemFile[GlbSemStruct.iSenNum],O_CREAT,0644,0);
                GlbSemStruct.iSenNum++ ;
            }
        }
        closedir(d);
    }

    while( 1 ) {
        struct timeval localtimex ;
        long secs,usecs ;
        gettimeofday(&localtimex,0x00) ;
        secs  = localtimex.tv_sec ;
        usecs = localtimex.tv_usec ;
        for(int idx=0;idx<GlbSemStruct.iSenNum;idx++){
            sem_post( GlbSemStruct.Sem[idx] ) ;
            printf("(%s)...secs=(%ld),usecs=(%ld)\n",GlbSemStruct.getFileName(idx),secs,usecs) ;
        }
        sleep( 1 ) ;
    }
} //main

 

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<math.h>
#include<sys/uio.h>
#include<time.h>
#include<string.h>
#include<sys/timeb.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <sys/time.h>

sem_t * iIOCSem1  ;

int main(int argc, char **argv)
{
    if(argc != 2 ){
        printf("please input like : ./sem2.exe 08000P or ./sem2.exe 90050 \n") ;
        exit( 0 ) ;
    }
    char inName[128]={0} ;
    strcpy(inName,argv[1]) ;
    iIOCSem1 = sem_open(inName,O_RDWR,S_IREAD | S_IWRITE,0);

    int inum=0 ;
    if( !sem_getvalue(iIOCSem1,&inum) ){
        printf("inum=(%d)\n",inum) ;
        for(int idx=0;idx<inum;idx++){
            sem_wait(iIOCSem1) ;
            //printf("-------------------------------------------\n") ;
        }
    }
    printf("ok....start \n") ;
    while( 1 ) {
        sem_wait(iIOCSem1) ;
        struct timeval localtimex ;
        long secs,usecs ;
        gettimeofday(&localtimex,0x00) ;
        secs  = localtimex.tv_sec ;
        usecs = localtimex.tv_usec ;
        printf("secs=(%ld),usecs=(%ld)\n",secs,usecs) ;
    }
} //main

 

g++ --std=c++11 -Wall -Werror sem1.cpp -pthread -o sem1.exe
g++ --std=c++11 -Wall -Werror sem2.cpp -pthread -o sem2.exe

 

 

創作者介紹
創作者 hedgezzz 的頭像
hedgezzz

hedgezzz的部落格

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