このプログラムを実行すると:
#include <sys/types.h>
#include <pthread.h>
#include <sys/mman.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main () {
int stat;
pthread_mutex_t *mutex = (pthread_mutex_t*)mmap (0, sizeof (pthread_mutex_t) + sizeof (long),PROT_READ | PROT_WRITE,
MAP_SHARED ,-1, 0);
long *data = (long*)(&mutex[1]); /* map 'data' after mutex */
int pid;
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init (mutex, &attr);
*data = 0;
pid = fork ();
pthread_mutex_lock (mutex);
(*data)++;
pthread_mutex_unlock (mutex);
if (!pid) /* child exits */
exit (0);
else
waitpid (pid, &stat, 0);
printf ("data is %ld\n", *data);
return 0;
}
私が持っていsegmentation fault
ます!問題は何ですか?そして、このエラーを解決するために何ができますか?
でデバグした後valgrind
、私はこのメッセージを持っています:
==28660== Process terminating with default action of signal 11 (SIGSEGV)
==28660== Access not within mapped region at address 0xF
==28660== at 0x4E35C10: pthread_mutex_init (pthread_mutex_init.c:83)
==28660== by 0x4008F0: main (in /home/program)
==28660== If you believe this happened as a result of a stack
==28660== overflow in your program's main thread (unlikely but
==28660== possible), you can try to increase the size of the
==28660== main thread stack using the --main-stacksize= flag.
==28660== The main thread stack size used in this run was 8388608.