News for hackers. Stuff that matters.
Re:The Beauty of Unix Pipelines by quickthrower2 on Thursday June 04, 2020 @06:22PM (#23422680)
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.
Re:The Beauty of Unix Pipelines by kccqzy on Thursday June 04, 2020 @06:55PM (#23422947)
It 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.
Re:The Beauty of Unix Pipelines by uryga on Thursday June 04, 2020 @08:42PM (#23423794)
would `become` be a good alternate name for it?
Re:The Beauty of Unix Pipelines by yjftsjthsd-h on Thursday June 04, 2020 @10:21PM (#23424376)
That's loosely correct.
Re:The Beauty of Unix Pipelines by asdff on Thursday June 04, 2020 @08:45PM (#23423813)
whats a simple example of using this?
Re:The Beauty of Unix Pipelines by gorgoiler on Friday June 05, 2020 @12:37AM (#23425126)
It’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

Re:The Beauty of Unix Pipelines by Icathian on Friday June 05, 2020 @09:19AM (#23428142)
Bash 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.
Re:The Beauty of Unix Pipelines by zwetan on Saturday June 06, 2020 @01:51AM (#23437131)
see here: 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 ;)

Re:The Beauty of Unix Pipelines by JdeBP on Friday June 05, 2020 @04:28AM (#23426269)
Well 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

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.

Re:The Beauty of Unix Pipelines by quickthrower2 on Friday June 05, 2020 @05:20AM (#23426536)
Thanks. 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.
Re:The Beauty of Unix Pipelines by gorgoiler on Friday June 05, 2020 @12:21AM (#23425046)
Its 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.