The following code should simulate a port with M places and N ships accessing
to it.
The problem: in the "entering_request" function, the program controls how many
ships are in port, if there are too many ships the program should block on a
condition variable. The problem is that it does not work, and I usually get
more than M ships at runtime. I think that I am misunderstanding something,
anybody could enlighten me?
#include
#include
#include
#include
#define N 10
#define M 5
struct port
{
pthread_mutex_t mutex;
pthread_cond_t full;
int ships;
int waiting;
} Port = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 };
void* ship (void* arg);
int main()
{
void* nothing;
pthread_t ships[N];
int i;
pthread_setconcurrency(N);
for (i=0; i=M)
{
Port.waiting++;
printf("ship %d blocked\n", pthread_self());
pthread_cond_wait(&Port.full, &Port.mutex);
printf("ship %d unblocked\n", pthread_self());
Port.waiting--;
}
}
void entering()
{
printf ("ship %d enters the port", pthread_self());
printf ("\n%d ships in port\n", Port.ships);
}
void leaving_entrance()
{
Port.ships++;
printf("Ships inside the port: %d\n", Port.ships);
pthread_mutex_unlock(&Port.mutex);
}
void stationing ()
{
int l, i;
srand(time(NULL));
printf("La ship %d is inside with other %d ships.\n",
pthread_self(),
Port.ships);
l = rand();
/* Wasting time */
for (i=0; i