gpt4 book ai didi

C Linux pthreads : sending data from one thread to another using shared memory gives unexpected result

转载 作者:太空宇宙 更新时间:2023-11-04 08:09:56 25 4
gpt4 key购买 nike

我正在编写一个程序,它将使用共享内存将 50 个整数从一个线程传输到另一个线程,并且在接收到整数后,接收线程将打印它们。

代码编译时没有任何错误或警告,但是当我运行它时,我得到以下输出:

pthread_create() for thread 1 returns: 0
pthread_create() for thread 2 returns: 0
Segmentation fault (core dumped)

到目前为止,这是我的代码:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/msg.h>
#include <string.h>

//define declarations
#define SIZE 50

//function declarations
void *send_data();
void *receive_data();

int main()
{
pthread_t thread1;
pthread_t thread2;
int ret_val_t1;
int ret_val_t2;

//create thread1
ret_val_t1 = pthread_create( &thread1, NULL, send_data, NULL);
if(ret_val_t1)
{
fprintf(stderr,"Error - pthread_create() return value: %d\n",ret_val_t1);
exit(EXIT_FAILURE);
}

//create thread2
ret_val_t2 = pthread_create( &thread2, NULL, receive_data, NULL);
if(ret_val_t2)
{
fprintf(stderr,"Error - pthread_create() return value: %d\n",ret_val_t2);
exit(EXIT_FAILURE);
}

printf("pthread_create() for thread 1 returns: %d\n",ret_val_t1);
printf("pthread_create() for thread 2 returns: %d\n",ret_val_t2);

//wait untill threads are done with their routines before continuing with main thread
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);

exit(EXIT_SUCCESS);
}

void *send_data(){
int shmid;
int *array;
int i = 0;
int SizeMem;
key_t key = 12345;

SizeMem = sizeof(*array)*SIZE;

shmid = shmget(key, SIZE*sizeof(int), IPC_CREAT);

array = (int *)shmat(shmid, 0, 0);


for(i=0; i<SIZE; i++)
{
array[i] = i;
}

for(i=0; i<SIZE; i++)
{
printf("\n%d---\n", array[i]);
}

printf("\nWritting to memory succesful--\n");

shmdt((void *) array);
}

void *receive_data(){
int shmid;
int *array;
int i = 0;
key_t key = 12345;

shmid = shmget(key, SIZE*sizeof(int), IPC_EXCL);

array = shmat(shmid, 0, SHM_RDONLY);

for(i=0; i<SIZE; i++)
{
printf("\n%d---\n", array[i]);
}

printf("\nRead to memory succesful--\n");

shmdt((void *) array);
}

最佳答案

使用the ipcs utility并查看 ipcs -m

的输出

根据 the shmget man page :

SYNOPSIS top

   #include <sys/ipc.h>
#include <sys/shm.h>

int shmget(key_t key, size_t size, int shmflg);

...

In addition to the above flags, the least significant 9 bits of shmflg specify the permissions granted to the owner, group, and others. These bits have the same format, and the same meaning, as the mode argument of open(2). Presently, execute permissions are not used by the system.

代码行

shmid = shmget(key, SIZE*sizeof(int), IPC_CREAT);

“shmflg 的最低 9 位”设置为零。

没有人有权读/写您的共享内存段。

这样会更好:

shmid = shmget(key, SIZE*sizeof(int), IPC_CREAT | 0600 );

这将授予拥有用户读/写权限。

关于C Linux pthreads : sending data from one thread to another using shared memory gives unexpected result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40181096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com