How to execute another program in C (using exec)

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

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

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

    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 4 года назад +53

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

  • @GAMEVUEG
    @GAMEVUEG 4 года назад +84

    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 года назад +21

      He's Saving so many semesters ahahahahahha

    • @techdoctorP
      @techdoctorP Год назад +3

      ​@@xxjblexx Every year😭

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

      @@techdoctorP every year

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

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

  • @yangyue2791
    @yangyue2791 11 месяцев назад +1

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

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

    Just worked with Shell project following your tutorials. Thanks

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

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

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

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

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

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

    You are saving my semester 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?

  • @alexd7256
    @alexd7256 20 дней назад

    You are the best, thank you! 💪🙏

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

    This is the best exec tutorial! :D

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

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

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

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

  • @axmedkayse01
    @axmedkayse01 9 месяцев назад

    Amazing video for exec and os in general.

  • @陳己見
    @陳己見 Год назад

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

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

    Thank you for the great explanation!

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

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

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

    AWESOME. Very clear explanation!

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

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

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

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

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

    very good explanation!Thank you!

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

    Nice, thanks bro!

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

    very helpful, thank You!

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

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

    great video, clearly explanation!

  • @juanfernandez1504
    @juanfernandez1504 4 года назад +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  4 года назад +2

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

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

    great video, very clear explanation

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

    Thank you for your videos

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

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

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

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

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

    • @CodeVault
      @CodeVault  4 года назад +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.

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

    You did it, thanks bud!

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

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

    🤩🤩🤩🤩🤩

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

    one of the best channels

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

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

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

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

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

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

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

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

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

    • @CodeVault
      @CodeVault  3 года назад +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

  • @malwa-ka-kisan
    @malwa-ka-kisan Год назад

    Good👌👌

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

    • @МаксимХавро-ч4ы
      @МаксимХавро-ч4ы 4 года назад

      @@CodeVault would be great!

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

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

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

      I fixed it. Thanks for telling me

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

    do you have video about heap data structure?

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

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

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

    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  3 года назад

      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

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

    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  3 года назад

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

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

      @@CodeVault fork only works in Linux envr

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

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

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

    Can we just use system ()? Why not?

  • @ΘωμάςΠασχάλης-κ8μ
    @ΘωμάςΠασχάλης-κ8μ 3 года назад

    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  3 года назад +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

    • @ΘωμάςΠασχάλης-κ8μ
      @ΘωμάςΠασχάλης-κ8μ 3 года назад

      @@CodeVault oh , Thank you so much!

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

    thanks sir

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

    You are a Legend!

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

    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  3 года назад +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.

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

    How to execute and pipe the details?

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

      You could replace stdin with an end of a pipe and write to it from another process. Similar to this but instead using stdin: code-vault.net/lesson/43zvcsz6o1:1603732432539

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

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

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

    nice explanation

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

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

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

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

    wow.. very informative...

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

    hey! thanks!!!!!

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

    Good and useful

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

    anything I know or did

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

    Noice

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