WEBVTT

1
00:00:00.160 --> 00:00:04.559
<v Speaker 1>Welcome to our deep dive into the world of Linux

2
00:00:04.559 --> 00:00:05.400
<v Speaker 1>system programming.

3
00:00:05.480 --> 00:00:09.160
<v Speaker 2>Yeah, we're going to be exploring the core the Linux

4
00:00:09.199 --> 00:00:12.919
<v Speaker 2>operating system. Okay, using this book is our guide. O'Reilly

5
00:00:13.000 --> 00:00:16.440
<v Speaker 2>dot Linux dot system dot Programming, dot second edition dot

6
00:00:16.480 --> 00:00:18.160
<v Speaker 2>pdf as our guide.

7
00:00:18.239 --> 00:00:20.800
<v Speaker 1>So we're really getting down into the nuts and.

8
00:00:20.760 --> 00:00:23.920
<v Speaker 2>Bolts we are. We're going to be uncovering any gritty Yeah,

9
00:00:24.239 --> 00:00:25.719
<v Speaker 2>how things work at the lowest level.

10
00:00:25.879 --> 00:00:26.320
<v Speaker 1>Awesome.

11
00:00:27.120 --> 00:00:29.800
<v Speaker 2>And you know, it's not just about code, it's about understanding,

12
00:00:29.839 --> 00:00:33.359
<v Speaker 2>like those fundamental building blocks that make Linux tick. Okay,

13
00:00:33.439 --> 00:00:36.560
<v Speaker 2>So think system calls. Yeah, those are the direct lines

14
00:00:36.600 --> 00:00:38.359
<v Speaker 2>to the heart of the OS, to the kernel, to

15
00:00:38.399 --> 00:00:41.039
<v Speaker 2>the kernel exactly, okay, and the logic that drives it all.

16
00:00:41.119 --> 00:00:43.600
<v Speaker 1>So we're not just driving the car. We're actually seeing

17
00:00:43.640 --> 00:00:45.119
<v Speaker 1>how the engine works exactly.

18
00:00:45.200 --> 00:00:47.759
<v Speaker 2>And here's a fun fact for you to kick things off.

19
00:00:47.799 --> 00:00:48.280
<v Speaker 2>Are you ready?

20
00:00:48.320 --> 00:00:48.679
<v Speaker 1>I'm ready?

21
00:00:48.759 --> 00:00:52.759
<v Speaker 2>Okay. Linux actually uses way fewer system calls than something

22
00:00:52.799 --> 00:00:53.399
<v Speaker 2>like Windows.

23
00:00:53.719 --> 00:00:54.079
<v Speaker 1>Really.

24
00:00:54.719 --> 00:00:57.799
<v Speaker 2>Yeah, we're talking hundreds versus potentially thousand.

25
00:00:58.000 --> 00:01:01.200
<v Speaker 1>Wow. I would have thought a complex system like Linux

26
00:01:01.240 --> 00:01:04.480
<v Speaker 1>would need way more ways to talk to its kernel.

27
00:01:04.560 --> 00:01:06.280
<v Speaker 2>That's where the elegance of Linux comes in.

28
00:01:06.439 --> 00:01:06.879
<v Speaker 1>Okay.

29
00:01:07.000 --> 00:01:11.000
<v Speaker 2>It achieves a lot with a surprisingly streamlined set of tools.

30
00:01:11.640 --> 00:01:12.439
<v Speaker 1>That's pretty cool.

31
00:01:12.519 --> 00:01:12.799
<v Speaker 2>Yeah.

32
00:01:12.879 --> 00:01:15.599
<v Speaker 1>So before we get too deep into the weeds, though,

33
00:01:16.120 --> 00:01:18.920
<v Speaker 1>let's take a step back and defind something. Okay, what

34
00:01:19.079 --> 00:01:20.840
<v Speaker 1>exactly is system programming?

35
00:01:21.040 --> 00:01:22.239
<v Speaker 2>Oh that's a great question.

36
00:01:22.840 --> 00:01:25.040
<v Speaker 1>When I think about using a computer, I don't think

37
00:01:25.040 --> 00:01:26.239
<v Speaker 1>about this level.

38
00:01:26.239 --> 00:01:28.640
<v Speaker 2>Right, You're not really meant to You're using all the

39
00:01:28.719 --> 00:01:31.840
<v Speaker 2>nice applications and things, exactly. The system programming is kind

40
00:01:31.879 --> 00:01:34.879
<v Speaker 2>of like, think of it like this. It's like being

41
00:01:34.879 --> 00:01:36.120
<v Speaker 2>the architect of a building.

42
00:01:36.719 --> 00:01:37.120
<v Speaker 1>Okay.

43
00:01:37.200 --> 00:01:39.680
<v Speaker 2>So you're working with the raw materials, the foundation, the

44
00:01:39.719 --> 00:01:41.359
<v Speaker 2>structural framework.

45
00:01:41.000 --> 00:01:43.359
<v Speaker 1>Not just like decorating the rooms exactly.

46
00:01:43.439 --> 00:01:45.159
<v Speaker 2>Yeah, you're building the load bearing walls.

47
00:01:45.359 --> 00:01:47.159
<v Speaker 1>Ooh, I like that metaphor.

48
00:01:47.239 --> 00:01:51.120
<v Speaker 2>Yeah. So we're writing software that interacts directly with the

49
00:01:51.159 --> 00:01:55.560
<v Speaker 2>core of the operating system, the kernel. Okay, and it's

50
00:01:55.799 --> 00:01:59.959
<v Speaker 2>closest partners okay, like the C library known as glib online.

51
00:02:00.840 --> 00:02:04.439
<v Speaker 2>It's the bedrock upon which all those user friendly applications

52
00:02:04.480 --> 00:02:04.879
<v Speaker 2>are built.

53
00:02:05.079 --> 00:02:07.359
<v Speaker 1>So if I'm writing a game or a web browser,

54
00:02:07.359 --> 00:02:11.159
<v Speaker 1>that's like decorating the rooms. But system programming is making

55
00:02:11.199 --> 00:02:11.719
<v Speaker 1>the rooms.

56
00:02:12.280 --> 00:02:13.280
<v Speaker 2>That's a great way to put it.

57
00:02:13.319 --> 00:02:14.599
<v Speaker 1>Okay, I like it. I'm with you.

58
00:02:15.039 --> 00:02:18.000
<v Speaker 2>And just like a good architect needs the right tools, right,

59
00:02:18.080 --> 00:02:21.159
<v Speaker 2>absolutely so do system programmers of course. So the g

60
00:02:21.240 --> 00:02:24.000
<v Speaker 2>and UC compiler okay, where GCC is one of our

61
00:02:24.039 --> 00:02:25.159
<v Speaker 2>main tools in this world.

62
00:02:25.240 --> 00:02:27.319
<v Speaker 1>So that's the blueprint and our hammer.

63
00:02:27.560 --> 00:02:29.240
<v Speaker 2>Yes, like that our tool belt.

64
00:02:29.319 --> 00:02:32.680
<v Speaker 1>And let's not forget about system calls. Ooh, yes, these

65
00:02:32.680 --> 00:02:36.120
<v Speaker 1>are like the language we use to communicate with the kernel. Okay,

66
00:02:36.479 --> 00:02:39.479
<v Speaker 1>So need to open a file, yep, send data over

67
00:02:39.520 --> 00:02:42.840
<v Speaker 1>the network, even just allocate some memory. It all starts

68
00:02:42.840 --> 00:02:45.639
<v Speaker 1>with a system call, got it. So you're directly asking

69
00:02:45.639 --> 00:02:47.080
<v Speaker 1>the kernel to do something for you.

70
00:02:47.400 --> 00:02:51.599
<v Speaker 2>Those are those direct lines of communication we talked about exactly.

71
00:02:51.919 --> 00:02:54.240
<v Speaker 1>And the book also spends a lot of time talking

72
00:02:54.240 --> 00:02:54.960
<v Speaker 1>about files.

73
00:02:55.080 --> 00:02:55.680
<v Speaker 2>Yes it does.

74
00:02:55.960 --> 00:02:59.039
<v Speaker 1>Why are files so central to Linux? Yeah? I was

75
00:02:59.080 --> 00:03:00.960
<v Speaker 1>wondering that too, because I know, I know we have them.

76
00:03:00.800 --> 00:03:03.800
<v Speaker 2>But well this might surprise you, but in Linux, pretty

77
00:03:03.840 --> 00:03:05.599
<v Speaker 2>much everything is represented as a file.

78
00:03:05.879 --> 00:03:08.000
<v Speaker 1>Wait a minute, what everything is a file?

79
00:03:08.120 --> 00:03:11.319
<v Speaker 2>We're talking devices, directories, even your terminal window.

80
00:03:11.360 --> 00:03:14.439
<v Speaker 1>Hold on, So my keyboard is a file. Yep, my

81
00:03:14.560 --> 00:03:15.639
<v Speaker 1>hard drive is a file.

82
00:03:16.199 --> 00:03:16.599
<v Speaker 2>It is.

