How to execute another program in C (using exec)

Поділитися
Вставка
  • Опубліковано 21 жов 2024
  • Check out our Discord server: / discord

КОМЕНТАРІ • 108

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

  • @GAMEVUEG
    @GAMEVUEG 4 роки тому +80

    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 роки тому +19

      He's Saving so many semesters ahahahahahha

    • @techdoctorP
      @techdoctorP Рік тому +2

      ​@@xxjblexx Every year😭

    • @sharkonstage
      @sharkonstage 4 місяці тому

      @@techdoctorP every year

  • @zaabimahdi
    @zaabimahdi 4 роки тому +7

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

  • @yangyue2791
    @yangyue2791 8 місяців тому

    ❤ 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 3 роки тому +2

    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.

  • @juggles5474
    @juggles5474 Рік тому

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

  • @bama2619
    @bama2619 3 роки тому +3

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

  • @AndrewCodeDev
    @AndrewCodeDev Рік тому

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

  • @陳己見
    @陳己見 Рік тому

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

  • @hugoirwanto9905
    @hugoirwanto9905 4 роки тому +5

    This is the best exec tutorial! :D

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

  • @1singe_
    @1singe_ 4 роки тому +6

    You are saving my semester thank you

  • @bassamnaji2668
    @bassamnaji2668 2 роки тому

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

  • @tuomasbazzan1523
    @tuomasbazzan1523 2 роки тому

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

  • @akashdev3342
    @akashdev3342 11 місяців тому

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

  • @axmedkayse01
    @axmedkayse01 6 місяців тому

    Amazing video for exec and os in general.

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

  • @multivalfran
    @multivalfran 2 роки тому

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

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

  • @dianafarhat9479
    @dianafarhat9479 Рік тому

    Thank you for the great explanation!

  • @LukeAvedon
    @LukeAvedon 2 роки тому

    AWESOME. Very clear explanation!

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

  • @navidnouri151
    @navidnouri151 3 роки тому

    very good explanation!Thank you!

  • @ilusionem2347
    @ilusionem2347 4 роки тому +1

    one of the best channels

  • @xinking2644
    @xinking2644 Рік тому

    great video, clearly explanation!

  • @nl6238
    @nl6238 2 роки тому +1

    Nice, thanks bro!

  • @fernandoguirao3748
    @fernandoguirao3748 2 роки тому

    great video, very clear explanation

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

  • @michalski9141
    @michalski9141 2 роки тому

    very helpful, thank You!

  • @ΘωμάςΠασχάλης-κ8μ
    @ΘωμάςΠασχάλης-κ8μ 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

    • @ΘωμάςΠασχάλης-κ8μ
      @ΘωμάςΠασχάλης-κ8μ 2 роки тому

      @@CodeVault oh , Thank you so much!

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

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

  • @adamloepker8057
    @adamloepker8057 Рік тому

    You did it, thanks bud!

  • @benjaminschwartz8275
    @benjaminschwartz8275 3 роки тому

    Thank you for your videos

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

  • @akshimmu
    @akshimmu 2 роки тому

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

  • @larussverris
    @larussverris 2 роки тому

    You are a Legend!

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

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

  • @itykhono
    @itykhono 5 місяців тому

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

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

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

  • @MSDelhi2
    @MSDelhi2 Рік тому

    nice explanation

  • @priyapandey9813
    @priyapandey9813 5 місяців тому

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

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

  • @malwa-ka-kisan
    @malwa-ka-kisan Рік тому

    Good👌👌

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

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

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

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

  • @AshokKumar-mk1nr
    @AshokKumar-mk1nr 4 роки тому

    wow.. very informative...

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

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

  • @prakhargupta2960
    @prakhargupta2960 3 роки тому

    Can we just use system ()? Why not?

  • @sto2779
    @sto2779 2 місяці тому

    How to execute and pipe the details?

    • @CodeVault
      @CodeVault  Місяць тому

      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

  • @abdullahsohail1781
    @abdullahsohail1781 2 роки тому

    🤩🤩🤩🤩🤩

  • @siyanibillinumberone4054
    @siyanibillinumberone4054 2 роки тому

    Good and useful

  • @sathvikmalgikar2842
    @sathvikmalgikar2842 2 роки тому

    thanks sir

  • @3tranger
    @3tranger 4 роки тому

    hey! thanks!!!!!

  • @ryanalnaser914
    @ryanalnaser914 2 роки тому

    anything I know or did

  • @ronjeremy1232
    @ronjeremy1232 4 роки тому

    Noice