Simulating the pipe "|" operator in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ค. 2024
  • Check out our Discord server: / discord

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

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

    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 6 หลายเดือนก่อน +2

      42 likes

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

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

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

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

  • @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. ❤ 💙 💜

  • @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!

  • @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

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

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

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

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

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

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

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

    Amazing video! You make abstract concepts look so clear!

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

      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.

  • @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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  • @faiza.s6509
    @faiza.s6509 2 ปีที่แล้ว +1

    Thank you so much, you are an amazing teacher!

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

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

  • @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!

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

    Thank you so much for your grateful work.

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

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

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

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

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

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

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

    Great vid and excellent explanation. Thank you.

  • @ozanparlayan3471
    @ozanparlayan3471 8 หลายเดือนก่อน

    thank you very much for this beautiful video

  • @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: th-cam.com/video/cex9XrZCU14/w-d-xo.html
      Or on our website: code-vault.net/course/46qpfr4tkz:1603732431896/lesson/16non3fdoh:1603732431950

  • @npreme9193
    @npreme9193 7 หลายเดือนก่อน

    Great content, thanks so much

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

    This helped me so much, thank you

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

    thank you so much!

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

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

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

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

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

    Thank you !

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

    You are a saint

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

    Thank you !!!

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

    thanks!

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

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

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

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

  • @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

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

    Great ! thanks a lot 😀

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

    Too good!!!

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

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

  • @tsakboolhak5357
    @tsakboolhak5357 3 ปีที่แล้ว +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  3 ปีที่แล้ว +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 3 ปีที่แล้ว +1

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

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

    saved me with my operating systems hw!!

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

    Amazing

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

    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.

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

    made my life easier thaank u a lot

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

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

  • @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 10 หลายเดือนก่อน

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

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

    Good video

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

    somebody should call a plumber...

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

    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  3 ปีที่แล้ว +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.

  • @baselarw99
    @baselarw99 25 วันที่ผ่านมา

    Can you please make a diagram explaining the parent-child tree?
    cause in your second fork, you will have 4 processes, right?
    and we want only 3. (parent and 2 child)

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

    Greate!

  • @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

  • @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 9 หลายเดือนก่อน

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

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

      I have the same question...

  • @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 6 หลายเดือนก่อน

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

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

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

  • @user-gh4lv2ub2j
    @user-gh4lv2ub2j 4 หลายเดือนก่อน

    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  3 หลายเดือนก่อน

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

  • @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

  • @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!

  • @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)

  • @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 3 ปีที่แล้ว

      @@mgking777 did you ever get this to work?

    • @mgking777
      @mgking777 3 ปีที่แล้ว +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.

  • @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

  • @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

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

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

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

      I will look into it

  • @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?

  • @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

  • @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!

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

    This code doesn't look clean to me since you don't handle a possible failure auf execlp... - the program will hang if you substitute "ping" by "pong" (or delete /usr/bin/ping on your system)...

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

    which IDE is that, eclipse?

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

      No. It's Visual Studio Code

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

      @@CodeVault Thank you

  • @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

  • @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 :)