"Imagination is far better than Intelligence" Subhankar Katyayan ' 00 44
7530847376
--- On Wed, 9/10/08, Subhankar Katyayan <[EMAIL PROTECTED]> wrote:
From: Subhankar Katyayan <[EMAIL PROTECTED]>
Subject: Error in the Program
To: [EMAIL PROTECTED]
Date: Wednesday, September 10, 2008, 9:06 AM
Hi,
The program which I have attached is taken from "sourceforge" and it's failing
(segmentation fault) when I am trying to execute it. Could you please look into
it and let me know why it's failing and how to resolve it.
./shmem -r 10 -w 4 -s 10 (W is the writer thread, if I give W as 4 or more it
fails)
But at the same time if I am trying to execute it as follows:
./shmem -r 10 -w 3 -s 10 ( it works fine)
When I tried to debug it using dbx on AIX, it's giving core in the following
function....
void * reader(void *parm)
Below is the error message attached for your reference..
midibm14:subh:/home/users/subh/demo #dbx a.out
Type 'help' for help.
reading symbolic information ...
(dbx) r -r 5 -w 3 -s 10 (this works fine)
a.out: Shared Memory Test Program
Number of writers thread = 3
Number of readers thread = 5
Shared Memory Size in Bytes = 10
Writer (0): shared memory checksum 0045
Reader (0) of Writer (0): checksum 0045
Reader (4) of Writer (2): checksum 0065
Reader (3) of Writer (2): checksum 0065
Reader (1) of Writer (0): checksum 0045
Reader (2) of Writer (0): checksum 0045
Reader (3) of Writer (0): checksum 0045
Reader (4) of Writer (0): checksum 0045
Writer (1): shared memory checksum 0055
Reader (0) of Writer (1): checksum 0055
Reader (1) of Writer (1): checksum 0055
Reader (2) of Writer (1): checksum 0055
Reader (3) of Writer (1): checksum 0055
Reader (4) of Writer (1): checksum 0055
Writer (2): shared memory checksum 0065
Reader (0) of Writer (2): checksum 0065
Reader (2) of Writer (2): checksum 0065
Reader (1) of Writer (2): checksum 0065
Main: Readers calculated segment successfully
successful!
execution completed
(dbx) r -r 5 -w 4 -s 10 (this fails)
a.out: Shared Memory Test Program
Number of writers thread = 4
Number of readers thread = 5
Shared Memory Size in Bytes = 10
Shmat Failed: Shared Memory Attach Error 11 : Too many open files
Writer (0): shared memory checksum 0045
Reader (0) of Writer (0): checksum 0045
Reader (4) of Writer (2): checksum 0065
Reader (3) of Writer (2): checksum 0065
Writer (3): shared memory checksum 0000
Segmentation fault in reader at line 48 in file "reader.c" ($t22)
48 cksum_r += *ptr++;
(dbx)
"Imagination is far better than Intelligence"
Subhankar Katyayan
' 00 44 7530847376
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAX_THREAD_NUMBER 1000
#define MAX_WRITER_NUMBER 100
#define MAX_READER_NUMBER 400
#define DEFAULT_NUM_READERS 2
#define DEFAULT_NUM_WRITERS 2
#define SHMEM_MODE (SHM_R | SHM_W)
#define DEFAULT_SHMEM_SIZE 200000
#define MB (1024*1024)
#define MAX_SHMEM_NUMBER 100
#define USAGE "\nUsage: %s [-r num_readers] [-w num_writers] [-s
shmem_size]\n\n" \
"\t-r num_readers number of thread (readers) to create\n" \
"\t-w num_writers number of thread (writers) to create\n" \
"\t-s buffer_size size of shared memory segment (bytes)\n" \
"\t (must be less than 256MB!)\n\n"
void parse_args (int, char **);
void *reader (void *);
void *writer (void *);
void sys_error (const char *, int);
void error (const char *, int);
void release ();
pthread_t * writer_th;
pthread_t * reader_th;
pthread_mutex_t mutex_r[MAX_WRITER_NUMBER];
pthread_mutex_t cond_mutex[MAX_WRITER_NUMBER];
int thread_hold[MAX_WRITER_NUMBER];
pthread_cond_t cond_var[MAX_WRITER_NUMBER];
int *read_count[MAX_WRITER_NUMBER];
unsigned long *checksum[MAX_WRITER_NUMBER];
char *shmptr[MAX_WRITER_NUMBER];
unsigned long cksum[MAX_WRITER_NUMBER];
int shmem_size = DEFAULT_SHMEM_SIZE;
pid_t parent_pid;
int num_readers = DEFAULT_NUM_READERS;
int buffer_size = DEFAULT_SHMEM_SIZE;
int num_writers = DEFAULT_NUM_WRITERS;
int shmid[MAX_THREAD_NUMBER + MAX_WRITER_NUMBER];
int main (int argc, char **argv)
{
pthread_attr_t newattr;
int i,j,k;
size_t Size;
unsigned long *ulptr;
parse_args (argc, argv);
printf ("%s: IPC Shared Memory TestSuite program\n", *argv);
printf ("\tNumber of writers = %d\n", num_writers);
printf ("\tNumber of readers = %d\n", num_readers);
printf ("\tBytes per writer = %d\n", buffer_size);
for (i=0; i<num_writers; i++) {
j=i*3;
Size=sizeof (int);
if ((shmid[j] = shmget (IPC_PRIVATE, Size, SHMEM_MODE)) < 0)
sys_error ("read_count shmget failed", __LINE__);
if ((long)(read_count[i] = (int *) shmat (shmid[j], 0, 0)) == -1)
sys_error ("shmat failed", __LINE__);
*(read_count[i]) = 0;
j++;
Size=sizeof (unsigned long) * num_readers;
if ((shmid[j] = shmget (IPC_PRIVATE, Size, SHMEM_MODE)) < 0)
sys_error ("checksum shmget failed", __LINE__);
if ((long)(checksum[i] = (unsigned long *) shmat (shmid[j], 0, 0)) ==
-1)
sys_error ("shmat failed", __LINE__);
ulptr=checksum[i];
for (k=0; k < num_readers; k++)
{
*ulptr = 0;
ulptr++;
}
Size=buffer_size;
j++;
if ((shmid[j] = shmget (IPC_PRIVATE, Size, SHMEM_MODE)) < 0)
sys_error ("shmptr shmget failed", __LINE__);
if ((long)(shmptr[i] = shmat (shmid[j], 0, 0)) == -1)
sys_error ("shmat failed", __LINE__);
}
writer_th = (pthread_t *) malloc ((size_t) (num_writers * sizeof
(pthread_t)));
reader_th = (pthread_t *) malloc ((size_t) (num_writers * num_readers *
sizeof (pthread_t)));
for (i=0; i<num_writers; i++) {
if (pthread_mutex_init(&mutex_r[i] , (pthread_mutexattr_t *)NULL) != 0)
sys_error ("Can't initialize mutex_r", __LINE__);
if (pthread_mutex_init (&cond_mutex[i], (pthread_mutexattr_t *)NULL))
sys_error ("Can't initialize cond_mutex", __LINE__);
if (pthread_cond_init (&cond_var[i], (pthread_condattr_t *)NULL))
sys_error ("cond_init(&cond_var) failed", __LINE__);
thread_hold[i]=1;
}
if (pthread_attr_init(&newattr))
sys_error ("attr_init(&newattr) failed", __LINE__);
if (pthread_attr_setdetachstate (&newattr, PTHREAD_CREATE_UNDETACHED))
sys_error ("attr_setdetachstate(&newattr) failed", __LINE__);
for (i = 0; i < num_writers; i++)
{
if (pthread_create (&writer_th[i], &newattr, writer, (void *)
(long)i))
sys_error ("writer: pthread_create failed", __LINE__);
k=i*num_readers;
for (j = k; j < (k + num_readers) ; j++)
{
if (pthread_create (&reader_th[j], &newattr, reader, (void *)
(long)j))
sys_error ("reader: pthread_create failed", __LINE__);
}
}
for (i = 0; i < num_writers; i++)
{
if (pthread_join( writer_th[i], NULL)) {
printf("writer_th: pthread_join return: %d\n",i);
sys_error("pthread_join bad status", __LINE__);
}
k=i*num_readers;
for (j = k; j < (k + num_readers) ; j++)
{
if (pthread_join( reader_th[j], NULL)) {
printf("reader_th: pthread_join return: %d\n",j);
sys_error("pthread_join bad status", __LINE__);
}
}
}
for (i = 0; i < num_writers; i++)
{
ulptr=checksum[i];
for (j=0; j<num_readers; j++) {
if (cksum[i] != *ulptr )
error ("checksums do not match", __LINE__);
}
}
printf ("\n\tMain: readers calculated segment successfully\n");
release();
printf ("\nsuccessful!\n");
return (0);
}
void *writer (void *parm)
{
int num_w = (int) (long)parm;
unsigned long cksum_w = 0;
char data = 0;
char *ptr;
data = num_w;
for (ptr=shmptr[num_w]; ptr < (shmptr[num_w]+buffer_size); ptr++) {
*ptr = data++;
cksum_w += *ptr;
}
if (pthread_mutex_lock (&cond_mutex[num_w]))
sys_error ("mutex_lock(&cond_mutex) failed", __LINE__);
thread_hold[num_w]=0;
if (pthread_cond_broadcast (&cond_var[num_w]))
sys_error ("cond_signal(&cond_var) failed", __LINE__);
if (pthread_mutex_unlock (&cond_mutex[num_w]))
sys_error ("mutex_unlock(&cond_mutex) failed", __LINE__);
cksum[num_w] = cksum_w;
printf ("\t\twriter (%03d): shared memory checksum %08lx\n", num_w,
cksum_w);
return NULL;
}
void *reader (void *parm)
{
int num_p = (int) (long)parm;
unsigned long cksum_r = 0;
int i;
int num_r;
int num_w;
char * ptr;
unsigned long *ulptr_r;
num_r=num_p % num_readers;
num_w=num_p - num_r;
num_w=num_w / num_readers;
ptr=shmptr[num_w];
ulptr_r=checksum[num_w];
if (pthread_mutex_lock (&cond_mutex[num_w]))
sys_error ("Can't take cond lock", __LINE__);
while (thread_hold[num_w])
{
if (pthread_cond_wait (&cond_var[num_w], &cond_mutex[num_w]))
sys_error ("cond_wait failed", __LINE__);
}
if (pthread_mutex_unlock (&cond_mutex[num_w]))
sys_error ("Can't release cond lock", __LINE__);
if (pthread_mutex_lock(&mutex_r[num_w]))
sys_error ("Can't take read lock", __LINE__);
(*(read_count [num_w]))++;
if (pthread_mutex_unlock(&mutex_r[num_w]))
sys_error ("Can't release read lock", __LINE__);
for (i=0; i<buffer_size; i++)
cksum_r += *ptr++;
if (pthread_mutex_lock(&mutex_r[num_w]))
sys_error ("Can't take read lock", __LINE__);
(*(read_count[num_w]))--;
if (pthread_mutex_unlock(&mutex_r[num_w]))
sys_error ("Can't release 1 read lock", __LINE__);
*ulptr_r = cksum_r;
printf ("\t\treader (%03d) of writer (%03d): checksum %08lx\n", num_r,
num_w, cksum_r);
return NULL;
}
void parse_args (int argc, char **argv)
{
int i;
int errflag = 0;
char *program_name = *argv;
extern char *optarg;
while ((i = getopt(argc, argv, "r:s:w:?")) != EOF) {
switch (i) {
case 'r':
num_readers = atoi (optarg);
break;
case 's':
buffer_size = atoi (optarg);
break;
case 'w':
num_writers = atoi (optarg);
break;
case '?':
errflag++;
break;
}
}
if (num_writers >= MAX_WRITER_NUMBER) {
errflag++;
fprintf (stderr, "ERROR: num_writers must be less than %d\n",
MAX_WRITER_NUMBER);
}
if (num_readers >= MAX_READER_NUMBER) {
errflag++;
fprintf (stderr, "ERROR: num_readers must be less than %d\n",
MAX_READER_NUMBER);
}
i=num_readers*num_writers;
if (i >= MAX_THREAD_NUMBER) {
errflag++;
fprintf (stderr, "ERROR: maximun threads number must be less
than %d\n", MAX_THREAD_NUMBER);
}
if (errflag) {
fprintf (stderr, USAGE, program_name);
exit (2);
}
}
void release ()
{
int i;
int j;
for (i=0; i<num_writers; i++) {
if (pthread_mutex_destroy(&cond_mutex[i]) != 0)
sys_error ("Can't destroy cond_mutex", __LINE__);
if (pthread_mutex_destroy(&mutex_r[i]) != 0)
sys_error ("Can't destroy mutex_r", __LINE__);
}
for (i=0; i<num_writers; i++) {
j=i*3;
if (shmctl (shmid[j], IPC_RMID, 0) < 0)
sys_error ("read_count shmctl failed", __LINE__);
j++;
if (shmctl (shmid[j], IPC_RMID, 0) < 0)
sys_error ("checksum shmctl failed", __LINE__);
j++;
if (shmctl (shmid[j], IPC_RMID, 0) < 0)
sys_error ("shmptr shmctl failed", __LINE__);
}
}
void sys_error (const char *msg, int line)
{
char syserr_msg [256];
sprintf (syserr_msg, "%s: %s\n", msg, strerror (errno));
error (syserr_msg, line);
}
void error (const char *msg, int line)
{
fprintf (stderr, "ERROR [line: %d] %s\n", line, msg);
if ( line >= 260 )
release ();
exit (-1);
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list