process_shared 0.2.0 → 0.2.1a

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,67 +0,0 @@
1
- require 'mach'
2
- require 'mach/functions'
3
-
4
- include Mach
5
- include Mach::Functions
6
-
7
- sem = Semaphore.new(:value => 0)
8
-
9
- # puts ">parent has sem #{sem.port}"
10
-
11
- # fork do
12
- # sleep 2 # make parent wait a bit
13
- # puts "in child..."
14
- # sem = Mach::Semaphore.new(:port => Mach::Task.self.get_bootstrap_port)
15
- # puts "child signaling sem #{sem.port}"
16
- # sem.signal
17
- # end
18
-
19
- # puts ">parent waiting on sem..."
20
- # sem.wait
21
- # puts ">parent done waiting!"
22
-
23
- def struct(*layout)
24
- yield FFI::Struct.new(nil, *layout)
25
- end
26
-
27
- puts "sizeof MsgPortDescriptor: #{MsgPortDescriptor.size}"
28
- port = Port.new
29
- port.insert_right(:make_send)
30
-
31
- Task.self.set_bootstrap_port(port)
32
- puts "> self:#{mach_task_self} bootstrap:#{port.port} (#{Mach::Functions::bootstrap_port.to_i})"
33
-
34
- child = fork do
35
- parent_port = Task.self.get_bootstrap_port
36
- puts "in child... self:#{mach_task_self} bootstrap:#{parent_port.port}"
37
-
38
- #port = Port.new
39
- #port.copy_send(parent_port)
40
-
41
- #sem = port.receive_right
42
-
43
- Task.self.copy_send(parent_port)
44
- puts "child sleeping"
45
- sleep 2
46
- puts "child signaling semaphore"
47
- sem.signal
48
- puts "child out"
49
- sleep 2
50
- end
51
-
52
- sleep 0.1
53
- if Process.wait(child, Process::WNOHANG)
54
- puts "child died!"
55
- #exit 1
56
- end
57
-
58
- Task.self.set_bootstrap_port(Mach::Functions::bootstrap_port)
59
- child_task_port = port.receive_right
60
- puts "parent: child task port is #{child_task_port}"
61
-
62
- sem.insert_right(:copy_send, :ipc_space => child_task_port)
63
-
64
- puts "parent waiting"
65
- sem.wait
66
- puts "parent done waiting!"
67
-
@@ -1,78 +0,0 @@
1
- require 'ffi'
2
-
3
- require 'mach'
4
- require 'mach/functions'
5
-
6
- include Mach
7
- include Mach::Functions
8
-
9
- def setup_recv_port
10
- port = new_memory_pointer :mach_port_t
11
- mach_port_allocate(mach_task_self, :receive, port)
12
- p = port.get_uint(0)
13
- mach_port_insert_right(mach_task_self, p, p, :make_send)
14
- p
15
- end
16
-
17
- def send_port(remote_port, port)
18
- puts "send_port: (in #{mach_task_self}) sending #{port} -> #{remote_port}"
19
- msg = FFI::Struct.new(nil,
20
- :header, MsgHeader,
21
- :body, MsgBody,
22
- :task_port, MsgPortDescriptor)
23
- msg[:header].tap do |h|
24
- h[:remote_port] = remote_port
25
- h[:local_port] = 0
26
- h[:bits] = (MachMsgType[:copy_send] | (0 << 8)) | 0x80000000 # MACH_MSGH_BITS_COMPLEX
27
- h[:size] = msg.size
28
- end
29
-
30
- msg[:body][:descriptor_count] = 1
31
-
32
- msg[:task_port].tap do |p|
33
- p[:name] = port
34
- p[:disposition] = MachMsgType[:copy_send]
35
- p[:type] = 0 # MACH_MSG_PORT_DESCRIPTOR;
36
- end
37
-
38
- mach_msg_send(msg)
39
- end
40
-
41
- def recv_port(recv_port)
42
- msg = FFI::Struct.new(nil,
43
- :header, MsgHeader,
44
- :body, MsgBody,
45
- :task_port, MsgPortDescriptor,
46
- :trailer, MsgTrailer)
47
-
48
- mach_msg(msg, 2, 0, msg.size, recv_port, 0, 0)
49
-
50
- msg.size.times do |i|
51
- print "%02x " % msg.to_ptr.get_uint8(i)
52
- end
53
- puts
54
-
55
- msg[:task_port][:name]
56
- end
57
-
58
- def sampling_fork
59
- parent_recv_port = setup_recv_port
60
- task_set_special_port(mach_task_self, :bootstrap, parent_recv_port)
61
-
62
- fork do
63
- parent_recv_port_p = new_memory_pointer :mach_port_t
64
- task_get_special_port(mach_task_self, :bootstrap, parent_recv_port_p)
65
- parent_recv_port = parent_recv_port_p.get_uint(0)
66
- puts "child self:#{mach_task_self} parent_recv:#{parent_recv_port}"
67
-
68
- child_recv_port = setup_recv_port
69
- puts "child sending #{mach_task_self}"
70
- send_port(parent_recv_port, mach_task_self)
71
- end
72
-
73
- task_set_special_port(mach_task_self, :bootstrap, Mach::Functions::bootstrap_port)
74
- child_task = recv_port(parent_recv_port)
75
- puts "parent received #{child_task}"
76
- end
77
-
78
- sampling_fork