Skip to content

Instantly share code, notes, and snippets.

@sposterkil
Created March 21, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sposterkil/6ad2609d45531c5114e9 to your computer and use it in GitHub Desktop.
Save sposterkil/6ad2609d45531c5114e9 to your computer and use it in GitHub Desktop.
Counting EX
void eastbound_car() {
printf("Eastbound Car (PID: %d) spawned\n", getpid());
// Get the Semaphore set
int sem_set_id = semget(SEM_KEY, NUM_SEMS, 0777);
// Get the Shared Memory
struct common *my_shared;
int shm_id = shmget(SHM_KEY, 0, 0);
my_shared = (struct common *)shmat(shm_id, 0, 0);
printf("Cars crossed: %d\n", my_shared->xed_count);
printf("Bridge Direction: %d\n", my_shared->bridge_dir);
...
}
output:
Eastbound Car (PID: 42188) spawned
Cars crossed: 0
Bridge Direction: 0
Eastbound Car (PID: 42188) is crossing the bridge
Eastbound Car (PID: 42188) spawned
Cars crossed: 1
Bridge Direction: 1
Eastbound Car (PID: 42188) is crossing the bridge
Westbound Car (PID: 42188) spawned
Cars crossed: 2
Bridge Direction: 1
Westbound Car (PID: 42188) is crossing the bridge
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment