sem_wait

来源:互联网 发布:caffe axis 编辑:程序博客网 时间:2024/06/05 09:41

注:转载自百度百科 http://baike.baidu.com/link?url=W8FI5e9aUJGkChgCuFwZyxFe6F97G_Z7fleMfS45Mqu4tqvD6TpJr7o7i_q-sjS410LjuT4_5y3TvtHCkgC5Vq

头文件

#include <semaphore.h>

函数原型

int sem_wait(sem_t * sem);

函数说明

sem_wait函数也是一个原子操作,它的作用是从信号量的值减去一个“1”,但它永远会先等待该信号量为一个非零值才开始做减法。也就是说,如果你对一个值为2的信号量调用sem_wait(),线程将会继续执行,这信号量的值将减到1。如果对一个值为0的信号量调用sem_wait(),这个函数就 会地等待直到有其它线程增加了这个值使它不再是0为止。如果有两个线程都在sem_wait()中等待同一个信号量变成非零值,那么当它被第三个线程增加 一个“1”时,等待线程中只有一个能够对信号量做减法并继续执行,另一个还将处于等待状态。sem_trywait(sem_t *sem)是函数sem_wait的非阻塞版,它直接将信号量sem减1,同时返回错误代码。

描述

sem_wait() 减小(锁定)由sem指定的信号量的值.如果信号量的值比0大,
  那么进行减一的操作,函数立即返回.
  如果信号量当前为0值,那么调用就会一直阻塞直到或者是信号量变得可以进行减一的操作
  (例如,信号量的值比0大),或者是信号处理程序中断调用
sem_trywait() 和 sem_wait()是一样的,除了如果不能够对信号量立即进行减一,
  那么sem_trywait()就会返回一个错误(错误号是AGAIN)而不是锁定.
  sem_timedwait() 和 sem_wait()是一样的,除了如果减一操作不能立即执行的话,
  abs_timeout 指定了调用应该被阻塞的时间限制.
  abs_timeout 参数指向了一个结构体指定了由秒和纳秒组成的绝对的超时值:
  从1970-01-01 00:00:00 +0000纪元开始的UTC,结构体的定义如下:
  struct timespec
  {
  time_t tv_sec; /* Seconds */
  long tv_nsec; /* Nanoseconds [0 .. 999999999] */
  };
  如果超时值已经超过了调用规定的值,那么信号量不能被立即锁定,
  之后sem_timedwait() 为超时失败(error设置为ETIMEDOUT).
如果操作立即生效,那么sem_timedwait() 永远不会返回超时的错误,不管abs_timeout的值.
  更进一步的是,在这种情况下abs_timeout值的有效性都不会检查.

返回值

所有的函数成功返回0,错误的话信号量的值不改动,返回-1.errno设定来标识错误.

错误

EINTR The call was interrupted by a signal handler; see signal(7).
  //调用被信号处理中断
EINVAL sem is not a valid semaphore.
  //sem不是有效的信号量
The following additional error can occur for sem_trywait():
  //下面的错误是sem_trywait()可能发生的:
EAGAIN The operation could not be performed without blocking (i.e., the
  semaphore currently has the value zero).
  //除了锁定无法进行别的操作(如信号量当前是0值).
The following additional errors can occur for sem_timedwait():
  //下面的错误是sem_timedwait()可能发生的:
EINVAL The value of abs_timeout.tv_nsecs is less than 0, or greater than or
  equal to 1000 million.
  //abs_timeout.tv_nsecs 的值比0小或者大于等于1000毫秒(译者注:纳秒的值不能比0小,不能比1秒大)
ETIMEDOUT
  The call timed out before the semaphore could be locked.
  //在信号量锁定之前就超时了

注意

对这些函数,信号处理程序总是会中断阻塞,不管是否使用了sigaction(2)的SA_RESTART标志位.

范例

//(有些琐碎的)程序以下展示了在一个未命名的信号量上的操作.程序请求2个命令行参数,
  //第一个参数指定一个秒的参数来作为报警的定时器来产生SIGALRM信号.
  //信号处理程序执行sem_post(3)来增加在main()函数中使用sem_wait()等待的信号量的值.
  //第二个命令行参数指定超时的长度,为sem_timedwait()使用秒为单位.
  //以下展示了程序的执行的不同效果.
$ ./a.out 2 3
  About to call sem_timedwait()
  sem_post() from handler
  sem_getvalue() from handler; value = 1
  sem_timedwait() succeeded
  $ ./a.out 2 1
  About to call sem_timedwait()
  sem_timedwait() timed out
  Program source
  #include <unistd.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <semaphore.h>
  #include <time.h>
  #include <assert.h>
  #include <errno.h>
  #include <signal.h>
  sem_t sem;
  #define handle_error(msg) /
  do
{
perror(msg);
exit(EXIT_FAILURE);
} while (0)
  static void
  handler(int sig)
  {
  write(STDOUT_FILENO, "sem_post() from handler/n", 24);
  if (sem_post(&sem) == -1)
  {
  write(STDERR_FILENO, "sem_post() failed/n", 18);
  _exit(EXIT_FAILURE);
  }
  }
  int
  main(int argc, char *argv[])
  {
  struct sigaction sa;
  struct timespec ts;
  int s;
  if (argc != 3)
  {
  fprintf(stderr, "Usage: %s /n",
  argv[0]);
  exit(EXIT_FAILURE);
  }
  if (sem_init(&sem, 0, 0) == -1)
  handle_error("sem_init");
  /* Establish SIGALRM handler; set alarm timer using argv[1] */
  sa.sa_handler = handler;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction(SIGALRM, &sa, NULL) == -1)
  handle_error("sigaction");
  alarm(atoi(argv[1]));
  /* Calculate relative interval as current time plus
  number of seconds given argv[2] */
  if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
  handle_error("clock_gettime");
  ts.tv_sec += atoi(argv[2]);
  printf("main() about to call sem_timedwait()/n");
  while ((s = sem_timedwait(&sem, &ts)) == -1 && errno == EINTR)
  continue; /* Restart if interrupted by handler */
  /* Check what happened */
  if (s == -1)
  {
  if (errno == ETIMEDOUT)
  printf("sem_timedwait() timed out/n");
  else
  perror("sem_timedwait");
  }
  else
  printf("sem_timedwait() succeeded/n");
  exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
  }
原创粉丝点击