In a C program, when you call a function, the CPU jumps to a new address and executes code there. If you want to run a completely different executable file from your program, can you just 'call' it like a function?
When a program is run, the OS performs the following steps:
malloc family).
Include three function calls:
1
2
cd ~/ostep-code/cpu-api
make
fork(), which initiate the creation of a new process. The return of this function call is assigned to variable rc.rc is negative, the function call failed and the program exits with return value 1. This line is evaluated within the parent process (since the child process creation failed).rc is non-negativefork call is successful, and you now have two process. main(), but begins immediately after fork().rc differs in each process. rc in the new process is 0.rc in the parent process is non-zero and actually is the pid of the new process.rc value. These two values should be the same.p1 several times.
1
./p1
fork(), which initiate the creation of a new process. The return of this fuction call is assigned to variable rc.rc is negative, the function call failed and the program exits with return value 1. This line is evaluated within the parent process (since the child process creation failed).rc is equal to 0.pid.rc is non-negative and not equal to 0) wait() function.
1
./p2
fork() lets you create and run a copy of the original process.exec() lets you run a different process in place of the copy of the original process.main fork(), which initiate the creation of a new process. The return of this function call is assigned to variable rc.rc is negative, the function call failed and the program exits with return value 1. This line is evaluated within the parent process (since the child process creation failed).rc is equal to 0.pid.wc in this case).exec replaces the current child process with a completely new process to execute the wc command.exec replaces the current child process with the new process for wc.rc is non-negative and not equal to 0) wait() function.
1
./p3
fork() and exec() is essential to the building of a Linux shell.fork(), but before the call to exec().In Unix, the shell is a program that interprets commands and acts as an intermediary between the user and the inner workings of the operating system. Providing a command-line interface (that is, the shell prompt or command prompt), the shell is analogous to DOS and serves a purpose similar to graphical interfaces like Windows, Mac, and the X Window System.
fork() to create a new child process (to run the program).exec() family functions in the scope of this child process to actually load and run this program.wait() to wait for the child process to finish (now with new process content) before giving user the shell prompt again.
1
2
3
wc p3.c
wc p3.c > newfile.txt
cat newfile.txt
wc is in the file system.p3 as in input to wc.fork() to create a new child process to run the command.> represents a redirection, thus closes the file descriptor to standard output and replaces it with a file descriptor to newfile.txt.
1
2
3
4
./p4
ls
cat p4.output
cat -n p4.c
wc p4 should have printed out to terminal.close(STDOUT_FILENO) closes the file descriptor that writes to the terminal (hence free up that particular file descriptor ID).open(“./p4.output”, …) creates a file descriptor for the p4.output file, but since the file descriptor ID for the terminal is now free, this file descriptor is assigned to p4.output.wc p4 is executed and attempts to write to terminal, it actually writes to p4.output instead.kill(): send signals to a process, including directive to pause, die, and other imperatives. signal(): to catch a signal.