C语言线程调度

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<semaphore.h>//信号量头文件
#define ARRAY_SIZE 1024
sem_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);
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值