Linux程序设计作业13

一、作业要求

1.给定如下程序代码,要求(1)对程序功能进行说明(2)给出运行结果

	#include<stdio.h>
    
    #include<string.h>
    
    #include<stdlib.h>
    
    #include<pthread.h>
    
    #include<unistd.h>
    
     
    
    pthread_t ntid;
    
    void printids(const char *s)
    
    {
    
     pid_t pid;
    
     pthread_t tid;
    
     pid=getpid();
    
     tid=pthread_self();
    
     printf("%s pid=%u tid=%u (0x%x)\n", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
    
     
    
    }
    
    void *thread_fun(void *arg)
    
    {
    
     printids(arg);
    
     return NULL;
    
     
    
    }
    
    int main()
    
    {
    
     int err;
    
     err=pthread_create(&ntid, NULL, thread_fun, "new thread");
    
     if(err!=0)
    
     {
    
      printf("pthread_create error\n");
    
      exit(1);
    
     }
    
     printids("main thread");
    
     sleep(2);
    
     return 0;
    
    }
    

2.给定如下程序代码, 程序中创建两个线程,counter的初值为0,每个线程均对counter执行10次加一操作,试图在最后counter应该等于20,请问如下代码是否能实现该功能? 若不能,需要对程序做何种修改?

   #include<stdio.h>
    
    #include<stdlib.h>
    
    #include<pthread.h>
    
     
    
    #define nLOOP 10
    
    int counter=0;
    
     
    
    void *pthread(void *vptr)
    
    {
    
     int i, val;
    
     for(i=0;i<nLOOP;i++)
    
     {
    
      counter=counter+1;
    
      sleep(vptr);
    
      printf("%x: %d\n", (unsigned int)pthread_self(), counter);
    
      }
    
      return NULL;
    
    }
    
     
    
    int main()
    
    {
    
     pthread_t tid1, tid2;
    
     pthread_create(&tid1, NULL, pthread, (void *)1);
    
     pthread_create(&tid2, NULL, pthread, (void *)2);
    
     pthread_join(tid1, NULL);
    
     pthread_join(tid1, NULL);
    
    }
    


二、作业答案

  • 程序功能说明

    在主函数所在的进程中,新建了一个线程,主线程和新创建的线程依次执行printids函数,分别打印出它们的所在的进程id和线程id。在这里,我们需要处理主线程和新线程之间的竞争,主线程需要休眠,如果主线程不休眠,他就可能退出,这样新线程还没有机会运行,整个进程可能就已经终止了。这种行为特征依赖于操作系统中的线程实现和调度算法。

  • 运行结果(可以看到进程id都是相同,但线程id不同,但是实际上他们的线程id是连续的内存单元)
    在这里插入图片描述

  • 不能实现该功能,未修改前程序运行如下(主要原因是thread1运行每加一休眠1s,thread2则每加一需要休眠2s,这样明显thread1更早结束,在主线程中因为两个pthread_join()函数的参数thread都是tid1,那么这样的的话主线程会结束,进程结束,thread2没机会运行。)
    在这里插入图片描述

  • 只需要把最后一个pthread_join()函数中的thread参数改为tid2即可,使得thread2运行完毕。

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

运行结果
在这里插入图片描述

抱歉是老师给的题目错了,那个就是tid2,下面写下正确的答案

加锁,可以加下验证啥的
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

#define nLOOP 10
int counter=0;

pthread_mutex_t mutex;

void *pthread(void *vptr)
{
  int i, val;
  for(i=0;i<nLOOP;i++)
  {  pthread_mutex_lock(&mutex);
    counter=counter+1;
    sleep(vptr);
    printf("%x: %d\n", (unsigned int)pthread_self(), counter);
	pthread_mutex_unlock(&mutex);
   }
   return NULL;
}

int main()
{
  pthread_t tid1, tid2;
  pthread_mutex_init(&mutex,NULL);
  pthread_create(&tid1, NULL, pthread, (void *)1);
  pthread_create(&tid2, NULL, pthread, (void *)2);
  pthread_join(tid1, NULL);
  pthread_join(tid2, NULL);
  pthread_mutex_destroy(&mutex);
}

运行结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值