How the Unix shell runs a program?

Member

by jewel , in category: General Help , 2 years ago

How the Unix shell runs a program?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by lonzo , 2 years ago

@jewel The Unix shell is one of the main components of a Unix System. There are 3 major components, the other two being the kernel, and programs.


The kernel is the brain box of the Unix operating system. It organizes everything for the operating system like allocating time and memory to programs, handling communication between various subsystems and components in the computer, and also handling the file structure.


The programs are mostly application software that runs logical sequences of commands in order to accomplish a particular task.


The shell on the other hand is also referred to as terminals. They can create a terminal session for running our programs. A shell can open up another sub-shell.


The way shell works are that they allow us to send some command to the kernel to run some programs. The following demonstrates how a shell, together with a kernel allows us to execute a program.

  • The user types and runs some kernel commands.
  • The shell finds the program and instructs the kernel to run the program
  • The kernel completes its job, returns control back to the shell and the shell displays the result.

Note that there is more than one type of shell program for the various UNIX flavors.

Member

by alivia , 7 months ago

@jewel 

When a program is executed in the Unix shell, the following steps typically occur:

  1. The user enters a command in the shell, typically in the form of a command followed by arguments or options.
  2. The shell parses the command to identify the executable program and any accompanying arguments or options.
  3. The shell searches for the executable program in the directories listed in the system's PATH variable. It looks for the program in each directory sequentially until it finds a match.
  4. Once the program is located, the shell creates a new process using the fork() system call. This creates a copy of the shell process.
  5. The shell then uses the exec() system call to replace the copy of the shell process with the executable program. This transfers control to the program code.
  6. The kernel loads the program into memory and starts executing it from the entry point specified in the program's binary file.
  7. The executed program performs its intended tasks, utilizing any provided arguments or options, and may interact with the system through system calls.
  8. After the program finishes execution or encounters an error, it returns control back to the kernel.
  9. The kernel informs the shell of the program's termination status, which allows the shell to display any output or error messages to the user.
  10. The shell prompt reappears, ready to accept the next command.


Overall, the Unix shell acts as an intermediary between the user and the kernel, facilitating the execution of programs by managing the process of creating new processes, searching for program executables, and handling the interaction between the user and the executed program.