Download c source code

  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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
main:
	shared agent_sem := semaphore(1)
	shared match := semaphore(0)
	shared paper := semaphore(0)
	shared tobacco := semaphore(0)

	shared mutex := semaphore(1)
	shared is_match := false
	shared is_paper := false
	shared is_tobacco := false

	shared match_ready := semaphore(0)
	shared paper_ready := semaphore(0)
	shared tobacco_ready := semaphore(0)

	create_thread(agent)
	create_thread(smoker_with_matches)
	create_thread(smoker_with_paper)
	create_thread(smoker_with_tobacco)

	create_thread(pusher_match)
	create_thread(pusher_paper)
	create_thread(pusher_tobacco)


agent:
	create_thread(agent_without_matches)
	create_thread(agent_without_paper)
	create_thread(agent_without_tobacco)

agent_without_matches:
	while true do
		wait(agent_sem)
		signal(paper)
		signal(tobacco)

agent_without_paper:
	while true do
		wait(agent_sem)
		signal(match)
		signal(tobacco)

agent_without_tobacco:
	while true do
		wait(agent_sem)
		signal(match)
		signal(paper)


smoker_with_matches:
	while true do
		wait(match_ready)
		make_cigarette()
		signal(agent_sem)
		smoke()

smoker_with_paper:
	while true do
		wait(paper_ready)
		make_cigarette()
		signal(agent_sem)
		smoke()

smoker_with_tobacco:
	while true do
		wait(tobacco_ready)
		make_cigarette()
		signal(agent_sem)
		smoke()


pusher_match:
	while true do
		wait(match)

		wait(mutex)
		if is_paper then
			is_paper := false
			signal(match_ready)
		else if is_tobacco then
			is_tobacco := false
			signal(tobacco_ready)
		else
			is_match := true
		signal(mutex)

pusher_paper:
	while true do
		wait(paper)

		wait(mutex)
		if is_match then
			is_match := false
			signal(paper_ready)
		else if is_tobacco then
			is_tobacco := false
			signal(tobacco_ready)
		else
			is_paper := true
		signal(mutex)

pusher_tobacco:
	while true do
		wait(tobacco)

		wait(mutex)
		if is_match then
			is_match := false
			signal(paper_ready)
		else if is_paper then
			is_paper := false
			signal(match_ready)
		else
			is_tobacco := true
		signal(mutex)