83
00:03:16.759 --> 00:03:18.599
<v Speaker 1>That's kind of blowing my mind a little bit.

84
00:03:18.639 --> 00:03:21.719
<v Speaker 2>It is. It's a beautifully unifying concept it is.

85
00:03:21.879 --> 00:03:24.639
<v Speaker 1>But how does the kernel keep track of all of that.

86
00:03:24.639 --> 00:03:26.759
<v Speaker 2>That's where inodes and file descriptors come in.

87
00:03:26.919 --> 00:03:29.319
<v Speaker 1>Okay, I'm intrigued, tell me more.

88
00:03:29.439 --> 00:03:32.919
<v Speaker 2>So. Think of an inode like a file's unique ID card.

89
00:03:33.080 --> 00:03:33.400
<v Speaker 1>Okay.

90
00:03:33.680 --> 00:03:39.800
<v Speaker 2>It holds all this essential information, size, permissions, ownership, time stamps.

91
00:03:39.840 --> 00:03:41.360
<v Speaker 1>It's like the file's permanent record.

92
00:03:41.800 --> 00:03:44.159
<v Speaker 2>It's it's the file's permanent record. A file descripture, on

93
00:03:44.199 --> 00:03:47.039
<v Speaker 2>the other hand, is like a temporary handle your program

94
00:03:47.159 --> 00:03:48.639
<v Speaker 2>gets to interact with that file.

95
00:03:48.719 --> 00:03:51.639
<v Speaker 1>Okay. So the inode is the files master record, and

96
00:03:51.759 --> 00:03:55.520
<v Speaker 1>the descriptor is my program's key to access it precisely.

97
00:03:55.759 --> 00:03:57.680
<v Speaker 2>I like it. Okay. Now, all these files they live

98
00:03:57.719 --> 00:04:00.560
<v Speaker 2>in a file system, right, But unlike some other systems

99
00:04:00.680 --> 00:04:03.120
<v Speaker 2>where you might have different drives and they have their

100
00:04:03.159 --> 00:04:05.759
<v Speaker 2>own separate spaces, like your C drive and your D drive.

101
00:04:05.879 --> 00:04:09.000
<v Speaker 2>Yeah yeah, yeah, Linux uses a unified name space.

102
00:04:09.120 --> 00:04:11.360
<v Speaker 1>So it's all just one big, happy family exactly.

103
00:04:11.560 --> 00:04:14.919
<v Speaker 2>It's like having one giant filing cabinet for everything, okay.

104
00:04:15.000 --> 00:04:17.519
<v Speaker 2>And this makes life much easier for programs. They don't

105
00:04:17.560 --> 00:04:20.720
<v Speaker 2>need to worry about where a file is physically located.

106
00:04:21.079 --> 00:04:23.759
<v Speaker 2>That makes sense now, Historically, this name space was shared

107
00:04:23.800 --> 00:04:26.519
<v Speaker 2>by everyone. Okay, but Linux has evolved.

108
00:04:26.800 --> 00:04:28.279
<v Speaker 1>It always does, it always does.

109
00:04:28.399 --> 00:04:31.800
<v Speaker 2>It's always changed to support per process name spaces.

110
00:04:31.839 --> 00:04:33.680
<v Speaker 1>Poopresss name spaces. Now, what is that?

111
00:04:34.040 --> 00:04:37.040
<v Speaker 2>It means that each process can have its own customized

112
00:04:37.120 --> 00:04:40.319
<v Speaker 2>view of the file system ooh if needed. So it's

113
00:04:40.360 --> 00:04:43.720
<v Speaker 2>like having your own personal filing cabinet within that larger one.

114
00:04:43.920 --> 00:04:46.600
<v Speaker 1>Okay, that's a cool feature. Yeah, all right, so we've

115
00:04:46.600 --> 00:04:49.399
<v Speaker 1>got files, we've got this unified name space. But what

116
00:04:49.480 --> 00:04:50.480
<v Speaker 1>about processes?

117
00:04:50.720 --> 00:04:52.040
<v Speaker 2>Ah? Yes, processes.

118
00:04:52.079 --> 00:04:54.759
<v Speaker 1>Those are those mysterious things that make our programs actually

119
00:04:54.839 --> 00:04:55.560
<v Speaker 1>come to life.

120
00:04:55.800 --> 00:04:59.040
<v Speaker 2>You're exactly right. A process is essentially a running instance

121
00:04:59.079 --> 00:05:03.279
<v Speaker 2>of your program. Imagine it's like this little virtualized world

122
00:05:03.680 --> 00:05:06.560
<v Speaker 2>with its own memory space, got it resources and the

123
00:05:06.600 --> 00:05:08.839
<v Speaker 2>illusion that it has the whole computer to itself.

124
00:05:08.920 --> 00:05:12.120
<v Speaker 1>So each program gets its own little sandbox to play in.

125
00:05:12.240 --> 00:05:14.680
<v Speaker 2>Yes, and it's unaware of the other kids playing in

126
00:05:14.680 --> 00:05:18.399
<v Speaker 2>the other sandboxes. Right, and that isolation is key for

127
00:05:18.560 --> 00:05:19.680
<v Speaker 2>stability and security.

128
00:05:19.839 --> 00:05:20.399
<v Speaker 1>Makes sense.

129
00:05:20.920 --> 00:05:23.560
<v Speaker 2>Now, A process goes through a whole life cycle, right,

130
00:05:24.120 --> 00:05:27.759
<v Speaker 2>it's born, It lives its life executing your code.

131
00:05:27.839 --> 00:05:28.720
<v Speaker 1>It has a purpose.

132
00:05:29.000 --> 00:05:31.680
<v Speaker 2>It has a purpose, and eventually it meets its end. Okay,

133
00:05:31.839 --> 00:05:34.759
<v Speaker 2>Sometimes it leaves behind a temporary ghost known as a

134
00:05:34.839 --> 00:05:35.720
<v Speaker 2>zombie process.

135
00:05:35.959 --> 00:05:38.560
<v Speaker 1>A zombie process that sounds a little ominous.

136
00:05:38.720 --> 00:05:41.120
<v Speaker 2>It does sound ominous, but don't worry. They're usually harmlessness.

137
00:05:41.160 --> 00:05:41.759
<v Speaker 1>Okay, good.

138
00:05:41.879 --> 00:05:44.319
<v Speaker 2>It just means that the process is finished running, but

139
00:05:44.439 --> 00:05:47.279
<v Speaker 2>its parent hasn't yet acknowledged its demise.

140
00:05:47.160 --> 00:05:48.519
<v Speaker 1>So it's still kind of hanging around.

141
00:05:48.600 --> 00:05:50.759
<v Speaker 2>Yeah, it's just waiting for that final goodbye.

142
00:05:51.079 --> 00:05:51.720
<v Speaker 1>Interesting.

143
00:05:51.879 --> 00:05:55.879
<v Speaker 2>But during its life, a process has certain permissions that

144
00:05:56.000 --> 00:06:00.879
<v Speaker 2>dictate what it can and can't do. It receive signals, okay,

145
00:06:01.199 --> 00:06:03.720
<v Speaker 2>those are like interprocess messages, and it has to handle

146
00:06:03.800 --> 00:06:04.720
<v Speaker 2>errors gracefully.

147
00:06:04.959 --> 00:06:07.240
<v Speaker 1>So every program needs some rules in some ways to

148
00:06:07.279 --> 00:06:08.439
<v Speaker 1>communicate exactly.

149
00:06:08.480 --> 00:06:17.759
<v Speaker 2>It needs some etiquette, some manners. Yeah, out reading and

150
00:06:17.759 --> 00:06:18.519
<v Speaker 2>writing data.

151
00:06:19.199 --> 00:06:22.480
<v Speaker 1>That's a great question, because, yeah, how do they do that.

152
00:06:23.040 --> 00:06:25.759
<v Speaker 2>Well, that's where input output are IO comes in the

153
00:06:25.839 --> 00:06:28.759
<v Speaker 2>art of reading and writing data, and in Linux this

154
00:06:28.879 --> 00:06:32.160
<v Speaker 2>relies heavily on those system calls we talked about.

155
00:06:31.959 --> 00:06:33.839
<v Speaker 1>Earlier, those direct lines to the kernel.

156
00:06:33.920 --> 00:06:34.360
<v Speaker 2>Exactly.

157
00:06:34.480 --> 00:06:34.879
<v Speaker 1>Awesome.

158
00:06:34.920 --> 00:06:37.319
<v Speaker 2>So imagine you're downloading a movie. Okay, the first thing

159
00:06:37.360 --> 00:06:38.800
<v Speaker 2>you do is you find the right url.

160
00:06:39.360 --> 00:06:41.839
<v Speaker 1>I find it on my favorite streaming service, Yes.

161
00:06:41.680 --> 00:06:44.079
<v Speaker 2>Exactly, that's like the open system call.

162
00:06:44.160 --> 00:06:44.439
<v Speaker 1>Okay.

163
00:06:44.800 --> 00:06:47.920
<v Speaker 2>Then you start receiving those data packets yep, that's read it.

