12月9号作业

本文通过实验展示了在多线程环境下,不同作用域变量的访问情况,包括全局变量、局部变量的可见性和修改效果,并提供了实现代码。

题目:

任务1:定义一个全局变量 int a=10,主线程能否访问到,分支线程能否访问到;     
任务2:分支线程中修改上述的a = 20, 问主线程中访问该a,是10还是20;
任务3:在主线程定义一个局部变量int b=1,分支线程能否访问到b;
任务4:在分支线程定义一个局部变量int c=2,主线程能否访问到c;
任务5:如果任务34不能访问到,则如何修改代码让对方能够访问到;

代码:

任务一代码:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

int a=10;

void *pthread_1(void *p)
{
    printf("分支线程%d\n",a);
}   

int main(int argc, const char *argv[])
{
    pthread_t b;
    if(pthread_create(&b,NULL,pthread_1,NULL)!=0)
    {
        perror("分支创建失败");
        return -1;
    }
    printf("主线程%d\n",a);
    while(1)
        sleep(1);
    return 0;
}                                                                                                     
                                                                                                      
                                                                                                      
                                                                                                      

运行结果:

ubuntu@ubuntu:12.9$ ./a.out
主线程10
分支线程10

 

任务二代码:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

int a=10;

void *pthread_1(void *p)
{
    a=20;
    printf("分支线程%d\n",a);
}

int main(int argc, const char *argv[])
{
    pthread_t b;
    if(pthread_create(&b,NULL,pthread_1,NULL)!=0)
    {   
        perror("分支创建失败");
        return -1;
    }
    sleep(1);
    printf("主线程%d\n",a);
    while(1)
        sleep(1);
    return 0;
}                                                                             
                                                                              

运行结果:

ubuntu@ubuntu:12.9$ ./a.out
分支线程20
主线程20

任务三代码:

 修改前代码:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

int a=10;

void *pthread_1(void *p)
{
    printf("分支线程%d\n",a_1);
}

int main(int argc, const char *argv[])
{
    pthread_t b;
    int a_1=30;
    if(pthread_create(&b,NULL,pthread_1,NULL)!=0)
    {
        perror("分支创建失败");
        return -1;
    }
    printf("主线程%d\n",a);
    while(1)
        sleep(1);
    return 0;
}                                                                                                   

修改前运行结果:

ubuntu@ubuntu:12.9$ gcc pthread.c -pthread
pthread.c: In function ‘pthread_1’:
pthread.c:9:28: error: ‘a_1’ undeclared (first use in this function)
  printf("分支线程%d\n",a_1);
                            ^~~
pthread.c:9:28: note: each undeclared identifier is reported only once for each function it appears in

 修改后代码:

#include<stdio.h>
  2 #include<pthread.h>
  3 #include<unistd.h>
  4 #include<stdlib.h>
  5 int a=10;
  6 
  7 void *pthread_1(void *p)
  8 {
  9     int *q=(int *)p;
 10     printf("分支线程%d\n",*q);
 11     return NULL;
 12 }
 13                                                                                                                                                     
 14 int main(int argc, const char *argv[])
 15 {   
 16     pthread_t b;
 17     int *p_1=NULL;
 18     int a_1=1;
 19     p_1=&a_1;
 20     if(pthread_create(&b,NULL,pthread_1,p_1)!=0)
 21     {   
 22         perror("分支创建失败");
 23         return -1;
 24     }
 25     //printf("主线程%d\n",a_1);
 26     while(1)
 27         sleep(1);
 28     return 0;
 29 }

修改后运行结果:

ubuntu@ubuntu:12.9$ ./a.out
分支线程1

 

 

 

任务四代码:

修改前代码:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

int a=10;

void *pthread_1(void *p)
{
    int c=2;
    printf("分支线程%d\n",2);
}

int main(int argc, const char *argv[])
{
    pthread_t b;
    if(pthread_create(&b,NULL,pthread_1,NULL)!=0)
    {
        perror("分支创建失败");
        return -1;
    }
    printf("主线程%d\n",c);
    while(1)
        sleep(1);
    return 0;
}                                                                         

 

修改前运行结果:

ubuntu@ubuntu:12.9$ gcc pthread.c -pthread
pthread.c: In function ‘main’:
pthread.c:21:25: error: ‘c’ undeclared (first use in this function)
  printf("主线程%d\n",c);
                         ^
pthread.c:21:25: note: each undeclared identifier is reported only onc
e for each function it appears in

 修改后代码:

#include<stdio.h>
  2 #include<pthread.h>
  3 #include<unistd.h>
  4 #include<stdlib.h>
  5 int a=10;
  6 
  7 void *pthread_1(void *p)
  8 {
  9     int c=2;
 10     p=&c;
 11     //printf("分支线程%d\n",c);
 12     return p;
 13 }
 14 
 15 int main(int argc, const char *argv[])
 16 {
 17     pthread_t b;
 18     int *p_1=NULL;
 19     int *p_2=NULL;
 20     if(pthread_create(&b,NULL,pthread_1,p_1)!=0)
 21     {
 22         perror("分支创建失败");
 23         return -1;
 24     }
 25     p_2=(int *)pthread_1(p_1);
 26     printf("主线程%d\n",*p_2);
 27     while(1)
 28         sleep(1);
 29     return 0;
 30 }                                                           

修改后结果:

ubuntu@ubuntu:12.9$ ./a.out
主线程2

答:

        任务一:都能

        任务二:20

        任务三:不能

        任务四:不能

        任务五 :利用参数列表和指针函数

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值