I'm trying to open multiple connections to my MySQL server from the
one executable, but I keep getting segmentation violations. Can
someone take a look at the following code and tell me if it works for
them, or what I am doing wrong? There must be something basic I am
missing (I guess). I am running mysql-3.22.21 on a 400MHz Pentium II
GNU/Linux Redhat 5.1 with 512MB of memory.
I built my version of mysql via:
./configure \
--prefix=/usr/local/mysql \
--with-unix-socket-path=/usr/local/mysql/.mysql-pipe
I have been compiling the following program with
gcc -g -o x x.c -L/usr/local/mysql/lib/mysql -I/usr/local/mysql -lmysqlclient -lpthread
Which you will need to change if you do not have MySQL installed under
/usr/local/mysql.
Also, in the code below, change the strings for UNIVERSE, USER, and
PASSWORD so you can connect to your own mysqld.
The program takes a single numeric argument (0 to 5) telling it what
to do. I get core dumps for behaviors 2, 3, and 4.
Thanks for any help. If no-one can help, I'll make a -g version of the
client library and get details on exactly where the SEGV is happening.
Terry.
--------------------------------------------------------
#include <stdio.h>
#include <pthread.h>
#include "mysql/mysql.h"
void *conn(void *arg);
#define t_create \
if (pthread_create(&thread_id, NULL, conn, (void *)1)) { \
fprintf(stderr, "error creating thread.\n"); \
exit(1); \
}
#define t_join \
if (pthread_join(thread_id, NULL)){ \
fprintf(stderr, "error rejoining thread.\n"); \
exit(1); \
}
int
main(argc, argv)
int argc;
char **argv;
{
int behavior = 0;
if (argc > 1){
behavior = atoi(argv[1]);
}
fprintf(stderr, "BEHAVIOR %d: ", behavior);
switch (behavior){
case 0:{
fprintf(stderr, "One connection, unthreaded.\n");
conn((void *)0);
break;
}
case 1:{
pthread_t thread_id;
fprintf(stderr, "One connection, threaded.\n");
t_create;
t_join;
break;
}
case 2:{
pthread_t thread_id;
fprintf(stderr, "Two connections, one unthreaded, one threaded.\n");
conn((void *)0);
t_create;
t_join;
break;
}
case 3:{
fprintf(stderr, "Two connections, unthreaded.\n");
conn((void *)0);
fprintf(stderr, "First done.\n");
conn((void *)0);
fprintf(stderr, "Second done.\n");
break;
}
case 4:{
pthread_t thread_id;
fprintf(stderr, "Two connections, threaded.\n");
t_create;
t_join;
fprintf(stderr, "First done.\n");
t_create;
t_join;
fprintf(stderr, "Second done.\n");
break;
}
case 5:{
fprintf(stderr, "Two connections, forked.\n");
if (fork()){
conn((void *)0);
fprintf(stderr, "First done.\n");
}
else {
conn((void *)0);
fprintf(stderr, "Second done.\n");
}
break;
}
default:{
fprintf(stderr, "unknown behavior.\n");
exit(1);
}
}
return 0;
}
void *
conn(void *arg)
{
int threaded = (int)arg;
MYSQL *mysql = (MYSQL *) malloc(sizeof(MYSQL));
if (!mysql){
fprintf(stderr, "bad mysql malloc.\n");
exit(1);
}
fprintf(stderr, "Attempting to connect (%sthreaded).\n", threaded ? "" : "un");
mysql_init(mysql);
if (!mysql_real_connect(mysql, "localhost", "USER", "PASSWORD", "UNIVERSE", 3306,
NULL, 0)){
fprintf(stderr, "bad mysql real connect (%sthreaded).\n", threaded ? "" : "un");
exit(1);
}
if (threaded){
fprintf(stderr, "Connected in thread %ld.\n", pthread_self());
}
else {
fprintf(stderr, "Connected (unthreaded).\n");
}
return;
}