Unix™ Systems Programming [Electronic resources] : Communication, Concurrency, and Threads نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Unix™ Systems Programming [Electronic resources] : Communication, Concurrency, and Threads - نسخه متنی

Prentice Hall

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










9.2 Sleep Functions


A process that voluntarily blocks for a specified time is said to sleep. The sleep function causes the calling thread to be suspended either until the specified number of seconds has elapsed or until the calling thread catches a signal.


SYNOPSIS
#include <unistd.h>
#unsigned sleep(unsigned seconds);
POSIX

The sleep function returns 0 if the requested time has elapsed or the amount of unslept time if interrupted. The sleep function interacts with SIGALRM, so avoid using them concurrently in the same process.

Example 9.10 beeper.c

The following program beeps every n seconds, where n is passed as a command-line argument.


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int sleeptime;
if (argc != 2) {
fprintf(stderr, ";Usage:%s n\n", argv[0]);
return 1;
}
sleeptime = atoi(argv[1]);
fprintf(stderr, "Sleep time is %d\n", sleeptime);
for ( ; ; ) {
sleep(sleeptime);
printf("\007");
fflush(stdout);
}
}

The nanosleep function causes the calling thread to suspend execution until the time interval specified by rqtp has elapsed or until the thread receives a signal. If nanosleep is interrupted by a signal and rmtp is not NULL, the location pointed to by rmtp contains the time remaining, allowing nanosleep to be restarted. The system clock CLOCK_REALTIME determines the resolution of rqtp.


SYNOPSIS
#include <time.h>
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
POSIX:TMR

If successful, nanosleep returns 0. If unsuccessful, nanosleep returns 1 and sets errno. The following table lists the mandatory errors for nanosleep.

errno

cause

EINTR

nanosleep interrupted by a signal

EINVAL

rqtp specifies a nanosecond value that is not in [0, 109)

The data structures used by nanosleep allow for nanosecond resolution, but the resolution of CLOCK_REALTIME is typically much larger, on the order of 10 ms. The nanosleep function is meant to replace usleep, which is now considered obsolete. The main advantage of nanosleep over usleep is that nanosleep, unlike sleep or usleep, does not affect the use of any signals, including SIGALRM.

Program 9.6 tests the resolution of the nanosleep function. It executes 100 calls to nanosleep with a sleep time of 1000 nanoseconds. If nanosleep had a true resolution of 1 ns, this would complete in 100 msec. The program takes about one second to complete on a system with a 10 ms resolution.


    / 276