164
00:06:48.160 --> 00:06:50.720
<v Speaker 2>And finally you save those packets to your hard drive.

165
00:06:51.000 --> 00:06:53.319
<v Speaker 2>That's right, right, okay, and when you're done, you close

166
00:06:53.360 --> 00:06:54.360
<v Speaker 2>the connection. That's closed.

167
00:06:54.639 --> 00:06:54.959
<v Speaker 1>Okay.

168
00:06:55.000 --> 00:06:56.920
<v Speaker 2>That makes a lot more sense now, Yeah, it's nice

169
00:06:56.920 --> 00:06:57.680
<v Speaker 2>little analogy.

170
00:06:57.759 --> 00:06:58.519
<v Speaker 1>Yeah, I like it.

171
00:06:58.920 --> 00:07:01.360
<v Speaker 2>So those are the basics of got it. What about

172
00:07:01.360 --> 00:07:02.639
<v Speaker 2>this buffer io i've heard about?

173
00:07:02.959 --> 00:07:04.800
<v Speaker 1>Yeah? Is that just like a fancy name for the

174
00:07:04.839 --> 00:07:05.360
<v Speaker 1>same thing.

175
00:07:05.720 --> 00:07:08.279
<v Speaker 2>Not exactly. Buffer io is all about performance.

176
00:07:08.399 --> 00:07:10.480
<v Speaker 1>Ooh performance. We always like that.

177
00:07:10.600 --> 00:07:13.920
<v Speaker 2>We always like that. So remember each system call has

178
00:07:13.959 --> 00:07:14.920
<v Speaker 2>a bit of overhead.

179
00:07:15.079 --> 00:07:15.480
<v Speaker 1>Okay.

180
00:07:15.600 --> 00:07:17.480
<v Speaker 2>So if you're reading a file like one bite at

181
00:07:17.519 --> 00:07:19.879
<v Speaker 2>a time, yeah, you're making a lot of system calls.

182
00:07:19.959 --> 00:07:21.680
<v Speaker 1>That sounds inefficient, it is.

183
00:07:21.720 --> 00:07:22.720
<v Speaker 2>That's where buffering comes in.

184
00:07:23.040 --> 00:07:24.480
<v Speaker 1>We're bundling those calls together.

185
00:07:24.519 --> 00:07:27.519
<v Speaker 2>They get it, Okay. The c standard io library, which

186
00:07:27.560 --> 00:07:32.480
<v Speaker 2>you access using stideo, right, it uses this technique. Ok

187
00:07:32.720 --> 00:07:36.879
<v Speaker 2>So it uses functions like fopen, fred, fright and f

188
00:07:36.959 --> 00:07:39.879
<v Speaker 2>close to work with files. But it does so in

189
00:07:40.000 --> 00:07:42.399
<v Speaker 2>chunks okay, and it reduces the number of times it

190
00:07:42.439 --> 00:07:44.079
<v Speaker 2>needs to talk directly to the kernel.

191
00:07:44.560 --> 00:07:46.720
<v Speaker 1>That makes sense. It's just being a little bit smarter

192
00:07:46.839 --> 00:07:47.279
<v Speaker 1>about it.

193
00:07:47.439 --> 00:07:48.759
<v Speaker 2>Exactly. It's being efficient.

194
00:07:49.040 --> 00:07:51.959
<v Speaker 1>It's like ordering in bulk exactly. I like it.

195
00:07:52.000 --> 00:07:54.920
<v Speaker 2>But what about when we have multiple threads uh oh

196
00:07:55.040 --> 00:07:56.279
<v Speaker 2>accessing the same file.

197
00:07:56.519 --> 00:07:58.959
<v Speaker 1>Yeah, that sounds like it could get a little messy.

198
00:07:59.000 --> 00:08:01.519
<v Speaker 2>You could get messy. That's where thread safety comes in. Okay,

199
00:08:01.560 --> 00:08:05.360
<v Speaker 2>So when you have multiple threads accessing shared resources like files,

200
00:08:05.920 --> 00:08:09.600
<v Speaker 2>you need mechanisms like locking to ensure that they don't

201
00:08:09.720 --> 00:08:12.839
<v Speaker 2>corrupt data, of course, by trying to write at the

202
00:08:12.879 --> 00:08:13.399
<v Speaker 2>same time.

203
00:08:13.879 --> 00:08:15.240
<v Speaker 1>Yeah, you don't want them to step in on each

204
00:08:15.240 --> 00:08:16.480
<v Speaker 1>other's toes exactly.

205
00:08:16.560 --> 00:08:18.199
<v Speaker 2>That's like having a traffic light system.

206
00:08:18.360 --> 00:08:19.120
<v Speaker 1>Oh. I like that.

207
00:08:19.279 --> 00:08:22.319
<v Speaker 2>Yeah, to control access to a busy intersection, keep.

208
00:08:22.199 --> 00:08:26.240
<v Speaker 1>Things flowing smoothly exactly. Okay, so buffer io is great,

209
00:08:26.639 --> 00:08:30.120
<v Speaker 1>but we need to be careful when multiple threads are involved.

210
00:08:30.120 --> 00:08:30.839
<v Speaker 2>We do, we do.

211
00:08:31.120 --> 00:08:34.200
<v Speaker 1>This is getting pretty deep. Yeah, what about techniques that

212
00:08:34.320 --> 00:08:38.480
<v Speaker 1>go beyond the basics of io Ooh okay, it's lurking

213
00:08:38.519 --> 00:08:40.159
<v Speaker 1>in those advanced waters.

214
00:08:40.279 --> 00:08:42.200
<v Speaker 2>Get ready to level up your IO skills.

215
00:08:42.279 --> 00:08:43.240
<v Speaker 1>I'm ready. Let's go.

216
00:08:43.279 --> 00:08:45.799
<v Speaker 2>We're going to talk about techniques like scatter gather io,

217
00:08:45.919 --> 00:08:50.200
<v Speaker 2>event pulling, and even memory mapping, each with their own superpowers.

218
00:08:50.399 --> 00:08:53.240
<v Speaker 1>Okay, I'm ready for those superpowers. All right, let's start

219
00:08:53.279 --> 00:08:57.120
<v Speaker 1>with scatter gather io. What kind of magic does that involve?

220
00:08:57.320 --> 00:09:00.519
<v Speaker 2>Imagine you have data coming from three different sources, okay,

221
00:09:01.000 --> 00:09:03.519
<v Speaker 2>and you need to write it all to a single

222
00:09:03.600 --> 00:09:07.279
<v Speaker 2>file instead of making three separate right calls.

223
00:09:07.480 --> 00:09:08.720
<v Speaker 1>Yeah, that sounds inefficient.

224
00:09:08.840 --> 00:09:11.320
<v Speaker 2>It is scatter gather io. Let's you do it in

225
00:09:11.360 --> 00:09:14.879
<v Speaker 2>one shot, okay, combining those data chunks into a single

226
00:09:14.879 --> 00:09:15.840
<v Speaker 2>efficient operation.

227
00:09:16.080 --> 00:09:19.480
<v Speaker 1>So it's like optimizing a delivery route by combining multiple

228
00:09:19.480 --> 00:09:20.639
<v Speaker 1>packages exactly.

229
00:09:20.759 --> 00:09:23.759
<v Speaker 2>I like it. Linux provides system calls like rehobar and

230
00:09:23.919 --> 00:09:27.759
<v Speaker 2>writeev and they use a special structure called iovec to

231
00:09:27.840 --> 00:09:29.679
<v Speaker 2>define those vectors of buffers.

232
00:09:29.759 --> 00:09:30.159
<v Speaker 1>Got it?

233
00:09:30.200 --> 00:09:31.240
<v Speaker 2>Making this magic happen.

234
00:09:31.279 --> 00:09:31.840
<v Speaker 1>That's awesome.

235
00:09:32.000 --> 00:09:33.840
<v Speaker 2>But what if you're dealing with a lot of files

236
00:09:33.879 --> 00:09:36.919
<v Speaker 2>and you need to be notified when something happens, like

237
00:09:37.039 --> 00:09:38.080
<v Speaker 2>new data arriving?

238
00:09:38.240 --> 00:09:40.679
<v Speaker 1>Ooh yeah, that sounds like a lot to keep track of.

239
00:09:41.159 --> 00:09:43.519
<v Speaker 2>Is that's where event polling comes in, like having a

240
00:09:43.559 --> 00:09:45.080
<v Speaker 2>surveillance system for your files.

241
00:09:45.279 --> 00:09:45.919
<v Speaker 1>I like it.

242
00:09:45.960 --> 00:09:49.159
<v Speaker 2>So the older system calls select and poll Okay. There

243
00:09:49.159 --> 00:09:52.039
<v Speaker 2>were early attempts at this okay, but they had limitations

244
00:09:52.159 --> 00:09:54.200
<v Speaker 2>when you had a huge number of files to watch.

245
00:09:54.360 --> 00:09:55.320
<v Speaker 1>Yeah, that makes sense.

246
00:09:55.480 --> 00:09:59.600
<v Speaker 2>Thankfully, Linux introduced the epole facility, okay, which is much

