flock函数c语言 c语言的clock函数( 三 )


int main(void)
{
PRINTF("%s\n", "hello!");
fprintf(stdout, "hello hust!\n");
return 0;
}
#define N 5
#define MAX 5
int nput = 0;
char buf[MAX][50];
char *buffer = "abcdefghijklmnopqrstuvwxyz0123456789";
char buf_r[100];
sem_t mutex,full,avail;
void *productor(void *arg);
void *consumer(void *arg);
int i = 0;
int main(int argc, char **argv)
{
int cnt = -1;
int ret;
int nput = 0;
pthread_t id_produce[10];
pthread_t id_consume;
ret = sem_init(mutex, 0, 1);
ret = sem_init(avail, 0, N);
ret = sem_init(full, 0, 0);
for(cnt = 0; cnt6; cnt ++ ){
//pthread_create(id_produce[cnt], NULL, (void *)productor, cnt);
pthread_create(id_produce[cnt], NULL, (void *)productor, (void *)cnt);
}
pthread_create(id_consume, NULL, (void *)consumer, NULL);
for(cnt = 0; cnt6; cnt ++){
pthread_join(id_produce[cnt], NULL);
}
pthread_join(id_consume,NULL);
sem_destroy(mutex);
sem_destroy(avail);
sem_destroy(full);
exit(EXIT_SUCCESS);
}
void *productor(void *arg)
{
while(1){
sem_wait(avail);
sem_wait(mutex);
if(nput = MAX * 3){
sem_post(avail);
//sem_post(full);
sem_post(mutex);
return NULL;
}
sscanf(buffer + nput, "%s", buf[nput % MAX]);
//printf("write[%d] \"%s\" to the buffer[%d]\n", (*(int*)arg), buf[nput % MAX],nput % MAX);
printf("write[%d] \"%s\" to the buffer[%d]\n", (int)arg, buf[nput % MAX],nput % MAX);
nput ++;
printf("nput = %d\n", nput);
sem_post(mutex);
sem_post(full);
}
return NULL;
}
void *consumer(void *arg)
{
int nolock = 0;
int ret, nread, i;
for(i = 0;iMAX * 3; i++)
{
sem_wait(full);
sem_wait(mutex);
memset(buf_r, 0, sizeof(buf_r));
strncpy(buf_r, buf[i % MAX], sizeof(buf[i % MAX]));
printf("read \"%s\" from the buffer[%d]\n\n",buf_r, i % MAX);
sem_post(mutex);
sem_post(avail);
//sleep(1);
}
return NULL;
}
谁给我解释下fcntl.h头文件 , 拭目以待!close(关闭文件)
相关函数openflock函数c语言,fcntlflock函数c语言 , shutdownflock函数c语言 , unlink,fclose
表头文件#includeunistd.h
定义函数int close(int fd);
函数说明当使用完文件后若已不再需要则可使用close()关闭该文件 , 二close()会让数据写回磁盘,并释放该文件所占用的资源 。参数fd为先前由open()或creat()所返回的文件描述词 。
返回值若文件顺利关闭则返回0,发生错误时返回-1 。
错误代码EBADF 参数fd 非有效的文件描述词或该文件已关闭 。
附加说明虽然在进程结束时,系统会自动关闭已打开的文件,但仍建议自行关闭文件 , 并确实检查返回值 。
范例参考open()
creat(建立文件)
相关函数read , write , fcntl,close,link,stat,umask,unlink,fopen
表头文件#includesys/types.h
#includesys/stat.h
#includefcntl.h
定义函数int creat(const char * pathname, mode_tmode);
函数说明参数pathname指向欲建立的文件路径字符串 。Creat()相当于使用下列的调用方式调用open()
open(const char * pathname ,(O_CREAT|O_WRONLY|O_TRUNC));
错误代码关于参数mode请参考open()函数 。
返回值creat()会返回新的文件描述词,若有错误发生则会返回-1,并把错误代码设给errno 。
EEXIST 参数pathname所指的文件已存在 。
EACCESS 参数pathname 所指定的文件不符合所要求测试的权限
EROFS 欲打开写入权限的文件存在于只读文件系统内
EFAULT 参数pathname 指针超出可存取的内存空间