How to use FIFO files to communicate between processes in C

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

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

  • @jayanth4861
    @jayanth4861 2 года назад +67

    You have no idea how many students are benefitting from this playlist. Thanks a lot, sir.

  • @tellmywifiloveher6474
    @tellmywifiloveher6474 3 года назад +33

    Bless you, sir. You are actually saving my entire degree

  • @kazimehedihasan3082
    @kazimehedihasan3082 4 года назад +18

    I love your playlist of "Processes and threads" . They explained the core concepts with simplicity and examples. Thanks a lot for your tutorials. Really appreciate it.

  • @anupkodlekere3633
    @anupkodlekere3633 4 года назад +1

    I've been following your c playlist and I really like it!

  • @shado1312
    @shado1312 Год назад +2

    I've done waaayyy more stuff by just viewing your videos in 3 h rather than weeks of work at University... GOD BLESS YOU SIR

  • @paulfan-vv3lf
    @paulfan-vv3lf 6 месяцев назад

    Thanks a lot, sir. You let me know clearly about what happened in fifo and how to use it.

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

    Foarte bine explicat, felicitari!

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

    I am refering his lecture as part of my Master's program for course System Programming. Indeed very helpful.

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

    I love your way of teaching

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

    this is awesome, its saving me from a lot of theoretical troubles, thanks alot

  • @mandardhas
    @mandardhas 3 года назад +7

    Very nice and crisply explained the concepts, especially with help of programs, really loved it. i have one request could you please explain shared memory and message queues also?

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

      I will look into it

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

    Good one bro
    Keep going👍

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

    great content - it's a standard here

  • @andreasleonidou3620
    @andreasleonidou3620 2 месяца назад +1

    Very helpful, thanks!!

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

    God bless you sir

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

    Thank you so much. 😊

  • @matthew.s
    @matthew.s Год назад

    I like the way he says bye

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

    Hi! Very helpful videos! Thanks! I have a question: why not use fopen, fprintf, fscanf and fclose to deal with fifos?

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

      i got a task in class to do this, and use that, didnt know what fifo was.

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

    Thanks for all the vids. Big help. How would I go about passing a structure instead of an array.
    I think o figured how to write it, but I can’t seem to read it in the other process. I just keep on getting a 0 instead the input that I read from the keyboard and try to print it to the screen.

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

      Depends on what you need to write. Usually you should be able to read/write the struct itself (similar to how you would read/write an int)

  • @gurulinggbiradar6982
    @gurulinggbiradar6982 11 месяцев назад

    thank you

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

    you just save my system programming exam tommorow

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

    When I send the array without the for loop , main2 gets right the two first numbers but from then on the other ones are wrong. In deed, some of them are even negative:
    Send: 12, 82, 6, 98 and 44;
    Received: 12, 82, -5376, 32767 and -5400.
    If I make the array of short int instead of int, it works better; but not completly:
    Send: 82, 7, 22, 1 and 1;
    Received: 82, 7, 22, 0 and 0.
    The last two numbers are always read as 0.

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

    Thank you very much for these tutorials. However now I'm having trouble sending a defined struct via a fifo file using write() and read(). Does it work the same as shown here in the video? And how would I go about making a process continuously read any data being written in a fifo?

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

      Yeah, it should work basically the same way (just pass the struct and sizeof that struct to it). Make sure you don't have any pointers in that struct though as they might be invalid in the other process.
      Continuously reading the data can be achieved using a loop like so: while (read(...) > 0) { ... }

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

    I'm still a little confused on the reading and writing. So I'm trying to do the "hw" you gave us in the video which is to write the sum from main2 and read it back on main1. Does that mean I have to open the fifo file again write the sum then close the file. While on main1 we open the file, read the sum then close the file again?
    I got the first part to work in when you said to write the file from main2 to main1, but when I am trying to read it I am only getting a sum of 0 instead of the sum that I had written

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

      Nvm I solved it I wasn't pointing to the sum address using &sum, but in the case of if lets say I was to enter 9 digits in a matrix (3x3), how would I go about the sizeof? if I was to put in the address of matrix[3][3] which is like 1 2 3 4 5 6 7 8 9 for example, how would I write that matrix value to my 2nd main?

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

      Actually matrices (even 2d or 3d etc.) are always contiguous. So let's say:
      int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
      The &matrix[2][2] is the exact same as (((int*)matrix) + 5)
      5 = 2*3 + 2
      So you would write it the same way as you'd write a simple array to the fifo

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

    Could you possibly make a video about the poll function, specifically in the context of monitoring multiple FIFOs.

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

    Ура!!! у меня получилась домашка. Спасибо за уроки.

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

    Hi! Your videos are great! I have a question, how would you manage to communicate three different processes with FIFO files? In sort of a master and two slaves way? I have been trying to do this, but I keep getting stuck because of how the open function blocks the flow. Thanks in advance!

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

      To be more clear, how can the master communicate with both slaves, simultaneously?

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

      Sorry for the late response. I think you just need to open multiple FIFOs, one for each slave

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

    Hello, a question. Is necessary to use arrays?

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

    ty bro ;d

  • @dirtyraccoon6839
    @dirtyraccoon6839 4 года назад

    how come you can send the adress of the array in the pipe , even though (not sure if I say it correctly) it is not in the range of main2 . The array has not been dynamically allocated so it is suprising to see that it worked

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

      You're not sending the address through the pipe. The read function takes an address as a parameter but, that function, looks at that address and sends the bytes that are at that address. And of course, sends however many bytes we specified in that read function.
      You're right though, sending actual addresses through a pipe is a bad idea and, usually, shouldn't be done.

  • @ghulammustafa-zb9ig
    @ghulammustafa-zb9ig 2 года назад

    What if I want to make a simple chat system using fork in which one process will write some text and the child would read it. Now the question is i want it to look realistic meaning I want to use two terminals. is this possible with fork and fifos meaning is it necessary to use fifo for a two-terminal display or we can do that with fork too?

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

      I'm sure there are hacky solutions to use fork and pipes in that situation but it's way easier to just have a fifo link those two processes

    • @ghulammustafa-zb9ig
      @ghulammustafa-zb9ig 2 года назад

      @@CodeVault yeah i have figured it out using mkfifo concept. I watched your video and create the program of my own. Thankyou though ❤️

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

    I know i'm asking that question after a whole year of publication of this video however, is there any way to pass by this method 2D int array?

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

      Of course there is. This video should help on the matter for 1D arrays: code-vault.net/course/46qpfr4tkz:1603732431896/lesson/phac6gv4cy:1603733529763
      But instead of sending just one size integer, you could send 2 integers representing the 2 sizes of that array and then simply send them one by one through the pipe/fifo.

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

      @@CodeVault thanks a lot for your reply, i will try do that

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

    I think, instead of sizeof(int) * 5 you could just use sizeof(arr)
    ;-)

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

      Yes, and I actually made a video on this: code-vault.net/lesson/qfxxk4v787:1603820089039
      Although, usually it's better to be explicit about the sizes in case that array, at some point, changes and becomes defined elsewhere or gets dynamically defined

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

      @@CodeVault Agree!

  • @Yuri-bt4wl
    @Yuri-bt4wl 10 месяцев назад

    Instead of "sizeof(int)*5" could we just use "sizeof(arr)"?
    Thank you!

    • @CodeVault
      @CodeVault  10 месяцев назад +1

      If arr is of type array then yes. But be careful, if you pass this array to a function, sizeof(arr) will return 8 (or 4 on 32-bit systems) since they will become pointers. Here's a video on that: code-vault.net/lesson/a4mqix89a0:1610303947019

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

    You are a fucking life saver.

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

    I have tried to exchange the data using the same FIFO file, but I was unable to! But the exercise worked with two FIFO's. I was wondering is it even possible to read and write in the same file with two processes.

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

      No. FIFOs (and pipes in general) are unidirectional. Meaning, you can only write or only read from one process, not both. If you try reading and writing to the same FIFO you'll just end up reading the content you just wrote anyways so it's not feasible. For bidirectional communication between processes you do need two pipes/fifos. I have a video on this (although for pipes but it can be applied easily for fifos): ruclips.net/video/8Q9CPWuRC6o/видео.html

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

      @@CodeVault Thanks a lot a man. I admire your determination to answer each and every comment, you are a hero sir! dmire you and you are inspiring me to learn more and work more

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

    If for some reason program that writes fails to start, it would mean that reader program waits forever at open call. How would one do so that blocking open call can be terminated?

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

      You can call open with the O_NONBLOCK flag set on the reading process. That way the reading process will return immediately and, when calling read, that will give the error EAGAIN if no writing ocurred
      Here's the documentation for O_NONBLOCK:
      When opening a FIFO with O_RDONLY or O_WRONLY set:
      1) If O_NONBLOCK is set, an open() for reading-only shall return without delay. An open() for writing-only shall return an error if no process currently has the file open for reading.
      2) If O_NONBLOCK is clear, an open() for reading-only shall block the calling thread until a thread opens the file for writing. An open() for writing-only shall block the calling thread until a thread opens the file for reading.
      And here's the documentation for read error with O_NONBLOCK:
      EAGAIN: The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block.

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

      @@CodeVault Thank you for your detailed response. It helped.

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

    Can you provide the source code for your videos ? It will help me remember your lesson (without rewatching the video) some weeks later ;)

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

      All the source codes for all the videos can be found on the website: code-vault.net/lesson/ix0kajqemd:1603732432335

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

      @@CodeVault I just discovered your website and i suppose i am not the only one who didn't know it exists. Maybe you can also put this link in your video descriptions to reach more people ;)

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

      It is on the newer ones. I think I will add it to the older videos too. Thanks for the suggestion!

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

      @@CodeVault Ah ok sorry i havent checked your new videos. Currently i am finishing Processes today and will begin Threads tomorrow to be ready for my exam. You are a very good teacher thank you 💪

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

    Is it possible to delete a named pipe after using it?

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

      Yeah, you can delete like any other file. This should be the function you need: linux.die.net/man/3/remove

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

    why is that only write operation becomes faster if done in single call and not the read operation?? I did not get the concrete reason.

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

      I don't understand the question... What do you mean by "operation becomes faster"?

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

      I mean you said its all the same if you read 5 integers in 1 call and 5 integers in a loop..... whereas in write operation you said its better to write in one call rather than in a loop.. I was referring to this .

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

      @@arnabbagchi4260 Performance-wise, it's better to read AND write in batches rather than in a for loop. Sorry if I wasn't clear in the video.

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

      @@CodeVault thanks.. I got it.. but can u explain why is it so?? Thanks in advance

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

      Ahh, the explanation is very simple. In computer science memory accesses are the most expensive. Reading an array from memory is much faster than reading several elements from that array. In principle, reading/writing more memory at once rather than in chunks is faster.
      Although, on most recent CPUs there is a cache which might automatically improve this, but you shouldn't rely on that in principle.

  • @maplus8230
    @maplus8230 11 месяцев назад

    where i can found the source code for this course?

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

      All the source codes for all my videos can be found on the website. Here's is the link to this course:
      code-vault.net/course/46qpfr4tkz:1603732431896/lesson/w8p1gp2y4x:1603733527568

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

    sir i have question of fifo, sir can you make C program Please.......

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

      Create a C program having “Client Server Communication” using “FIFO”. A
      server using a FIFO is created to handle client requests. Server displays the menu
      of operation to the clients:
      1.addition
      2.subtarction
      3. multiplication
      4. Disconnect
      and accepts input (selection for executing the desired operation and 2 parameters
      to operate on) from the particular client. There should be at least 3 clients. Each
      client submits a request consisting of “selected operation number and two
      operands “. Server reads the request from particular Client and after performing
      the selected operation write the result back to the that particular client's FIFO.
      Client read the FIFO and display the result on its terminal.

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

      I'm sorry, we're not here to solve your homework. I suggest you first try to implement it yourself, get something working, and only then ask questions. If I give you the answer straight up, you won't learn anything and there's no point in that.

  • @user-zz6ld7hf8z
    @user-zz6ld7hf8z 3 года назад

    can you use c++ to write FIFO?? plz

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

      Sure you can use anything you see here in C++ if you're on a Unix platform

    • @user-zz6ld7hf8z
      @user-zz6ld7hf8z 3 года назад

      @@CodeVault i use arduino send light_sensor message to c++bulider and i don't know how to make a
      graphics of fifo.

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

    The two files used in the vidos does not have anything to do with FIFO. at least we should mkfifo at first. The first example just write some data into a file, how is it possible that the problem hangs and wait for "reader" ????????????????????????

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

      That's what we do at the beginning of the video. FIFOs are files

  • @Hacker-vq3xk
    @Hacker-vq3xk 3 года назад

    how to transfer sum rom main 2 to main 1 ?

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

      The same way we sent it from main 1 to main 2. Just reopen the fifo the other way around.

    • @Hacker-vq3xk
      @Hacker-vq3xk 3 года назад

      @@CodeVault same fifo? thanks for eply

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

      I think so yea. Just make sure you close it only after the read is finished

    • @Hacker-vq3xk
      @Hacker-vq3xk 3 года назад

      @@CodeVault ❤ thanks

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

    Where are the source code?

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

      You can find them on our website: code-vault.net/lesson/ix0kajqemd:1603732432335