247
00:09:59.600 --> 00:10:01.600
<v Speaker 2>more effe at monitoring multiple files.

248
00:10:01.679 --> 00:10:04.559
<v Speaker 1>So it's like upgrading from a security guard watching grainy

249
00:10:04.639 --> 00:10:08.360
<v Speaker 1>CCTV footage, yes, to like a high tech system with

250
00:10:08.559 --> 00:10:09.759
<v Speaker 1>AI powered alerts.

251
00:10:10.519 --> 00:10:13.639
<v Speaker 2>That's a great analogy. And finally, we have memory mapping,

252
00:10:14.440 --> 00:10:16.440
<v Speaker 2>which is where things get really interesting.

253
00:10:16.600 --> 00:10:17.879
<v Speaker 1>Okay, I'm intrigued.

254
00:10:18.240 --> 00:10:21.440
<v Speaker 2>Tell me mar Imagine you have this massive file I

255
00:10:21.440 --> 00:10:24.799
<v Speaker 2>think a huge database or a high resolution image. With

256
00:10:24.960 --> 00:10:28.759
<v Speaker 2>memory mapping, you can map that entire file into your

257
00:10:28.799 --> 00:10:31.759
<v Speaker 2>process's address space. Oh wow, you can treat it as

258
00:10:31.840 --> 00:10:34.320
<v Speaker 2>if it were already loaded into memory. No need to

259
00:10:34.360 --> 00:10:35.080
<v Speaker 2>read it in chunks.

260
00:10:35.120 --> 00:10:36.879
<v Speaker 1>So I have instant access.

261
00:10:36.519 --> 00:10:37.720
<v Speaker 2>You have direct access.

262
00:10:37.879 --> 00:10:39.240
<v Speaker 1>Why wouldn't we always do this.

263
00:10:39.320 --> 00:10:42.519
<v Speaker 2>It's a fantastic tool for performance, especially with large files.

264
00:10:43.720 --> 00:10:49.000
<v Speaker 2>The system calls map and mun map are your keys

265
00:10:49.039 --> 00:10:49.879
<v Speaker 2>to this kingdom.

266
00:10:50.039 --> 00:10:51.039
<v Speaker 1>I'm ready to rule.

267
00:10:51.679 --> 00:10:54.240
<v Speaker 2>But it's important to understand that there are different ways

268
00:10:54.279 --> 00:10:57.559
<v Speaker 2>memory mapping can work, and you can use flags like

269
00:10:57.679 --> 00:10:59.279
<v Speaker 2>map shared and map private.

270
00:10:59.440 --> 00:11:01.159
<v Speaker 1>Interesting, what's the difference So with.

271
00:11:01.240 --> 00:11:05.679
<v Speaker 2>Map shared If multiple processes map the same file, any

272
00:11:05.799 --> 00:11:08.720
<v Speaker 2>changes one process makes are visible to the others. Okay,

273
00:11:08.799 --> 00:11:11.440
<v Speaker 2>it's like a shared Google doc got it map Private,

274
00:11:11.519 --> 00:11:15.480
<v Speaker 2>on the other hand, creates a private copy for each process,

275
00:11:15.960 --> 00:11:17.759
<v Speaker 2>so changes are isolated.

276
00:11:17.440 --> 00:11:20.320
<v Speaker 1>So it's like working on my own personal copy exactly. Okay,

277
00:11:20.360 --> 00:11:20.840
<v Speaker 1>that makes sense.

278
00:11:20.919 --> 00:11:23.279
<v Speaker 2>Yeah, so memory mapping incredibly powerful.

279
00:11:23.360 --> 00:11:24.440
<v Speaker 1>Wow, very cool.

280
00:11:24.480 --> 00:11:27.080
<v Speaker 2>But all this talk about memory makes me realize we

281
00:11:27.120 --> 00:11:29.879
<v Speaker 2>haven't really talked about how Linux actually manages memory.

282
00:11:30.039 --> 00:11:30.960
<v Speaker 1>Yeah. That's a big one.

283
00:11:31.039 --> 00:11:31.840
<v Speaker 2>Yeah, it's a big one.

284
00:11:31.840 --> 00:11:33.639
<v Speaker 1>How does it ensure everything runs smoothly?

285
00:11:33.960 --> 00:11:37.399
<v Speaker 2>Well? Memory management is crucial, I would think. So. Yeah,

286
00:11:37.440 --> 00:11:40.799
<v Speaker 2>it's the unsung hero of any operating system. Okay, making

287
00:11:40.840 --> 00:11:44.080
<v Speaker 2>sure that each process gets fair share of memory. Okay,

288
00:11:44.120 --> 00:11:45.720
<v Speaker 2>and that everything plays nicely together.

289
00:11:45.799 --> 00:11:47.200
<v Speaker 1>So it's like a master conductor.

290
00:11:47.600 --> 00:11:50.080
<v Speaker 2>It's orchestrating this complex symphony.

291
00:11:50.480 --> 00:11:51.000
<v Speaker 1>I like that.

292
00:11:51.320 --> 00:11:53.919
<v Speaker 2>So first we need to understand the process address space.

293
00:11:54.200 --> 00:11:57.399
<v Speaker 2>Remember those isolated sandboxes we talked about earlier. Yes, well,

294
00:11:57.440 --> 00:12:00.679
<v Speaker 2>the sandboxes are further divided into pages, which are these

295
00:12:00.799 --> 00:12:04.919
<v Speaker 2>chunks of memory. Okay, not all pages are created equal, though, Oh,

296
00:12:05.039 --> 00:12:08.759
<v Speaker 2>you have valid pages that correspond to actual data in

297
00:12:08.840 --> 00:12:12.440
<v Speaker 2>RAM okay, or on disc and you have invalid pages

298
00:12:13.000 --> 00:12:15.000
<v Speaker 2>that represent unallocated regions.

299
00:12:15.320 --> 00:12:18.120
<v Speaker 1>So accessing an invalid page is like falling into a

300
00:12:18.200 --> 00:12:19.600
<v Speaker 1>trapdoor in the sandbox.

301
00:12:19.840 --> 00:12:21.559
<v Speaker 2>That's a great way to put it. And trying to

302
00:12:21.600 --> 00:12:25.120
<v Speaker 2>access memory that's not yours is a big no no. Yeah,

303
00:12:25.159 --> 00:12:29.200
<v Speaker 2>that's bad and usually results in a segmentation violation, a

304
00:12:29.240 --> 00:12:30.240
<v Speaker 2>programmer's nightmare.

305
00:12:30.399 --> 00:12:32.799
<v Speaker 1>Oh, segmentation violation. I've heard that term.

306
00:12:32.960 --> 00:12:35.559
<v Speaker 2>It's not good. That's bad, it's not good.

307
00:12:35.639 --> 00:12:36.240
<v Speaker 1>I don't like that.

308
00:12:36.279 --> 00:12:39.879
<v Speaker 2>But thankfully, goodness, we have the memory management unit or MMU.

309
00:12:40.039 --> 00:12:41.279
<v Speaker 1>Okay, the MMU.

310
00:12:41.399 --> 00:12:44.039
<v Speaker 2>The MMU to keep things in check. It acts as

311
00:12:44.080 --> 00:12:48.240
<v Speaker 2>a translator, converting those virtual addresses used by our program

312
00:12:48.480 --> 00:12:52.360
<v Speaker 2>into physical addresses okay, where that data actually resides in RAM.

313
00:12:52.600 --> 00:12:55.799
<v Speaker 1>So the MMU is like a postal worker who knows

314
00:12:55.840 --> 00:12:58.360
<v Speaker 1>the real address behind every po box.

315
00:12:58.559 --> 00:13:00.279
<v Speaker 2>Another perfect analogy.

316
00:13:00.320 --> 00:13:00.799
<v Speaker 1>I'm falling.

317
00:13:00.879 --> 00:13:04.679
<v Speaker 2>So the MMU also plays a vital role in memory protection,

318
00:13:05.600 --> 00:13:09.039
<v Speaker 2>making sure that processes don't wander into memory territory that

319
00:13:09.120 --> 00:13:10.159
<v Speaker 2>doesn't belong to them.

320
00:13:10.519 --> 00:13:11.559
<v Speaker 1>They stay in their own lanes.

321
00:13:11.600 --> 00:13:13.639
<v Speaker 2>They stay in their own lane. Okay, but what about

322
00:13:13.679 --> 00:13:16.240
<v Speaker 2>when a program needs to allocate memory on the fly.

323
00:13:16.759 --> 00:13:20.039
<v Speaker 1>Yeah, like when I'm adding things to my shopping cart online.

324
00:13:19.519 --> 00:13:22.600
<v Speaker 2>Exactly, and the website needs to store that information somewhere

325
00:13:22.759 --> 00:13:26.279
<v Speaker 2>right exactly. That's where dynamic memory allocation comes in, okay,

326
00:13:26.879 --> 00:13:29.240
<v Speaker 2>and it's handled through a special region of memory called

