进程、线程和进程间通信(线程的创建和回收)

线程的创建

 #include  <pthread.h>
 int  pthread_create(pthread_t *thread, const
       pthread_attr_t *attr, void *(*routine)(void *), void *arg);

成功返回0,失败时返回错误码
thread 线程对象
attr 线程属性,NULL代表默认属性
routine 线程执行的函数
arg 传递给routine的参数 ,参数是void * ,注意传递参数格式,

编译错误分析:

createP_t.c:14:36: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
     ret = pthread_create(&tid,NULL,testThread,NULL);
                                    ^
In file included from createP_t.c:1:0:
/usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int * (*)(char *)

意义:表示pthread_create参数3的定义和实际代码不符合,期望的是void * (*)(void ) ,实际的代码是int * ()(char )
解决方法:改为pthread_create(&tid,NULL,(void
)testThread,NULL);

createP_t.c:(.text+0x4b):对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status   --------这个链接错误,

表示pthread_create这个函数没有实现
解决方法:编译时候加 -lpthread

注意事项:1. 主进程的退出,它创建的线程也会退出。
线程创建需要时间,如果主进程马上退出,那线程不能得到执行

获取线程的id
通过pthread_create函数的第一个参数;通过在线程里面调用pthread_self函数

线程间参数传递:(重点难点)

//错误打印版本
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void *testThread(void *arg){
    printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
   // return NULL;
    printf("input arg=%d\n",(int)*arg);
    pthread_exit(NULL);
    printf("after pthread exit\n");
}
int main(){
    pthread_t tid;
    int ret;
    int arg = 5;
   
    ret = pthread_create(&tid,NULL,testThread,(void *)&arg);

    printf("This is main thread,tid=%lu\n",tid);    
    sleep(1);
}

编译错误:

createP_t.c:8:34: warning: dereferencing ‘void *’ pointer
     printf("input arg=%d\n",(int)*arg);
                                  ^
createP_t.c:8:5: error: invalid use of void expression
     printf("input arg=%d\n",(int)*arg);

错误原因是void 类型指针不能直接用取值(arg),因为编译不知道数据类型。
解决方法:转换为指定的指针类型后再用
取值 比如:*(int *)arg

  1. 通过地址传递参数,注意类型的转换
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void *testThread(void *arg){
    printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
   // return NULL;
    printf("input arg=%d\n",*(int *)arg);
    pthread_exit(NULL);
    printf("after pthread exit\n");
}
int main(){
    pthread_t tid;
    int ret;
    int arg = 5;
   
    ret = pthread_create(&tid,NULL,testThread,(void *)arg);

    printf("This is main thread,tid=%lu\n",tid);    
    sleep(1);
}

  1. 值传递,这时候编译器会告警,需要程序员自己保证数据长度正确
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void *testThread(void *arg){
    printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
   // return NULL;
    printf("input arg=%d\n",((int)arg);
    pthread_exit(NULL);
    printf("after pthread exit\n");
}
int main(){
    pthread_t tid;
    int ret;
    int arg = 5;
   
    ret = pthread_create(&tid,NULL,testThread,void *)arg);

    printf("This is main thread,tid=%lu\n",tid);    
    sleep(1);
}

运行错误:

//错误版本
#include <stdio.h>
#include <unistd.h>

void *testThread(void *arg){
    printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
   // return NULL;
    printf("This is %d thread.\n", *(int*)arg);
    pthread_exit(NULL);
    printf("after pthread exit\n");
}
int main(){
    pthread_t tid[5];
    int ret;
    int arg = 5;
    int i;
    for(i=0;i<5;i++){   
        ret = pthread_create(&tid[5],NULL,testThread,(void *)&i);
     
//        sleep(1);   
        printf("This is main thread,tid=%lu\n",tid[i]);    
    }

}
*** stack smashing detected ***: ./mthread_t terminated

已放弃 (核心已转储)

