aboutsummaryrefslogtreecommitdiff
path: root/sync/writer.c
blob: 0a7a1fe581ae8c3c0df0851cdeb4c41baeb7c2c8 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <types.h>


int main(argc, argv)
int argc;
char **argv;
{
	int shmid, i, pid, id;
	char *mem;
	timeval *s;

	if (argc != 1) {
		printf("usage: reader [id]\n");
		exit(1);
	}

	id = atoi(argv[1]);

	if (argc < 2) {
		printf("usage: sync [number readers] [number writers]\n");
		exit(1);
	}

	if ((shmid = shmget(52, 1<<14, IPC_CREAT | 0666)) == -1){
		perror("shmget: shmget failed");
		exit(1);
	}

	if ((mem = shmat(shmid, NULL, 0)) == (char *) -1) {
		perror("shmat");
		exit(1);
	}
	
	srand(time(NULL));


	while (1) {
		s->tv_sec = rand() % (id * 2;
		for (i = 0; i < 1<<14; i++) {
			mem[i]= 0x30 + id;
			select(0, NULL, NULL, NULL, s);
		}
	}
}