aboutsummaryrefslogtreecommitdiff
path: root/sync/writer.c
blob: b1ab9edf2d350f2a2a3c3cdea2c20178982a96f3 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/select.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

char *mem;

void quit(signum)
int signum;
{
	shmdt(mem);
	exit(1);
}

int main(argc, argv)
int argc;
char **argv;
{
printf("\tW STARTING\n");
	int shmid, i, pid, id;
	char *mem;

	if (argc < 2) {
		printf("usage: writer [id]\n");
		exit(1);
	}

	id = atoi(argv[1]);

printf("\tW%d. started...\n", id);

	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);
	}

	signal(SIGQUIT, quit);
printf("\tW%d. SHMID: %d\n", id, shmid);
	
	srand(time(NULL));

	while (1) {
printf("\tW%d. testing...\n", id);
		rand() % id;
printf("\tW%d. writing...\n", id);
		for (i = 0; i < 1<<14; i++) {
			mem[i]= 0x30 + id;
		}
printf("\tW%d. waiting...\n");
		sleep(rand() % ((id * 2) + 1));
	}
}