Some one can informe me how to solve this compile error :
tarek.c: In function ‘main’:
tarek.c:33: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/ccAEGS6k.o: In function `main':
tarek.c:(.text+0x45): undefined reference to `pthread_mutexattr_init'
tarek.c:(.text+0x56): undefined reference to `pthread_mutexattr_setpshared'
collect2: ld returned 1 exit status
when i compile this program (Two process shared the same value using critical section) :
#include <sys/types.h>
#include <pthread.h>
#include <sys/mman.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.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);
}
Explicitly mention
-pthreadlibrary .Do compilation like this:
It will solve your
For
exit()include#include <stdlib.h>For
waitpid()include#include <sys/wait.h>for more man exit() and man waitpid()