Experiment No 11 (OSII)
Experiment No 11 (OSII)
Theory: Write Theory about Time, Sleep and Clock Management in Unix
operating system.
Time:
There are several time-related system calls, stime, time, times, and alarm.
The first two deal with global system time, and the latter two deal with time
for individual processes.
stime allows the suepruser to set a global kernel variable to a value that
gives the current time:
stime(pvalue);
where pvalue points to a long integer that gives the time as measured in
seconds from midnight before (00:00:00) January 1, 1970, GMT. The clock
interrupt handler increments the kernel variable once a second.
time retrieves the time as set by stime.
time(tloc);
where tloc points to a location in the user process for the return
value. time returns this value from the system call, too.
times retrieves the cumulative times that the calling process spent executing
in user mode and kernel mode and the cumulative times that all zombie
children had executed in user mode and kernel mode.
struct tms *tbuffer;
times(tbuffer);
where the structure tms contains the retrieved times, and is defined by:
struct tms {
// time_t is the data structure for time
time_t tims_utime; // user time of process
time_t tm_stime; // kernel time of process
time_t tms_cutime; // user time of children
time_t tms_cstime; // kernel time of children
}
times returns the elapsed time "from an arbitrary point in the past", usually
the time of system boot.
Clock:
The functions of the clock interrupt handler are to:
Run the Code and take screenshot of code and output both.
Time:
#include <stdio.h>
#include <sys/types.h>
#include <sys/times.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
// Corrected the function declaration for child()
int main() {
int i;
pt1 = times(&pb1);
if (fork() == 0) {
child(i);
pt2 = times(&pb2);
(pt2 - pt1),
(pb2.tms_utime - pb1.tms_utime),
(pb2.tms_stime - pb1.tms_stime),
(pb2.tms_cutime - pb1.tms_cutime),
(pb2.tms_cstime - pb1.tms_cstime));
return 0;
void child(int n) {
int i;
t2 = times(&cb2);
n,
(t2 - t1),
(cb2.tms_utime - cb1.tms_utime),
(cb2.tms_stime - cb1.tms_stime));
Clock:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int main() {
signal(SIGALRM, alarmHandler);
alarm(5);
while (1) {
return 0;
}
Conclusion.