How to execute another program in C (using exec)

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

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

  • @sleepaleart12
    @sleepaleart12 3 года назад +45

    Dude your videos are life saving! I'm beginning to actually understand my computer systems class because of them. Thank you so much!

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

    Thank you, sir, for great explanation and examples. Good job!

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

    I've really been enjoying your content. Thanks for all your great work and well constructed presentations.

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

    Thank you very much for this video. Excellent explanation and demo. Saved me many hours or reading and messing about.

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

    Just worked with Shell project following your tutorials. Thanks

  • @zaabimahdi
    @zaabimahdi 4 года назад +7

    I just finished my Shell Project yesterday ! you are reading my mind ...

  • @GAMEVUEG
    @GAMEVUEG 3 года назад +78

    Bro is there a way to thank you financially for your help ? You saved my semester, I would like to thank you for that

    • @xxjblexx
      @xxjblexx 2 года назад +18

      He's Saving so many semesters ahahahahahha

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

      ​@@xxjblexx Every year😭

  • @1singe_
    @1singe_ 3 года назад +6

    You are saving my semester thank you

  • @liviusalecu5084
    @liviusalecu5084 2 года назад +10

    In case you have troubles working with the _execl() function and with the #include , try solely including and using the execl() function.
    This temporary solved my issues.
    Thank you for all the clear explanations!

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

      unistd is only for unix based system and most probablly u cant use unistd in windows and also in windows with process u need to use _execl() but in unix u can use execl()

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

    You are the hero of CS students the world over! Thanks so much

  • @hugoirwanto9905
    @hugoirwanto9905 3 года назад +5

    This is the best exec tutorial! :D

  • @yangyue2791
    @yangyue2791 2 месяца назад

    ❤ A real Vault of Code!!! Your videos helped me a lot to understand not only how to use system calls, but also the principles of OS. I did bad in my undergraduate OS and computer network courses. Now I am taking graduate OS courses and reviewing the knowledge. Following your video, I am confident in being an OS/computer network engineer or researcher!

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

    In my opinion, you are much better than uni's doctors 🌚.
    THANK YOU SO MUCH FOR YOUR EFFORT!!

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

    You can use a buffer to write OUTPUT and ERROROUTPUT to a Buffer string or byte Buffer and print it out after execution.

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

    You did it, thanks bud!

  • @axmedkayse01
    @axmedkayse01 15 дней назад

    Amazing video for exec and os in general.

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

    Thank you for the great explanation!

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

    AWESOME. Very clear explanation!

  • @user-gd9iu4kg9v
    @user-gd9iu4kg9v 6 месяцев назад

    Thanks a lot, much clearer than my professor's instruction

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

    Great video, you were ultra helpful. Thank you very much

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

    Thank you so much for this video.. I understood everything.. thanks alot.. . loved it..

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

    very good explanation!Thank you!

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

    Great video. Thank you for your great explanation! Helped me a lot!
    On a side note, I believe this video is out of order in the Unix Processes in C playlist.

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

      It was a standalone video but I thought it was important enough to add it to the playlist. Were some concepts difficult to understand in this video?

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

    great video, very clear explanation

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

    great video, clearly explanation!

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

    very helpful, thank You!

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

    one of the best channels

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

    Nice, thanks bro!

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

    Thank you for your videos

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

    so since the execute function replaces the memory of the main program there is no way to return to the print statement at the bottom? Follow up question: Does that mean that a program can only have one execute function and its usually at the end, since everything after that is ignored?

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

      Yes, unless you're creating a child process to execute that command on. Follow up lesson: code-vault.net/lesson/as15alvu0a:1603732432433

  • @AshokKumar-mk1nr
    @AshokKumar-mk1nr 4 года назад

    wow.. very informative...

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

    You are a Legend!

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

    Please do videos on creating libraries and linking it to a code and building it on linux

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

    Hi ! Thanks for this video ! I just don't understand why did you put in the second argument the same argument thant for the first in the execl function ? Can you explain me ? Thanks !

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

      First "ping" is the program we want to execute. All the other arguments are sent to the ping program's arguments (to the argv and argc parameters in main basically). So, that second "ping" will be argv[0] inside the "ping" program

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

    nice explanation

  • @user-zj7wu9mt9k
    @user-zj7wu9mt9k 2 года назад

    I have a question, how can you pass argv[i] in the execlp inside the string parameters? For example I want to call sed s/str1/str2/g but the str1 and str2 is argv[2] and argv[3] which is given from the user. So in fact I want to do execlp{"sed" , "sed" , "s/argv[2]/argv[3]/g", 0} ,but argv[2] , argv[3] supposed to be the input from the user in terminal.

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

      You can simply use a function that formats your string like:
      sprintf(res, "s/%s/%s/g", argv[2], argv[3]);
      And then just pass that res string to execlp

    • @user-zj7wu9mt9k
      @user-zj7wu9mt9k 2 года назад

      @@CodeVault oh , Thank you so much!

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

    Question, so if I were to fork this into a new process, would still close everything?

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

      I'm not sure what you mean, can explain your question a bit better?

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

    Awesome videos man but damn if I am wearing headphones I think your keystrokes are giving some bass lol

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

      Yeah, I think I fixed the issue in later videos, sorry about that

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

    you probably get alot of comments like this but this video was indeed life saving :D

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

    thanks sir

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

    Great Video! Thanks for the explanations about "v" "l" "e" that helps me a lot to remember this function. But i didn't understand this envp string and how to use it. Is there maybe an other video about that? So i watched the video about the main function parameters but it was not enough for me to work with envp. 🤔 Thank you so much for produce all this great videos!!!

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

      I need to make a video on this envp variable. The main jist of it is, all the operating systems provide you with variables that you can set system-wide. These are being passed (usually by the OS) and accessed in the main function of each program.

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

      @@CodeVault Would be nice =) i use it now with execve and extract the PATH= part first. But would be nice to know about the other informations in envp and why execve will have this variable.. idk. 😅

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

    Hello Sir, thanks for the tutorial, that is very helpfull. but currently I am having a problem. When I execute the command "ls -R foldername > redirectionfile". It works in terminal, but in C It always yells the error "cannot access '>': No such file or directory". How can I solve this issue?

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

      ls -R foldername > redirection
      Is actually a bash command since > is part of bash. You'll have to use sh with:
      sh -c "ls -R foldername > redirection"
      That third parameter will have to be one string that you pass to exec.

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

    hey! thanks!!!!!

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

    What if i want to execute a script shell throughout a c program, i got the path, but which function to use and what would the arguments be? Thank youu

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

      You can actually use sh like so to execute a shell script:
      execlp("sh", "-c", "ls -la", NULL);

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

      @@CodeVault you probably have no idea but you saved me, thaaank youuuu!!

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

    Is the _ necessary? My IDE seems to work without the underscore.

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

      Microsoft Visual C compiler has these "_" prefixed functions, on other compilers you don't need them. These "exec" functions are originally from Unix systems (namely the unistd.h library) but got adopted by other platforms/compilers too.

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

    What about using the perror or strerror functions instead of using directly errno ?

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

      You can use that as well! I don't like perror because there's no format string so I usually use fprintf(stderr, ...) instead

  • @BenDover-yn3kp
    @BenDover-yn3kp 2 года назад +1

    How can I use the execv function in order to execute system commands, like "date"?

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

      With execvp you could just pass "date" as the first argument to the function (and the first element in the argv array). If you specifically need to use execv, you'll have to pass in the path to the "date" executable instead. You can find the path by running this command on your computer:
      whereis date
      You can do this for any command you don't know the path of

  • @malwa-ka-kisan
    @malwa-ka-kisan 11 месяцев назад

    Good👌👌

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

    do you have video about heap data structure?

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

      No, I'm sorry, we don't have much about data structures yet

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

    Good and useful

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

    how can we use the _exec if you are going to send the project to another person ,because the adress or the path will change so how can we deal with it ?

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

      You can use execlp (or any exec function that ends in p). This one uses the PATH variable and you only need to give it the executable's name and not the whole path

  • @ShivamSharma-nb6gh
    @ShivamSharma-nb6gh 2 года назад

    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

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

      Haven't tried that yet so I'm not sure how it works on WSL. How do you call fork?

    • @ShivamSharma-nb6gh
      @ShivamSharma-nb6gh 2 года назад

      @@CodeVault instead of local machine select wsl in VS'configuration and then it works

    • @ShivamSharma-nb6gh
      @ShivamSharma-nb6gh 2 года назад

      Btw I'm using createprocess() now

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

    🤩🤩🤩🤩🤩

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

    Let's say there is a date command that prints the date on the screen. I want to call it, but then I want my C code to continue. How to do that? Just call a process, wait for it, and when it's finished, continue the C code. I don't want the called process to take my program over. Thanks.

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

      You can use fork() (or CreateProcess()) to spawn another process, then, in the spawned process call exec while in the parent process wait for the child process to finish executing.
      I might make a video on this.

    • @user-lu1nx4vz5x
      @user-lu1nx4vz5x 4 года назад

      @@CodeVault would be great!

  • @cr.6848
    @cr.6848 2 года назад

    I writing a linux shell and have the task to execute programs with a child process but i dont really unterstand if their is a way that the parent (main process) dont wait for the child, without the waitpid i have the weird bug that my child use the same while loop as the main. I thought that if i call execvp, that the child only execute this.

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

      I'm not sure I understand your issue. Can you send in the code?

    • @cr.6848
      @cr.6848 2 года назад

      @@CodeVault
      So my Problem is that i have to wait for the child process to finish, if the parent dont wait for the child with waitpid, its run also the main code and print printf("Give Input:
      ");. How can i stop that ? Does i have to wait for the process to finish ?
      I thought if i execute execvp() the child process never comes pack to the main function. Also if i dont use waitpid, it doesnt print "something", but i know why

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

      So basically you always have to wait for a child process regardless if you used execvp on it or not (otherwise there are certain resources that are almost never released). BUT, you don't have to wait right after calling fork, you can call waitpid anywhere in the parent process, for example, right before your return 0 in the main function. That way your while loop can continue.

    • @cr.6848
      @cr.6848 2 года назад

      @@CodeVault ahhh thank you very mutch

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

    How would I use it to launch programe from another if it step out the code in other word my main program gonna end itself ?

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

      You can use fork() in this case. Take a look at this video: code-vault.net/course/46qpfr4tkz:1603732431896/lesson/as15alvu0a:1603732432433

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

      @@CodeVault fork only works in Linux envr

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

      @@CodeVault I’ve found another solution using shell exec in windows envr

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

    If I want to exec some program that the user input, like, the user puts " firefox ", how can I exec that program, because the path can change of pc for pc...

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

      There's execp/execlp/execvp for that which makes use of the PATH environment variable. If the user set it correctly, you shouldn't need to mess with paths

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

      @@CodeVault like, the user set manually the dir of the program alright?

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

      @@CodeVault import sys
      from subprocess import run
      def start(navegador, site):
      run([navegador, site])
      def main():
      from threading import Thread
      from time import sleep
      navegador = input("navegador: ")
      site = input("site: ")
      tempo = input("tempo: ")
      thread = Thread(target=start, args=(navegador, site))
      thread.start()
      sleep(60*tempo)
      run(["pkill", navegador]) 
      Im learning C now, and I was trying to make this same code of python, but in C

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

      Yes

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

      @@CodeVault thanks for your attention man, Im waiting for new videos :) bye

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

    This video is in the wrong order in the playlist can you fix it pls ?

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

      I fixed it. Thanks for telling me

  • @priyapandey9813
    @priyapandey9813 3 дня назад

    What if we want to execute rest of the program seamlessly?

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

    Can we just use system ()? Why not?

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

    Noice

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

    anything I know or did

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

    Hey there I have a question. Say I have my own program "myprogram.exe" and I want to call "myprogram" instead of ping(which you called usign execl() function). I have done this I can call "myprogram" and it runs as expected. Now the part I don't get is how do I make my "myprogram" to get those arguments passed through execl() and act accordingly ? Like how does this argument passing work ?
    Let me give you an example, I want to print '24' on screen so I write my "myprogram.exe" to print '24' only on a certain condition. And I want that condition be evaluated from the argument that is passed by execl() when it called. Just like ping accepted the argument and acted accordingly, how can I make my "myprogram" to act according to arguments passed from execl()?
    Englighten me if you have any idea about it.

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

      That's exactly what argc and argv are for. You can learn more about them in this video: code-vault.net/lesson/dbijqbwu2a:1603733526118