c语言进程创建撤销唤醒阻塞,怎么用c语言编程 实现创建原语、撤销原语、阻塞原语和唤醒原语...

#include sem_t empty; //定义信号量

sem_t applefull;

sem_t orangefull;

void *procf(void *arg) //father线程

{

while(1){

sem_wait(&empty); //P操作

printf("%s\n",(char *)arg);

sem_post(&applefull); //V操作

sleep(7);

}

}

void *procm(void *arg) //mother线程

{

while(1){

sem_wait(&empty);

printf("%s\n",(char *)arg);

sem_post(&orangefull);

sleep(3);

}

}

void *procs(void *arg) //son线程

{

while(1){

sem_wait(&orangefull);

printf("%s\n",(char *)arg);

sem_post(&empty);

sleep(2);

}

}

void *procd(void *arg) //daughter线程

{

while(1){

sem_wait(&applefull);

printf("%s\n",(char *)arg);

sem_post(&empty);

sleep(5);

}

}

main()

{

pthread_t father; //定义线程

pthread_t mother;

pthread_t son;

pthread_t daughter;

sem_init(&empty, 0, 1); //信号量初始化

sem_init(&applefull, 0, 0);

sem_init(&orangefull, 0, 0);

pthread_create(&father,NULL,procf,"father input an apple."); //创建线程

pthread_create(&mother,NULL,procm,"mother input an orange.");

pthread_create(&daughter,NULL,procd,"daughter get an apple.");

pthread_create(&son,NULL,procs,"son get an orange.");

while(1){} //循环等待

}

另外,站长团上有产品团购,便宜有保证

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值