How to execute another program in C (using exec)

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

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

  • @liviusalecu5084
    @liviusalecu5084 2 ปีที่แล้ว +13

    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!

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

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

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

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

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

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

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

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

    This is the best exec tutorial! :D

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

    You are saving my semester thank you

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

    AWESOME. Very clear explanation!

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

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

  • @zaabimahdi
    @zaabimahdi 4 ปีที่แล้ว +7

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

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

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

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

    ❤ 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!

  • @user-gd9iu4kg9v
    @user-gd9iu4kg9v 9 หลายเดือนก่อน

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

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

    Thank you for the great explanation!

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

    Amazing video for exec and os in general.

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

    very good explanation!Thank you!

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

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

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

    great video, clearly explanation!

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

      He's Saving so many semesters ahahahahahha

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

      ​@@xxjblexx Every year😭

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

      @@techdoctorP every year

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

    great video, very clear explanation

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

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

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

    one of the best channels

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

    Nice, thanks bro!

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

    very helpful, thank You!

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

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

      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?

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

    You did it, thanks bud!

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

    Thank you for your videos

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

    You are a Legend!

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

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

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

    nice explanation

  • @AshokKumar-mk1nr
    @AshokKumar-mk1nr 4 ปีที่แล้ว

    wow.. very informative...

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

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

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

  • @malwa-ka-kisan
    @malwa-ka-kisan ปีที่แล้ว

    Good👌👌

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

    thanks sir

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

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

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

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

    Good and useful

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

    hey! thanks!!!!!

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

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

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

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

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

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

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

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

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

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

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

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

    🤩🤩🤩🤩🤩

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

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

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

  • @cr.6848
    @cr.6848 3 ปีที่แล้ว

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

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

    • @cr.6848
      @cr.6848 3 ปีที่แล้ว

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

      @@CodeVault ahhh thank you very mutch

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

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

    42 Students, don`t be lazy at least subscribe and like this dude !)
    Thank y so much!!!

  • @ShivamSharma-nb6gh
    @ShivamSharma-nb6gh 3 ปีที่แล้ว

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

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

    • @ShivamSharma-nb6gh
      @ShivamSharma-nb6gh 3 ปีที่แล้ว

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

    • @ShivamSharma-nb6gh
      @ShivamSharma-nb6gh 3 ปีที่แล้ว

      Btw I'm using createprocess() now

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

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

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

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

      Already addressed .. Thanks

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

      I fixed it. Thanks for telling me

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

    Can we just use system ()? Why not?

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

      Yes, you can too

  • @ronjeremy1232
    @ronjeremy1232 4 ปีที่แล้ว

    Noice

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

    anything I know or did

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

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

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