使用互斥锁的缓冲区错误“读/写超出范围”

时间:2019-03-15 17:24:33

标签: locking pthreads mutex

我正在c中使用多个线程,并且在使用segfault.stensal.com时遇到了此错误...我似乎无法弄清楚为什么我要读取和写入边界错误。这里的任何方向都很好。我知道锁通常在您有2个或更多线程修改相同或相似数据时使用,因此我将修改缓冲区以使其更像是堆栈,但是暂时,我想确保代码能正常工作一个简单的数组。

===========#0扩展运行时消息的开头===========

运行时错误: [越界写入]
  继续执行会导致不确定的行为,中止!

-
- Writing 4 bytes to 0x818a6d4 will corrupt the adjacent data.
- 
- The memory-space-to-be-written (start:0x8189730, size:4000 bytes) is bound to 'buffer' at
-     file:/prog.c::14, 0
- 
-  0x8189730               0x818a6cf
-  +------------------------------+
-  |the memory-space-to-be-written|......
-  +------------------------------+
-                                     ^~~~~~~~~~
-           the write starts at 0x818a6d4 that is 4 bytes after the memory end.
- 
- Stack trace (most recent call first) of the write.
- [1]  file:/prog.c::86, 18
- [2]  file:/musl-1.1.10/src/thread/pthread_create.c::168, 17
- [3]  file:/musl-1.1.10/src/internal/c_abi_func.c::51, 8
-
```(56,208)

============ End of #0 stensal runtime message ============

=========== Start of #1 stensal runtime message ===========

  Runtime error: **[out-of-bounds read]**  
  Continuing execution can cause undefined behavior, abort!

```stensal-diagnostic-info
-



My code is as follows:



#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define BUFFER_SIZE (50)

typedef struct  {

        int pc1, pc2;

} Prodcons;

Prodcons buffer[500];

pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;

int pc1C;
int pc2C;


void *Producer();
void *Consumer();


int main(int argc, char *argv[])
{
        pthread_t tid1;
        pthread_t tid2;
        pthread_attr_t attr;
        void *my_Ptr;

        if (argc == 1)
        {
                printf("usage: ./assn3 <number to factor>\n");
                return -1;
        }

        if (argc < 0)
        {
                fprintf(stderr, "%d must be >= 0\n", atoi(argv[1]));
                return -1;
        }


        for(int i = 0; i < argc - 1 ; i++)
        {
            pthread_mutex_lock(&lock1);
            buffer[i].pc1 = (atoi(argv[i + 1]));
            pc1C++;
            pthread_mutex_unlock(&lock1);

        }

        pthread_attr_init(&attr);

        pthread_create(&tid1, &attr, Producer, NULL);
        pthread_create(&tid2, &attr, Consumer, NULL);

        pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);
}

void *Producer()
{

        for (int j = 0; j < pc1C; j++)
        {

            pthread_mutex_lock(&lock2);

            int myNum = buffer[j].pc1;




            if (myNum == 1)
            {
                    buffer[pc2C].pc2 = 1;
                    pc2C++;
            }

            while (myNum % 2 == 0)
            {
                   buffer[pc2C].pc2 = 2;
                   myNum = myNum/2;
                   pc2C++;
            }

            for (int i = 3; i <= sqrt(myNum); i += 2)
            {
                    while (myNum % i == 0)
                   {
                            buffer[pc2C].pc2 = i;
                            myNum = myNum/i;
                            pc2C++;
                   }
            }


            if (myNum > 2)
            {
                   buffer[pc2C].pc2 = myNum;
                   pc2C++;
            }


            buffer[pc2C].pc2 = -1;

            pthread_mutex_unlock(&lock2);

        }

        pthread_exit(NULL);

}



void *Consumer()
{
    for (int i = 0; i < pc1C; i++)
        {

                printf("%d: ", buffer[i].pc1);

                int j = 0;

                while (buffer[j].pc2 != -1 )
                {
                   //   printf("%d ",buffer[j].pc2);
                    j++;
                }

                printf("\n");
        }

    pthread_exit(NULL);

}

0 个答案:

没有答案