c语言中的函数指令 c语言函数详解( 四 )


printf("in the child!\n");
read(file_pipes_1[0],buffer, BUFSIZ);
printf("in the child, read_result is \"%s\"\n",buffer);
write(file_pipes_2[1],ch2p, sizeof(ch2p));
printf("in the child, write_result is \"%s\"\n",ch2p);
exit(EXIT_SUCCESS);
default://parent
close(file_pipes_1[0]);
close(file_pipes_2[1]);
printf("in the parent!\n");
write(file_pipes_1[1], p2ch, sizeof(p2ch));
printf("in the parent, write_result is \"%s\"\n",p2ch);
read(file_pipes_2[0],buffer, BUFSIZ);
printf("in the parent, read_result is \"%s\"\n",buffer);
exit(EXIT_SUCCESS);
}
}
}
}
#ifndef DBG
#define DBG
#endif
#undef DBG
#ifdef DBG
#define PRINTF(fmt, args...) printf("file-%s line-%d: " \
fmt, __FILE__, __LINE__, ##args)
#else
#define PRINTF(fmt, args...) do{}while(0);
#endif
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;
}
C语言中函数返回的命令是Gotoandplay不是 。c语言退出整个程序或函数的命令是return、goto、break、break 。其中return表示从被调用函数返回主调函数继续执行,返回时可附带一个返回值,所以Gotoandplay并不是返回命令 。
【c语言中的函数指令 c语言函数详解】关于c语言中的函数指令和c语言函数详解的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读