【问题标题】:shared memory in c, question about the outputc中的共享内存,关于输出的问题
【发布时间】:2019-11-26 21:06:41
【问题描述】:

我试图用 C 实现共享内存,但我的输出出现了问题。

我尝试让一个名为 TA 的线程将成绩放入共享内存空间,并让“学生”线程输出成绩。

在 TA 线程中:

const int SIZE = 4096;
const char *name_ta = "Students_Information_ta";    
int studentGrade = (int)((random() % (100 - 80 + 1)) + 80); // TA gives out a grade to a student
char *grade = (char *)&studentGrade;

/* shared memory file descriptor */
int shm_fd_ta;
/* pointer to shared memory object */
void *ptr_ta;

/* create the shared memory segment of ta */
shm_fd_ta = shm_open(name_ta, O_CREAT | O_RDWR, 0666);
/* configure the size of the shared memory segment of ta*/
ftruncate(shm_fd_ta, SIZE);
/* map the shared memory segment of ta in the address space of the process */
ptr_ta = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_ta, 0);
if (ptr_ta == MAP_FAILED)
{
    printf("Map failed\n");
    return -1;
}

/* write to the shared memory region */
sprintf(ptr_ta, "%d", grade);
ptr_ta += strlen(grade);

这是学生线程中的输出句子:

/* name of shared memory object */
const char *name_ta = "Students_Information_ta";
/* size of shared memory object in bytes */
const int SIZE = 4096;

int shm_fd_ta;
void *ptr_ta;

/* open the shared memory segment of ta */
shm_fd_ta = shm_open(name_ta, O_RDWR, 0666);
if (shm_fd_ta == -1)
{
    printf("shared memory failed\n");
    exit(-1);
}

/* map the shared memory segment of ta in the address space of the process */
ptr_ta = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_ta, 0);
if (ptr_ta == MAP_FAILED)
{
    printf("Map failed\n");
    exit(-1);
}

printf("The grade assigned by the TA is %d\n", ptr_ta); // student receives a grade

我以为它应该给我80到100之间的数字的等级,但实际上输出的是一些非常大的数字,比如251142144。也许它已经输出了地址。我该如何解决这个错误?

【问题讨论】:

  • 你认为这是做什么的:char *grade = (char *)&studentGrade;?

标签: c pointers shared-memory


【解决方案1】:
char *grade = (char *)&studentGrade;

好的,所以grade 是指向字符的类型指针,但指向一个整数。不知道你为什么要这样做,但没关系。

sprintf(ptr_ta, "%d", grade);

您告诉sprintf 打印一个整数,但您将一个指针传递给它,该指针指向一个指向整数的字符。 sprintf 函数当然不知道如何处理这种不匹配的指针,尤其是当你告诉它你要传递一个整数时。

为什么不:

sprintf(ptr_ta, "%d", studentGrade);

如果你告诉它你要给它一个整数,也许给它一个整数。

--

ptr_ta = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_ta, 0);
if (ptr_ta == MAP_FAILED)
{
    printf("Map failed\n");
    exit(-1);
}

现在,ptr_ta 是指向您打印的字符串的指针。

printf("The grade assigned by the TA is %d\n", ptr_ta); // student receives a grade

你告诉printf 你要传递一个整数,但是你传递了一个指向字符串的指针。这应该如何工作?

我会改变很多东西,但这是一个开始:

printf("The grade assigned by the TA is %s\n", (char *) ptr_ta); // student receives a grade

【讨论】:

  • 另外,为什么要ptr_ta += strlen(grade);?既然grade 不是字符串,你为什么要把它传递给strlen?它是一个指向整数值的指针,对吧?
  • 谢谢,现在给我80到100之间的数字,但是……每次都给我81,你能告诉我怎么解决吗?
  • 是的,它是一个指向整数值的指针……所以,我应该使用其他类似 sizeof?
  • @Crow0913 阅读random 的文档。除非您以不同的方式播种,否则它每次都会为您提供相同的序列。由于你写了一个字符串到共享内存中,如果你想在字符串之后找到下一位共享内存,你需要加上字符串的长度。但是grade 是整数,而不是字符串。
  • 所以,我应该尝试用字符串替换grade...?
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多