3 ms·
Can you give me any clue as to what execve does? I looked at the man page but none the wiser. Sounds like magic from what I read there. I'm from a Windows backg
by quickthrower2 6y ago
Can you give me any clue as to what execve does? I looked at the man page but none the wiser. Sounds like magic from what I read there. I'm from a Windows backgroud and not used to pipes.
- kccqzy 6y agoIt replaces the executable in the current process with a different executable. It's kind of like spawning a new process with an executable, except it's the same PID, and file descriptors without CLOEXEC remain open in their original state.
- uryga 6y agowould `become` be a good alternate name for it?
- yjftsjthsd-h 6y agoThat's loosely correct.
- asdff 6y agowhats a simple example of using this?
- gorgoiler 6y agoIt’s how all new processes in Unix are created. There is no “spawn new process running this code” operation. Instead: (1) you duplicate yourself with fork() (2) the parent process continues about its business (3) the created process calls execve() to become something else This is the mechanism by which all new processes are created, from init (process 1) to anything you run from your shell. Real life examples here, in this example of a Unix shell: https://github.com/mirror/busybox/blob/master/shell/ash.c https://github.com/mirror/busybox/blob/master/shell/ash.c
- Icathian 6y agoBash is probably the best example of this style of working. You have a shell process that forks and spawns child processes from itself that are turned into whatever program you've called from your terminal.
- zwetan 6y agosee here: https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/e... plenty of basic examples with execv, execve, execvp, etc. if the execv() call succeed then it completely replaces the program running in the process that calls it you can do plenty of tricks with that ;)
- gorgoiler 6y agoIts how you launch (execute) a new program. Such programs typically don’t have any interaction so the v and the e parts refer to the two ways you can control what the program does. v: a vector (list) of input parameters (aka arguments) e: an environment of key-value pairs The executed program can interpret these two in any way it wishes to, though there are many conventions that are followed (and bucked by the contrarians!) like using “-“ to prefix flags/options. This is in addition to any data sent to the program’s standard input — hence the original discussion about using pipes to send the output of one command to the input of another.
- JdeBP 6y agoWell you have a Windows background, so you know BASIC, right? And you know that BASIC has a CHAIN statement, right? (-: Yes, I know. You've probably never touched either one. But the point is that this is simply chain loading. It's a concept not limited to Unix. execve() does it at the level of processes, where one program can chain to another one, both running in a single process. But it is most definitely not magic, nor something entirely alien to the world of Windows. * https://en.wikibooks.org/wiki/QBasic/Appendix#CHAIN https://en.wikibooks.org/wiki/QBasic/Appendix#CHAIN If you are not used to pipes on Windows, you haven't pushed Windows nearly hard enough. The complaint about Microsoft DOS was that it didn't have pipes. But Windows NT has had proper pipes all along since the early 1990s, as OS/2 did before it since 1987. Microsoft's command interpreter is capable of using them, and all of the received wisdom that people knew about pipes and Microsoft's command interpreters on DOS rather famously (it being much discussed at the time) went away with OS/2 and Windows NT. And there are umpteen ways of improving on that, from JP Software's Take Command to various flavours of Unix-alike tools. And you should see some of the things that people do with FOR /F . Unix was the first with the garden hosepipe metaphor, but it has been some 50 years since then. It hasn't been limited to Unix for over 30 of them. Your operating system has them, and it is very much worthwhile investigating them.
- quickthrower2 6y agoThanks. I use pipes, occasionally ( I admit I prefer to code in a scripting language ) in Windows. It’s just execve that I was not familiar with.