linux 线程编译指令i,linux线程篇之(一):线程的创建与应用
#include#include
void thread1(void)//线程1
{
int i=0;
for(i=0;i<6;i++)
{
printf("this is a pthread1.\n");
if(i=2)
{
pthread_exit(0);
}
sleep(1);
}
}
void thread2(void)//线程2
{
int i;
for(i=0;i<3;i++)
{
printf("this is a pthread2.\n");
}
pthread_exit(0);
}
int main(void)
{
pthread_t id1,id2;
int i,ret;
ret=pthread_create(&id1,NULL,(void *)thread1,NULL);//创建线程1
if(ret!=0)
{
printf("create pthread error !\n");
exit(1);
}
ret=pthread_create(&id2,NULL,(void *)thread2,NULL);//创建线程2
if(ret!=0)
{
printf("create pthread error !\n");
exit(1);
}
pthread_join(id1,NULL);//等待进程结束
pthread_join(id2,NULL);
exit(0);
}
/*-------------------------------------------------------
1.进程是系统中程序执行和资源分配的基本单位。每个进程都拥有自己的数据段,代码段
和堆栈段,这就造成了进程在进行切换等操作时都需要有比较负责的上下文切换等动作.为
了进一步减少处理机的空转时间支持多个处理器和减少上下文切换开销,进程在演化中出现
了另一个概念--线程。它是一个进程内的基本调度单位,也可以称为轻量级进程。线程是在
供享内存空间中并发的多道执行路径,它们共享一个进程的资源,如文件描述和信号处理。因
此,大大减少了上下文切换的开销。
2.同进程一样,线程也将相关的变量值放在线程控制表内。一个进程可以有多个线程,也就是
有多个线程控制表及堆栈寄存器,但却共享一个用户地址空间。由于线程共享了进程的资源和
地址空间,因此,任何线程对系统资源的操作都会给其他线程带来影响,因此,多线程中的同步
就是非常重要的问题了。
3.线程分类
线程按照其调度者可以分为用户级线程和核心级线程两种。
1)用户级线程
用户级线程主要解决的是上下文切换的问题,它的调度算法和调度过程全部由用户自行选择决定,
在运行时不需要特定的内核支持。在这里,操作系统往往会提供一个用户空间的线程库,该线程
库提供了线程的创建,调度,撤消等功能,而内核依然仅对进程进行管理。
如果一个进程中的某个线程调用了一个阻塞的系统调用,那么该进程包括该进程中的其他所有
线程也同时被阻塞。这种用户级的重要缺点是在一个进程中的多个线程的调度中无法发挥多处理器
的优势。
2)核心级线程
这种线程允许进程中的线程按照同一相对优先调度方法进行调度,这样就可以发挥多处理器的
并发优势。
现在大多数系统都采用用户级线程与核心进程并存的方法。一个用户级线程可以对应一个或几
个核心级线程,也就是"一对一"或"多对一"模型。这样既可满足多处理机系统的需要,也可以最大
限度地减少调度开销。
4.线程的创建和退出
1)函数说明
创建线程实际上就是确定调用该线程函数的入口点,这里通常使用的函数是pthread_creat.
在线程创建以后,就开始运行相关的线程函数,在该函数运行完之后,该线程也就退出了,这也
是线程退出的一种方法。另一种退出线程的方法是使用函数pthread_exit,这是线程的主动
行为。在使用线程函数时,不要随意使用exit退出函数进行出错处理,由于exit的作用是使
调用进程终止,往往一个进程包含多个线程,因此,在使用exit之后,该进程中的所以线程都
终止了。因此,在线程中就可以使用pthread_exit来替代进程中的exit.
2)由于一个进程中的多个线程是共享数据段的,因此通常在现成退出之后,退出线程所占用的
资源并不会随着线程的终止而得到释放。正如进程之间可以用wait()系统调用来同步终止并释放
资源一样,线程之间也有类似机制,那就是 pthread_join()函数。pthread_join()可以用于将
当前线程挂起,等待线程的结束。这个函数是一个线程阻塞的函数,调用它的函数将一直等待
到被等待的线程结束为止,当函数返回时,被等待线程的资源就被收回。
3)pthread_creat函数语法要点
I:所需要的头文件:#includeII:函数原型
int pthread_create((pthread_t *thread,pthread_attr_t *attr,void *(*start_routine)(void *),
void *arg))
III:函数的输入参数
thread:线程标识符
attr:线程属性设置
start_routine:线程函数的起始地址
arg:传递给start_routine的参数
IIII:函数返回值
成功:0
出错:-1
4)pthread_exit函数的语法特点
I:所需要的头文件:#incldueII:函数原型
void pthread_exit(void *retval)
III:函数输入参数
retval:pthread_exit()调用者线程的返回值,可由其他函数如pthread_jion来检索获取
5)pthread_join函数语法特点
I:所需要的头文件:#includeII:函数原型
int pthread_join((pthread_t th,void **thread_return))
III:函数的输入参数
th:等待线程的标示符
thread_return:用户定义的指针,用来存储被等待线程的返回值(不为NULL时)
IIII:函数返回值
成功:0
出错:-1
5.实验:
1)因为pthread的库不是linux系统的库,所以在进行编译的时候要加上
-lpthread
eg:gcc filename -lpthread
2)
[root@localhost the_ninth_step]# gcc thread.c -lpthread -o thread
[root@localhost the_ninth_step]# ./thread
this is a pthread1.
this is a pthread2.
this is a pthread2.
this is a pthread2.
------------------------------------------------------*/
9200-Boot 1.0 (Jun 2 2010 - 08:31:05)
Copy 9200-Uboot.bin from 0x10020000 to 0x23F00000...
Enter 9200-Uboot
Boot Begin from 0x23F00000...
U-Boot 2009.06-rc3 ( 6鏈?13 2010 - 14:10:36)
I2C: ready
DRAM: 64 MB
Flash: 8 MB
In: serial
Out: serial
Err: serial
Hit any key to stop autoboot: 3 2 1 0
## Booting kernel from Legacy Image at 100a0000 ...
Image Name: Linux-2.6.20
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 959080 Bytes = 936.6 kB
Load Address: 20008000
Entry Point: 20008000
Verifying Checksum ... OK
Loading Kernel Image ... OK
OK
Starting kernel ...
Uncompressing Linux............................................................... done, booting the kernel.
Linux version 2.6.20 () (gcc version 4.3.2 (Sourcery G++ Lite 2008q3-72) ) #19 Tue Nov 16 15:42:09 CST 2010
CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=c0007177
Machine: Atmel AT91RM9200-DK
Memory policy: ECC disabled, Data cache writeback
Clocks: CPU 179 MHz, master 59 MHz, main 18.432 MHz
CPU0: D VIVT write-back cache
CPU0: I cache: 16384 bytes, associativity 64, 32 byte lines, 8 sets
CPU0: D cache: 16384 bytes, associativity 64, 32 byte lines, 8 sets
Built 1 zonelists. Total pages: 16256
Kernel command line: root=/dev/ram rw initrd=0x20a00000,2337031 ramdisk_size=5120 init=/linuxrc console=ttys0,115200 mem=64M
AT91: 128 gpio irqs in 4 banks
PID hash table entries: 256 (order: 8, 1024 bytes)
Console: colour dummy device 80x30
Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)
Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)
Memory: 64MB = 64MB total
Memory: 60580KB available (1712K code, 188K data, 92K init)
Mount-cache hash table entries: 512
CPU: Testing write buffer coherency: ok
NET: Registered protocol family 16
Generic PHY: Registered new driver
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 2048 (order: 1, 8192 bytes)
TCP bind hash table entries: 1024 (order: 0, 4096 bytes)
TCP: Hash tables configured (established 2048 bind 1024)
TCP reno registered
checking if image is initramfs...it isn't (no cpio magic); looks like an initrd
Freeing initrd memory: 2282K
NetWinder Floating Point Emulator V0.97 (extended precision)
io scheduler noop registered
io scheduler anticipatory registered (default)
at91_spi: Baud rate set to 5990400
AT91 SPI driver loaded
AT91 Watchdog Timer enabled (5 seconds, nowayout)
atmel_usart.0: ttyS0 at MMIO 0xfefff200 (irq = 1) is a ATMEL_SERIAL
atmel_usart.1: ttyS1 at MMIO 0xfffc4000 (irq = 7) is a ATMEL_SERIAL
RAMDISK driver initialized: 16 RAM disks of 5120K size 1024 blocksize
SMSC LAN83C185: Registered new driver
eth0: Link now 100-FullDuplex
eth0: AT91 ethernet at 0xfefbc000 int=24 100-FullDuplex (52:54:4f:19:f7:72)
eth0: SMSC LAN83C185 PHY
AT91RM9200-NOR:0x00800000 at 0x10000000
NOR flash on AT91RM9200DK: Found 1 x16 devices at 0x0 in 16-bit bank
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
cfi_cmdset_0001: Erase suspend on write enabled
AT91RM9200-NOR:using static partition definition
Creating 7 MTD partitions on "NOR flash on AT91RM9200DK":
0x00000000-0x00020000 : "boot "
0x00020000-0x00060000 : "u-boot"
0x00060000-0x00080000 : "env"
0x00080000-0x000a0000 : "no thing"
0x000a0000-0x002a0000 : "uImage"
0x002a0000-0x007a0000 : "ramdisk"
0x007a0000-0x00800000 : "jffs2"
at91_cf: irqs det #64, io #0
udc: at91_udc version 3 May 2006
mice: PS/2 mouse device common for all mice
at91_rtc at91_rtc: rtc core: registered at91_rtc as rtc0
AT91 Real Time Clock driver.
i2c /dev entries driver
at91_i2c at91_i2c: AT91 i2c bus driver.
TCP cubic registered
NET: Registered protocol family 1
NET: Registered protocol family 17
at91_rtc at91_rtc: setting the system clock to 1998-01-01 00:00:14 (883612814)
RAMDISK: Compressed image found at block 0
VFS: Mounted root (ext2 filesystem).
Freeing init memory: 92K
mknod: /dev/rtc0: File exists
Fri Sep 3 13:18:00 UTC 2010
hello,andy!!!
eth0: Link now 100-FullDuplex
eth0: Link now 100-FullDuplex
eth0: Setting MAC address to 52:54:4f:19:f7:73
eth0: Link now 100-FullDuplex
DEVICE_NAME initialized
the minor is 0
0:set,1:clear,2:quit;2
Processing /etc/profile... Done
# ifcon[5D
# ifconfig [J
eth0 Link encap:Ethernet HWaddr 52:54:4F:19:F7:73
inet addr:192.168.0.247 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:24 Base address:0xc000
#
linux 线程编译指令i,linux线程篇之(一):线程的创建与应用相关推荐
- linux 保存编译log,(转)Linux下编译安装log4cxx
一个项目的服务器端在Linux平台下,用到了开源日志库log4cxx,这个库是apache项目的一个子库.功能很不错.下面记录下它的编译和安装过程. 第一步安装apr-1.3.8,顺序不能错,它必须首 ...
- linux 模块化编译,手把手教Linux驱动1-模块化编程 module
大家好,从本篇起,一口君将手把手教大家如何来学习Linux驱动,预计会有20篇关于驱动初级部分知识点.本专题会一直更新,有任何疑问,可以留言或者加我微信. Linux的开发者,遍布世界各地,他们相互之 ...
- Linux常用基础指令、Linux常用工具(软件包)使用带图详解
目录 目录相关指令: 文件相关指令: 压缩解压缩指令: 匹配查找指令: 权限相关指令: Linux常用工具: 指令使用规则:指令 [该指令的详细操作选项] [操作对象(通常有路径)],后面两可不加. ...
- linux emule 编译 wx-config --libs,linux下编译wxwidgets所写程序所遇到的问题
记录linux下编译wxwidgets所遇到的问题 1./home/hrd/baohualib/wxWidgets-3.0.2/include/wx/platform.h:184:22: 致命错误: ...
- linux make编译卡死,为linux内核编译make文件时出现问题?
我刚刚开始使用Linux内核开发,并且遇到了编译make文件的问题.为linux内核编译make文件时出现问题? 这是hello世界的教程. 我的HELLO-1.C文件 * * hello−1.c − ...
- linux显示界面指令规范,linux的常规操作命令
linux的高版本推出了用户界面,但今天我们还是来学习一下命令. 一. linux的基本命令 1. whoami //查询当前操作用户 who am I //查询当前的登录用户 我是谁 2. pwd ...
- linux下编译安装ntfs,linux下编译安装ntfs
"mount: you must specify the filesystem type" 错误提示是这个 mount /dev/hda1 /media/windows/ 在 ...
- linux 内核编译不能打字,linux系统升级后,手动编译的kernel无法启动问题
linux系统升级后,手动编译的kernel无法启动问题 linux系统升级后,手动编译的kernel无法启动问题 做开发相关,需要编译3.18的kernel,x86_64的,但是我的deepin升级 ...
- linux内核编译最详细,Linux内核编译详细教程,linux内核编译
Linux内核编译详细教程,linux内核编译 尝试编译下Linux-kernel 4.14.14,使用Ubuntu 16.04 64位 系统. kernel-4.14.14 内核文件约96MB,解压 ...
最新文章
- boost::noncopyable介绍
- freemarker程序开发
- php input多选,利用css3更改input单选和多选样式的方法
- [GKCTF 2021]XOR
- 安装kube-prometheus项目:k8s部署prometheus、监控k8s核心组件、添加告警(微信、钉钉、企业微信)、进行数据持久化
- 记一次笑哭的unterminated string literal报错
- 电子信息工程班徽设计_蜻蜓AI说专业:与5G时代息息相关的电子信息工程专业怎么样?...
- 利用c#开发一个telnet unix服务器或者防火墙的小工具(转)
- C# 学生简单管理系统 数据库 1.0版本
- Apache 首位华人董事吴晟谈开源:我对中国开源短期内是消极的
- git 远程代码回滚master
- 容易被PHP程序员忽视的几个要点
- c语言数字代码,小O的数字 (C语言代码)
- Cannot find class [***] for bean with name '***' defined in file[***]
- python把文件中的邮箱分类 保存到相应的文件里面
- Bellman-ford算法图解
- DCDC Buck电源芯片选型
- 通过CocoCut插件和m3u8下载器下载网页视频并转换成mp4格式
- matlab里newc,Matlab超简单教程
- 学习总结《反本能---如何对抗你的习以为常》
热门文章
- ssm基于儿童教育网站答辩PPT模板
- 【答辩问题】计算机专业本科毕业设计答辩的一般程序2
- mysql.8.0.13ZIP安装教程_关于mysql 8.0.13zip包安装方法
- hashmap put过程_HashMap为什么线程不安全?
- c语言do while语句用法6,c语言do while的用法
- CString、string、char*的综合比较
- 如何在10亿个数中找到前1000大的数?
- 洛谷 P1137 旅行计划
- 《LoadRunner性能测试巧匠训练营》——1.4 性能测试分类详解
- Comparable和Comparator