Linux线程的创建
一、线程与进程的区别
1、线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源。
2、进程是资源分配的基本单位。所有与该进程有关的资源,都被记录在进程控制块中。以表示该进程拥有这些资源或正在使用它们。
3、进程也是抢占处理机的调度单位,它拥有一个完整的虚拟地址空间。当进程发生调度时,不同的进程拥有不同的虚拟地址空间,而同一进程内的不同线程共享同一地址空间。
二、线程的创建
<span style="font-size:18px;">int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void *),
void *restrict arg
);</span>
第一个参数thread 是指向pthread_t的指针
函数的返回值
三、不带参数的线程创建实例
</pre><pre name="code" class="cpp">#include<stdio.h>
#include<pthread.h>
void *thread1(void)
{
while(1)
{
printf("i am is thread1\n");
sleep(1);
}
}
int main()
{
int ret;
pthread_t id1;
ret = pthread_create(&id1,NULL,(void *)thread1,NULL);
if(ret != 0)
{
printf("thread1 create error quit\n");
exit(1);
}
while(1)
{
printf("i am main pthread\r\n");
sleep(1);
}
return 0;
}
编译和运行结果如下:记得编译的时候加上-lpthread
四、带参数的线程创建实例
<span style="font-size:18px;">#include<stdio.h>
#include<pthread.h>
struct argue
{
int id;
char p;
};
void *thread1(void)
{
printf("i am is thread1\n"); }
void *thread2(void *arg)
{
struct argue *ar=(struct argue *)arg;
printf("i am thread2:arg is:%d %c\n",ar->id,ar->p); }
int main()
{
int ret;
struct argue arg;
printf("iam main threan\n");
pthread_t id1,id2;
ret = pthread_create(&id1,NULL,(void *)thread1,NULL);
if(ret != 0)
{
printf("thread1 create error quit\n");
exit(1);
}
arg.id=1;
arg.p='w';
ret = pthread_create(&id2,NULL,(void *)thread2,(void *)&arg);
if(ret != 0)
{
printf("thread2 create error quit\n");
exit(1);
} sleep(3);
return 0; }</span>
Linux线程的创建的更多相关文章
- linux线程控制-2(线程控制函数)
记录肖堃老师讲解的linux线程 1. 创建线程 int pthread_create( (pthread_t *thread, pthread_attr_t *attr, void *(*start ...
- Linux线程-创建
Linux的线程实现是在内核以外来实现的,内核本身并不提供线程创建.但是内核为提供线程[也就是轻量级进程]提供了两个系统调用__clone()和fork (),这两个系统调用都为准备一些参数,最终都用 ...
- linux内核中创建线程方法
1.头文件 #include <linux/sched.h> //wake_up_process() #include <linux/kthread.h> //kthread_ ...
- Linux多线程编程——线程的创建与退出
POSIX线程标准:该标准定义了创建和操纵线程的一整套API.在类Unix操作系统(Unix.Linux.Mac OS X等)中,都使用Pthreads作为操作系统的线程.Windows操作系统也有其 ...
- 三十六、Linux 线程——线程基本概念及线程的创建和终止
36.1 线程介绍 36.1.1 线程的基本概念 进程是资源管理的最小单位,线程是程序执行的最小单位 每个进程都有自己的数据段.代码段和堆栈段. 线程通常叫做轻型的进程,它包含独立的栈和 CPU 寄存 ...
- linux内核中创建线程方法【转】
本文转载自:https://www.cnblogs.com/Ph-one/p/6077787.html 1.头文件 #include <linux/sched.h> //wake_up_p ...
- Linux 系统编程 学习:09-线程:线程的创建、回收与取消
Linux 系统编程 学习:09-线程:线程的创建.回收与取消 背景 我们在此之前完成了 有关进程的学习.从这一讲开始我们学习线程. 完全的开发可以参考:<多线程编程指南> 在Linux ...
- 关于linux的一点好奇心(五):进程线程的创建
一直以来,进程和线程的区别,这种问题一般会被面试官拿来考考面试者,可见这事就不太简单.简单说一点差异是,进程拥有独立的内存资源信息,而线程则共享父进程的资源信息.也就是说线程不拥有内存资源,所以对系统 ...
- APUE学习之多线程编程(一):线程的创建和销毁
一.线程标识 和每个进程都有一个进程ID一样,每个线程也有一个线程ID,线程ID是以pthread_t数据类型来表示的,在Linux中,用无符号长整型表示pthread_t,Solaris ...
随机推荐
- 2.SQL语言进阶
0.实验数据 表1.course表 表2.student表 表3.sc表 1.SQL连接 内连接 select * from student,sc where student.sno=sc.sno;/ ...
- C++STL笔记
C++STL 1.vector 向量,长度可变的数组 头文件 #include<vector> 1.1vector的定义 vector<typename> name; 例如: ...
- DataAdapter&&DataSet 帮助理解小程序
// 2105/07/08 // DataAdapter&&DataSet using System; using System.Collections.Generic; using ...
- MySQL账户管理
body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...
- Kettle 4.4.2源码分析
一.获取并编译源码 1.1.获取源码 SVN:svn://source.pentaho.org/svnkettleroot/archive/Kettle/branches GitHub:https:/ ...
- 使用LVS+keepalived实现mysql负载均衡的实践和总结
前言 经过一段时间的积累,数据库的架构就需要根据项目不断的进行变化. 从单台数据库,到了两台数据库的主从,再到读写分离,再到双主,现在进一步需要更多的数据库服务器去支撑更加可怕的访问量. 那么经过那么 ...
- 通讯录--(iOS9独有的方法)
导入库文件 #import <ContactsUI/ContactsUI.h> #pragma mark iOS9 新出的点击通讯录的获取信息的办法 #pragma mark - 先弹 ...
- ehcache memcache redis 区别
之前用过redis 和 memcache ,没有ehcache 的开发经验,最近也查阅不少文档和博客,写一些总结,也有不少内容总结与诸多博客中的博主总结: Ehcache EhCache 是一个纯J ...
- matplotlib根据Y轴数量伸缩画图的py脚本
#coding:utf-8import numpy as npimport matplotlib.pyplot as plt #X,Y轴数据y = [20,59,11,12,16,20,15,12,1 ...
- jmeter的http cookies管理器使用
关于Cookie不过多介绍,测试UI的小伙伴们应该对此有深深的爱和恨~ 本文介绍如何:1.获取Cookie.2.保存Cookie 3.引用Cookie 最终达到Cookie类似无法失效的目的~ Coo ...