327
00:13:29.279 --> 00:13:33.240
<v Speaker 2>the heap. The heap, you use functions like malik, calic,

328
00:13:33.639 --> 00:13:37.639
<v Speaker 2>realic and free to request and release memory from this

329
00:13:37.759 --> 00:13:38.759
<v Speaker 2>heap as needed.

330
00:13:39.039 --> 00:13:41.639
<v Speaker 1>So the heap is like a self storage facility. It

331
00:13:41.720 --> 00:13:44.679
<v Speaker 1>is where programs can rent units of various sizes.

332
00:13:44.840 --> 00:13:46.200
<v Speaker 2>That's a great way to visualize it.

333
00:13:46.240 --> 00:13:47.519
<v Speaker 1>Okay, I'm picturing it.

334
00:13:47.559 --> 00:13:50.759
<v Speaker 2>But managing the heap can get tricky. You can end

335
00:13:50.840 --> 00:13:54.240
<v Speaker 2>up with something called fragmentation. Fragmentation okay, where you have

336
00:13:54.360 --> 00:13:57.759
<v Speaker 2>enough free space overall, ok but it's scattered around small chunks.

337
00:13:57.799 --> 00:13:59.279
<v Speaker 1>You can't fit that big couching.

338
00:13:59.360 --> 00:14:01.200
<v Speaker 2>You can't fit that big couchin exactly.

339
00:14:01.320 --> 00:14:05.919
<v Speaker 1>Now you technically have enough space exactly. That's frustrating.

340
00:14:06.039 --> 00:14:09.399
<v Speaker 2>It is frustrating. But Linux has some clever ways to

341
00:14:09.440 --> 00:14:12.480
<v Speaker 2>deal with that, okay. Good. For larger chunks of memory,

342
00:14:12.519 --> 00:14:15.480
<v Speaker 2>it can use something called anonymous memory mapping.

343
00:14:15.759 --> 00:14:17.639
<v Speaker 1>Anonymous memory mapping.

344
00:14:17.360 --> 00:14:21.080
<v Speaker 2>Yes, okay, reserving a contiguous block of memory. It's like

345
00:14:21.120 --> 00:14:22.639
<v Speaker 2>renting a whole warehouse.

346
00:14:22.279 --> 00:14:26.039
<v Speaker 1>Unit instead of trying to squeeze into those few small lockers.

347
00:14:26.360 --> 00:14:27.559
<v Speaker 2>Exactly, that makes sense.

348
00:14:27.759 --> 00:14:30.759
<v Speaker 1>Yeah, now, I remember you mentioned memory locking earlier. Yes,

349
00:14:30.879 --> 00:14:32.240
<v Speaker 1>when would we want to use that?

350
00:14:32.720 --> 00:14:37.000
<v Speaker 2>Memory locking is particularly useful when you have a critical

351
00:14:37.039 --> 00:14:41.399
<v Speaker 2>piece of code that needs to access memory with absolutely

352
00:14:41.440 --> 00:14:42.159
<v Speaker 2>no delays.

353
00:14:42.279 --> 00:14:43.519
<v Speaker 1>Of course, you.

354
00:14:43.440 --> 00:14:45.559
<v Speaker 2>Wouldn't want the kernel to swap that memory.

355
00:14:45.279 --> 00:14:47.519
<v Speaker 1>Out to disc right, No, that would be terrible.

356
00:14:47.519 --> 00:14:49.000
<v Speaker 2>It'd be like pausing a movie right in the middle

357
00:14:49.000 --> 00:14:52.840
<v Speaker 2>of an action scene. So memory locking done through the

358
00:14:52.919 --> 00:14:58.480
<v Speaker 2>lock system call okay. It ensures that those critical memory

359
00:14:58.480 --> 00:14:59.799
<v Speaker 2>pages stay in.

360
00:14:59.799 --> 00:15:00.919
<v Speaker 1>Rank they're locked in.

361
00:15:00.960 --> 00:15:04.159
<v Speaker 2>They're locked in guaranteeing predictable performance.

362
00:15:04.200 --> 00:15:06.320
<v Speaker 1>So we're putting a do not disturb sign on those

363
00:15:06.360 --> 00:15:07.559
<v Speaker 1>memory pages, you got.

364
00:15:07.440 --> 00:15:11.120
<v Speaker 2>It, preventing the kernel from relocating them. Awesome. It's a

365
00:15:11.159 --> 00:15:15.399
<v Speaker 2>powerful tool. It sounds like it for performance sensitive applications. Yeah,

366
00:15:15.399 --> 00:15:19.279
<v Speaker 2>but it's important to use it judiciously, as over using

367
00:15:19.399 --> 00:15:20.519
<v Speaker 2>it can have drawbacks.

368
00:15:20.759 --> 00:15:22.519
<v Speaker 1>Of course, too much of a good thing.

369
00:15:22.639 --> 00:15:24.799
<v Speaker 2>Exactly, especially in multi threaded.

370
00:15:24.519 --> 00:15:26.639
<v Speaker 1>Environments right where things are already complicated.

371
00:15:26.759 --> 00:15:27.279
<v Speaker 2>Exactly.

372
00:15:27.399 --> 00:15:30.480
<v Speaker 1>This whole deep dive into memory management has been fascinated.

373
00:15:30.559 --> 00:15:31.840
<v Speaker 2>It is a fascinating topic.

374
00:15:31.919 --> 00:15:35.440
<v Speaker 1>It really is. I'm starting to realize just how much

375
00:15:35.480 --> 00:15:39.399
<v Speaker 1>complexity is hidden beneath the surface of something as seemingly

376
00:15:39.440 --> 00:15:40.759
<v Speaker 1>simple as running a program.

377
00:15:40.799 --> 00:15:43.879
<v Speaker 2>And we've only just begun. Oh No, there's a whole

378
00:15:44.000 --> 00:15:47.279
<v Speaker 2>universe of fascinating concepts and techniques to explore. Bring it on.

379
00:15:47.519 --> 00:15:50.279
<v Speaker 2>I'm real, but I'm glad you're starting to appreciate the

380
00:15:50.320 --> 00:15:52.639
<v Speaker 2>elegance and power of Linux system programming.

381
00:15:52.679 --> 00:15:55.799
<v Speaker 1>Absolutely i am. I'm already looking forward to uncovering more

382
00:15:55.840 --> 00:15:59.320
<v Speaker 1>of these hidden gems right as we continue our deep dive.

383
00:15:59.639 --> 00:16:04.039
<v Speaker 2>Awesome, Now that we've explored memory management, let's shift gears

384
00:16:04.039 --> 00:16:06.600
<v Speaker 2>a bit, okay and talk about something a bit more tangible.

385
00:16:06.919 --> 00:16:07.679
<v Speaker 1>All right, I'm with you.

386
00:16:07.840 --> 00:16:08.840
<v Speaker 2>Files and directories.

387
00:16:09.120 --> 00:16:10.960
<v Speaker 1>Okay, I can definitely relate to those.

388
00:16:11.080 --> 00:16:12.279
<v Speaker 2>Yeah, we all have to deal with those.

389
00:16:12.360 --> 00:16:16.559
<v Speaker 1>I spend way too much time organizing files on my computer. So, right,

390
00:16:16.759 --> 00:16:19.360
<v Speaker 1>what kind of insights can system programming give us about

391
00:16:19.399 --> 00:16:20.440
<v Speaker 1>files and directories?

392
00:16:20.519 --> 00:16:22.320
<v Speaker 2>Well, first of all, we need a way to gather

393
00:16:22.440 --> 00:16:25.600
<v Speaker 2>information about a file. Okay, think of it like a

394
00:16:26.000 --> 00:16:30.480
<v Speaker 2>detective doing some reconnaissance. That's where the STAT family system

395
00:16:30.519 --> 00:16:33.159
<v Speaker 2>calls comes in. They give you all sorts of details

396
00:16:33.200 --> 00:16:36.720
<v Speaker 2>about a file, file sized, permissions, timestamps.

397
00:16:37.679 --> 00:16:39.320
<v Speaker 1>So it's like a file's fingerprint.

398
00:16:39.480 --> 00:16:42.840
<v Speaker 2>Exactly. It's a file's fingerprint, okay. So you can find

399
00:16:42.840 --> 00:16:44.879
<v Speaker 2>out if it's a regular file or a directory, when

400
00:16:44.919 --> 00:16:47.279
<v Speaker 2>it was last modified, who owns it, and even what

401
00:16:47.279 --> 00:16:48.720
<v Speaker 2>permissions are set, so.

402
00:16:48.600 --> 00:16:50.799
<v Speaker 1>Who can ReadWrite, or execute it?

403
00:16:51.000 --> 00:16:51.559
<v Speaker 2>Exactly.

404
00:16:51.600 --> 00:16:53.000
<v Speaker 1>Wow, that's a lot of information.

405
00:16:53.159 --> 00:16:55.240
<v Speaker 2>It's a treasure trove of data, it is.

406
00:16:55.360 --> 00:16:58.399
<v Speaker 1>So once we've done our investigation, how do we actually

