0% found this document useful (0 votes)
7 views7 pages

Experiment No 11 (OSII)

This document outlines an experiment focused on Time, Sleep, and Clock Management in Unix operating systems. It details system calls related to time management, such as stime, time, and times, and provides code examples demonstrating their usage. Additionally, it includes a clock management example using signal handling for alarms.

Uploaded by

devab52664
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Experiment No 11 (OSII)

This document outlines an experiment focused on Time, Sleep, and Clock Management in Unix operating systems. It details system calls related to time management, such as stime, time, and times, and provides code examples demonstrating their usage. Additionally, it includes a clock management example using signal handling for alarms.

Uploaded by

devab52664
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment no 11

Title: Study and Implement Time, Sleep and Clock Management.

Aim: To Learn about Time, Sleep and Clock Management.

Theory: Write Theory about Time, Sleep and Clock Management in Unix
operating system.

Write as it is given below

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:

 restart the clock


 schedule invocation of internal kernel functions based on internal
timers
 provide execution profiling capability for the kernel and for user
processes
 gather system and process accounting statistics,
 keep track of time
 send alarm signals to processes on request
 periodically wake up the swapper process
 control process scheduling

Problem Statement: Demonstrate Time, Sleep and Clock Management.

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()

void child(int n);

int main() {

int i;

struct tms pb1, pb2;

long pt1, pt2;

// Corrected the usage of times() function

pt1 = times(&pb1);

for (i = 0; i < 10; i++) {

// Added braces for better readability

if (fork() == 0) {

child(i);

exit(0); // Exiting child process after its work is done

for (i = 0; i < 10; i++) {

wait(NULL); // Changed wait((int *) 0) to wait(NULL)


}

pt2 = times(&pb2);

// Corrected printf statement

printf("parent real %ld user %ld sys %ld csys %ld\n",

(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;

// Corrected the function definition for child()

void child(int n) {

int i;

struct tms cb1, cb2;

long t1, t2;


t1 = times(&cb1);

for (i = 0; i < 10000; i++) {

// Empty loop for simulating some work

t2 = times(&cb2);

// Corrected printf statement

printf("child %d: real %ld user %ld sys %ld\n",

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>

// Signal handler function for handling alarm signal

void alarmHandler(int signum) {

printf("Alarm triggered! Time's up.\n");


// Perform actions to handle the alarm, if needed

int main() {

// Register the alarm signal handler

signal(SIGALRM, alarmHandler);

printf("Setting alarm for 5 seconds...\n");

// Set the alarm to trigger after 5 seconds

alarm(5);

// Wait indefinitely to keep the program running

// The alarm will interrupt the sleep after 5 seconds

while (1) {

sleep(1); // Sleep for 1 second at a time

return 0;

}
Conclusion.

You might also like