I want to create a mini shell for UNIX just to know the ins and outs of everything. I am having some confusions understanding things that I used to get for granted. This is kinda philosophical question. When I creating a “shell”, I assume I have a UNIX with no shell, so what would be the std in and std out in this case? doesnt functions like system() and exec() use the shell to execute programs, so if I am creating a shell in the first place. How do these functions work?
Share
There are several functions in the
execfamily:execve(2),execl(3),execle(3),execlp(3),execv(3),execvp(3). The first one,execve(2)is provided by the operating system kernel as a system call. (Well, okay, the function that programs call is provided by the system C library, but it is a simple wrapper around the system call.) The other functions provide slightly different semantics and are implemented in terms of theexecve(2)function.The shells could use
execvp(3)orexeclp(3)to provide thePATHsearch for executables, but at leastbash(1)hashes the full pathname of executables to provide a performance benefit. (Seebash(1)built-inhashfor details.)system(3)is implemented via/bin/sh -c, as you’ve surmised.The standard input and output is set up by whichever program spawned the shell. If a user logs in on the console directly, it’ll be handled by
agetty(8)ormgetty(8)or whichevergetty-alike program handles direct logins. If a user logs in viasshd(8), thensshd(8)is in charge of creating theptyand delegating the terminal slave to the shell. If a user creates their shells viaxterm(1)or other terminal emulators, then those processes will be responsible for hooking up the standard input, output, and error for the shell.