UNIX共享内存和信号用C [英] UNIX Shared memory and Semaphores in C

查看:102
本文介绍了UNIX共享内存和信号用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始一个星期前理解和信号量和共享内存,居然创造了这个项目的工作;问题是我找不到任何毛病。我一直在看它几个小时,一切都似乎是正确的。在code编译,我可以创建构建,但是当我执行它什么也不会发生。

I started a week ago understanding and working with semaphores and shared memory, and actually created this program; the problem is I can't find anything wrong with it. I've been looking at it for hours and everything seems correct. The code compiles and i can create the build but when I execute it nothing happens.

#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;
}

孩子们应该写共享内存中的基体,而父亲应该读取共享存储阵列和打印矩阵。
你能不能帮我这个?感谢您的帮助...

The children are supposed to write the the matrix in shared memory, and the father is supposed to read the shared memory array and the print the matrix. Can you help me with this? Thanks for the help ...

推荐答案

这里的主要错误是作家比你传递给他们,采取不同类型的参数的gcc -Wall 所指出的:

The primary error here is that reader and writer take a different type of argument than you're passing to them, as gcc -Wall points out:

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]’

由于提供,该方案在家长和每个孩子segfaulted。当我改变的参数类型作家 INT ** M INT米[MAX_SIZE] [MAX_SIZE] (与下面的补丁一起),程序运行成功,据我可以告诉。

As provided, the program segfaulted in the parent and every child. When I changed the parameter type of reader and writer from int **m to int m[MAX_SIZE][MAX_SIZE] (along with the fixes below), the program ran successfully, as far as I can tell.

有其他一些错误:


  • 您需要的#include&LT; SYS / wait.h方式&gt;

  • 全球 INT ** P 未使用,其初始化具有相同类型的错误作为读写器的功能做到了。

  • 阅读器的的printf 呼叫需要一个格式字符串;我用%D

  • 当乔纳森·莱弗勒指出,需要调用叉()只有一次通过循环每次在

  • You need to #include <sys/wait.h>.
  • The global int **p isn't used and its initialization has the same type error as the reader and writer functions did.
  • The printf call in reader needs a format string; I used "%d ".
  • As Jonathan Leffler pointed out, you need to call fork() only once each time through the loop in main.

除了最后那些被编译器警告凸显。

All but the last of those were highlighted by compiler warnings.

在研究为什么这个计划失败了,我也用 strace的-f 来确定哪些系统调用和流程实际上破获。信号量相关的系统调用,例如,似乎成功返回 - 虽然乔纳森指出,应检查其返回值的错误,因为尽早失败使得它更容易调试问题。

In studying why this program was failing, I also used strace -f to identify which syscalls and processes were actually busted. The semaphore-related syscalls, for example, appear to be returning successfully--although as Jonathan pointed out, you should check their return values for errors, because failing as early as possible makes it much easier to debug problems.

这篇关于UNIX共享内存和信号用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