-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.c
More file actions
53 lines (44 loc) · 1.45 KB
/
4.c
File metadata and controls
53 lines (44 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
============================================================================
Name : 4
Author : Piyush Singh
Description : Write a program to measure how much time is taken to execute 100 getppid ( )
system call. Use time stamp counter.
Date: 18th Sep, 2025.
============================================================================
*/
#include <stdio.h> // For printf()
#include <stdlib.h> // For exit()
#include <sys/time.h> // For gettimeofday(), struct timeval
#include <unistd.h> // For getppid()
int main()
{
struct timeval start, end;
long elapsed;
// Step 1: Record start time
if (gettimeofday(&start, NULL) == -1)
{
perror("❌ gettimeofday (start) failed");
exit(1);
}
// Step 2: Call getppid() 100 times
for (int i = 0; i < 100; i++)
getppid(); // System call to get parent process ID
// Step 3: Record end time
if (gettimeofday(&end, NULL) == -1)
{
perror("❌ gettimeofday (end) failed");
exit(1);
}
// Step 4: Calculate elapsed time in microseconds
elapsed = (end.tv_sec - start.tv_sec) * 1000000L +
(end.tv_usec - start.tv_usec);
// Step 5: Display result
printf("⏱️ Time taken for 100 getppid() calls: %ld microseconds\n", elapsed);
return 0;
}
/*
Output :
./4 ─╯
⏱️ Time taken for 100 getppid() calls: 23 microseconds
*/