File: sem2.C

package info (click to toggle)
librudiments0 0.27-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,528 kB
  • ctags: 2,284
  • sloc: cpp: 14,657; sh: 7,547; ansic: 2,664; makefile: 945; xml: 15
file content (30 lines) | stat: -rw-r--r-- 769 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) 2001  David Muse
// See the file COPYING for more information

#include <sys/types.h>
#include <sys/ipc.h>
#include <rudiments/semaphoreset.h>
#include <rudiments/permissions.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, const char **argv) {

	// attach to the semaphore set keyed to /tmp/sem containing 2 semaphores
        semaphoreset	sem;
	sem.attach(ftok("/tmp/sem",0),2);

	// loop 10 times, printing 1 and 2, synchronizing with another process
	// using the semaphores
        for (int i=0; i<10; i++) {
                sem.wait(1);
                printf("1\n");
                sem.signal(0);
        
                sem.wait(1);
                printf("3\n");
                sem.signal(0);
        }

	unlink("/tmp/sem");
}