Hi,
For some reason, I can't get a thread that waits on a condition to start when I call notify from another thread. They are separate processes, actually, and I create a shared memory segment between them.
Here's the process that waits.
Code:
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include<pthread.h>
#include <errno.h>
#include<stdio.h>
#define NUMSEG 5
#define PROJID 1235
struct _SYNC
{
int busy[NUMSEG];
int dataready[NUMSEG];
int dataend[NUMSEG];
//pthread_mutex_t lock[NUMSEG];
//pthread_cond_t dready[NUMSEG];
pthread_mutex_t * lock;
pthread_cond_t *dready;
}*syncvar;
typedef struct _SYNC SYNC;
int main(int argv, char *argc[])
{
key_t key;
int shmid;
if ((key = ftok("proxy", PROJID)) == -1) {
perror("ftok");
exit(1);
}
if ((shmid = shmget(key, sizeof(SYNC), IPC_CREAT | 0666)) == -1) {
perror("shmget");
exit(1);
}
syncvar = (SYNC *) shmat(shmid, (void *)0, 0);
if (syncvar == (SYNC *)(-1)) {
perror("shmat");
exit(1);
}
syncvar->lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)*NUMSEG);
syncvar->dready = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)*NUMSEG);
pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
pthread_mutexattr_t m_attr;
pthread_mutexattr_init(&m_attr);
pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED);
int i;
for(i=0;i<NUMSEG;i++)
{
//pthread_mutex_destroy(&(syncvar->lock[i]));
//pthread_cond_destroy(&(syncvar->dready[i]));
pthread_mutex_init(&(syncvar->lock[i]),&m_attr);
pthread_cond_init(&(syncvar->dready[i]),&cond_attr);
}
pthread_mutex_lock(&(syncvar->lock[0]));
syncvar->busy[0] = 5;
//syncvar->busy[1] = 6;
//syncvar->dready[0]=5;
printf("%d\n",syncvar->busy[2]);
pthread_cond_wait(&(syncvar->dready[0]),&(syncvar->lock[0]));
printf("%d\n",syncvar->busy[1]);
pthread_mutex_unlock(&(syncvar->lock[0]));
/* if (shmdt(syncvar) == -1) {
perror("shmdt");
exit(1);
}*/
printf("I have successfully got out of wait loop\n");
shmdt(syncvar);
shmctl(shmid, IPC_RMID, NULL);
/* pthread_t threads[NUMTHREADS];
int rc;
for(int threadid=0;threadid<NUMTHREADS;threadid++)
{
rc=pthread_create(&threads[threadid],NULL,syncplay,(void *)threadid);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(int i=0;i<NUMTHREADS;i++)
{
rc=pthread_join(threads[i],NULL);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
*/
}
Here is the process that signals
Code:
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include<pthread.h>
#include <errno.h>
#include<stdio.h>
#define NUMSEG 5
#define PROJID 1235
struct _SYNC
{
int busy[NUMSEG];
int dataready[NUMSEG];
int dataend[NUMSEG];
//pthread_mutex_t lock[NUMSEG];
//pthread_cond_t dready[NUMSEG];
pthread_mutex_t * lock;
pthread_cond_t * dready;
}*syncvar;
typedef struct _SYNC SYNC;
int main(int argv, char *argc[])
{
key_t key;
int shmid;
if ((key = ftok("proxy", PROJID)) == -1) {
perror("ftok");
exit(1);
}
if ((shmid = shmget(key, sizeof(SYNC), 0666)) == -1) {
perror("shmget");
exit(1);
}
syncvar = (SYNC *) shmat(shmid, (void *)0, 0);
if (syncvar == (SYNC *)(-1)) {
perror("shmat");
exit(1);
}
//syncvar->lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)*NUMSEG);
//syncvar->dready = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)*NUMSEG);
pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
pthread_mutexattr_t m_attr;
pthread_mutexattr_init(&m_attr);
pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED);
int i;
for(i=0;i<NUMSEG;i++)
{
pthread_mutex_init(&(syncvar->lock[i]),&m_attr);
pthread_cond_init(&(syncvar->dready[i]),&cond_attr);
}
pthread_mutex_lock(&(syncvar->lock[0]));
printf("%d\n",syncvar->busy[0]);
// printf("%d\n",syncvar->busy[1]);
//syncvar->busy[2]=7;
while(syncvar->busy[0] != 5)
{
printf("ready to signal\n");
pthread_cond_signal(&(syncvar->dready[0]));
syncvar->busy[1]=8;
printf("signalled\n");
}
pthread_mutex_unlock(&(syncvar->lock[0]));
printf("I have successfully signalled\n");
/* if (shmdt(syncvar) == -1) {
perror("shmdt");
exit(1);
}*/
shmdt(syncvar);
shmctl(shmid, IPC_RMID, NULL);
/* pthread_t threads[NUMTHREADS];
int rc;
for(int threadid=0;threadid<NUMTHREADS;threadid++)
{
rc=pthread_create(&threads[threadid],NULL,syncplay,(void *)threadid);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(int i=0;i<NUMTHREADS;i++)
{
rc=pthread_join(threads[i],NULL);
if (rc) {
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
*/
}
I know the shared memory works because "5" is written by one process and read by another.
I am at a loss.
Here is how I compile
Code:
gcc -o shmem1 shmsample1.c -lpthread
gcc -o shmem shmsample.c -lpthread
run shmem first in one terminal, then in another, shmem1
also create a file "proxy"... it can just be anything in there..
Both codes init the condition and the attrs... even only making the "waiting" process init, it's still deadlocked....
Any ideas?