簡體   English   中英

C語言中的UNIX共享內存和信號量

[英]UNIX Shared memory and Semaphores in C

我一周前開始了解並使用信號量和共享內存,並實際創建了該程序。 問題是我找不到任何問題。 我已經看了好幾個小時,一切似乎都是正確的。 代碼可以編譯,我可以創建構建,但是執行時什么也沒有發生。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h> 
#include <sys/shm.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <semaphore.h>

#define MAXCHILDS 4
#define MAX_SIZE 10
#define MAX_WRITES 4

typedef struct{
    int m[MAX_SIZE][MAX_SIZE];
} matrix;

/*fork variables*/
pid_t child[MAXCHILDS];
/*semphores variables */
sem_t *empty, *full, * mutex;
/*share memory id*/
int shmid;
/*shared memory array pointer */
matrix * sh_mem;
/*pointer to matrix*/
int **p;

void init(){
      /*create pointer to matrix*/
      p = &sh_mem->m;
     /*semaphores unlink and creation */    
     sem_unlink("EMPTY");
     empty=sem_open("EMPTY",O_CREAT|O_EXCL,0700,MAX_WRITES);
     sem_unlink("FULL");
     full=sem_open("FULL",O_CREAT|O_EXCL,0700,0);
     sem_unlink("MUTEX");
     mutex=sem_open("MUTEX",O_CREAT|O_EXCL,0700,1);
    /*initialize shared memory */
    shmid = shmget(IPC_PRIVATE,sizeof(matrix),IPC_CREAT|0777);
    /*map shared memory*/
    sh_mem = (matrix*)shmat(shmid,NULL,0);
    if(sh_mem== (matrix*)(-1)){
        perror("shmat");
    }
}

void writer(int ** m){
    int i,k;
    for(i = 0;i<MAX_SIZE;i++){
        for(k= 0;k<MAX_SIZE;k++){
            m[i][k] = 0;
        }
    }
}

void reader(int **m){
    int i = 0;
    int k = 0;
    for(i = 0;i<MAX_SIZE;i++){
        for(k= 0;k<MAX_SIZE;k++){
            printf(m[i][k]);
        }
        printf("\n");
    }
}

void terminate() {
  sem_close(empty);
  sem_close(full);
  sem_close(mutex);
  sem_unlink("EMPTY");
  sem_unlink("FULL");
  sem_unlink("MUTEX");
  shmctl(shmid, IPC_RMID, NULL);
}

int main(int argc, char **argv)
{
    int i;
    init();

    for(i = 0;i<MAXCHILDS;i++){
        if ((child[i] = fork()) < 0) // error occured
        {
            perror("Fork Failed");
            exit(1);
        }
        if ((child[i] = fork())==0){
            writer(sh_mem->m);
            exit(0);
        }
    }
    /*father*/  
    reader(sh_mem->m);
    wait(NULL);

    terminate();

    return 0;
}

孩子應該在共享內存中寫入矩陣,父親應該讀取共享內存陣列並打印矩陣。 你能幫我嗎? 謝謝您的幫助 ...

此處的主要錯誤是,正如gcc -Wall指出的那樣, readerwriter采用的參數類型與傳遞給他們的參數類型不同。

test.c: In function ‘main’:
test.c:92:13: warning: passing argument 1 of ‘writer’ from incompatible pointer type [enabled by default]
test.c:49:6: note: expected ‘int **’ but argument is of type ‘int (*)[10]’
test.c:97:5: warning: passing argument 1 of ‘reader’ from incompatible pointer type [enabled by default]
test.c:58:6: note: expected ‘int **’ but argument is of type ‘int (*)[10]’

按照規定,該程序在父母和每個孩子中均被隔離。 當我將readerwriter reader的參數類型從int **m更改為int m[MAX_SIZE][MAX_SIZE] (以及以下修復程序)時,據我所知,該程序成功運行。

還有許多其他錯誤:

  • 您需要#include <sys/wait.h>
  • 沒有使用全局int **p並且其初始化與讀取器和寫入器函數具有相同的類型錯誤。
  • readerprintf調用需要格式字符串。 我使用了"%d "
  • 正如喬納森·萊夫勒(Jonathan Leffler)指出的那樣,您每次在main的循環中只需要調用一次fork()即可。

編譯器警告突出顯示了除最后一個以外的所有內容。

在研究該程序失敗的原因時,我還使用了strace -f來確定實際破壞了哪些系統調用和進程。 例如,與信號量相關的系統調用似乎已成功返回-盡管正如喬納森(Jonathan)指出的那樣,您應該檢查它們的返回值是否有錯誤,因為盡早失敗會使調試問題變得更加容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM