c/c++中stdlib.h头文件下的库函数(转载)

来源:互联网 发布:淘宝联盟旧版本5.2苹果 编辑:程序博客网 时间:2024/05/20 09:24

===================================================
atof()将字符串转换成浮点数的函数
原形:double atof(const char *s)
功能:把s所指向的字符串转换成double类型。
   s格式为:符号 数字.数字 E符号 数字
返回值:字符串的转换值。
头文件:math.h、stdlib.h

atoi()将字符串转换成整型数的函数
原形:int atoi(const char *s)
功能:把s所指向的字符串转换成int类型。
   s格式为:符号 数字
返回值:字符串的转换值。若出错则返回0。
头文件:stdlib.h

atol()将字符串转换成长整型数的函数
原形:long atol(const char *s)
功能:把s所指向的字符串转换成long int类型。
   s格式为:符号 数字
返回值:字符串的转换值。若出错则返回0。
头文件:stdlib.h
===================================================
calloc()分配内存函数
原形:void *calloc(size_t nitems,size_t size)
功能:在堆中分配一块内存,将该内存的内容全部清0。
返回值:返回指向新分配内存的指针。空间不够则返回NULL。
头文件:stdlib.h、calloc.h
===================================================
free()释放已分配内存的函数
原形:void free(void *block)
功能:释放由calloc、malloc、realloc函数调用所分配的内存。
头文件:stdlib.h、alloc.h
====================================================
gcvt()把双精度数转化成字符串的函数
原形∶char*gcvt(value,ndigit,buf)
  其中∶double value是待转化数
     int ndigit是转化长度
     char *buf保存转化后的地址
头文件∶stdlib.h
====================================================
geninterrupt()执行中断函数
原形:void geninterrupt(int n)
调用方式:geninterrupt(软中断号)
功能:产生一个8086软中断
注:需先向寄存器传递入口信息(用伪变量)
  例如  伪变量=要赋的值(入口信息);
      调用geninterrupt()函数后得接受出口信息
  例如  变量名=伪变量
伪变量∶Turbo C 允许使用伪变量直接访问相应的8086寄存器。伪变量的类型有两种。
  ① unsigned int : _AX、 _BX、 _CX、 _DX、 _CS、 _DS、 _SS、 _ES、_SP、 _BP、 _DI、 _SI
  ② unsigned char: _AL、 _AH、 _BL、 _BH、 _CL、 _CH、 _DL、 _DH
===================================================
getenv()读取环境变量的当前值的函数
原形:char *getenv(const char *name)
用法:s=getenv("环境变量名");
   需先定义char *s;
功能:返回一给定的环境变量值,环境变量名可大写或小写。如果指定的变量在环境中未定义,则返回一空串。
头文件:stdlib.h
====================================================
itoa()把整形数转换为字符串的函数
原形:char *itoa(int value,char *string,int radix)
功能:把value的值转换为以NULL结束的字符串,并把结果存在string中。radix是转换的基数值,在2到36之间。分配给string的空间必须可容纳返回的所有字节(最多17字节)。
返回值:指向string的指针
头文件:stdlib.h
==================================================
_lrotl()将一个无符号长整形数左循环移位的函数
原形:unsigned long _lrotl(unsigned long value,int count)
功能:将value向左循环移动count位。
返回值:将value向左循环移动count位后的值。
头文件:stdlib.h

_lrotr()将一个无符号长整形数右循环移位的函数
原形:unsigned long _lrotr(unsigned long value,int count)
功能:将value向右循环移动count位。
返回值:将value向右循环移动count位后的值。
头文件:stdlib.h

 

ltoa()把长整形数转换为字符串的函数
原形:char *ltoa(long value,char *string,int radix)
功能:把value的值转换为以NULL结束的字符串,并把结果存在string中。radix是转换的基数值,在2到36之间。分配给string的空间必须可容纳返回的所有字节(最多33字节)。
返回值:指向string的指针
头文件:stdlib.h