407
00:16:58.440 --> 00:17:02.480
<v Speaker 1>manipulate files, like changing permissions or ownership?

408
00:17:02.600 --> 00:17:07.720
<v Speaker 2>Well, Linux provides these handy system calls like shab and

409
00:17:07.799 --> 00:17:12.160
<v Speaker 2>choun Okay, So schmad lets you tweak those permissions. Okay,

410
00:17:12.279 --> 00:17:13.839
<v Speaker 2>who can do what with a file?

411
00:17:14.240 --> 00:17:14.599
<v Speaker 1>Got it?

412
00:17:14.799 --> 00:17:17.920
<v Speaker 2>Choun, on the other hand, lets you change the owner

413
00:17:18.000 --> 00:17:20.839
<v Speaker 2>and group, essentially deciding who's in control.

414
00:17:20.960 --> 00:17:23.640
<v Speaker 1>It's like being the administrator of your own little file kingdom.

415
00:17:23.880 --> 00:17:26.759
<v Speaker 2>Exactly. You get to decide who has access and what

416
00:17:26.799 --> 00:17:27.599
<v Speaker 2>they're allowed to do.

417
00:17:27.759 --> 00:17:29.119
<v Speaker 1>I like it. I have the power.

418
00:17:29.319 --> 00:17:32.440
<v Speaker 2>Now. Sometimes you need to store extra information about a file, okay,

419
00:17:32.680 --> 00:17:35.920
<v Speaker 2>something beyond those basic attributes we get from stack. Yeah,

420
00:17:36.000 --> 00:17:37.559
<v Speaker 2>that's where extended attributes come in.

421
00:17:37.640 --> 00:17:40.519
<v Speaker 1>Extended attributes, that sounds interesting. What are we talking about here.

422
00:17:40.680 --> 00:17:42.119
<v Speaker 2>Think of them like custom labels.

423
00:17:42.240 --> 00:17:42.480
<v Speaker 1>Okay.

424
00:17:42.519 --> 00:17:45.279
<v Speaker 2>You can attach to files so you can store things

425
00:17:45.359 --> 00:17:49.599
<v Speaker 2>like author information, version numbers, even application specific data.

426
00:17:50.200 --> 00:17:52.559
<v Speaker 1>So it's like adding notes to a file that only

427
00:17:52.599 --> 00:17:54.720
<v Speaker 1>certain programs or users can see.

428
00:17:55.039 --> 00:17:57.400
<v Speaker 2>That's a good way to think about it, okay. And

429
00:17:57.559 --> 00:18:03.000
<v Speaker 2>Linux provides system like get center, set sadder, and list

430
00:18:03.079 --> 00:18:06.319
<v Speaker 2>sadder to manage these extended attributes.

431
00:18:06.519 --> 00:18:08.440
<v Speaker 1>Okay, so we've got our files all figured out, but

432
00:18:08.440 --> 00:18:09.519
<v Speaker 1>what about directories?

433
00:18:09.920 --> 00:18:11.240
<v Speaker 2>Ah? Yes, directory?

434
00:18:11.319 --> 00:18:14.039
<v Speaker 1>How do we navigate those and keep everything organized?

435
00:18:14.200 --> 00:18:16.200
<v Speaker 2>Well, first, we need to know where we are in

436
00:18:16.240 --> 00:18:19.079
<v Speaker 2>this vast file system, right right. Of course, that's where

437
00:18:19.079 --> 00:18:19.960
<v Speaker 2>get SETI comes in.

438
00:18:20.000 --> 00:18:21.000
<v Speaker 1>Handy, gainst SETI.

439
00:18:21.079 --> 00:18:24.000
<v Speaker 2>It tells you the absolute path to your current working directory.

440
00:18:24.319 --> 00:18:26.680
<v Speaker 2>Think of it like a GPS for the file system.

441
00:18:26.920 --> 00:18:28.799
<v Speaker 1>So, no, we're getting lost in a maze.

442
00:18:28.519 --> 00:18:30.759
<v Speaker 2>Of folders exactly. You always know where you are.

443
00:18:30.839 --> 00:18:31.359
<v Speaker 1>I like it.

444
00:18:31.400 --> 00:18:34.319
<v Speaker 2>And when it comes to actually creating and managing directories,

445
00:18:34.400 --> 00:18:37.000
<v Speaker 2>we have m deer to create new ones and room

446
00:18:37.079 --> 00:18:40.160
<v Speaker 2>deer to remove empty ones. Okay, those are familiar, Yeah,

447
00:18:40.240 --> 00:18:43.000
<v Speaker 2>those are the classics. Of course, we can't forget about links.

448
00:18:43.200 --> 00:18:45.440
<v Speaker 1>Links, Yes, those are always.

449
00:18:45.160 --> 00:18:47.039
<v Speaker 2>A bit tricky for me, maybe be a little bit tricky.

450
00:18:47.039 --> 00:18:50.559
<v Speaker 1>I always get hard links and symbolic links confused, right,

451
00:18:50.640 --> 00:18:51.759
<v Speaker 1>what's the difference again?

452
00:18:52.079 --> 00:18:54.319
<v Speaker 2>So think of a hard link like an alias for

453
00:18:54.359 --> 00:18:54.759
<v Speaker 2>a file.

454
00:18:54.880 --> 00:18:55.319
<v Speaker 1>Okay.

455
00:18:55.440 --> 00:18:57.279
<v Speaker 2>It points directly to the files in.

456
00:18:57.279 --> 00:18:59.279
<v Speaker 1>Ode, right, that unique idea, That unique.

457
00:18:59.039 --> 00:19:02.079
<v Speaker 2>Idea we talked about earlier. So it's like having multiple

458
00:19:02.200 --> 00:19:03.720
<v Speaker 2>entryways leading to the same room.

459
00:19:03.960 --> 00:19:04.279
<v Speaker 1>Okay.

460
00:19:04.359 --> 00:19:06.440
<v Speaker 2>A symbolic link, on the other hand, is more like

461
00:19:06.440 --> 00:19:10.079
<v Speaker 2>a signpost okay, pointing to the actual file. It stores

462
00:19:10.119 --> 00:19:12.599
<v Speaker 2>the path to the target file okay, and can even

463
00:19:12.640 --> 00:19:14.559
<v Speaker 2>point to files on different file systems.

464
00:19:14.799 --> 00:19:17.759
<v Speaker 1>So a hard link is like having multiple copies of

465
00:19:17.799 --> 00:19:20.519
<v Speaker 1>the same key, yes, while a symbolic link is like

466
00:19:20.559 --> 00:19:23.079
<v Speaker 1>having a note that says the key is under the

467
00:19:23.079 --> 00:19:23.720
<v Speaker 1>flower pot.

468
00:19:23.839 --> 00:19:25.400
<v Speaker 2>I love that analogy. That's perfect.

469
00:19:25.440 --> 00:19:26.839
<v Speaker 1>Okay. Good, I'm glad I got that one right.

470
00:19:26.920 --> 00:19:30.559
<v Speaker 2>Yeah, And Linux gives us system calls like link for

471
00:19:30.640 --> 00:19:34.759
<v Speaker 2>hard links, some link for symbolic links, and read link

472
00:19:35.000 --> 00:19:37.000
<v Speaker 2>to find out where a symbolic link is pointing.

473
00:19:37.759 --> 00:19:41.200
<v Speaker 1>Very cool. This whole world of files and directories is

474
00:19:41.240 --> 00:19:42.720
<v Speaker 1>starting to make a lot more sense now.

475
00:19:42.799 --> 00:19:43.759
<v Speaker 2>Good. I'm glad to hear that.

476
00:19:44.039 --> 00:19:46.160
<v Speaker 1>But I'm curious what about when we want to do

477
00:19:46.200 --> 00:19:49.000
<v Speaker 1>things like copy or move files. Okay, do we need

478
00:19:49.039 --> 00:19:51.400
<v Speaker 1>to write a bunch of code to handle that ourselves.

479
00:19:51.480 --> 00:19:53.119
<v Speaker 2>Here's where things get really clever.

480
00:19:53.400 --> 00:19:54.559
<v Speaker 1>Okay, I'm intrigued.

481
00:19:54.839 --> 00:19:59.960
<v Speaker 2>Linux doesn't have specific system calls for copying or moving files.

482
00:20:00.160 --> 00:20:03.240
<v Speaker 2>It doesn't, no, but it gives us the building blocks

483
00:20:03.240 --> 00:20:04.200
<v Speaker 2>to do it ourselves.

484
00:20:04.279 --> 00:20:04.680
<v Speaker 1>Okay.

485
00:20:04.799 --> 00:20:07.519
<v Speaker 2>And it all comes back to that core concept. Everything

486
00:20:07.519 --> 00:20:10.319
<v Speaker 2>in Linux is a file right right, even devices.

487
00:20:10.519 --> 00:20:13.839
<v Speaker 1>Okay, I'm following along so far. But how do devices

488
00:20:13.880 --> 00:20:15.519
<v Speaker 1>help us with copying files?

489
00:20:15.880 --> 00:20:19.319
<v Speaker 2>So some devices act as data sources or sinks. Okay. So,

