YouTube2Text

pipes 1 — Transcript

by juancarlosjromer · 3,045 words · 443 segments · language en · Watch on YouTube

Full transcript

  1. 0:01Good morning. Welcome to this new video
  2. 0:05on operating systems. In it, we will
  3. 0:08try to show or introduce pipe
  4. 0:10programming. This video consists of
  5. 0:14three parts. The first part is a basic
  6. 0:17theoretical introduction with some
  7. 0:19simple examples. The second part is the
  8. 0:22presentation of an example problem, and
  9. 0:25the third part is the solution to the
  10. 0:27example problem. Well, let's get
  11. 0:30started. Introduction to pipe
  12. 0:36programming. One of the simplest forms
  13. 0:40of communication between related
  14. 0:43processes is unnamed pipes, also called
  15. 0:46unnamed pipes, whose system call to
  16. 0:49create the resource is in pipe and we
  17. 0:52pass it as an argument a vector of
  18. 0:55integers used between related processes
  19. 0:58, which are processes created with the
  20. 1:01FOR system call, which creates a
  21. 1:04relationship between a parent process
  22. 1:06and a child process. This system call
  23. 1:10causes the child process to inherit the
  24. 1:12descriptors of the pipe communication
  25. 1:14resource created by the parent process.
  26. 1:18The pipe is an area of memory where
  27. 1:20processes can write and read in a fifo
  28. 1:23order, thus allowing communication. In
  29. 1:29all the code examples, we assume that
  30. 1:31the functions that are executed never
  31. 1:33fail. We do this to reduce the
  32. 1:36complexity of the code and focus on
  33. 1:38pipe handling. Here we see a very
  34. 1:45simple example of a process that
  35. 1:48creates a pipe, creates a child, and
  36. 1:50both the parent and child display the
  37. 1:56read and write descriptors that can be
  38. 1:59used to read from and write to the pipe
  39. 2:02. Pipes are best known for their use
  40. 2:07from the shell command line, where
  41. 2:10processes are connected through a pipe
  42. 2:12and data flow is in one direction, as
  43. 2:15shown in the following command line,
  44. 2:17where the pipe is the vertical bar. The
  45. 2:22LS process writes its output to the
  46. 2:25pipe. The SOR process reads its input
  47. 2:29from the pipe and writes its output to
  48. 2:31another pipe, and the GRP command reads
  49. 2:33its input from the pipe and writes its
  50. 2:36output to the screen. To connect two or
  51. 2:43more processes through a pipe, we have
  52. 2:46to create a pipe before a process
  53. 2:48creates a child process, since the
  54. 2:50child inherits the table of open file
  55. 2:53descriptors from the parent, and will
  56. 2:56also inherit the copy of the
  57. 2:57descriptors associated with the pipe.
  58. 3:03Establishing communication between
  59. 3:05processes using pipes is quite simple,
  60. 3:08since pipes provide a layer of
  61. 3:10synchronization of locks in writing and
  62. 3:13reading, meaning that the programmer
  63. 3:15does not have to include codes for
  64. 3:18synchronization in the communication
  65. 3:20between two processes. A process
  66. 3:25writing to the pipe can fill the pipe
  67. 3:27and another process reading the pipe
  68. 3:29can change the pipe, since reading from
  69. 3:31the pipe is destructive. In these cases
  70. 3:35, when the pipe is full, the writer
  71. 3:37magically blocks writing and when the
  72. 3:40pipe is empty, the reader magically
  73. 3:43blocks reading as well. Of course, for
  74. 3:47this to happen, certain conditions must
  75. 3:49be met, which we will see later. If we
  76. 3:54add to this that reading from the pipe
  77. 3:56is destructive, allowing the pipe to be
  78. 3:58used in such a way that it never
  79. 4:00exhausts its storage capacity and that
  80. 4:03reading is done in the same order in
  81. 4:05which it was written to the pipe, all
  82. 4:07this makes communication very efficient
  83. 4:09with very little programming. Of course
  84. 4:14, we must have a great command of the
  85. 4:17pipe in programming so that deadlocks
  86. 4:20do not occur. First, it must be taken
  87. 4:24into account that although a P allows
  88. 4:26many processes to write to it and many
  89. 4:28processes to read from it concurrently,
  90. 4:30the most appropriate thing would be to
  91. 4:33have only one writer and one reader on
  92. 4:35a pipe at a given time. This will avoid
  93. 4:39unwanted deadlocks. Something to keep
  94. 4:42in mind is that pipes only allow
  95. 4:44related processes to communicate. We
  96. 4:47can say that related processes are
  97. 4:50those that are created using a fork and
  98. 4:52that have a parent-child relationship.
  99. 4:56Let's do an example of a reader process
  100. 4:59and a writer process using a pipe. The
  101. 5:02diagram shows a parent process writing
  102. 5:05to a pipe and a child process reading
  103. 5:07from a pipe. Here we have the code and
  104. 5:12we can see on line seven the definition
  105. 5:15of the child variable of type PIT T on
  106. 5:19line 8 or the declaration of the vector
  107. 5:22of two integers to store what the pipe
  108. 5:25system call returns. On line nine we
  109. 5:30execute the pipe system call passing it
  110. 5:33as an argument the vector p of two
  111. 5:36integers. On line 10 we create the
  112. 5:39child. On line 11 we ask if the Fork
  113. 5:43system call returned -1, then it could
  114. 5:46not create the child and we return the
  115. 5:49yel -2. On line 12 we ask if we are in
  116. 5:53the execution of the child. If we are
  117. 5:57in the execution of the child, the
  118. 6:00child closes the write descriptor
  119. 6:02because it will only read and displays
  120. 6:05the values of the file descriptors. On
  121. 6:09line 18 the execution of the parent
  122. 6:13begins. The parent closes its reading
  123. 6:16channel because it will only write and
  124. 6:19also displays the pipe descriptors
  125. 6:21obtained from the pipe system call. It
  126. 6:25is important to highlight the close P1
  127. 6:27instruction in the child process and
  128. 6:30the close P0 instruction in the parent
  129. 6:32process, since this specifies that
  130. 6:34there is only one reader process and
  131. 6:37one writer process. Otherwise, we could
  132. 6:40end up with problems or some type of
  133. 6:43deadlock. Well, here we are going to
  134. 6:46show two reader processes and two
  135. 6:48writer processes. Although only one
  136. 6:51parent process and one child process
  137. 6:53are shown, it is important to note that
  138. 6:55there are two writers and two readers.
  139. 6:58This means that each process fulfills
  140. 7:00the function of writer and reader at
  141. 7:02the same time. The diagram shows how
  142. 7:06the parent process writes and reads and
  143. 7:09how the child process also writes and
  144. 7:11reads. It is important to note that
  145. 7:14writing is done through the same end
  146. 7:17and reading is also done through the
  147. 7:19same end. This is represented in the C
  148. 7:23code by the descriptors. This means
  149. 7:26that there is a descriptor for writing
  150. 7:28and whenever something is written, it
  151. 7:30will be written to that descriptor and
  152. 7:32there is only one descriptor for
  153. 7:34reading and whenever something is read,
  154. 7:36it will be read from that descriptor.
  155. 7:39Unlike the previous code, this code
  156. 7:41does not include the close in the child
  157. 7:44process and does not include the close
  158. 7:47in the parent process. This makes the
  159. 7:50big difference that in this code we
  160. 7:53have two processes: writers and two
  161. 7:56reader processes. It is important to
  162. 8:00note that communication in pipes is
  163. 8:03unidirectional or half-duplex. This
  164. 8:06means that writing is done through file
  165. 8:09descriptor 1 and always reading is done
  166. 8:12through file descriptor 2. What happens
  167. 8:14to a writer process when it tries to
  168. 8:16write to a full pipe? A writer process
  169. 8:21tries to write the alphabet in the pipe
  170. 8:24many times and blocks when the pipe is
  171. 8:26full. In this case, the only process is
  172. 8:31a writer and a reader. By default, when
  173. 8:35a pipe is created, the process or
  174. 8:37processes that obtain the read and
  175. 8:40write descriptors, having inherited the
  176. 8:43descriptors from the execution of the
  177. 8:45pipe system call, are readers and
  178. 8:48writers. Unless otherwise specified,
  179. 8:50the close function can be used to close
  180. 8:53the descriptor that it is not going to
  181. 8:56use, thus converting a process into a
  182. 8:58writer-only or reader-only process. The
  183. 9:03diagram shows a single process that
  184. 9:05creates a pipe and by default it is a
  185. 9:08writer and a reader. That process
  186. 9:13writes to the pipe constantly. Of
  187. 9:18course, since the pipe has a finite
  188. 9:20storage capacity, at some point the
  189. 9:23pipe will fill up. What will happen
  190. 9:27then when the process tries to write to
  191. 9:31a full pipe, knowing that the process
  192. 9:34is also a reader because it did not
  193. 9:38close the file descriptor? The
  194. 9:42following code shows a reader and
  195. 9:45writer process in Pythes. You will see
  196. 9:49that there is no system call here that
  197. 9:51creates any child processes, but there
  198. 9:53is a system call that creates the pipe.
  199. 9:56So this process that creates the pipe
  200. 9:58becomes both a reader and a writer of
  201. 10:01the pipe. Furthermore, we do not see
  202. 10:04any call to close. Note that the
  203. 10:07process enters an infinite writing
  204. 10:10cycle in the PEP. So, if it writes
  205. 10:14constantly, it is assumed that at some
  206. 10:16point, if there is no reader emptying
  207. 10:21the pipe at the reading end, the pipe
  208. 10:24will fill up. When the pipe fills up,
  209. 10:29then the process will try to write to
  210. 10:32the pipe again. At that point, the
  211. 10:37process will be blocked in the WR
  212. 10:39system call to write to file descriptor
  213. 10:431, waiting infinitely until a reader
  214. 10:46process reads and makes space in the
  215. 10:49pipe. In this case, since no reading is
  216. 10:54seen on the pipe, the process will be
  217. 10:57blocked indefinitely. Well, this
  218. 11:00process is a reader and writer because
  219. 11:03it did not close P0 indicating that it
  220. 11:06would not read from the pipe. So, here
  221. 11:09the process writes until the pipe is
  222. 11:11full. When the pipe is full, since the
  223. 11:16pipe has a limited byte capacity to
  224. 11:18store, the process blocks on the right
  225. 11:21waiting for the pipe to be read so that
  226. 11:24free space is generated for writing,
  227. 11:27since reading is destructive. It should
  228. 11:31be noted that the writer process blocks
  229. 11:33on writing when the pipe is full and if
  230. 11:36there is a reader process. This is very
  231. 11:39important. It blocks on writing if
  232. 11:41there is a reader process. If the pipe
  233. 11:45is full and the process wants to write
  234. 11:48to the pipe, but there is no reader, it
  235. 11:51is a completely different situation.
  236. 11:54What happens to the reader when it
  237. 11:56tries to read from an empty pipe? A
  238. 12:00reader process reads from a pipe and
  239. 12:02blocks when the pipe is empty. The
  240. 12:05diagram shows a parent process that is
  241. 12:08now constantly reading from a pipe. It
  242. 12:13is the same situation as the previous
  243. 12:15case or similar. Why? Because the
  244. 12:18parent process is both a reader and a
  245. 12:20writer. It has to be both a reader and
  246. 12:23a writer for this situation to occur.
  247. 12:26In this case, the only process is a
  248. 12:28writer and a reader. By default, when a
  249. 12:30pipe is created, the process or
  250. 12:32processes that obtain the read and
  251. 12:34write descriptors are readers and
  252. 12:36writers. If not, if otherwise specified
  253. 12:38, the close function can be used to
  254. 12:40close the descriptor that is not going
  255. 12:42to be used, thus converting a process
  256. 12:44into a reader-only or writer-only
  257. 12:46process. We are reiterating this, but
  258. 12:50this will help you understand the
  259. 12:52handling or programming of PPES. Here
  260. 12:58we have some code that shows a process
  261. 13:00that creates a pipe, has no children,
  262. 13:03is a single process and that is
  263. 13:05constantly reading from a pipe. Of
  264. 13:09course, this process will block on the
  265. 13:12first attempt to read. Why? Because the
  266. 13:16pipe is empty. It is very important
  267. 13:20that in order for a reader process to
  268. 13:23block in a read when the pipe is empty,
  269. 13:26there must be at least one writer
  270. 13:29process. If we have a reader process
  271. 13:33that tries to read from an empty P and
  272. 13:36there are no writers, the situation is
  273. 13:38different. This process is a reader and
  274. 13:44writer because it did not close P1
  275. 13:46indicating that it would not write to
  276. 13:48the pipe. So here the process reads
  277. 13:51until the pipe is empty. When the pipe
  278. 13:54is empty, in fact the pipe is empty,
  279. 13:56since no process writes to it. As we
  280. 13:59mentioned before, the process will
  281. 14:02block in the first RIP attempt it makes
  282. 14:05on P sub0. The process blocks in the
  283. 14:08RIT waiting for a write to be made to
  284. 14:10the P so that the reading can be
  285. 14:12unblocked. Of course, since there is no
  286. 14:15writing, no process or the process
  287. 14:19itself is not writing, this process
  288. 14:22remains blocked indefinitely in the RIP
  289. 14:26. It is important to note that the
  290. 14:29reader process blocks in reading when
  291. 14:31the pipe is empty and if there is a
  292. 14:33writer process. Why? If there is no
  293. 14:37writer process, then the situation is
  294. 14:39completely different. In the two
  295. 14:42previous cases we had parent processes
  296. 14:45without children. Well, perhaps we
  297. 14:48could have avoided using the word
  298. 14:50parent and simply used process, but
  299. 14:53anyway, in the two previous cases we
  300. 14:55had parents and only processes. In this
  301. 14:59case we already have two processes. We
  302. 15:02have a parent process and a child
  303. 15:04process. What happens to the writer
  304. 15:07process when all the reader processes
  305. 15:10finish? This means that we will have a
  306. 15:13writer or a writer process on a pipe,
  307. 15:16but there will be no reader processes.
  308. 15:22Let's suppose that a child process
  309. 15:24writes the alphabet in the pipe and the
  310. 15:26parent process reads the alphabet from
  311. 15:28the same pipe. But when the reader
  312. 15:32reads the letter r, it closes the
  313. 15:34reading channel or, in other words, it
  314. 15:37closes P0. So, we no longer have a
  315. 15:42reader, only the child process is left
  316. 15:46writing to the PE. So, the child
  317. 15:50process is immediately terminated by
  318. 15:53the operating system. For this to
  319. 15:56happen we have a writer and a reader
  320. 16:02and only one writer and one reader.
  321. 16:05That's why the parent closes its
  322. 16:07channel or closes its write descriptor
  323. 16:09file. It will only read. There we show
  324. 16:14that we only have one reader and the
  325. 16:16child closes its read descriptor file.
  326. 16:20There we have exactly one child process
  327. 16:23that is only going to write. In other
  328. 16:25words, we have a reader and a writer.
  329. 16:29That is what we need to show this
  330. 16:31situation. The writer process is
  331. 16:35terminated in writing when the reader
  332. 16:38process closes its read descriptor file
  333. 16:41, executing, of course, the close of p
  334. 16:44or simply the child process, this,
  335. 16:47sorry, the parent process, which is the
  336. 16:50reader, ends. So, we repeat, we have a
  337. 16:54writer process, only one writer process
  338. 16:57, we have only one reader process. And
  339. 17:01what happens when we have a writer
  340. 17:05without a reader? Well, at the same
  341. 17:09time that there is no longer a reader
  342. 17:12process, the writer process ends as
  343. 17:15long as it is in the WR right. Okay,
  344. 17:18here we have the code that shows this
  345. 17:20situation. We see that on line 8 the
  346. 17:24process creates the pipe, on line 9 the
  347. 17:27process creates a child. On line 12 the
  348. 17:30execution of the parent process begins,
  349. 17:34which on line 13 closes the write
  350. 17:36descriptor file because it is only
  351. 17:39going to read. Okay, then we have a
  352. 17:43loop that the parent process reads on
  353. 17:49the pipe until it reads the letter R.
  354. 17:53When it reads the letter R, it exits
  355. 17:56the loop and closes the reading writer
  356. 18:00file. This means that the parent is no
  357. 18:03longer a writer. It stopped being a
  358. 18:07writer on line 13 and now stops being a
  359. 18:10reader on line 21. This does not mean
  360. 18:13that the child, whose code starts on
  361. 18:16line 25, is now a writer. It has
  362. 18:20written the alphabet up to the letter
  363. 18:22R. On the letter R it says, "I'm not
  364. 18:26reading anymore." So, when the child
  365. 18:31process wants to write on line 29 with
  366. 18:34the WR on the descriptor file, it will
  367. 18:37find or at that moment there is no
  368. 18:40reader, so the child process is
  369. 18:43automatically terminated. What happens
  370. 18:48to the reader process when all the
  371. 18:50writer processes finish? We maintain
  372. 18:54the same scenario as in the previous
  373. 18:56case, but now considering that the
  374. 18:59writer process, when it writes the
  375. 19:02letter R to the pipe, closes its
  376. 19:04writing channel with the close of P
  377. 19:07sub1. Then the child process reads from
  378. 19:13the pipe until it is empty and is not
  379. 19:15blocked, it continues its execution.
  380. 19:19The diagram shows the same diagram as
  381. 19:22the previous case: a parent that only
  382. 19:25reads, a child that only writes; and we
  383. 19:28consider, and it is important, that the
  384. 19:31reader process does not block in
  385. 19:33reading when the pipe is empty and
  386. 19:36there are no writers, because it or the
  387. 19:39writers closed their file descriptor by
  388. 19:42executing clock (P1) or simply
  389. 19:45terminated. So, let's summarize. A
  390. 19:50reader process does not block in
  391. 19:53reading when there are no writers. It
  392. 19:58continues its execution. Here we have
  393. 20:01the code, the C language of this
  394. 20:04situation. Eh, we see that when the
  395. 20:08child writes the letter R in the pipe,
  396. 20:11eh, it exits the writing cycle and
  397. 20:14closes its writing descriptor file.
  398. 20:19Since it closed its reading writer file
  399. 20:22on line 26, it means that there is no
  400. 20:24longer a writing process in the
  401. 20:26communication. So, look at line 13 to
  402. 20:33line 22, how the parent process reads
  403. 20:40until the pipe is empty, but when the
  404. 20:43pipe is empty, we put an infinite loop
  405. 20:47there to show that it continues
  406. 20:50executing. Well, the most important
  407. 20:53characteristics of pipes. Pipes only
  408. 20:57allow parent processes to communicate.
  409. 20:59We can say that parent processes are
  410. 21:01those that are created using a fork and
  411. 21:04that have a parent-child relationship.
  412. 21:07Communication between processes using
  413. 21:10pipes is unidirectional. This means
  414. 21:14that only one end of the pipe is
  415. 21:16written to and the other end of the
  416. 21:18pipe is read to, represented in the
  417. 21:20code by the ray p sub1 and the read p
  418. 21:22sub0. The writer process blocks on
  419. 21:26writing when the pipe is full and if
  420. 21:29there is at least one reader process,
  421. 21:31the reader process blocks on reading
  422. 21:34when the pipe is empty and if there is
  423. 21:37at least one writer process. Reading
  424. 21:40from a pipe is destructive. The reading
  425. 21:43order is fifo. The writer process is
  426. 21:47terminated on writing by the system.
  427. 21:50When the reader process closes its
  428. 21:52reading channel with cloper or
  429. 21:54terminates, which is the same as saying
  430. 21:57when there are no more reader processes
  431. 21:59, the writer process is terminated in
  432. 22:01the attempt to write to the pipe. A
  433. 22:05reader process, a reader process if it
  434. 22:10is blocked in the read in an empty pipe
  435. 22:12waiting for a writer process to write,
  436. 22:15but the writer closes its writing
  437. 22:17channel with the clock P1 or for some
  438. 22:20reason terminates, the reader process
  439. 22:22is unblocked and continues with the
  440. 22:25logical execution of its code if it was
  441. 22:27programmed that way. Well, we have
  442. 22:31finished the first part of the
  443. 22:32introduction to Pipes programming.

About this transcript

This page contains the full transcript of pipes 1 by juancarlosjromer, generated from the public captions YouTube serves with the video. The transcript has 3,045 words across 443 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.