summaryrefslogtreecommitdiff
path: root/schulung_tools/ipc_shm/recv.c
blob: 8a083381055ebe976608617099357b45f0aba3ff (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
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include "shm.h"

int main(void)
{
	struct share_data *data;
	int rv;

	if (create_share() != 0)
		return 1;

	data = get_share();
	if (!data)
		return 1;

	init_share(data);

	if (pthread_mutex_lock(&data->m) != 0)
		return 1;

	printf("recv (%d): waiting for signal\n", getpid());

	do {
		rv = pthread_cond_wait(&data->c, &data->m);
		if (rv != 0) {
			if (rv == EOWNERDEAD) {
				printf("recv(%d): recover mutex\n", getpid());
				data->msg[0] = 0;
				pthread_mutex_consistent(&data->m);
			} else {
				/* maybe rv == ENOTRECOVERABLE */
				return 1;
			}
		}
	} while (rv != 0);

	/* set "received" uprobe here */

	printf("recv (%d): received signal: %s\n", getpid(), data->msg);
	pthread_mutex_unlock(&data->m);

	remove_share();

	return 0;
}