Professional Documents
Culture Documents
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define BRIDGE_DIRECTION_NORTH 0x01
#define BRIDGE_DIRECTION_SOUTH 0x02
pthread_mutex_unlock(&bridge->mutex);
}
static void leave(bridge_t *bridge)
{
pthread_mutex_lock(&bridge->mutex);
bridge->cars--;
pthread_cond_signal(&bridge->empty);
pthread_mutex_unlock(&bridge->mutex);
}
static void drive(bridge_t *bridge, int direction)
{
arrive(bridge, direction);
leave(bridge);
}
static void* north(void *data)
{
drive((bridge_t *) data, BRIDGE_DIRECTION_NORTH);
return NULL;
}
static void* south(void *data)
{
drive((bridge_t *) data, BRIDGE_DIRECTION_SOUTH);
return NULL;
}
static int run(int ns, int nn)
{
int i, n = ns + nn;
pthread_t thread[n];
for (i = 0; i < n; i++)
if (pthread_create(&thread[i], NULL, i < ns ? north : south, &shared_bridge))
{
printf("thread creation failed\n");
return EXIT_FAILURE;
}
for (i = 0; i < n; i++)
if (thread[i])
pthread_join(thread[i], NULL);
return EXIT_SUCCESS;
}
int main(int argc, char **argv)
{
int c, ns = 0, nn = 0;
printf("Enter the South and North cars:");
scanf("%d %d",&ns,&nn);
if (nn <= 0){
printf("number of cars going north must be > 0\n");
exit(EXIT_FAILURE);
}
else if (ns <= 0) {
printf("number of cars going south must be > 0\n");
exit(EXIT_FAILURE);
}
return run(ns, nn);
}
THANK YOU