Thank guys for the input , but I was wondering if mainstream shells like bash/csh use a structure similar to Corona's eg. for their execution. If that's the case, they'd be need to create separate executables for all their builtin commands as a part of their initialization sequences.
There are external commands to match almost every shell builtin -- the UNIX standard requires it. You don't always get a shell as fancy as you want, so there has to be a fallback.
But no -- I don't think so. Shell builtins are definitely not the same as externals, builtins are clearly faster.
Look carefully at what builtins do for you. There's commands like read which read from stdin, and commands like echo which write to stdout -- but you don't get things like cat which do both. That's intentional -- it can keep the builtin entirely inside the shell without risking deadlocking(parts of the shell itself waiting for other parts of the shell itself). It just does whatever's next in the list and carries on, if there's any wait involved its not it's fault.
echo is a particularly simple to do with a builtin. I tried to build a shell once, and managed situations like echo | cat like this:
As long as the message is smaller than the pipe's buffer, you don't have to wait -- just squirt it in the write end and close.
---------- Post updated at 06:38 PM ---------- Previous update was at 06:20 PM ----------
I think what I ended up doing was building a list much more complicated than {"echo", "tac", "more", NULL}, it was a structure with all three file descriptors(stdin/stdout/stderr), and a string for the name.
I opened everything in advance, including all pipes and redirections.
If I wanted process 0 to read from file.txt I could just go processlist[0].fd[0]=open("filename.txt", O_RDONLY);
If I wanted processes 2 and 3 joined with a pipe, I'd do
If I wanted a builtin to print into the first process, I'd create a pipe, squirt in a message, and shove the read-end in the array along with everything else.
And then I'd do one big loop to create every process.
In retrospect, this was silly. Every time I fork()ed that huge wad of pipes I had to close so much junk that didn't need cloning in the first place.
Might be better to just do it as you go. Or maybe I should have played with close-on-exec and only copied the pipes I actually needed. (I slightly lied. Not all files get cloned on fork(), you can pick FD's you don't want being cloned and turn that off.)
Last edited by Corona688; 10-07-2011 at 09:44 PM..
Hi,
Can the cd command be invoked using pipes???
My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Hi...Can anyone please guide me on FIFO Pipes in UNIX.I have lerant things like creating fifo pipes,using them for reads and writes etc.I want to know what is the maximum amount of memory that such a pipe may have? Also can anyone guide me on where to get info on this topic from? (4 Replies)
This is something I've given a lot of thought to and come up with no answer.
Say you have a data stream passing from a file, through process A, into process B. Process A only modifies a few bytes of the stream, then prints the rest of the stream unmodified. Is there any way to stream the file... (4 Replies)
im trying to figure out how to do the following:
using pipes to combine grep and find commands to print all lines in files that start with the letter f in the current directory that contain the word "test" for example?
again using pipes to combine grep and find command, how can I print all... (1 Reply)
I am really new to UNIX and programming in general and so apologies if this thread is a bit simple.
I have searched and found a piece of sample code for a training program I am currently undertaking, but seeing as I am relatively new, I dont completely understand how it works.
Here is the... (1 Reply)
I am really new to UNIX and programming in general and so apologies if this thread is a bit simple.
I have searched and found a piece of sample code for a training program I am currently undertaking, but seeing as I am relatively new, I dont completely understand how it works.
Here is the... (6 Replies)
Hello all, I am trying to learn more about programming Unix pipes in C.
I have created a pipe that does od -bc < myfile | head
Now, I am trying to create od -bc < myfile | head | wc
Here is my code, and I know I might be off, thats why I am here so I can get some clarification.
#include... (1 Reply)
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
Create a pipe to show the number of people who are logged into the system right now.
Create a pipe to show... (2 Replies)