3

これは、256 から 1024 バイトの範囲のデータをバッファーに送信する文字関数の一部です。

    semaphore_p(semMutexAll);//critical section
    int charByte =rand() % 768 + 256;
    for(i=0 ; i<charByte ; i++)
         semaphore_p(semBuff);//contor buffer overflow
    semaphore_v(semMutexAll);

これは、10ミリ秒でバッファから2バイトを読み取る関数cpuの一部です

while(1)
{
      semaphore_v(semBuff);
      semaphore_v(semBuff);
      usleep(10000);
}

ここにセマフォがあります

    semBuff = semget((key_t)1234,1,0666|IPC_CREAT); //Gets shared memory Id --> 1234 key
    semChar = semget((key_t)1235,1,0666|IPC_CREAT);
    semImage  = semget((key_t)1236,1,0666|IPC_CREAT);       
    semAudio  = semget((key_t)1237,1,0666|IPC_CREAT);
    semVideo  = semget((key_t)1238,1,0666|IPC_CREAT);
    semMutexCpu  = semget((key_t)1239,1,0666|IPC_CREAT);
    semMutexAll  = semget((key_t)1230,1,0666|IPC_CREAT);

    set_semvalue(semBuff,BUFFSIZE);
    set_semvalue(semChar,MAXCHR);
    set_semvalue(semImage,MAXIMAGE);
    set_semvalue(semAudio,MAXAUDIO);
    set_semvalue(semVideo,MAXVIDEO);
    set_semvalue(semMutexCpu,1);
    set_semvalue(semMutexAll,1);

ここに共有メモリがあります

    key_t ShmKEY;
    key_t ShmQ;

    int ShmContID;
    int ShmQID;
    int status;

    queue *buffArea;
    buffArea = (queue*)malloc(sizeof(queue));
    initQueue(buffArea);
    control * params;
    params = (control*)malloc(sizeof(control));

    ShmKEY = ftok("./", 'A');
    ShmQ = ftok("./", 'B');

    if((ShmContID = shmget(ShmKEY,sizeof(params),IPC_CREAT | 0666)) == -1)
    { 
        //Gets shared memory Id --> IPC_PRIVATE key
        perror("shmget");
        exit(1);    
    }

    if((ShmQID = shmget(ShmQ,sizeof(buffArea),IPC_CREAT | 0666)) == -1)
    {
        //Gets shared memory Id --> IPC_PRIVATE key
        perror("shmget");
        exit(1);    
    }

ここにセマフォヘッダーがあります

    #ifndef SHAREDSMPH_H_
    #define SHAREDSMPH_H_

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/ipc.h>
    #include <sys/types.h>
    #include <sys/sem.h>
    #include <sys/wait.h>
    #include <string.h>
    #include <errno.h>

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

    static int semBuff;
    static int semChar;
    static int semImage;
    static int semAudio;
    static int semVideo;
    static int semMutexCpu;
    static int semMutexAll;

    static int set_semvalue(int sem_id,int val)
    {
        union semun sem_union;
        sem_union.val=val;
        if(semctl(sem_id,0,SETVAL,sem_union) == -1)
        {
            fprintf(stderr, "Failed to semvalue\n");        
            return(0);
        }
        return (1);
    }

    static void del_semvalue(int sem_id)
    {
        union semun sem_union;
        if(semctl(sem_id,0,IPC_RMID,sem_union) == -1)
            fprintf(stderr, "Failed to delete semaphore\n");
    }

    static int get_semvalue(int sem_id)
    {
        union semun sem_union;

        int value = semctl(sem_id,0,GETVAL,sem_union);  
        //printf("semvalue:%d\n",value);
        return value;
    }

    static int semaphore_p(int sem_id)
    {
        struct sembuf sem_b;
        sem_b.sem_num=0;
        sem_b.sem_op=-1; /* P */
        sem_b.sem_flg=SEM_UNDO;

        if(semop(sem_id,&sem_b,1) == -1)
        {
            printf("semaphore_p failed\n");
            return (0); 
        }
        return (1);
    }

    static int semaphore_v(int sem_id)
    {
        struct sembuf sem_b;
        sem_b.sem_num=0;
        sem_b.sem_op=1; /* V */
        sem_b.sem_flg=SEM_UNDO;
        if(semop(sem_id,&sem_b,1) == -1)
        {
            printf("semaphore_v failed\n");
            return (0); 
        }
        return (1);
    }
    #endif

このコードに何か問題がありますか? 特にセマフォ、問題はセマフォの値が常に異なるプロセスで初期化されることです。プロセスでsemaphore_vおよびsemaphore_p関数を呼び出すと、正確に動作しますが、たとえばsemBuffセマフォは常に1024に初期化されます。

4

0 に答える 0