Simulating the pipe "|" operator in C

Поделиться
HTML-код
  • Опубликовано: 19 май 2024
  • Check out our Discord server: / discord

Комментарии • 136

  • @evangelosspyromilios5994
    @evangelosspyromilios5994 2 года назад +132

    Students from 42 Salute you ! (Pipex project is pipe simulation in C)

  • @ronoxdegrand
    @ronoxdegrand 3 года назад +48

    These videos have helped me ALOT in my OS course at college

  • @CARDSSerus
    @CARDSSerus 3 года назад +46

    I looked online for hours for material on a project I'm working on, finally found your channel, and found videos on basically everything I needed. You deserve way more attention

    • @3IMAD69
      @3IMAD69 4 месяца назад +2

      42 likes

  • @Grux
    @Grux 3 года назад +13

    OOOH man, this series was a huge helping hand in my shell project, so grateful for all the great vids of yours, thanks man!

  • @sharonmaldonado2114
    @sharonmaldonado2114 Год назад +4

    You have no idea the help you have provided. I was completely lost in my systems programming class during our final project, but this playlist you have made has helped me truly understand every little detail of pipes, forks, and exec functions. Thank you!

  • @msd-007
    @msd-007 2 года назад +3

    Thanks a lot. Your videos are damn excellent and help a lot in my projects. You explain everything and it's meaning , not like other sites that just give an example. Thanks from the depth of my heart. ❤ 💙 💜

  • @fahidsarker4858
    @fahidsarker4858 2 года назад +16

    you explained in 20 mins what my prof couldn't explain in 1 month. You earned a sub :-)

  • @anasajaanan7087
    @anasajaanan7087 9 месяцев назад

    I love your videos, you have the ability to make any concept very very easy ^_^

  • @sukitd
    @sukitd 9 месяцев назад

    Thank you so much for your grateful work.

  • @simon29o
    @simon29o 3 года назад +2

    Thanks a lot ! I'm creating a shell (like TCSH) and your videos helps me a lot understand how to implement it

  • @harshildesai997
    @harshildesai997 3 года назад +4

    Thank you so much for making this whole series .
    It helps me a lot in my academics

  • @aminabeid8136
    @aminabeid8136 2 года назад

    dude, you channel is a treasure, thank u so much!!!

  • @samgak_imbap862
    @samgak_imbap862 3 года назад +2

    You are amazing at explaining this... Thank you for helping understanding how to do my assignments

  • @aristuzabaru
    @aristuzabaru 3 года назад +2

    Amazing video! You make abstract concepts look so clear!

    • @dsinemoi1mouton
      @dsinemoi1mouton 2 года назад

      That's because it isn't abstract imo. Just not so commonly shared knowledge, and documentation barely tells anything about use cases.
      Amazing video indeed. Helps a ton.

  • @juangil6227
    @juangil6227 2 года назад +3

    people: it's just another c video.
    me: bro, you literally saved my life

  • @zyghom
    @zyghom 2 года назад

    imagine being Unix and Linux user for really long time, and using bash pipes on daily basis, only today I got the point about how the pipes work - thanks to your videos about the pipes - amazing!

  • @ronitchinda6857
    @ronitchinda6857 Год назад

    Nice content : the way he explains it , is really awesome! Thanks , just keep making these kind of videos!

  • @jackiehe8257
    @jackiehe8257 2 года назад

    Thank you so much for these videos! Great content and very clear step by step instruction!

  • @wenlinf1023
    @wenlinf1023 2 года назад

    thank you so much! couldn't understand how pipe works even after asking my professor. this video is very clear and helpful

  • @faiza.s6509
    @faiza.s6509 2 года назад +1

    Thank you so much, you are an amazing teacher!

  • @sony1979iq
    @sony1979iq 2 года назад +1

    very nice explanation, one of my favorites channel in c learning

  • @lukacsistvan6301
    @lukacsistvan6301 2 года назад +1

    Thanks man. I can learn more from your videos than from courses at university

  • @31GuitarKid
    @31GuitarKid 3 года назад +1

    This video helped a lot with a lab for my OS class! Thanks!

  • @Edysamaha
    @Edysamaha 2 года назад

    Thank you very much!! I’ve been searching for this info for 2 days

  • @juanguappo
    @juanguappo 2 года назад

    You are a legend, thank you for this videos!!!

  • @kawaranai6742
    @kawaranai6742 2 года назад

    Great lecture, helped me understand better than every written source I read. Request you to either number the lectures, or add them to a playlist (maybe there's one already?).

    • @CodeVault
      @CodeVault  2 года назад

      There is one already: ruclips.net/video/cex9XrZCU14/видео.html
      Or on our website: code-vault.net/course/46qpfr4tkz:1603732431896/lesson/16non3fdoh:1603732431950

  • @ozanparlayan3471
    @ozanparlayan3471 6 месяцев назад

    thank you very much for this beautiful video

  • @mouadse
    @mouadse Месяц назад

    Great vid and excellent explanation. Thank you.

  • @hectordoyle4718
    @hectordoyle4718 4 года назад +5

    yo, i feel like you are my private tutor hahah
    thank you so much!

  • @npreme9193
    @npreme9193 5 месяцев назад

    Great content, thanks so much

  • @NicosRap
    @NicosRap 2 года назад

    Great video! Thank you so much! So clear and consise!

  • @zeuspolancosalgado3385
    @zeuspolancosalgado3385 3 года назад +4

    I think my school should just outsource some of our professors and show us your videos

    • @sony1979iq
      @sony1979iq 2 года назад +2

      totally agree, many teachers dont have ability to deliver complex things in simple way, such as this channel

  • @nik9911
    @nik9911 2 года назад

    This helped me so much, thank you

  • @moamenalkatib2703
    @moamenalkatib2703 3 года назад

    thank you so much!

  • @benshapiro9731
    @benshapiro9731 Год назад

    highkey saving my ass in my OS class, thank you good sir

  • @TheMrBlueKoolaid
    @TheMrBlueKoolaid 2 года назад

    Thank you !

  • @olcaar
    @olcaar 3 года назад +2

    Wow, you are an absolute life saver!
    thanks a lot for awesome and very well explained video.
    is there any way we can see all the codes you show in your videos? like at git or something?

    • @CodeVault
      @CodeVault  3 года назад +1

      Thank you! You can check out all the source codes for any of the videos over on the website: code-vault.net/lesson/oxyoxbvnak:1603732432935

  • @tsakboolhak5357
    @tsakboolhak5357 2 года назад +1

    Again thanks a lot with your videos! Your explanations are crystal clear! anyway i have a question : pipe function does open both fds , right? so if pipe() went well but another error occured, shouldnt we close the fds before returning error code?

    • @CodeVault
      @CodeVault  2 года назад +4

      That is true... technically you should have a lot more error checking and closing of fds than I show on the video, but I didn't want to clutter the program with that, it would be difficult to follow. Also, a lot of the fds that we don't close get automatically closed by the operating system

    • @tsakboolhak5357
      @tsakboolhak5357 2 года назад +1

      @@CodeVault allright, thanks for your time and your response!!

  • @boutaina7925
    @boutaina7925 2 года назад

    Thank you !!!

  • @Victor_Marius
    @Victor_Marius 3 года назад

    somebody should call a plumber...

  • @abdullahgarra8477
    @abdullahgarra8477 2 года назад

    saved me with my operating systems hw!!

  • @vishalbhatane1608
    @vishalbhatane1608 3 года назад +1

    thanks!

  • @HAPPYSPY
    @HAPPYSPY 2 года назад

    You are a saint

  • @OytunKuday
    @OytunKuday 2 года назад

    thank you very much it helped our homework!!!!

  • @gursahibsingh5177
    @gursahibsingh5177 2 года назад

    Too good!!!

  • @Viktor_Kardakov
    @Viktor_Kardakov 2 года назад

    Спасибо большое! Вы очень хорошо всё объяснили.

  • @chetanvenkatesh6051
    @chetanvenkatesh6051 3 года назад

    Amazing

  • @ahinoj
    @ahinoj Год назад

    Good video

  • @poincareelcartografo5406
    @poincareelcartografo5406 10 месяцев назад

    On freeBSD it seems there is no need to close the main file descriptors: the program finish without doing so.
    However, I have closed them because I believe it is a good practice rule.

  • @3finggaz
    @3finggaz 2 года назад

    Great ! thanks a lot 😀

  • @duanedsilva3573
    @duanedsilva3573 3 года назад +1

    You can use system function to execute terminal commands with pipe operator.

    • @CodeVault
      @CodeVault  3 года назад +3

      Oh, is there a function for doing this automatically? How would you do it?

    • @filintodelgado
      @filintodelgado 8 месяцев назад

      I was thinking the same thing but It is always good to learn something new

  • @jingwu1425
    @jingwu1425 3 года назад

    Could you create a video explaining popen()/pclose()?

  • @emrederman880
    @emrederman880 3 года назад

    made my life easier thaank u a lot

  • @rezcov
    @rezcov 2 года назад

    thank you for this video. Could you please clearify, why did you type name of command twice ("ping", "ping", ... "grep", "grep")? Cann't find manual regarding this cases.

    • @CodeVault
      @CodeVault  2 года назад +3

      It's just the way you have to initialize argv.
      execlp("ping", "ping", "google.com");
      Here, the first argument determines the program to be run and then the next two represent argv in the program we're running. It's a convention to pass the name of the program we are running as the first element of argv.

  • @video0607
    @video0607 2 года назад

    Greate!

  • @ansismaleckis1296
    @ansismaleckis1296 2 года назад +1

    If I understand correctly pipe() simply creates two temporary files to read and write. Can I achieve the same result by creating/opening/closing/deleting the temporary files myself? I understand that pipe() might be more convenient in some circumstances but is there any fundamental difference?

    • @CodeVault
      @CodeVault  2 года назад

      A pipe does NOT create any temporary files. Everything is managed in the kernel's memory. While you could simulate a pipe's behavior by creating temporary files, it wouldn't be the same. More info here: superuser.com/questions/81679/does-pipe-have-to-write-temporary-file

  • @hackedyou116
    @hackedyou116 6 месяцев назад

    @CodeVault, why did u do 2 forks here? in the child process you can do ping and in parent you can do grep right?

  • @user-gh4lv2ub2j
    @user-gh4lv2ub2j 2 месяца назад

    Are there any books that I should read to make me suck less with pipes? Linux programminmg by example i assume yes, but is there one from the perspective of a pure-c user in general?

    • @CodeVault
      @CodeVault  Месяц назад

      The C Programming language by the original creators of C would be a good start I think

  • @steptoevs996
    @steptoevs996 3 года назад

    How can I read data such a variables from Pipes when using dup2() and write()/read() functions ?

    • @CodeVault
      @CodeVault  3 года назад

      You can't make another program read/write specific variables through the pipe you dup2 on. You have to somehow do it beforehand (before the exec call) and maybe pass it as an argument

  • @vanessahaefeli5108
    @vanessahaefeli5108 Год назад

    Your videos are so usefull!!! I think the code on your website isn't finished. the last "close" aren't on it 😉

    • @CodeVault
      @CodeVault  Год назад +1

      Oh you're right! Should be fixed now. Thanks for pointing that out!

  • @SwenneHacks
    @SwenneHacks 2 года назад

    What vscode extension is he using that shows information/manual-info about functions on hover?

    • @CodeVault
      @CodeVault  2 года назад

      It's just the C/C++ extension from Microsoft. It should work out of the box

  • @swastikdas4251
    @swastikdas4251 4 месяца назад

    sir in process 2 the exec system call is reading from arguments how can it read from stdin

    • @CodeVault
      @CodeVault  4 месяца назад

      This video should explain how you can achieve that: code-vault.net/lesson/43zvcsz6o1:1603732432539

  • @damonrobot906
    @damonrobot906 3 года назад

    how would this be modified to support an arbitrary number of commands the user decides? eg. using n commands and n-1 pipes
    Thanks for your time Sergiu

    • @CodeVault
      @CodeVault  3 года назад +1

      This video partially looks into this: code-vault.net/lesson/v5cml4fg0w:1610639843712
      But instead of sending the number through the pipes you dup2 the STDIN and STDOUT of most processes and call exec at the end.

    • @mgking777
      @mgking777 3 года назад

      @@CodeVault When i exactly do this but, replace read and write with dup2 for STDIN and STDOUT and exec function , then there is no ouput on my console :( Im trying since hours but it wont work

    • @maxcamilleri7004
      @maxcamilleri7004 2 года назад

      @@mgking777 did you ever get this to work?

    • @mgking777
      @mgking777 2 года назад +1

      ​@@maxcamilleri7004 ​ Yes. You need to set the pipes differently. He is using for the first pipe fd[1]. But we have to do it differently. So if i = 0 we are at the beginning. So you need to use fd[i] because you wanna write in to the first pipe. Then in the "middle pipes" of the loop , when i > 0 you need to read from fd[i - 1] , because you wanna read from the "read end" of the previous pipe. Then write to the next pipe fd[i]. The middle part repeats untill we arrived the last pipe. Than we just need to read from fd[i - 1] and not write anything.

  • @chocolatteu
    @chocolatteu 2 года назад

    hello, how would the code change if I want n number of processes instead of creating the child processes one by one?

    • @CodeVault
      @CodeVault  2 года назад

      There's this video on the topic: code-vault.net/lesson/v5cml4fg0w:1610639843712
      Combine it with what you learned in the pipe simulation video and should work

  • @dantearcuri3426
    @dantearcuri3426 3 года назад +1

    How could you make this work with more than 1 pipe command? like ping | grep | wc for example.

    • @CodeVault
      @CodeVault  3 года назад +2

      Same exact way but opening 2 pipes and calling dup2 4 times (one for stdout of ping, one for stdout of grep, one for stdin of grep and one for stdin for wc). Just makes sure you're closing the appropriate ends of each pipe in all processes.

    • @mgking777
      @mgking777 3 года назад +1

      @@CodeVault Hey , i am trying to make more than one pipe but i cannot reach the next one. For the middle proces between the two pipes i was trying to make dup2(fd[i]READ_END],STDIN_FILENO) and after that dup2(fd[i+1]WRITE_END],STDOUT_FILENO) and then execvp(...). What is a better way to do that?

    • @mgking777
      @mgking777 3 года назад +1

      nevermind i got the solution

    • @karanarjun6245
      @karanarjun6245 3 года назад

      @@CodeVault we have forked 2 times... So total 4 process are created... So I am bit confused...so we don't need to close fd in those process?

    • @lulu-wj8yt
      @lulu-wj8yt 3 года назад +1

      @@mgking777 Hi, i'm trying to do the same thing. where did you get the solution if you don't mind sharing the link ? thanks

  • @maxmustermann9654
    @maxmustermann9654 Год назад

    Is it necessary to create two child processes? Why can't we just execute the first exec in the child process and the second one in the parent process?
    Something like this:
    main() {
    pid = fork();
    if(pid == 0) {
    ....
    exec
    }
    .....
    wait(NULL);
    exec;
    }
    That also works, but what is the big difference compared to creating two child processes?

    • @CodeVault
      @CodeVault  Год назад +1

      You can do this but there is one issue. The wait(NULL) placement. Since we have to wait for the child process to finish execution before executing the parent process. In some cases, this is fine, in others you would want the processes to run in parallel and, if you wanted that, you can't wait for the child process

    • @maxmustermann9654
      @maxmustermann9654 Год назад

      ​@@CodeVault Allright, thanks!

  • @jerrylin9018
    @jerrylin9018 2 года назад

    Hello, I'm trying to do
    output of Process A to input of process B and output of process B to input of A using what you did. so A->B->A->B.....
    Do you know what's wrong with my code?
    int AtoB[2],BtoA[2];
    pipe(AtoB);
    pipe(BtoA);
    if(pipe(AtoB)==-1){return 1;}
    if(pipe(BtoA)==-1){return 1;}
    int pid,pid1;
    //process A
    if (pid==0){
    dup2(AtoB[1], STDOUT_FILENO);
    close(AtoB[0]);
    close(AtoB[1]);
    dup2(BtoA[0], STDIN_FILENO);
    close(BtoA[0]);
    close(BtoA[1]);
    execv(Process A); //don't worry, I did write a cpp for B and used the right argv to call it;
    }
    else if (pid

    • @CodeVault
      @CodeVault  2 года назад

      I suggest you go to discord.code-vault.net and ask there. It's probably much faster

  • @matejdrazic7780
    @matejdrazic7780 2 года назад

    How can you save the program output to a string?

    • @CodeVault
      @CodeVault  2 года назад

      Just replace the stdout of the program with a pipe end. Then, on another process read from the pipe's read end

    • @xiLike2Doitx
      @xiLike2Doitx 2 года назад

      @@CodeVault can I read on the parent process? Is that why it's not working for me so far?

  • @ilyazlatkin4594
    @ilyazlatkin4594 2 года назад

    Why it doesn't work with execv, but works with execlp?

    • @CodeVault
      @CodeVault  2 года назад +1

      Probably because execv is missing the PATH variable. So you have to pass in the full path to your program (not just the executable's name)

  • @yersonc8682
    @yersonc8682 Год назад

    can you simulate the ">" operator plis? :(

  • @navguest1740
    @navguest1740 3 года назад

    which IDE is that, eclipse?

    • @CodeVault
      @CodeVault  3 года назад +1

      No. It's Visual Studio Code

    • @navguest1740
      @navguest1740 3 года назад

      @@CodeVault Thank you

  • @manmmtk
    @manmmtk 3 года назад +1

    What if we hv so many pipe?

    • @CodeVault
      @CodeVault  3 года назад +1

      It could get tricky. There's a video on dealing with any number of pipes: code-vault.net/course/46qpfr4tkz:1603732431896/lesson/v5cml4fg0w:1610639843712

  • @naitside3410
    @naitside3410 2 года назад

    Ты очень хорошо объясняешь, только я ничего не понял так как говоришь не по русски. Я понял только что pipex это вертикальные полочки

    • @CodeVault
      @CodeVault  2 года назад

      I might consider adding subtitles for different languages at some point. Thanks for the feedback!

  • @MrTaandreo
    @MrTaandreo Год назад +1

    If you are from 42 i know that you are here :)

  • @naitside3410
    @naitside3410 2 года назад

    I rewrote the code, and I can’t go in, you can viklasty the code, be kind

    • @CodeVault
      @CodeVault  2 года назад

      You can go to discord.code-vault.net and ask there for help