Working with multiple pipes

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ม.ค. 2025

ความคิดเห็น • 81

  • @Clarin3t1
    @Clarin3t1 2 ปีที่แล้ว +24

    Sir, this playlist on processes has really saved my life. I did not understand from my teacher explaining at all, and from your videos I understand everything MUCH better. AMAZING videos, thank you so much.

  • @TheCStrike2
    @TheCStrike2 3 ปีที่แล้ว +7

    youre the realest man. An absolute life saver. Like the only useful guy covering this stuff.

  • @joeliquori
    @joeliquori 3 ปีที่แล้ว +7

    Great video man, I had an exercise for my class and you helped me understand this concept fully. Much appreciated for your hard work and thoughtful explanation!

  • @masoudsultanian9887
    @masoudsultanian9887 4 ปีที่แล้ว +2

    Thank you very much for your exhaustive explanation. In this video you disambiguate my ambiguities with several processes communicating with pipe(). your job is brilliant.

  • @liaolii
    @liaolii ปีที่แล้ว

    This playlist has really helped me with my class project. Thanks a lot!

  • @shouider
    @shouider 2 ปีที่แล้ว +10

    My rule of thumb for remembering what ends 0 and 1 represent is "1 is for wr1te". Hopefully it helps!

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว +3

      Nice trick!

    • @TravellerMann
      @TravellerMann ปีที่แล้ว

      My rule is this: since len(read) < len(write), so we assign 0 to read and 1 to write. 😊

  • @priyeshtandel2101
    @priyeshtandel2101 2 ปีที่แล้ว +2

    ☺ Thank you for this series

  • @zxuiji
    @zxuiji 2 ปีที่แล้ว

    3:38, personally I wouldn't create all the pipes in one process, rather I would open pipes & do the fork (or just open pipes with an existing process), let's say we have 4 processes, named A (the one we coded), B, C, & D, the messages would I would send would look like this:
    A to B: Open pipes with "C" & declare "C" your "next" process & declare "A" your "prev" process
    A to C: Open pipes with "D" & declare "D" your "next" process & declare "B" your "prev" process
    A to D: Declare "A" your "next" process & declare "C" your "prev" process
    A to B: Set "X" as "N" & handle "X" & Pass "this" to "next"
    B to C: Set "X" as "N" & handle "X" & Pass "this" to "next"
    C to D: Set "X" as "N" & handle "X" & Pass "this" to "next"
    D to A: Set "X" as "N" & handle "X" & Pass "this" to "next"
    Each process will check the origin of a message, if it finds itself it will not pass it on, this method does require an agreed upon messaging system, in this case I would probably use a string

  • @muhammadabdullah-kp3lh
    @muhammadabdullah-kp3lh 2 ปีที่แล้ว +1

    Great video
    !! Helped a lot in college assignment

  • @play_sports_and_read_books
    @play_sports_and_read_books 3 ปีที่แล้ว +3

    So the number of pipes will be 2n^2
    Which means we will close them 2n^2 times.
    There should be 2 open in each child.
    For child n it will be read of child (n-1) and write of child (n)
    The main process will be write of 0 (which is the value of n for the parent process) and the read will be of (0-1) which is the max value of (n)
    When implementing the code think about the number of children is (n) and the parent is the last process. Following this logic will make what i wrote above work without subtracting an extra one or so.
    For example, in the video above we had 2 children and 1 parent so we had a total of 3 processes(but think of n here to be 2, but when declaring the array declare it to n+1), child1(n-1) will be child1(0) [for reading]
    And child1(n) will be child1(1) [for writing]
    And child2(n-1) will be child2(1)....etc.
    The same logic applies to the parent as in writing he will be fd[0] and in reading he will be fd[n] or in this case fd[2]

    • @Merlz.
      @Merlz. 5 หลายเดือนก่อน

      how would we keep track of the n through the processes? for example, we dont know the number, it could be changing each time it runs. My question is, how would we know which num we are currently in when writing the code? (i dont think i worded that correctly)

    • @Merlz.
      @Merlz. 5 หลายเดือนก่อน

      like, imagine we have multiple pipes but we cant know how many there are, how do I know which fd[this is][]?

  • @mrericwonghk
    @mrericwonghk 9 หลายเดือนก่อน +1

    Hi sir! May I ask why after calling fork() twice in the video there are only 3 processes instead of 4? Thanks!

    • @CodeVault
      @CodeVault  9 หลายเดือนก่อน

      Because I have some returns in each child process, therefore none of the child processes call fork()

  • @the_teemo1
    @the_teemo1 3 ปีที่แล้ว +4

    shouldn't waitpid() go before the write and read functions for the parent? i.e., whats the point of waiting AFTER the parents read and write already? thanks

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +4

      wait is NOT really used for synchronization here. Read calls will wait for data to be in the calls and write calls will wait for space to be available in the pipe's buffer. So you don't need any synchronization between processes here. waitpid is simply there to RELEASE RESOURCES, otherwise, the resources occupied by the child process won't be released, and would cause a RESOURCE LEAK.

    • @the_teemo1
      @the_teemo1 3 ปีที่แล้ว +1

      @@CodeVault thanks for the quick response!

  • @hihiirokane_gbf
    @hihiirokane_gbf ปีที่แล้ว

    Thank you so much for saving my hw.

  • @etinosaizekor6533
    @etinosaizekor6533 3 ปีที่แล้ว

    Amazing video. Big thanks!

  • @amirelhalabi1955
    @amirelhalabi1955 2 ปีที่แล้ว +1

    shouldn"t we add waitpid(pid1,NULL,0) in the if(pid2==0) statement? pid2 should wait pid 1 to finish also before it starts i assume , or maybe the waitpid works only between parent and child proccess?if yes can you explain or do you have any video about this?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว +2

      I think a similar question was asked in the comments here but basically:
      No wait is needed. The read() function already waits for something to be inside the pipe for it to be read. Similarly, the write() statement will wait for there to be enough space in the pipe's buffer for it to write. That's why we only have to wait at the end of the processes and we don't have to worry about order of execution for the processes

  • @PranujAmatya
    @PranujAmatya 4 หลายเดือนก่อน

    I have a question about closing the pipes. It seems it would be easier to also generalise closing all of them with one single function at the end of each process. Is it wrong or somehow inadvisable to close all pipes at the end of each process instead of closing the useless pipes at the beginning and the used ones at the end?
    PS: huge fan of your videos and the way you prepare your lectures. They’ve helped me a lot in this learning process. May you get the attention you deserve. 🖖

    • @CodeVault
      @CodeVault  3 หลายเดือนก่อน

      When reading from a pipe you actually get an EOL if all writer fds have been closed. Therefore it's advisable to close them as soon as you don't need them

  • @AndyAzuron
    @AndyAzuron 4 ปีที่แล้ว +4

    Great video, love the in-depth explanation with drawings. Another question: how do you manage the pipe of more than 2 child processes in a for loop without hardcoded close(fd[x][x]). E.g. for (i=0;i

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +5

      That was the first idea regarding this video but it was way too complicated and difficult to follow. But I will make a video on this as so many have actually requested this already. Thanks for the suggestion!

    • @chocolatteu
      @chocolatteu 2 ปีที่แล้ว +6

      @@CodeVault hello! is there a video on this already?

    • @harryviennot5114
      @harryviennot5114 2 ปีที่แล้ว +4

      @@CodeVault we need this video!!

  • @avt996
    @avt996 4 ปีที่แล้ว +1

    thank you a lot :) great great explanation

  • @aribenjamin862
    @aribenjamin862 ปีที่แล้ว

    Thanks for a great video! How does this work if I decide to split it into multiple functions? How does process communicate with each other through pipes, across functions? Do I simply pass the fd, and it will read/write from the same place?

    • @CodeVault
      @CodeVault  ปีที่แล้ว +1

      Just make the fd global, initialize the pipes before forking and, after forking, the global fds will have same value in both processes

  • @liaolii
    @liaolii ปีที่แล้ว

    Would it be bad to have just a single pipe for this single all the process either just read or just write when communicating to the other?

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      Yes, it would cause issues. Take the main process for example: you write x to the pipe then read from it. More often than not, you would be reading the same x you wrote to the pipe thus, causing the rest of the processes to wait forever for something to be written to that pipe

  • @Sandy-mv3tl
    @Sandy-mv3tl 3 ปีที่แล้ว +1

    Hi. Isn't it possible that a process reads from a particular pipe before another process has written to it? Since they are running in parallel?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +2

      A read call on a pipe will WAIT for something to be there in the pipe to be read. Subsequently a write call on a pipe will WAIT for there to be enough space in the buffer.

  • @tseegiitseku2932
    @tseegiitseku2932 ปีที่แล้ว

    thank so much, i would like to ask in this case how many forks do i need? as i see it is 1, right?

    • @CodeVault
      @CodeVault  11 หลายเดือนก่อน +1

      Each fork creates a new process. Depends on how many pipes you want to create

    • @tseegiitseku2932
      @tseegiitseku2932 11 หลายเดือนก่อน

      thank you so much
      @@CodeVault

    • @tseegiitseku2932
      @tseegiitseku2932 11 หลายเดือนก่อน

      thank you so much
      @@CodeVault

  • @hasti1374
    @hasti1374 4 ปีที่แล้ว

    Thank you very much. The concept was explained entirely. What if the order of processes is essential; for instance: we want first process1 and then 2 and finally 3 be run

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      That just depends on which process writes first to the pipes. In the video it's the parent process that FIRST writes and then reads (while all the others FIRST read then write). You'd just have to change which process FIRST writes to the pipes.

  • @narasarajv5278
    @narasarajv5278 4 ปีที่แล้ว

    Super explanation helped a lot...

  • @Victor_Marius
    @Victor_Marius 4 ปีที่แล้ว

    for n processes:
    n pipes
    2 * n file descriptors
    2 * n^2 total close calls

  • @Luca-ts8dq
    @Luca-ts8dq ปีที่แล้ว

    In this example, for every process you always close the unused 4 fd [ ] [ ]s, run some code, then close the outstanding 2 fd [ ][ ]s. Would it not be more comfortable to close all 6 together maybe iterating with a for loop towards the end of each process? Thanks again very much also for this lesson, I agree with everybody here appreciating the excellence of you help.

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      When closing all writing ends you get an EOF sent to all the readers when the last writing fd was closed therefore you can figure out that whatever needed to be sent got sent. If we leave all the unused writing fds opened until the end of the program you no longer get this EOF. In the video I don't give an example of this aspect so I might make a video regarding this later on

  • @manuelconte623
    @manuelconte623 2 ปีที่แล้ว

    is it necessary to have "pid1" and "pd2" to create the processes? wouldn't just "pid" be enough to create n processes?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      You can create the processes without storing the return value of the fork call. It will just be a bit more tricky to distinguish between the processes in the code

  • @ShivamSharma-nb6gh
    @ShivamSharma-nb6gh 3 ปีที่แล้ว

    Family of exec function is not working in visual studio on Windows connected to WSL UBUNTU Linux . Fork() is working fine but exec family is not working neither it is showing any error it's just ignored in program 😦
    Please help is, there anything with directory @CodeVault

  • @zxuiji
    @zxuiji 2 ปีที่แล้ว

    8:56, here I would just declare an unsigned integer and use it's bits as booleans, something like
    ...
    if ( pid1 == 0 )
    {
    unsigned int i, keep = (5u

  • @xiLike2Doitx
    @xiLike2Doitx 2 ปีที่แล้ว

    loved every second. really helpful stuff.
    I was successfully able to implement df->grep and fetch the results back at main.
    just couple questions on my mind:
    1. what if I cant tell the size of input I'm about to read?
    reading the stdout of grep could be 1 line of many. at the moment I've just set a buffer@255 to read back but doesn't actually cover all cases.
    2. what about passing variables on forking? Do I need to write them to the child process from the main?
    for example if I want grep to catch a certain 'device', is it something that can be passed by inheritance ?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      1. Technically, you could read character by character (or byte by byte, or int by int etc.) and stop reading whenever you encounter a certain value (like 0, for example).
      2. If you assign variables before forking then those variables are available with the same values in both processes. fork() copies all the memory from parent to child
      int a = 15;
      fork();
      // both parent and child will have a with value 15

  • @Marc-og6vs
    @Marc-og6vs 4 ปีที่แล้ว

    Thank you for the videos, I have learned so much already from them. I have a question as I’m a bit stuck on the challenge at the end of the video. I have added a for loop to initialize ‘n’ the file descriptors successfully but I cannot work out how to create ‘n’ pids and fork them. Any clues to point me in the right direction would be greatly appreciated!

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +1

      After you fork in the for loop, break when in the child process and let the loop continue when in the parent process

  • @sukitd
    @sukitd ปีที่แล้ว

    I tried to input as your coding, but I got no change of x in main. Could you help to share a code for co-checking?

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      Sure, you can send the code here or on discord (discord.code-vault.net) and I will check it out

  • @zakarialahmouz3902
    @zakarialahmouz3902 2 ปีที่แล้ว

    The best tutorial

  • @esthervig4213
    @esthervig4213 2 ปีที่แล้ว

    This video is really good 👍.
    I have one question, what if one child process starts to run before information is sent in pipe, will it read -1 and go to the next code line? Thanks.

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      No. read() waits for data to be available for reading inside the pipe's buffer. Similarly write() waits for space to be available inside the pipe's buffer

  • @hasti1374
    @hasti1374 4 ปีที่แล้ว

    could you please provide a tutorial on sending a signal amongst processes; like kill signal

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      In the processes playlist there's about 5 videos on Linux signals. Here's the first one:
      th-cam.com/video/5We_HtLlAbs/w-d-xo.html

  • @mikelgarciarodriguez6961
    @mikelgarciarodriguez6961 3 ปีที่แล้ว

    Why you should close fds that are not going to be used in the child process? What happend if you don´t do it?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +2

      Not much really... The operating system will eventually clean them up. it's just good practice as you can't be sure the resources are always cleaned up. It's similar to how you should free dynamically allocated memory. If the program runs for a few seconds, the leaked resources are probably going to be cleaned up eventually, but if it runs for hours or days and keeps hogging and leaking memory, it could cause a problem and completely slow down or not allow other processes to work properly.

  • @Wcverhaagh
    @Wcverhaagh 3 ปีที่แล้ว

    Really helpful and greatly increased my knowledge of pipes for my os class. one question though. I am trying to make this code so it can infinitely handle pipes (mimicking a c shell) but I'm a bit confused on how many pids, should be ran per pipe entered in a command line. in the prior pipe video in the grep example had 1 " | " symbol in the equation yet in this video you said with three pipes you need only two pids still. This leaves me a bit confused if the input is something like " xyz | 123 | abc" in which you type three pipes how do you only have 2 pids and not 3? Would appreciate if you could give me some incite.

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      I'm not sure exactly what you mean by "pids". In this example I use 3 processes but, for one of the process, I simply use the main process instead of creating a new one using fork

  • @fodge6419
    @fodge6419 2 ปีที่แล้ว

    hi i make the same code but i don't have the same result i don't know why ? i check five time for be sure to have the same to you but I don't understand why that not work I work in macOS systeme that can be the cause of the pb ?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว

      Try copying the code from the website code-vault.net/lesson/mrvonl5pmz:1603732432986 and see if that works. Also lookout for compilation warnings

  • @PepeBote11a
    @PepeBote11a 3 ปีที่แล้ว

    amazing

  • @sulkhan5302
    @sulkhan5302 4 ปีที่แล้ว

    why can't we use the same fd in different child processes? why do we need a new for each?

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +1

      fds are indeed copied whenever you're calling fork() and you can use them in multiple processes. It works well if you have either
      - 1 writer process and 1 or more reader processes and you don't care about the reading order
      - 1 reader process and 1 or more writer processes and you don't care about the writing order
      Although, if you have more than 1 writing processes connected to more than 1 reading processes you have absolutely no control over who read/writes what. So you might have process 1 write data that is read by process 2 but that data should've been read by process 1 etc.

    • @ansismaleckis1296
      @ansismaleckis1296 3 ปีที่แล้ว

      @@CodeVault What if the main waits for the child process to finish before forking again? Wouldn't one pipe be enough for such a case? Also, isn't it a big overhead creating and managing a large number of pipes?
      I need to code a terminal for my school assignment and it has to work with any number of pipes. My main waits for the child process to terminate before forking again so I think I could get away with just one pipe. Is that a good idea?

  • @youssefdawoud6296
    @youssefdawoud6296 2 ปีที่แล้ว

    a7la mesa 3alik ya sa7by 💘

  • @catbreadcrab9530
    @catbreadcrab9530 10 หลายเดือนก่อน

    Anyone have a github repo for attempting a for loop :D

  • @11rick11
    @11rick11 3 ปีที่แล้ว

    good😃

  • @RahulDhole7
    @RahulDhole7 3 ปีที่แล้ว

    got error expected ‘int *’ but argument is of type ‘int’
    extern int pipe (int __pipedes[2]) __THROW __wur;

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      I'm not sure what you did there. Sounds like you're assigning to a pointer the return value of the pipe() function

  • @dealvin
    @dealvin 3 ปีที่แล้ว

    why you use 6 pipes?. If you use only one fd[2] on every child process and parent. What happens? .
    I think that my fd[] fork with my process child.

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      If you use only 1 pipe then you never control which process reads which data from the pipe. This video explains what's going to happen: code-vault.net/lesson/s8kevbur2c:1603732432386

  • @GAMEVUEG
    @GAMEVUEG 4 ปีที่แล้ว +1

    please open a tipee account, so I can donate to you.

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +4

      Very soon we will add donations. Thanks for the intent still!