490
00:20:19.359 --> 00:20:23.680
<v Speaker 2>for example, there's the null device devnyl, which is like

491
00:20:23.720 --> 00:20:25.880
<v Speaker 2>a black hole that discards anything you write to it.

492
00:20:25.960 --> 00:20:28.680
<v Speaker 1>Okay, So it's like a digital trash can exactly.

493
00:20:28.680 --> 00:20:32.200
<v Speaker 2>And then there's the zero device dev zero, which provides

494
00:20:32.240 --> 00:20:33.640
<v Speaker 2>an endless stream of zeros.

495
00:20:33.759 --> 00:20:36.920
<v Speaker 1>An endless stream of zeros okay, useful for certain things.

496
00:20:36.960 --> 00:20:38.759
<v Speaker 2>I can see where this is going. Yeah, we can

497
00:20:38.880 --> 00:20:42.400
<v Speaker 2>use these devices as intermediaries to copy data exactly.

498
00:20:42.480 --> 00:20:44.559
<v Speaker 1>So to copy a file, you can open the source

499
00:20:44.559 --> 00:20:48.519
<v Speaker 1>file for reading, okay, open the destination file for writing okay,

500
00:20:48.640 --> 00:20:50.480
<v Speaker 1>and then just stream the data from the source to

501
00:20:50.519 --> 00:20:51.160
<v Speaker 1>the destination.

502
00:20:51.319 --> 00:20:52.200
<v Speaker 2>Okay, that makes sense.

503
00:20:52.279 --> 00:20:54.319
<v Speaker 1>Moving a file can be as simple as renaming it.

504
00:20:54.440 --> 00:20:57.759
<v Speaker 2>Which essentially changes its location within the file system.

505
00:20:57.880 --> 00:21:01.960
<v Speaker 1>So it's all about leveraging those existing file io mechanisms

506
00:21:02.079 --> 00:21:02.960
<v Speaker 1>in clever ways.

507
00:21:03.079 --> 00:21:04.279
<v Speaker 2>It is. It's very elegant.

508
00:21:04.359 --> 00:21:06.960
<v Speaker 1>It's amazing how much you can accomplish with those basic

509
00:21:07.000 --> 00:21:07.680
<v Speaker 1>building blocks.

510
00:21:07.799 --> 00:21:09.319
<v Speaker 2>It is. It's the power of abstraction.

511
00:21:10.000 --> 00:21:12.640
<v Speaker 1>Now, before we move on, there's one more powerful tool

512
00:21:12.680 --> 00:21:15.920
<v Speaker 1>I want to introduce you to. Okay, hit me in

513
00:21:15.960 --> 00:21:19.200
<v Speaker 1>notify and notify. The name sounds familiar, but I can't

514
00:21:19.240 --> 00:21:20.880
<v Speaker 1>quite place it. What is it used for?

515
00:21:21.359 --> 00:21:24.640
<v Speaker 2>So and Notify is like a real time surveillance system

516
00:21:25.079 --> 00:21:26.200
<v Speaker 2>for your files and directories.

517
00:21:26.359 --> 00:21:26.680
<v Speaker 1>Okay.

518
00:21:26.880 --> 00:21:29.880
<v Speaker 2>Imagine you want to be notified whenever a file is

519
00:21:29.920 --> 00:21:33.000
<v Speaker 2>modified or a new file is added to a directory. Yeah,

520
00:21:33.079 --> 00:21:34.440
<v Speaker 2>and Notified can do that for you.

521
00:21:34.519 --> 00:21:37.680
<v Speaker 1>Wow, that sounds incredibly useful. I can imagine using that

522
00:21:37.720 --> 00:21:40.000
<v Speaker 1>for all sorts of things like backing up files or

523
00:21:40.240 --> 00:21:42.480
<v Speaker 1>keeping track of changes made by other users.

524
00:21:42.519 --> 00:21:44.079
<v Speaker 2>Exactly. It's very versatile.

525
00:21:44.160 --> 00:21:44.400
<v Speaker 1>Okay.

526
00:21:44.599 --> 00:21:48.359
<v Speaker 2>And it uses this file descripture based interface okay, so

527
00:21:48.400 --> 00:21:50.960
<v Speaker 2>you can integrate it seamlessly into your application.

528
00:21:51.160 --> 00:21:51.559
<v Speaker 1>Awesome.

529
00:21:51.640 --> 00:21:55.000
<v Speaker 2>You can add watches for specific files or directories and

530
00:21:55.039 --> 00:21:57.440
<v Speaker 2>then receive events whenever those watches are triggered.

531
00:21:57.519 --> 00:21:59.880
<v Speaker 1>So it's like setting up trip wires around my file

532
00:22:00.119 --> 00:22:01.720
<v Speaker 1>alerting me to any changes.

533
00:22:02.119 --> 00:22:04.680
<v Speaker 2>That's a great analogy. And it can monitor a wide

534
00:22:04.759 --> 00:22:11.079
<v Speaker 2>range of events from file access and modification to creation, deletion, renaming. Wow,

535
00:22:11.200 --> 00:22:14.160
<v Speaker 2>it even provides details about what changed and who made

536
00:22:14.200 --> 00:22:14.680
<v Speaker 2>the change.

537
00:22:14.799 --> 00:22:16.279
<v Speaker 1>That's pretty powerful it is.

538
00:22:16.599 --> 00:22:17.559
<v Speaker 2>It's a powerful tool.

539
00:22:17.799 --> 00:22:20.400
<v Speaker 1>So in Notifies sounds like a must have tool for

540
00:22:20.519 --> 00:22:22.440
<v Speaker 1>any serious system programmer.

541
00:22:22.519 --> 00:22:23.319
<v Speaker 2>I would agree with that.

542
00:22:23.559 --> 00:22:26.960
<v Speaker 1>This whole deep dive into file and directory management has

543
00:22:27.000 --> 00:22:28.319
<v Speaker 1>been really eye opening.

544
00:22:28.480 --> 00:22:29.880
<v Speaker 2>It's a fascinating area.

545
00:22:29.960 --> 00:22:32.400
<v Speaker 1>I'm starting to realize just how much goes on behind

546
00:22:32.440 --> 00:22:35.279
<v Speaker 1>the scenes to keep everything organized and running smoothly.

547
00:22:35.480 --> 00:22:37.000
<v Speaker 2>There's a lot happening under the hood.

548
00:22:37.079 --> 00:22:40.559
<v Speaker 1>There is so much complexity. Yeah, but it's also really elegant.

549
00:22:40.640 --> 00:22:43.720
<v Speaker 2>It is when you start to understand the principles behind it. Yes, now,

550
00:22:43.799 --> 00:22:48.839
<v Speaker 2>let's shift our focus to another crucial aspect of system programming. Okay, signals.

551
00:22:49.039 --> 00:22:51.400
<v Speaker 1>Signals. It sounds like we're about to enter the world

552
00:22:51.440 --> 00:22:53.839
<v Speaker 1>of inner process communication, where we're.

553
00:22:53.640 --> 00:22:55.839
<v Speaker 2>Going to explore how processes talk to each other.

554
00:22:56.000 --> 00:22:58.759
<v Speaker 1>Okay, So what exactly are signals and how do they

555
00:22:58.759 --> 00:22:59.519
<v Speaker 1>work in Linux?

556
00:22:59.759 --> 00:23:02.880
<v Speaker 2>So, signals are the way processes talk to each other.

557
00:23:03.000 --> 00:23:06.200
<v Speaker 2>Think of them like text messages, okay, short asynchronous bursts

558
00:23:06.240 --> 00:23:06.799
<v Speaker 2>of information.

559
00:23:07.599 --> 00:23:10.400
<v Speaker 1>So they're like little pings between processes letting them know

560
00:23:10.440 --> 00:23:11.079
<v Speaker 1>what's going on.

561
00:23:11.359 --> 00:23:12.319
<v Speaker 2>That's a good way to put it.

562
00:23:12.400 --> 00:23:12.599
<v Speaker 1>Yeah.

563
00:23:12.640 --> 00:23:15.680
<v Speaker 2>Yeah. For example, let's say you're running a program and

564
00:23:15.720 --> 00:23:19.559
<v Speaker 2>you press futrol plus c to stop it. What's happening

565
00:23:19.599 --> 00:23:22.599
<v Speaker 2>is that you're sending a signal to that process, okay,

566
00:23:22.720 --> 00:23:23.720
<v Speaker 2>telling it to terminate.

567
00:23:23.799 --> 00:23:25.519
<v Speaker 1>So that's what's going on behind the scenes. When I

568
00:23:25.559 --> 00:23:27.839
<v Speaker 1>hit ut trol plus c, it is. I never realized

569
00:23:27.880 --> 00:23:29.039
<v Speaker 1>it was a signal being sent.

570
00:23:29.160 --> 00:23:32.039
<v Speaker 2>It is, and the kernel is like the postal service

571
00:23:32.240 --> 00:23:34.400
<v Speaker 2>delivering these signals to the right processes.