malloc()分配内存函数
原形:void *malloc(size_t size)
功能:从堆中分配大小为size字节的块。win32也适用。
返回值:返回新分配内存的地址,若无足够内存,返回NULL。
头文件:alloc.h、stdlib.h
============================================================
putenv()将字符串放入当前环境中的函数
原形:int putenv(const char *name)
用法例:putenv("PATH=C:/B/TC");
功能:把字符串name加到当前程序运行的环境中。当程序结束后,原环境将得到恢复。
返回值:0(成功);-1(失败)
头文件:stdlib.h
============================================================
realloc()重新分配内存函数
原形:void *realloc(void *block,size_t size)
   block指向用malloc、calloc或realloc已得到的内存。
   size是重新分配的字节。
返回值:重分配的块地址。若不能重分配,则返回NULL。
头文件:stdlib.h
=============================================================
_rotl()将一个无符号整形数左循环移位的函数
原形:unsigned _rotl(unsigned value,int count)
功能:将value向左循环移动count位。
返回值:将value向左循环移动count位后的值。
头文件:stdlib.h
=============================================================
_rotr()将一个无符号整形数右循环移位的函数
原形:unsigned _rotr(unsigned value,int count)
功能:将value向右循环移动count位。
返回值:将value向右循环移动count位后的值。
头文件:stdlib.h
=============================================================

 

 


《多线程编程--对pthread_cond_wait()函数的理解》

/************pthread_cond_wait()的使用方法**********/
    pthread_mutex_lock(&qlock);  
    pthread_cond_wait(&qready, &qlock);
    pthread_mutex_unlock(&qlock);
/*****************************************************/
The mutex passed to pthread_cond_wait protects the condition.The caller
passes it locked to the function, which then atomically places the
calling thread on the list of threads waiting for the condition and
unlocks the mutex. This closes the window between the time that the
condition is checked and the time that the thread goes to sleep waiting
for the condition to change, so that the thread doesn't miss a change
in the condition. When pthread_cond_wait returns, the mutex is again
locked.
上面是APUE的原话,就是说pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t
*mutex)函数传入的参数mutex用于保护条件,因为我们在调用pthread_cond_wait时,如果条件不成立我们就进入阻塞,但是进入阻
塞这个期间,如果条件变量改变了的话,那我们就漏掉了这个条件。因为这个线程还没有放到等待队列上,所以调用pthread_cond_wait前要先锁
互斥量,即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁,使得
其它线程可以获得加锁的权利。这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程。当pthread_cond_wait返回的时候又自动给mutex加锁。
实际上边代码的加解锁过程如下:
/************pthread_cond_wait()的使用方法**********/
pthread_mutex_lock(&qlock);    /*lock*/
pthread_cond_wait(&qready, &qlock); /*block-->unlock-->wait() return-->lock*/
pthread_mutex_unlock(&qlock); /*unlock*/
/*****************************************************/

 

 

#define _GNU_SOURCE
#include <unistd.h>
#include <pthread.h>

#include <stdlib.h>
#include <stdio.h>

//static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
// 这里虽然是 P/V ,但是用 cond 确实更方便。
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int i = 0;

void get ()
{
  pthread_mutex_lock (&mutex);
  while ( i == 0 ) // 队列下限
    pthread_cond_wait (&cond, &mutex); // 唤醒其它线程进行检测。

  --i;
  pthread_cond_signal (&cond);
  printf ("Current size: %d/n", i);

  pthread_mutex_unlock (&mutex);
}

void put ()
{
  pthread_mutex_lock (&mutex);
  while ( i == 3 ) // 队列上限
    pthread_cond_wait (&cond, &mutex);

  ++i;
  pthread_cond_signal (&cond); // 唤醒其它线程
  printf ("Now size: %d/n", i);

  pthread_mutex_unlock (&mutex);
}

void *thf ( void *arg )
{
  while ( 1 )
    {
      put ();
    }
}

int main ()
{
  pthread_t tid;
  pthread_create (&tid, NULL, thf, NULL);

  sleep (3);
  while ( 1 )
    get ();
}

 

原创粉丝点击