Practical-o6
Title : Demonstration of fork, execve and wait system calls
Prerequisite :Handling C programs in Linux :
Step 1: Write the C Program
$gedit hello.c
write your C code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Save
Step 2: Compile the C Program
$ sudo apt update
$ sudo apt install gcc
$ gcc -o hello hello.c
This command tells GCC to compile `hello.c` and output an executable named `hello`.
Step 3: Run the Executable
$./hello
This should output:
Hello, World!
Step 4: Debugging (Optional)
If your program has issues, you may need to debug it. `gdb` (GNU Debugger) is a powerful debugging tool for C programs.
$sudo apt update
$sudo apt install gdb
Compile the program with the `-g` flag to include debugging information:
$gcc -g -o hello hello.c
Run `gdb` with the compiled executable:
gdb ./hello
Inside `gdb`, you can use commands like `run` to start the program, `break` to set breakpoints, `step` and `next` to step through the code, and `print` to inspect variables.
Step 5: Profiling (Optional)
Profiling helps you analyze the performance of your program. `gprof` is a profiling tool you can use.
Compile with Profiling Information
$gcc -pg -o hello hello.c
Run the executable to generate profiling data:
$./hello
Analyze the Profiling Data
gprof ./hello gmon.out > analysis.txt
Inspect `analysis.txt` to see the profiling results.
Demonstration of the `fork`, `execve`, and `wait` system calls in C :
These are used for process control in Unix-like operating systems.
1. `fork` is used to create a new process by duplicating the calling process.
2. `execve` is used to execute a new program in the current process.
3. `wait` is used to wait for a state change in a child process, typically its termination.
Below is a C program that demonstrates these system calls:
$gedit fork_exec_wait.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
// Fork a child process
pid = fork();
if (pid < 0) {
// Fork failed
perror("Fork failed");
exit(1);
} else if (pid == 0) {
// Child process
printf("Child process (PID: %d)\n", getpid());
// Arguments for execve
char *argv[] = { "/bin/ls", "-l", NULL };
char *envp[] = { NULL };
// Replace the child process image with a new process image
if (execve("/bin/ls", argv, envp) < 0) {
perror("execve failed");
exit(1);
}
} else {
// Parent process
printf("Parent process (PID: %d)\n", getpid());
// Wait for the child process to finish
if (wait(&status) < 0) {
perror("wait failed");
exit(1);
}
if (WIFEXITED(status)) {
printf("Child exited with status %d\n", WEXITSTATUS(status));
} else {
printf("Child did not exit successfully\n");
}
}
return 0;
}
Explanation:
1. Forking:
- `pid = fork();` creates a new process. The new process is the child process, which is a copy of the parent process. The return value of `fork` is zero in the child process and the child's PID in the parent process.
2. Executing a new program:
- In the child process, we use `execve("/bin/ls", argv, envp);` to replace the child process image with a new process image (in this case, the `ls` command). The first argument is the path to the new program, the second is the argument list, and the third is the environment list.
3.Waiting for the child process:
- In the parent process, `wait(&status);` waits for the child process to change state. This usually means waiting for it to terminate. `wait` returns the PID of the child that changed state and stores the termination status in the integer pointed to by `status`.
Compile and Run:
$gcc -o fork_exec_wait fork_exec_wait.c
$./fork_exec_wait
This program will create a child process, replace its image with the `ls` command, and the parent process will wait for the child to finish execution and then print the child's exit status.
Expected Output
When you compile and run this program, the output should look something like this:
Explanation:
1. Parent process (PID: 3015):- This line is printed by the parent process immediately after the `fork` system call.
2. Child process (PID: 3016): This line is printed by the child process immediately after the `fork` system call.
3. Output of `ls -l` command: The child process executes the `ls -l` command, listing the directory contents in long format. The exact output will depend on the contents of the directory where the program is run.
4. Child exited with status 0: This line is printed by the parent process after the child process has finished executing and terminated with an exit status of 0, indicating successful execution.
No comments:
Post a Comment