572
00:23:34.640 --> 00:23:38.319
<v Speaker 1>Okay, So what happens when a process receives a signal?

573
00:23:38.519 --> 00:23:40.799
<v Speaker 1>Does it have to like constantly check for them like

574
00:23:40.839 --> 00:23:42.799
<v Speaker 1>we check our phones for messages.

575
00:23:43.240 --> 00:23:48.960
<v Speaker 2>Not quite No. Linux provides a mechanism for processes to

576
00:23:49.119 --> 00:23:52.920
<v Speaker 2>register signal handlers. Signal handlers these are special functions that

577
00:23:52.960 --> 00:23:55.440
<v Speaker 2>are executed when a specific signal arrives.

578
00:23:55.480 --> 00:23:57.799
<v Speaker 1>So it's like setting up a voicemail box. For specific

579
00:23:57.839 --> 00:23:58.759
<v Speaker 1>types of messages.

580
00:23:59.079 --> 00:24:02.200
<v Speaker 2>That's a great analogy. And just like with voicemail, a

581
00:24:02.279 --> 00:24:06.200
<v Speaker 2>process can choose to handle a signal, ignore it, or

582
00:24:06.319 --> 00:24:07.680
<v Speaker 2>let the default action happen.

583
00:24:08.240 --> 00:24:11.200
<v Speaker 1>And what's the default action? Hopefully it's not always terminating

584
00:24:11.200 --> 00:24:11.839
<v Speaker 1>the process.

585
00:24:12.359 --> 00:24:15.119
<v Speaker 2>Well, it depends on the signal. Okay, Some signals are

586
00:24:15.119 --> 00:24:19.200
<v Speaker 2>designed to be fatal, like the infamous sigsahv.

587
00:24:19.559 --> 00:24:20.759
<v Speaker 1>Oh yeah, I've heard of that.

588
00:24:20.720 --> 00:24:24.799
<v Speaker 2>One, which indicates a segmentation fault ray, meaning your program

589
00:24:24.799 --> 00:24:27.519
<v Speaker 2>tried to access memory it wasn't supposed to ouch. That

590
00:24:27.599 --> 00:24:30.640
<v Speaker 2>sounds painful, it's not good. And in that case, terminating

591
00:24:30.680 --> 00:24:33.160
<v Speaker 2>the process is probably the best course of action.

592
00:24:33.160 --> 00:24:35.359
<v Speaker 1>Right, stop it before it causes any more trouble.

593
00:24:35.559 --> 00:24:39.119
<v Speaker 2>Exactly, it prevents further damage. But for many signals, the

594
00:24:39.319 --> 00:24:42.640
<v Speaker 2>default action might be something less drastic, like pausing the

595
00:24:42.680 --> 00:24:45.160
<v Speaker 2>process or to simply ignoring the signal.

596
00:24:45.400 --> 00:24:47.799
<v Speaker 1>So there's a whole range of responses depending on the

597
00:24:47.839 --> 00:24:48.599
<v Speaker 1>type of signal.

598
00:24:48.720 --> 00:24:50.359
<v Speaker 2>Exactly. It's a rich vocabulary.

599
00:24:50.559 --> 00:24:53.240
<v Speaker 1>This is pretty intricate. But what happens if a process

600
00:24:53.319 --> 00:24:55.519
<v Speaker 1>is in the middle of something important and it doesn't

601
00:24:55.519 --> 00:24:57.079
<v Speaker 1>want to be interrupted by signals?

602
00:24:57.119 --> 00:25:00.400
<v Speaker 2>Ah, good question. Linux has a solution for that too, okay,

603
00:25:00.480 --> 00:25:02.160
<v Speaker 2>I was hoping. So signal masking.

604
00:25:02.279 --> 00:25:03.720
<v Speaker 1>Signal masking, you.

605
00:25:03.680 --> 00:25:08.640
<v Speaker 2>Can temporarily block specific signals from being delivered, okay, essentially

606
00:25:08.640 --> 00:25:09.720
<v Speaker 2>putting them on hold.

607
00:25:09.759 --> 00:25:12.319
<v Speaker 1>So it's like setting your phone to do not disturb mode.

608
00:25:12.119 --> 00:25:13.440
<v Speaker 2>Exactly when you need to focus.

609
00:25:13.519 --> 00:25:13.880
<v Speaker 1>Okay.

610
00:25:13.960 --> 00:25:17.599
<v Speaker 2>The sigproc mask system call okay is your tool for

611
00:25:17.680 --> 00:25:20.400
<v Speaker 2>controlling which signals get through and which ones are blocked.

612
00:25:20.680 --> 00:25:22.720
<v Speaker 1>I like it. We have the power. You have the power,

613
00:25:22.799 --> 00:25:25.160
<v Speaker 1>so we can block signals. But how do we retrieve

614
00:25:25.279 --> 00:25:28.240
<v Speaker 1>those pending signals once we're ready to deal with them.

615
00:25:28.680 --> 00:25:30.920
<v Speaker 2>There are a couple of options, okay. One is to

616
00:25:31.039 --> 00:25:33.920
<v Speaker 2>use the SIG suspend system.

617
00:25:33.519 --> 00:25:34.559
<v Speaker 1>Call sixty cent.

618
00:25:34.799 --> 00:25:38.160
<v Speaker 2>This allows you to temporarily unblock a set of signals

619
00:25:38.599 --> 00:25:42.519
<v Speaker 2>while simultaneously suspending the process waiting for one of those

620
00:25:42.559 --> 00:25:43.440
<v Speaker 2>signals to arrive.

621
00:25:43.519 --> 00:25:45.400
<v Speaker 1>So it's like saying, wake me up when one of

622
00:25:45.400 --> 00:25:47.000
<v Speaker 1>these specific messages.

623
00:25:46.640 --> 00:25:50.279
<v Speaker 2>Arrives, precisely. Another option is to use sigpending okay, to

624
00:25:50.359 --> 00:25:53.920
<v Speaker 2>peak at the queue of pending signals without actually unblocking them.

625
00:25:53.960 --> 00:25:56.720
<v Speaker 2>It's like checking your voicemail without listening to any messages.

626
00:25:56.400 --> 00:25:59.279
<v Speaker 1>Okay, so just seeing if anything's there exactly. This whole

627
00:25:59.319 --> 00:26:02.240
<v Speaker 1>signal handle system is surprisingly sophisticated.

628
00:26:02.759 --> 00:26:04.839
<v Speaker 2>Is there's a lot of thought that goes into it.

629
00:26:04.920 --> 00:26:08.079
<v Speaker 1>I never realized how much thought goes into managing these

630
00:26:08.200 --> 00:26:09.559
<v Speaker 1>interprocess messages.

631
00:26:09.759 --> 00:26:11.960
<v Speaker 2>Yeah, it's a crucial part of making sure that everything

632
00:26:12.039 --> 00:26:15.599
<v Speaker 2>runs smoothly and that processes can cooperate effectively.

633
00:26:15.680 --> 00:26:18.039
<v Speaker 1>Wow, this deep dive has been packed with information.

634
00:26:18.319 --> 00:26:20.599
<v Speaker 2>It has. We've covered a lot of ground, from.

635
00:26:20.359 --> 00:26:24.640
<v Speaker 1>Those fundamental concepts of files, processes, and signals to those

636
00:26:24.720 --> 00:26:26.359
<v Speaker 1>advanced techniques we've just touched upon.

637
00:26:26.400 --> 00:26:27.359
<v Speaker 2>It's been quite a journey.

638
00:26:27.400 --> 00:26:29.759
<v Speaker 1>It feels like we've uncovered a whole hidden layer of

639
00:26:29.799 --> 00:26:30.960
<v Speaker 1>the operating system.

640
00:26:31.039 --> 00:26:34.000
<v Speaker 2>And that's the beauty of Linux system programming. There's always

641
00:26:34.039 --> 00:26:37.400
<v Speaker 2>something new to discover, something deeper to explore. It's a

642
00:26:37.480 --> 00:26:38.880
<v Speaker 2>journey that never really ends.

643
00:26:39.400 --> 00:26:42.279
<v Speaker 1>I'm definitely feeling that sense of wonder. This deep dive

644
00:26:42.359 --> 00:26:44.920
<v Speaker 1>has not only deepened my understanding of Linux, but has

645
00:26:44.960 --> 00:26:49.559
<v Speaker 1>also sparked a real desire to keep learning and experimenting.

646
00:26:50.039 --> 00:26:52.359
<v Speaker 2>That's the best outcome we could hope for, and to

647
00:26:52.440 --> 00:26:55.839
<v Speaker 2>our listeners, we encourage you to continue your own explorations

648
00:26:55.880 --> 00:26:59.319
<v Speaker 2>into the fascinating world of Linux system programming. There's a

649
00:26:59.400 --> 00:27:02.559
<v Speaker 2>universe of no waiting to be discovered, and who knows

650
00:27:02.599 --> 00:27:04.039
<v Speaker 2>what amazing things you might create.

651
00:27:04.240 --> 00:27:06.559
<v Speaker 1>Well said, Until next time, Happy coding everyone,
