C语言线程调度

来源:互联网 发布:用友t3软件多少钱 编辑:程序博客网 时间:2024/06/07 19:36
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<semaphore.h>//信号量头文件#define ARRAY_SIZE 1024sem_t bin;//信号量char values[ARRAY_SIZE];void *func(void * message){   sem_wait(&bin);//当信号量为0时等待,直到信号量大于1   while(strncmp("end",values,3))   {   printf("your input str num is %d\n",strlen(values)-1);   sem_wait(&bin);   }   pthread_exit(NULL);//线程结束}int main(){    pthread_t thread;    int res;    void * result;    res=pthread_create(&thread,0,func,NULL);//创建一个线程,特性为默认特性,如果返回值不为0则出错。    if(res)    {       perror("thred init error !\n");       exit(EXIT_FAILURE);    }    printf("input your str,end by end\n");    while(strncmp(values,"end",3))    {      fgets(values,ARRAY_SIZE,stdin);      sem_post(&bin);//信号量增加1    }    printf("wait thread to end.............\n");    if(pthread_join(thread,&result))//等待线程结束,如果返回值不为0则出错    {       perror("phtread join error !\n");       exit(EXIT_FAILURE);    }    printf("join ok\n");    sem_destroy(&bin);//销毁线程    exit(EXIT_SUCCESS);}


原创粉丝点击