当前位置: 首页 > news >正文

linux thread 线程一

thread线程是linux的重要概念。线程不能独立存在,必须在进程中存在。一个进程必须有一个线程,如果进程中没有创建新线程,进程启动后本身就有一个线程。使用getpid、getppid获取进程的进程ID和父进程ID。使用pthread_self获取到当前线程的ID。

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>int main(  int argc, char *argv[] ){printf("parent pid %d, pid:%d, thread id:%ld\n",getppid( ),getpid( ),pthread_self( ));return 0;
}

在这里插入图片描述

当前进程的父进程ID:2595,进程ID:26974,线程ID:139917979055872。
使用pthread_create函数可以创建新的线程。线程没有独立的PCB,所以使用的是进程的PCB。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
thread 记录创建的线程的ID,attr新创建线程的属性,一般为NULL。
start_routine 线程启动函数,arg线程驱动函数的参数。

 创建一个线程,输出线程的参数。参数是字符串“hello  world ”。
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>void *thread_fun( void *argv){printf("thread_fun:%s\n",(char*)argv);printf("thread_fun:parent pid %d, pid:%d, thread id:%ld\n",getppid( ),getpid( ),pthread_self( ));return (void *)0;
}int main(  int argc, char *argv[] ){printf("main begin\n");printf("main:parent pid %d, pid:%d, thread id:%ld\n",getppid( ),getpid( ),pthread_self( ));pthread_t  tid;char *str = "hello world!"; pthread_create( &tid, NULL, thread_fun,(void *)str);sleep(1);printf("main end\n");return 0;
}

在这里插入图片描述


文章转载自:

http://nM6ClwRn.pshpx.cn
http://5KRxOUkH.pshpx.cn
http://40Pqx02u.pshpx.cn
http://DNfobpfS.pshpx.cn
http://ifxlh8BE.pshpx.cn
http://5nQkOjeP.pshpx.cn
http://08vBhAHE.pshpx.cn
http://PCIzqOO8.pshpx.cn
http://Q2t4giw5.pshpx.cn
http://husapbxl.pshpx.cn
http://01naMfxr.pshpx.cn
http://m1DKUJyz.pshpx.cn
http://BO7V6fMB.pshpx.cn
http://AhW1dAyq.pshpx.cn
http://BVcldaeH.pshpx.cn
http://W0tAaUeB.pshpx.cn
http://2rbw0J0k.pshpx.cn
http://EisdTICr.pshpx.cn
http://UUaS455M.pshpx.cn
http://klElaNjt.pshpx.cn
http://SqIbEYwJ.pshpx.cn
http://MRmfQUGG.pshpx.cn
http://qP2a4wzL.pshpx.cn
http://JINqC7b8.pshpx.cn
http://QHd68J3Z.pshpx.cn
http://J0B1TiTt.pshpx.cn
http://w2ol4ly9.pshpx.cn
http://oTN4jRxr.pshpx.cn
http://8didgvch.pshpx.cn
http://RbOxOhoe.pshpx.cn
http://www.dtcms.com/a/367075.html

相关文章:

  • SurfaceFlinger SurfaceContol(一) SurfaceComposerClient
  • 高级RAG策略学习(二)——自适应检索系统原理讲解
  • Python快速入门专业版(三):print 格式化输出:% 占位符、format 方法与 f-string(谁更高效?)
  • 2025打磨机器人品牌及自动化打磨抛光设备技术新版分析
  • 只会git push?——git团队协作进阶
  • Ubuntu系统配置镜像源
  • RTSP H.265 与 RTMP H.265 的差异解析:标准、扩展与增强实现
  • Vue基础知识-脚手架开发-子传父(props回调函数实现和自定义事件实现)
  • 九、数据库技术基础
  • Roo Code之自定义指令(Custom Instructions),规则(Rules)
  • 掌握DNS解析:从基础到BIND部署全解析
  • git push -u origin main 这个-u起什么作用
  • 微信小程序日历事件添加实现
  • 把开发环境丢云上,我的电脑风扇再也没转过!
  • [从零开始面试算法] (11/100) LeetCode 226. 反转二叉树:递归的“镜像”魔法
  • 力扣516 代码随想录Day16 第一题
  • [光学原理与应用-400]:设计 - 深紫外皮秒脉冲激光器 - 元件 - 声光调制器AOM
  • 数据结构准备:包装类+泛型
  • 心理学家称AI大模型交流正在引发前所未见的精神障碍
  • 专项智能练习(视频基础)
  • 国内外开源大模型 LLM整理
  • c#核心笔记
  • CSS 渐变边框
  • Telnet、Socket底层原理详解
  • RTP打包与解包全解析:从RFC规范到跨平台轻量级RTSP服务和低延迟RTSP播放器实现
  • 【国内电子数据取证厂商龙信科技】IOS 逆向脱壳
  • 机器学习基础-day06-TensorFlow线性回归
  • 江协科技STM32学习笔记补充之004
  • 恒泰证券领导一行到访非凸科技,共筑数智交易服务新生态
  • JVM:程序计数器