原因:栈被破坏了(数组越界)

 for(i=0;i<5;i++){   
        ret = pthread_create(&tid[5],NULL,testThread,(void *)&i);
        这里面数组直接踩到了5,栈并没有这个空间
正确代码
#include <stdio.h>
#include <unistd.h>

void *testThread(void *arg){
    printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
   // return NULL;
    printf("This is %d thread.\n", (int)arg);
   // pthread_exit(NULL);

    printf("after pthread exit\n");
}
int main(){
    pthread_t tid[5];
    int ret;
    int arg = 5;
    int i;
    for(i=0;i<5;i++){   
        ret = pthread_create(&tid[i],NULL,testThread,(void *)i);
     
//        sleep(1);   
        printf("This is main thread,tid=%lu\n",tid[i]);    
    }

}

线程的回收:

指令查看线程:ps -eLf|grep 文件名
查看进程:ps -ef|grep 文件名

使用pthread_join 函数

#include  <pthread.h>
 int  pthread_join(pthread_t thread, void **retval);

注意:pthread_join 是阻塞函数,如果回收的线程没有结束,则一直等待

编译错误:

pjoin.c:13:5: error: unknown type name ‘pthead_tpthead_t tid;

错误类型:未知的类型pthead_t
错误可能:1拼写错误,2对应的头文件没有包含

pjoin.c:18:12: warning: format ‘%s’ expects argument of type ‘char *, but argument 2 has type ‘void *[-Wformat=]
     printf("thread ret=%s\n",retv);

错误类型:参数不匹配,期望的是char * ,但参数retv是void *
解决:在参数前面加强制类型转换(char*)retv

使用线程的分离
两种方式:
1 使用pthread_detach

int pthread_detach(pthread_t thread); 成功:0;失败:错误号
--指定该状态,线程主动与主控线程断开关系。线程结束后(不会产生僵尸线程)
pthread_attr_t attr; /*通过线程属性来设置游离态(分离态)*/
pthread_detach(tid);//tid为线程号
pthread_detach(pthread_self());//pthread_self()为获取本身线程号的函数

2 创建线程时候设置为分离属性

  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

通过内存大小查看线程是否被回收。

//pjoin.c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *func(void *arg){
    printf("This is child thread\n");
    sleep(25);
    pthread_exit("thread return");

}


int main(){
    pthread_t tid[100];
    void *retv;
    int i;
    for(i=0;i<100;i++){
        pthread_create(&tid[i],NULL,func,NULL);
    }
    for(i=0;i<100;i++){
       // pthread_join(tid[i],&retv);
       // printf("thread ret=%s\n",(char*)retv);
    }
    while(1){    
        sleep(1);
    } 

}

再开启一个终端

ps -ef|grep pjoin
top -p 线程号

在这里插入图片描述
25秒前的内存为891656
在这里插入图片描述
25秒后的内存为172512
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
进程线程是操作系统中两种基本的执行单元,它们各自有独特的特点,并用于管理计算机资源的不同方式。 **进程(Process):** 1. **特点:** - **独立性**:每个进程都有自己的地址空间,包括代码、数据和堆栈等,互不影响。 - **并发执行**:多个进程可以同时在处理器上执行,但切换开销较大,因为涉及保存和恢复上下文。 - **资源隔离**:进程之间有明显的边界,相互之间通信通常通过管道、消息队列等方式。 - **创建/销毁**:进程创建和销毁是独立的,资源分配和回收较为独立。 - **系统资源占用大**:进程间通信通常涉及系统调用,效率相对较低。 **线程(Thread):** 1. **特点:** - **共享资源**:线程共享同一进程的地址空间,所以它们可以访问同一块内存,提高了协作效率。 - **轻量级**:相对于进程线程创建和销毁速度更快,切换开销较小。 - **并发性**:一个进程中可以包含多个线程,它们可以在同一时间内执行,提高CPU利用率。 - **同步/互斥**:线程间通信更灵活,使用锁、信号量等机制实现同步。 - **调度灵活**:现代操作系统可以轻松地调度线程,使得高优先级线程优先执行。 **区别:** - **资源归属**:进程有自己的资源,线程共享同一进程的资源。 - **并发粒度**:进程的并发级别较高,线程更细粒度,适合CPU密集型任务。 - **系统开销**:进程创建/销毁开销大,线程小。 - **通信机制**:进程间通信复杂,线程间可以直接访问共享数据。 - **调度控制**:操作系统更容易控制和调度线程,提高全局性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值