Shell Scriping:
- The different variants of exec do slightly different things. Some take a variable number of string arguments. Others take a list of strings. Still others let you specify the environment that the process runs with. This particular variant expects a program name and an array (also called a vector, hence the ‘v’) of string arguments (the first one has to be the program name). The ‘p’ means that instead of providing the full file path of the program to run, we’re going to give its name, and let the operating system search for the program in the path.
- Once fork() returns, we actually have two processes running concurrently. The child process will take the first if condition (where pid == 0).
- We use waitpid() to wait for the process’s state to change. Unfortunately, waitpid() has a lot of options (like exec()). Processes can change state in lots of ways, and not all of them mean that the process has ended. A process can either exit (normally, or with an error code), or it can be killed by a signal. So, we use the macros provided with waitpid() to wait until either the processes are exited or killed. Then, the function finally returns a 1, as a signal to the calling function that we should prompt for input again.
- build-in commands are like CD and Exit. If you want to change directory, you need to use the function chdir(), the thing is, the current directory is a property of a process. So if you wrote a program called cd that changed directory, it would just change its own current directory, and then terminate. Its parent process's current directory would be unchanged. Same thing with exit.
- A forward declaration is when you declare (but don’t define) something, so that you can use its name before you define it.
Problem Set:
First Set
- Q: How is a system call different from a function call?
- A System Call provides abstractions to user program and managing computer’s hardware resources; its functionality is generally defined by POSIX for UNIX/ or Windows Standards.
- A system call involves trapping into Kernel, which executes an interuption to Kernel. A Function Call generally refers to a call to a piece of code in the user space, thought, there might be system calls involved in a function call in specific cases such as fopen().
- System call involves a mode switch from user to kernel.
- Functionality: system calls are set by OS developer, function call can be arbitarily written by the programmer