2022-12-9作业

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


任务1:

代码实现如下:

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

int a = 10;

//线程执行体,该执行体中指定了线程应该做什么任务
void *callBack(void *arg)     //void *arg = NULL;
{
    while(1)
    {
        printf("this is other function\n");
        printf("a = %d\n", a);
        sleep(1);
    }
}

int main(int argc, const char *argv[])
{
    //创建一个分支线程
    pthread_t tid;
    if(pthread_create(&tid, NULL, callBack, NULL) != 0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }

    while(1)
    {
        printf("this is main function\n");
        printf("a = %d\n", a);                         
        sleep(1);
    }
    
    return 0;
}

测试结果如下:

ubuntu@ubuntu:05_2022-12-9$ gcc 01_text.c -pthread
ubuntu@ubuntu:05_2022-12-9$ ./a.out 
this is main function
a = 10
this is other function
a = 10
this is main function
a = 10
this is other function
a = 10
^C
ubuntu@ubuntu:05_2022-12-9$         


任务2:

代码实现如下:

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

int a = 10;

//线程执行体,该执行体中指定了线程应该做什么任务
void *callBack(void *arg)     //void *arg = NULL;
{
    while(1)
    {
        a = 20;       
        printf("this is other function\n");
        printf("a = %d\n", a);
        sleep(1);
    }
}

int main(int argc, const char *argv[])
{
    //创建一个分支线程
    pthread_t tid;
    if(pthread_create(&tid, NULL, callBack, NULL) != 0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }

    while(1)
    {
        printf("this is main function\n");
        printf("a = %d\n", a);                         
        sleep(1);
    }
    
    return 0;
}

测试结果如下:

 ubuntu@ubuntu:05_2022-12-9$ gcc 01_text.c -pthread
ubuntu@ubuntu:05_2022-12-9$ ./a.out 
this is main function
a = 10
this is other function
a = 20
this is main function
a = 20
this is other function
a = 20
^C
ubuntu@ubuntu:05_2022-12-9$ 


任务3:

代码实现如下:

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

//int a = 10;

//线程执行体,该执行体中指定了线程应该做什么任务
void *callBack(void *arg)     //void *arg = NULL;
{
    while(1)
    {
        //a = 20;
        printf("this is other function\n");
        printf("b = %d\n", b);
        sleep(1);
    }
}

int main(int argc, const char *argv[])
{
    int b = 1; 
    //创建一个分支线程
    pthread_t tid;
    if(pthread_create(&tid, NULL, callBack, NULL) != 0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }
                                         
    while(1)
    {
        printf("this is main function\n");
        printf("b = %d\n", b);
        sleep(1);
    }
    
    return 0;
}

 测试结果如下:

ubuntu@ubuntu:05_2022-12-9$ gcc 01_text.c -pthread
01_text.c: In function ‘callBack’:
01_text.c:14:22: error: ‘b’ undeclared (first use in this function)
   printf("b = %d\n", b);
                      ^
01_text.c:14:22: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:05_2022-12-9$ 


任务4:

代码实现如下:

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

//int a = 10;

//线程执行体,该执行体中指定了线程应该做什么任务
void *callBack(void *arg)     //void *arg = NULL;
{   
    int c = 2;                                          
    while(1)
    {
        //a = 20;
        printf("this is other function\n");
        printf("c = %d\n", c);
        sleep(1);
    }
}

int main(int argc, const char *argv[])
{   
    //创建一个分支线程
    pthread_t tid;
    if(pthread_create(&tid, NULL, callBack, NULL) != 0)
    {   
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }
    
    while(1)
    {   
        printf("this is main function\n");
        printf("c = %d\n", c);
        sleep(1);
    }
    
    return 0;
}

 测试结果如下:

ubuntu@ubuntu:05_2022-12-9$ gcc 01_text.c -pthread
01_text.c: In function ‘main’:
01_text.c:33:22: error: ‘c’ undeclared (first use in this function)
   printf("c = %d\n", c);
                      ^
01_text.c:33:22: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:05_2022-12-9$ 


任务5:

(1)在主线程定义一个局部变量int b=1,使分支线程访问到b:

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

//线程执行体,该执行体中指定了线程应该做什么任务
void *callBack(void *arg)     //void *arg = &b;       
{   
    while(1)
    {
        printf("this is other function\n");
        printf("b = %d\n", *(int *)arg);    //将void *arg强转成int *类型,再解引用
        sleep(1);
    }
}

int main(int argc, const char *argv[])
{
    int b = 1;
    //创建一个分支线程
    pthread_t tid;
    if(pthread_create(&tid, NULL, callBack, &b) != 0)  
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }   
    
    while(1)
    {
        printf("this is main function\n");
        printf("b = %d\n", b);
        sleep(1);
    }   
    
    return 0;
}   

 测试结果如下:

ubuntu@ubuntu:05_2022-12-9$ gcc 01_text.c -pthread
ubuntu@ubuntu:05_2022-12-9$ ./a.out 
this is main function
b = 1
this is other function
b = 1
this is main function
b = 1
this is other function
b = 1
^C
ubuntu@ubuntu:05_2022-12-9$ 

(2) 在分支线程定义一个局部变量int c=2,使主线程访问到c;

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>

//线程执行体,该执行体中指定了线程应该做什么任务
void *callBack(void *arg)     //void *arg = &c1;        
{
    int c = 2;
    *(int*)arg = c;

    while(1)
    {
        printf("this is other function: c = %d\n", c);
        sleep(1);
    }
}

int main(int argc, const char *argv[])
{
    int c1 = 0;
    //创建一个分支线程
    pthread_t tid;
    if(pthread_create(&tid, NULL, callBack, &c1) != 0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }
    sleep(1);
    while(1)
    {
        printf("this is main function: c = %d\n", c1);
        sleep(1);
    }
    return 0;
}

测试结果如下:

ubuntu@ubuntu:05_2022-12-9$ gcc 01_text.c -pthread
ubuntu@ubuntu:05_2022-12-9$ ./a.out 
this is main function: c = 2
this is other function: c = 2
this is other function: c = 2
this is main function: c = 2
this is other function: c = 2
this is main function: c = 2
^C
ubuntu@ubuntu:05_2022-12-9$ 


 总结:

1.根据测试结果:定义一个全局变量 int a=10,主线程能访问到,分支线程也能访问到;     

2.根据测试结果:分支线程中修改上述的a = 20, 问主线程中访问该a=20;

3.根据测试结果:编译不通过,在主线程定义一个局部变量int b=1,分支线程不能访问到b;

4.根据测试结果:编译不通过,在分支线程定义一个局部变量int c=2,主线程不能访问到c;

5.根据测试结果:可以通过修改代码使分支线程访问到在主线程定义的局部变量。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值