简体   繁体   English

在c中从/向共享内存读取和写入int

[英]reading and writing int from/to shared memory in c

I'm trying to make a program where a thread writes an integer into a shared memory location and then the other thread reads and prints that integer.我正在尝试编写一个程序,其中一个线程将一个整数写入共享内存位置,然后另一个线程读取并打印该整数。 the problem I'm facing is that the second thread keeps reading the integer as -1.我面临的问题是第二个线程一直将整数读取为 -1。 here is my code:这是我的代码:

#include <stdio.h> 
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h> 
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>


struct args {
    void* memptr;
    sem_t* semptr;
};

void *p1(void *vargp) 
{
    void* memory = ((struct args*)vargp)->memptr;
    sem_t* semaphore = ((struct args*)vargp)->semptr;
    //sem_wait(semaphore);
    //sleep(0.5);
    for(int i=0; i<=10; i++)
    {
        if (!sem_wait(semaphore)) {
            printf("got in if p1\n");
            sprintf(memory, "%d", i);
            sem_post(semaphore);
            sleep(1);
        }
    }
    if (!sem_wait(semaphore)) {
        sprintf(memory, "%d", 0);
        sem_post(semaphore);
        sleep(1);
    }
    sleep(0.1);
}

void *p2(void *vargp) 
{
    void* memory = ((struct args*)vargp)->memptr;
    sem_t* semaphore = ((struct args*)vargp)->semptr;
    sleep(0.1);
    while(1)
    {
        if (!sem_wait(semaphore)) {
            printf("got in if p2\n");
            if((int)memory == 0){
                break;
            }
            printf("%d\n", (int)memory);
            sem_post(semaphore);
            sleep(1);
        }
    }
} 

const int ByteSize = 4;
const char* SharedName = "memNameTest";
const char* SemaphoreName = "semNameTest";


int main() 
{   
    int fd = shm_open(SharedName, O_RDWR, 0644);
    ftruncate(fd, ByteSize);
    void* memptr = mmap(0, ByteSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    sem_t* semptr = sem_open(SemaphoreName, O_CREAT, 0644, 0);
    sem_post(semptr);
    struct args *Share = (struct args *)malloc(sizeof(struct args));
    Share->memptr = memptr;
    Share->semptr = semptr;

    pthread_t thread1, thread2; 
    printf("Before Thread\n"); 
    pthread_create(&thread1, NULL, p1, (void*)Share);
    pthread_create(&thread2, NULL, p2, (void*)Share);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    printf("After Thread\n");



    munmap(memptr, ByteSize);
    close(fd);
    sem_close(semptr);
    unlink(SharedName);
    return 0;
    exit(0); 
}

I have tried changing (int)memory into *((int*)memory) but that resulted in a segmentation error.我曾尝试将(int)memory更改为*((int*)memory)但这导致了分段错误。

(edit) as suggested I tried this in a single-threaded program and got it to work as follows: (编辑)按照我的建议,我在单线程程序中尝试了这个,并按如下方式工作:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h> 
#include <unistd.h>
#include <string.h>

int main()
{

    /* the size (in bytes) of shared memory object */
    const int SIZE = 4;
    /* name of the shared memory object */
    const char* SharedName = "memoryInt";
    /* create the shared memory object */
    int shm_fd = shm_open(SharedName, O_CREAT | O_RDWR, 0644);
    /* configure the size of the shared memory object */
    ftruncate(shm_fd, SIZE);
    /* memory map the shared memory object */
    void* memptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0); 

    for(int i=1; i<=10; i++){
        /* write to the shared memory object */
        //sprintf(memptr, "%d", i);
        memcpy(memptr, &i, sizeof(int));
        printf("%d\n", *((int*)memptr));
        sleep(1);
    }

    return 0;
}

though this still doesn't work in a multi-threaded program as i get a segmentation fault.尽管这在多线程程序中仍然不起作用,因为我遇到了分段错误。 this is the output:这是输出:

Before Thread
got in if p1
Segmentation fault

First, you have to show what happen on your terminal when you compile your program.首先,您必须显示编译程序时终端上发生的情况。 Secondly, the function sprintf has the declaration:其次,函数 sprintf 有以下声明:

sprintf(char *str, const char *format, ...);

That means the p1 will write the null terminated string of character.这意味着 p1 将写入以空字符结尾的字符串。 In your code, i dont understand why you use the void pointer memory instead of using char pointer as the description.在您的代码中,我不明白您为什么使用 void 指针memory而不是使用 char 指针作为描述。 You should verify the read/write function by using single-threaded before applying to the multi-thread.在应用到多线程之前,您应该使用单线程验证读/写功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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