操作系统作业:关于LINUX里的 pthread 源程序

利用POSIX thread (pthread), 编一个message alarm程序。

通过stdin,以行为单位输入两个field,前者是数字(时间),后者是message.

实现流程是,经过第一个field时间后stdout第二个field信息。

处理流程不能间断,要连续进行。

#include <stdio.h>

#include <string.h>

#include <pthread.h>

 

#define MaxBuffSize 50

 

struct t_arg {

           unsigned int time;

           char msg[MaxBuffSize];

};

 

// Thread function

void *msgServiceRoutine(void *arg)

{

           struct t_arg tMsg;

          

           tMsg.time = ((struct t_arg*)arg)->time;

           strcpy(tMsg.msg, ((struct t_arg*)arg)->msg);

 

           sleep(tMsg.time);

           printf("MSG = '%s'/n", tMsg.msg);

}

 

void main()

{

           pthread_t thread_t;

           struct t_arg tMsg;

          

           for(;;) {

                     scanf("%d %s",&tMsg.time, tMsg.msg);

                     pthread_create(&thread_t, NULL, msgServiceRoutine,&tMsg);

           }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值