简体   繁体   中英

C semaphore from Python

I have a C code in which I have implemented a locking mechanism using semaphore. The basic flow is as below -

int s=1;
void wait(){

    while(s<=0);
    s--;
}

void pipeData(paTestData *data){

    wait();

    SAMPLE *tempBuff = (SAMPLE*)malloc(sizeof(SAMPLE)*FRAME_SIZE);
    int readCount   = FRAME_SIZE;

    while(readCount > 0){

        tempBuff[FRAME_SIZE - readCount] = data->sampleValues[data->readFromCB];        
        data->readFromCB++;
        readCount--;
    }

    fd = open(fifoPipe, O_WRONLY);

    write(fd, tempBuff, sizeof(tempBuff));
    close(fd);

    free(tempBuff);

}

int callBack(){

    // Perform data acquisition and call foo

   foo();

    // Rest code here
}

The python code at the reader end is as below:

with open(FIFO, 'rb') as fifo:
                print("Pipe opened")
                count=0
                while count<framelen:
                    Rsample = fifo.read()
                    frame = np.fromstring(Rsample,dtype=np.float32)

                    if (len(frame)>0):
                        print(frame)
                        count=count + len(frame)

The data on the other end of the PIPE is being processed by a Python Script. The problem is that the reading of the PIPE on python end is not able to fetch the full set of the data.

It's because I had read that for named PIPE for every write, there should a reader or else the pipe open is blocked during next iteration.

In this case, after writing 10 samples to the PIPE, the python's reader implementation is able to read only first two samples and the PIPE is readily available for next write set.

This is the reason why I was looking for a locking mechanism for the same.

My doubts are -

1) Is there a way in which I could increment the s variable (kind of what a signal() function in C would do) every time the python script is finished polling up all data from PIPE.

2) Is there any other smooth implementation of such a problem, any other IPC technique between C and Python enabling lock mechanism?

Thanks!

write(fd, tempBuff, sizeof(tempBuff));

The above line only write 4/8 bytes (32-bit/64-bit) of data from tempBuff to pipe

If you want to write all data within tempBuff, you need to change the line to

write(fd, tempBuff, sizeof(SAMPLE)*FRAME_SIZE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM