1 00:00:06,930 --> 00:00:09,933 - In this video we'll talk about interprocess communication. 2 00:00:11,130 --> 00:00:13,800 So how does that work? 3 00:00:13,800 --> 00:00:17,940 Well, to start with, there are system five IPCs 4 00:00:17,940 --> 00:00:20,490 and these are the old way to handle communication 5 00:00:20,490 --> 00:00:21,633 between processes. 6 00:00:22,470 --> 00:00:24,750 They were introduced with the introduction of Unix 7 00:00:24,750 --> 00:00:28,680 so that's really old, in modern Linux distributions, 8 00:00:28,680 --> 00:00:32,730 there are other methods as well, like remote procedure calls 9 00:00:32,730 --> 00:00:36,720 and the desktop bus, shortly known as D-Bus, 10 00:00:36,720 --> 00:00:39,240 which allows processes to communicate directly 11 00:00:39,240 --> 00:00:40,083 to one another. 12 00:00:41,060 --> 00:00:43,510 Let's first have a look at IPC. 13 00:00:43,510 --> 00:00:45,220 So interprocess communication 14 00:00:45,220 --> 00:00:48,600 or IPC is an old mechanism to enable communication 15 00:00:48,600 --> 00:00:51,270 between processes in user space 16 00:00:51,270 --> 00:00:55,170 and there are different IPC mechanisms like shared files. 17 00:00:55,170 --> 00:00:59,160 That is where two programs read and write to a shared file 18 00:00:59,160 --> 00:01:03,030 or shared memory, which semaphores, which is about values 19 00:01:03,030 --> 00:01:07,200 in memory that can be tested and set by multiple programs. 20 00:01:07,200 --> 00:01:10,860 Then there are the names and unnamed pipes where the output 21 00:01:10,860 --> 00:01:13,450 of one program is used as input by another. 22 00:01:13,450 --> 00:01:16,440 There are message queues where one process writes 23 00:01:16,440 --> 00:01:19,050 through the queue and multiple processes can access. 24 00:01:19,050 --> 00:01:21,420 There are sockets, which is a local 25 00:01:21,420 --> 00:01:22,620 or network port that is used 26 00:01:22,620 --> 00:01:25,740 for communication and there are signals. 27 00:01:25,740 --> 00:01:29,770 These are generated by the system to alert processes. 28 00:01:29,770 --> 00:01:32,973 All right, so how can we show IPC information? 29 00:01:33,870 --> 00:01:36,450 Well, there is the IPC's command for an overview 30 00:01:36,450 --> 00:01:39,093 of all IPCs that have currently been allocated. 31 00:01:40,260 --> 00:01:43,590 Now on most modern Linux distributions, this won't show much 32 00:01:43,590 --> 00:01:46,023 because IPCs are not very common anymore. 33 00:01:47,340 --> 00:01:49,770 You can use IPC's minus P to see names 34 00:01:49,770 --> 00:01:53,190 of processes which are using these system five IPCs 35 00:01:53,190 --> 00:01:55,350 or LSIPC to get a summary 36 00:01:55,350 --> 00:01:58,110 of usage of all IPC resource types. 37 00:01:58,110 --> 00:02:00,610 And then there is good old top which you can use 38 00:02:01,500 --> 00:02:03,890 for monitoring shared memory usage. 39 00:02:03,890 --> 00:02:05,103 Let's have a look. 40 00:02:08,250 --> 00:02:10,570 So if you type IPCs then this is what we get, 41 00:02:10,570 --> 00:02:15,570 nothing and IPC's minus P is showing the processes 42 00:02:18,390 --> 00:02:20,340 but again, nothing works. 43 00:02:20,340 --> 00:02:22,800 Now how about LSIPC? 44 00:02:22,800 --> 00:02:27,800 LSIPC is showing the different IPC resources 45 00:02:28,020 --> 00:02:31,700 that are available and the uses, as well as the limitation. 46 00:02:31,700 --> 00:02:35,490 And as you can see, nothing, nothing and nothing. 47 00:02:35,490 --> 00:02:40,490 As I said, IPCs are kind of old school, but IPCs are 48 00:02:40,980 --> 00:02:43,770 historically so important that at least you need to 49 00:02:43,770 --> 00:02:45,080 know a little bit about them. 50 00:02:45,080 --> 00:02:47,553 Let's have a look at the more modern approach. 51 00:02:49,410 --> 00:02:51,990 So for your convenience, the steps in this demo are 52 00:02:51,990 --> 00:02:54,153 on the slide, let me run them for you. 53 00:02:56,430 --> 00:02:59,760 So for this demo we need multiple terminals, 54 00:02:59,760 --> 00:03:00,870 from terminal one, 55 00:03:00,870 --> 00:03:04,153 I'm going to use mkfifo, myfifo, this is a named pipe 56 00:03:10,230 --> 00:03:13,620 and mkfifo will make it for me. 57 00:03:13,620 --> 00:03:18,620 L is minus L on my fifo is showing the pipe I just created. 58 00:03:19,800 --> 00:03:23,970 First character is a P, P for named pipe and cat, 59 00:03:23,970 --> 00:03:27,469 my fifo is showing nothing at all. 60 00:03:27,469 --> 00:03:31,323 All right, now I'm going to a new terminal and I need to 61 00:03:38,340 --> 00:03:40,320 make sure that I'm in the same directly so 62 00:03:40,320 --> 00:03:43,530 that I can see this my fifo and I am going to 63 00:03:43,530 --> 00:03:44,820 use a cat greater 64 00:03:44,820 --> 00:03:49,810 than my fifo and then I'm typing hello world 65 00:03:51,450 --> 00:03:53,883 And control C to quit my command. 66 00:03:55,050 --> 00:03:57,210 And now back to terminal number one. 67 00:03:57,210 --> 00:03:58,043 What do we see? 68 00:03:58,043 --> 00:04:00,720 Well we see that the hello world has been written 69 00:04:00,720 --> 00:04:05,610 to the myfifo, so that's how this name pipe is working. 70 00:04:05,610 --> 00:04:07,050 LS minus L is showing 71 00:04:07,050 --> 00:04:11,430 that myfifo still exists as a named pipe. 72 00:04:11,430 --> 00:04:14,553 And if you use unlink on myfifo, 73 00:04:16,770 --> 00:04:19,610 then you can see that it doesn't exist anymore. 74 00:04:19,610 --> 00:04:21,139 Unlink is the elegant way. 75 00:04:21,139 --> 00:04:23,133 It's not really removing it, 76 00:04:23,980 --> 00:04:26,070 it's unlinking the named pipe 77 00:04:26,070 --> 00:04:28,383 from the current execution environment. 78 00:04:30,810 --> 00:04:33,273 The last thing I wanna talk about is D-Bus. 79 00:04:34,140 --> 00:04:35,370 So what is D-Bus? 80 00:04:35,370 --> 00:04:38,310 D-Bus is a modern way for IPC 81 00:04:38,310 --> 00:04:40,020 which originates from the GNOME 82 00:04:40,020 --> 00:04:42,063 and KDE desktop environments. 83 00:04:42,960 --> 00:04:45,000 It provides a software bus abstraction 84 00:04:45,000 --> 00:04:47,160 to which all processes connect. 85 00:04:47,160 --> 00:04:50,070 And D-Bus provides one virtual channel which 86 00:04:50,070 --> 00:04:51,840 ensures that point to point relations 87 00:04:51,840 --> 00:04:53,980 between processes are no longer required 88 00:04:54,870 --> 00:04:59,520 or most systems, multiple buses are used simultaneously. 89 00:04:59,520 --> 00:05:00,660 So to work with D-Bus, 90 00:05:00,660 --> 00:05:03,090 the application needs to be D-Bus aware. 91 00:05:03,090 --> 00:05:05,300 It doesn't work for all applications. 92 00:05:05,300 --> 00:05:06,720 And again that is 93 00:05:06,720 --> 00:05:10,890 because D-Bus has a background of graphical desktops. 94 00:05:10,890 --> 00:05:14,220 So D-Bus aware applications expose uniform interfaces 95 00:05:14,220 --> 00:05:17,503 that are provided by object paths and code for 96 00:05:17,503 --> 00:05:21,333 a DBUs aware application can be written in many languages. 97 00:05:22,200 --> 00:05:24,510 And you can use the D-Bus send commands 98 00:05:24,510 --> 00:05:28,510 from the Linux CLI to explore what D-Bus is doing 99 00:05:29,730 --> 00:05:32,850 and that results in commands like this. 100 00:05:32,850 --> 00:05:34,290 So what do we see here? 101 00:05:34,290 --> 00:05:36,300 Well we see D-Bus send 102 00:05:36,300 --> 00:05:40,080 and this D-Bus send is addressing a specific destination 103 00:05:40,080 --> 00:05:42,930 which is the network manager 104 00:05:42,930 --> 00:05:47,930 and then it is going to use properties.set. to set a string 105 00:05:48,570 --> 00:05:49,830 on the network manager. 106 00:05:49,830 --> 00:05:51,990 And the string is networking enabled 107 00:05:51,990 --> 00:05:56,190 and the variant boolean false, that is going to 108 00:05:56,190 --> 00:05:59,793 disable networking on the network manager. 109 00:06:00,810 --> 00:06:02,550 So how do we manage D-Bus? 110 00:06:02,550 --> 00:06:05,970 Well typically you don't because D-Bus is normally managed 111 00:06:05,970 --> 00:06:09,540 by using API calls from your applications or scripts. 112 00:06:09,540 --> 00:06:11,910 But there is a few commands that you can use from the shell 113 00:06:11,910 --> 00:06:15,870 and let give a bit of insight like busctl list, 114 00:06:15,870 --> 00:06:19,500 which lists the bus names or D-Bus send 115 00:06:19,500 --> 00:06:23,170 which allows you to send commands to specific object paths. 116 00:06:23,170 --> 00:06:24,303 Let me show you. 117 00:06:26,430 --> 00:06:30,000 So busctl list is showing all the different 118 00:06:30,000 --> 00:06:33,038 D-Bus that are available and as you can see, 119 00:06:33,038 --> 00:06:36,030 there's quite a couple of them. 120 00:06:36,030 --> 00:06:39,780 Now I would like to to show you some commands 121 00:06:39,780 --> 00:06:42,330 but these commands as we have seen are pretty tough 122 00:06:42,330 --> 00:06:44,910 and that is why I created a file 123 00:06:44,910 --> 00:06:49,773 with the name D-Bus commands.sh, which is, 124 00:06:50,730 --> 00:06:54,829 which contains a couple of example commands. 125 00:06:54,829 --> 00:06:56,290 Now this is an interesting one. 126 00:06:56,290 --> 00:06:58,840 All of these commands are own network manager 127 00:06:59,700 --> 00:07:01,270 so you can try all of them 128 00:07:01,270 --> 00:07:03,120 and I'm just going to learn one of them. 129 00:07:03,120 --> 00:07:06,603 So D-Bus send will list active connections. 130 00:07:07,440 --> 00:07:11,000 So we see that it as properties get, 131 00:07:11,000 --> 00:07:12,900 that's a part you need to understand. 132 00:07:12,900 --> 00:07:14,520 So we get properties, 133 00:07:14,520 --> 00:07:18,150 on this specific naming attribute that is known to D-Bus 134 00:07:18,150 --> 00:07:22,680 and the properties are obtained from the network manager 135 00:07:22,680 --> 00:07:26,853 and we are looking for string value on active connections. 136 00:07:28,110 --> 00:07:33,110 So if I copy all of this then we can see what it is doing. 137 00:07:36,010 --> 00:07:38,580 Now as you can see, these are not really 138 00:07:38,580 --> 00:07:42,180 the commands that you are easily going to compose yourself 139 00:07:42,180 --> 00:07:46,770 and that is because that was never the meaning of D-Bus. 140 00:07:46,770 --> 00:07:49,500 But what we see here is active connections 141 00:07:49,500 --> 00:07:52,530 and the variant is an array and active connection, 142 00:07:52,530 --> 00:07:54,660 active connection number four. 143 00:07:54,660 --> 00:07:57,540 So what can we do with this active connection 144 00:07:57,540 --> 00:08:01,983 number four, well we can run the next command. 145 00:08:03,360 --> 00:08:08,360 So in this D-Bus sent, I probably changed 146 00:08:08,790 --> 00:08:12,180 that in the file itself before copying it. 147 00:08:12,180 --> 00:08:15,303 So Vim on D-Bus command.sh. 148 00:08:16,140 --> 00:08:20,260 I'm looking for connection number four 149 00:08:21,200 --> 00:08:24,660 in the example desk connection number six. 150 00:08:24,660 --> 00:08:28,260 And now I can copy all of this 151 00:08:28,260 --> 00:08:32,703 so that we can ask D-Bus to send us the devices. 152 00:08:40,380 --> 00:08:41,213 And there we go. 153 00:08:41,213 --> 00:08:43,740 And here we can see the devices, which again is number. 154 00:08:43,740 --> 00:08:45,990 Well I would say I'm going to stop here 155 00:08:45,990 --> 00:08:50,990 have a look at this, this example D-Bus commands.sh file. 156 00:08:51,930 --> 00:08:54,840 Play with it if you're interested in how it works. 157 00:08:54,840 --> 00:08:57,480 But don't forget, this is nice to know, this is not 158 00:08:57,480 --> 00:09:00,330 something that you will typically do as an administrator.