Calling fork multiple times (part 2) (With a practical example)

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ก.ค. 2024
  • Source code can be found here:
    code-vault.net/lesson/v5cml4f...
    ===== Support us through our store =====
    code-vault.net/shop
    ===== Check out our website =====
    code-vault.net
    ===== Check out our Discord server =====
    discord.code-vault.net

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

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

    You're so helpful man, very nice instructions and love the visual aids.

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

    Thank you for the whole playlist and everything, you have no idea how helpful it has been.

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

    Thanks a lot for being that helpful, it let to me understand how multi-pipe processes work. Great channel, great videos!

  • @user-tf8qe9fl9x
    @user-tf8qe9fl9x 2 ปีที่แล้ว

    Thank you for the lesson

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

    Super useful guide!

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

    Lovely explanation

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

    thank you very much! You help my Operating system project!!!!

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

    Thank you very much

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

    You're great!

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

    thank youu so much !!!

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

    I shared your playlist to all my classmates. Thank you :)
    Do you have any suggestion about where to study Unix Processes deeper? (Actually I'm reading Manpages but it takes an ethernity)

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

      Man pages are really the best. There are some books out there, but if you want up-to-date information, man pages are still better

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

    This was so cool 😎

  • @Cry.laughing
    @Cry.laughing 3 ปีที่แล้ว +3

    Thank you , If only the Processes series were organized in a playlist ، that would be very helpful

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

      They are! Here's a link: code-vault.net/course/46qpfr4tkz:1603732431896/lesson/16non3fdoh:1603732431950

    • @Cry.laughing
      @Cry.laughing 3 ปีที่แล้ว

      @@CodeVault great, I appreciate it

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

      @@CodeVault please give the link in the description

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

    Great video, thank you very much! Is it possible to simulate a ring communication, such that the main process passes the received number to existing children?

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

      I'm not sure what you mean

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

    thaaaaaaaaaank youuuuuuuu

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

    I got so impressed. May I ask how to make a loop with this? after printing result, I want to send y to pids[0], so which I can make another round of calculation.

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

      Right now, the issue is that the processes finish execution right after they finish writing. Just add a loop around that read/write process in every child process and another loop around the write/read operations in the main process. The condition... I'm not sure what it could be, you could leave it an infinite loop for now until you figure it out (but could be a special value you send through the pipe)

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

    when I run valgrind --track-fds=yes ./a.out with the code there are open file descriptors specifically for this line;
    for(int i = 0; i < PROCESS_NUM + 1; i++) // Create pipes for processes
    {
    if(pipe(pipes[i]) == -1){
    perror("pipes");
    exit(1);
    }
    }
    Do you know why this happens? If so How can I get rid of them?

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

      I did omit a few issues with resource management regarding this code so as to not make it too complex. Basically, what can happen there is the creation of a pipe could occur after some pipes have already been created. Because of that, you will need another for loop in that if block closing all the other pipes that have been opened before so as to not create a resource leak

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

    @CodeVault when you are writing 5 in the pipe in the main process the first Child needs that value to increment it. What if the first child executes first before the main process writes the value in pipe.
    Will the child process wait for the main process to write value in the pipe?

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

      Yes. The read call on an empty pipe will always pause the process, also, the write call on a full pipe will do the same thing.

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

    so gooddddd

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

    How would this code differ if you wanted each child process to be the parent of the next process forked? ie. nested processes rather than them all having the same parent. Thanks for the video

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

      Just change the condition. Call fork() when the pid returned by the previous fork is not 0 (it's the parent)

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

    This video helped me soooooo much is adapting this with exec hard ? like each child exec a process and the next reads from output?

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

      Not really. Just apply what I showed in this video: code-vault.net/course/46qpfr4tkz:1603732431896/lesson/oxyoxbvnak:1603732432935

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

    If inside the child processes "if block" we call another function to separate the child logic, would it be sufficient to call _exit(0) as well?

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

      Yea, so, in the video I use "return" simply because we are in the main function and that terminates the program. But, if you want to terminate the program outside of the main function you can use the exit() function instead

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

    @CodeVault i have some code and i am not sure how will it end up
    it goes like
    execm is printf("M");
    int main()
    {
    int p=fork();
    if(p)
    {
    printf("A");
    p=fork();
    if(p)
    {
    printf("B");
    exec("execm);
    fork();
    }
    }
    printf("C");
    return 0;
    }
    Any idea how it goes and what prints!

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

      There's this video about exec that I think you should watch: code-vault.net/lesson/as15alvu0a:1603732432433

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

    Hi. Will the pipe creation and closing method presented here work if we're dealing with one-way communication? (the parent will only read from each child once and each child will write to the parent once )

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

      Yes. In fact pipes can only be used for one way communication

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

      @@CodeVault Thanks. What should I write in place of y in "write(pipes[y][1]), &x, sizeof(int)" if I want to write to the parent process only?

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

      Idk how your code looks

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

    hi! first of all thanks a lot for your incredible videos, you really manage to break down complicated topics into understandable parts!!
    in this case theres just one thing, thats not clear to me: how exactly does the wait(NULL) loop work? And why do the anterior lines (76-78) not get executed.
    From my intuition i would say that these statement get blocked since read() is still blocked, until the last process writes into the corresponding pipe, however can I really be sure that the program doesnt close the pipe before I can read from it? Also how can I be sure that printf() in line 76 doesnt print "The final result is 5" without waiting for reading from the pipe?

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

      The wait(NULL) loop works simply because wait(NULL) waits for ANY child process to finish its execution. If the process has no child processes then it immediately returns, thus, doing nothing for processes that has no children. The while loop is there because we want to wait for ALL child processes... wait(NULL) only waits for one at a time
      read() always waits for something to be written in the pipe's buffer. That should answer your questions.

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

    Do you have any resources regarding fork system call and assigning forks to specific tasks? ie parent creates multiple children and each child gets assigned to a specific task (function).
    In an implementation I did I used an integer variable "i" and used that to determine what function should be called, like you did in this example. My wondering is that since # of processes created = 2^n - 1 (where n is the number of processes desired) is there a way to utilize the child of the child in such a way that you don't need to check what iteration you are, in the loop?
    Sorry if I am not getting my thoughts across clearly, maybe just provide some other resources/videos on the topic and I can come up with a better question
    Thanks for sharing also!

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

      There is this article regarding process affinity (I assume that this is what you are referring to): www.linuxjournal.com/article/6799

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

    Would there be an issue if you'd close all the pipes at once after you've read and written to them?

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

      You mean at the end of the program? There could be some issues since closing the last write end of a pipe sends an EOF to the remaining open read ends. So if you close them later on, you might have a delay if you are waiting for that EOF.
      In this example I don't think it's an issue but if, for whatever reason, you want to send an indefinite amount of integers throughout the loop, then waiting for EOF to end the process is a common practice

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

      ​@@CodeVault Thank you for the response!
      Specifically in your example, inside a child process, when you are closing some pipes then using dup2 and then closing the others would it also be sufficient to do the following:
      - Use dup2
      - Close all pipes
      Essentially not needing to remember which ones to close before duping and after?

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

      Ahhh, yeah, that could also work I think

  • @m.preacher2829
    @m.preacher2829 2 ปีที่แล้ว

    when the child process returns, it would not enter the for loop again. So only the main process created the new process in each for loop? so it created 4 processes?

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

    I wish u could know that u saved some brazilian students in OS subject from UNIFESP

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

    First of, a big thank you for this whole IPC series. I have a question that I agree most of us watching this will probably be looking for. This is what I want to achieve
    // Child code
    int main(){
    int x = -1;
    scanf("%d", &x);
    x *= x;
    printf("%d", x);
    }
    I have a parent process that creates multiple child processes, the child process uses exec* to spin up a process of the above code
    I want the parent to be able to send a number to the child process using STDIN and receive the output from the child using STDOUT
    In short, how do I redirect the STDIN and STDOUT of the child processes. So far I've only managed to redirect the STDOUt using dup2, trying the same with STDIN fails. I'd really appreciate any help or pointers to where I can get a solution to this.

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

      There is this video which I think should help, it's basically simulating the pipe operator in bash: code-vault.net/lesson/oxyoxbvnak:1603732432935

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

      @@CodeVault in this case, will we have tor redirect the STDOUT and STDIN for each of the n+1 pipes ?

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

    Hi CodeVault I am not able to run fork in my visual studio code.... I have to use it in my assignment and i am stuck at error no such file or directroy please help me out in this as my assginment deadline is aproaching and I am stuck on this part

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

      You need either Linux or WSL (on Windows) since these are Linux specific functions

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

    How would this work with execvp?

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

      Just make sure you use dup2 to properly link stdout and stdin of the new processes then call execvp. If done correctly each process launched should read from the previous' stdout and write to the next stdin

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

    It's awesome. Where can i find the code?

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

      Here it is: code-vault.net/lesson/v5cml4fg0w:1610639843712

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

    Hello I have a question. What if I don't want Childs to return from the loop but just break it and continue doing something after that. How to check when it is the child and when it is the parent process so they don't all of them execute the code after the for loop ?

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

      You'll need an array of pids for that.
      int pids[4];
      int i;
      for (i = 0; i < 4; i++) {
      pids[i] = fork();
      if (pids[i] == 0) break;
      }
      // If that array of pids contains a 0, that means, the process is a child, otherwise, it's the main parent
      int isChild = 0;
      for (i = 0; i < 4; i++) {
      if (pids[i] == 0) {
      isChild = 1;
      }
      }
      if (isChild) {
      // executing in a child process
      } else {
      // executing in a parent process
      }

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

      @@CodeVault great thank you very much and one last thing, im working on one project for interprocess communication, using a model of many clients-one server where client is requesting random line (n requests from each client) from a file and server is returning this line, and i'm asked to find also average time for request respond of each client.... However when i find the average time (in the code of the childs) i cant find any way to print them all together at the end of the program, because each child is randomly requesting and it's not sure that each child will execute serial it's code... any idea for that problem(if you understand it)

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

      Hmm... that's a bit more tricky. You could write the result through the same pipe you do the communication, send it to the server, save the results in an array somewhere in your program and just print them all at the end

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

      @@CodeVault yeah it was but I found it... anyway thank you

  • @m.preacher2829
    @m.preacher2829 2 ปีที่แล้ว

    my result are printed out of order. but the final result is correct. how can i fix this?

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

      You could have just one thread/process printing all the output after all of them are finished executing

  • @coindreaufrancois-xavier2655
    @coindreaufrancois-xavier2655 ปีที่แล้ว

    What if we don't know at the begining how many fork there will be ?

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

      You can just change the loop condition to fit your needs

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

    I like this vedio

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

    Make a video about the feature - access

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

      I don't quite understand what feature you mean. Can you elaborate on this?

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

    Excellent video. But I am confused with this result : How it is 14 ?
    fork();
    printf("P1
    ");
    fork();
    printf("P2
    ");
    fork();
    printf("P3
    ");

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

      14 what exactly? I don't quite understand the question

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

      @@CodeVault thanks for your reply. As you discussed fork() in the video. If you run this code, you will get 14 prints of P1, P2 & P3. If you explain the steps how this code printed 14 times, that would be great.

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

      @@shahnurarefinbd Oh! It's simple. The first fork() creates a second process. You have 2 processes that call the P1 printf. The second fork creates a second process for each of those 2 processes (so you get 4 in total). P2 printf is called by 4 processes. The third fork creates a second process for each of those 4 processes (so you get 8 in total). P3 printf is called by 8 processes.
      In total we have 2 + 4 + 8 = 14 printf calls