Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <time.h>
- int main() {
- pid_t pid1, pid2;
- clock_t start_time, end_time;
- start_time = clock(); // Start measuring main process execution time
- printf("Main process: PID=%d, Parent PID=%d\n", getpid(), getppid());
- // Create the first child process
- pid1 = fork();
- if (pid1 < 0) {
- perror("Fork failed");
- return 1;
- } else if (pid1 == 0) {
- printf("Child 1: PID=%d, Parent PID=%d\n", getpid(), getppid());
- return 0;
- }
- // Create the second child process
- pid2 = fork();
- if (pid2 < 0) {
- perror("Fork failed");
- return 1;
- } else if (pid2 == 0) {
- printf("Child 2: PID=%d, Parent PID=%d\n", getpid(), getppid());
- return 0;
- }
- // Wait for both child processes to complete
- waitpid(pid1, NULL, 0);
- waitpid(pid2, NULL, 0);
- end_time = clock(); // Stop measuring main process execution time
- double execution_time_ms = (double)(end_time - start_time) * 1000 / CLOCKS_PER_SEC;
- printf("Main process execution time: %.2f ms\n", execution_time_ms);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement