posix thread概述(示例代码)
一个简单的alarm实例
errors.h头文件
#ifndef __ERRORS_H
#define __ERORRS_H #include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h> #ifdef DEBUG
#define DPRINTF(arg) printf arg
#else
#define DPRINTF(arg)
#endif #define err_abort(code, text) do { \
fprintf(stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror(code)); \
abort(); \
} while() #define errno_abort(text) do { \
fprintf(stderr, "%s at \"%s\":%d: %s\n", \
text, __FILE__, __LINE__, strerror(errno));\
abort();\
}while() #endif
errors.h
普通实现:alarm.c
#include"errors.h" /* alarm的普通实现 */
int main(int argc, char* argv[])
{
int seconds;
char line[];
char message[]; while()
{
printf("Alarm> ");
if(fgets(line, sizeof(line), stdin) == NULL ) exit();
if(strlen(line) <= ) continue;
if(sscanf(line, "%d %64[^\n]", &seconds, message) < )
fprintf(stderr, "Bad command\n");
else
{
sleep(seconds);
printf("(%d) %s\n", seconds, message);
} }
}
alarm.c
多进程实现:alarm_fork.c
#include "errors.h"
#include<sys/types.h>
#include<wait.h> /* alarm的多进程实现 */
int main(int argc, char* argv[])
{
int status;
char line[];
int seconds;
pid_t pid;
char message[]; while()
{
printf("Alarm> ");
if(fgets(line, sizeof(line), stdin) == NULL ) exit();
if(strlen(line) <= ) continue;
if(sscanf(line, "%d %64[^\n]",
&seconds, message) < )
{
fprintf(stderr, "Bad command\n");
}
else
{
pid = fork();
if(pid == (pid_t)-)
errno_abort("fork");
if(pid == (pid_t))
{
sleep(seconds);
printf("(%d) %s\n", seconds, message);
exit();
}
else
{
do
{
pid = waitpid((pid_t)-, NULL, WNOHANG);
if(pid == (pid_t)-)
errno_abort("wait child");
}while(pid != (pid_t));
}
}
}
}
alarm_fork.c
多线程实现:alarm_thread.c
#include<pthread.h>
#include "errors.h" /* alarm的多线程实现 */ typedef struct alarm_tag {
int seconds;
char message[];
} alarm_t; void * alarm_thread(void *arg)
{
alarm_t *alarm = (alarm_t*)arg;
int status; status = pthread_detach(pthread_self());
if( status != )
err_abort(status, "pthread_detach");
sleep(alarm->seconds);
printf("(%d) %s\n", alarm->seconds, alarm->message);
free(alarm);
return NULL;
} int main(int argc, char* argv[])
{
int status;
char line[];
alarm_t *alarm;
pthread_t thread; while()
{
printf("Alarm> ");
if(fgets(line, sizeof(line), stdin ) == NULL ) exit();
if(strlen(line) <= ) continue;
alarm = (alarm_t*)malloc(sizeof(alarm_t));
if( alarm == NULL )
errno_abort("malloc");
if( sscanf(line, "%d %64[^\n]",
&alarm->seconds, alarm->message) < )
{
fprintf(stderr, "Bad command\n");
free(alarm);
}
else
{
status = pthread_create(&thread, NULL, alarm_thread, alarm);
if( status != )
err_abort(status, "pthread_creat");
}
}
}
alarm_thread.c
在以上alarm_thread.c代码执行时候,发现如果输入正好是128-1的倍数,将会出现Bad Command的提示。
追究下原因,原来是fgets导致:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or
a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
#include<stdio.h>
#include<unistd.h>
#include<error.h>
#include<stdlib.h> void main(void)
{
char line[];
char* message; while()
{
printf("INPUT> ");
if(fgets(line, sizeof(line), stdin ) == NULL ) exit();
message = (char*)malloc();
if( sscanf(line, "%2[^\n]",
message) < )
{
fprintf(stderr, "sscanf error.\n");
}
else
{
printf("message: %s\n", message);
}
free(message);
}
} /*
*root@jdu-virtual-machine:~# ./a.out
*INPUT> aaa
*message: aa
*sscanf error.
*INPUT> INPUT> ^C
*/
Breakpoint at 0x4007ca: file test.c, line .
(gdb) r
Starting program: /root/a.out
INPUT> aaa Breakpoint , main () at test.c:
message = (char*)malloc();
(gdb) p line
$1 = "aaa"
(gdb) n
if( sscanf(line, "%2[^\n]",
(gdb)
printf("message: %s\n", message);
(gdb) p message
$2 = 0x602010 "aa"
(gdb) n
message: aa
free(message);
(gdb)
}
(gdb)
printf("INPUT> ");
(gdb)
if(fgets(line, sizeof(line), stdin ) == NULL ) exit();
(gdb) Breakpoint , main () at test.c:
message = (char*)malloc();
(gdb) p line
$3 = "\n\000a"
(gdb) n
if( sscanf(line, "%2[^\n]",
(gdb)
fprintf(stderr, "sscanf error.\n");
(gdb)
sscanf error.
free(message);
(gdb)
posix thread概述(示例代码)的更多相关文章
- posix thread概述
1. 基本概念 一个Unix进程可以理解为一个线程加上地址空间.文件描述符和其他数据.异步表明事情相互独立发生, 除非有强加的依赖性. 并发指实际可能是穿行发生的事情好像同时发生一样.并行指并发序列同 ...
- C/C++ 开源库及示例代码
C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- ActiveMQ笔记(1):编译、安装、示例代码
一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...
- redis 学习笔记(2)-client端示例代码
redis提供了几乎所有主流语言的client,java中主要使用二种:Jedis与Redisson 一.Jedis的使用 <dependency> <groupId>redi ...
- posix thread 浅谈
用Posix thread进行多线程设计,就不怕跨平台了,因为很多OS都兼容Posix thread,如Linux/Windows等,甚至嵌入式系统上(如rt-thread)都支持posix thre ...
- python开源项目及示例代码
本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...
- java 添加一个线程、创建响应的用户界面 。 演示示例代码
javajava 添加一个线程.创建响应的用户界面 . 演示示例代码 来自thinking in java 4 21章 部分的代码 夹21.2.11 thinking in java 4免费下载: ...
- python开源项目及示例代码(转)
本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...
随机推荐
- Failed to lunch test error when run with Appium (已解决)
[2015-08-10 15:09:07 - androidtest1] Performing android.test.InstrumentationTestRunner JUnit launch[ ...
- Zabbix探索:使用msmtp进行邮件告警
在Nagios时代就已经使用msmtp发送告警了,不过那时候偷懒,使用mutt发送来简化格式. 在Zabbix时代,更多人使用msmtp,所以官方论坛上有个zext_msmtp.sh的脚本,但是不要以 ...
- [转载]su认证失败
Ubuntu 安装后,root用户默认是被锁定了的,不允许登录,也不允许 "su" 到 root.有人说这是个不好的实践,特别是对于服务器来说.我觉得对于桌面用户来说,这样安全性更 ...
- shell文件/路径处理
#在此加上是不是目录的判断. if [ "`ls -A $dir`" = "" ]; then echo "$dir is empty" e ...
- 将Magento后台汉化的方法
方法一: 打开/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Attribute/Set/Main.php文件, 找到几个用来显示的代码,替换成 ...
- HDU-4678 Mine 博弈SG函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4678 题意就不说了,太长了... 这个应该算简单博弈吧.先求联通分量,把空白区域边上的数字个数全部求出 ...
- Linux江湖01:玩转Linux系统的方法论 (转载)
http://www.blogjava.net/youxia/archive/2015/01/08/linux001.html 2014年上半年,我是在写RCP系列.然后,由于要准备研究生毕业论文和答 ...
- python 定制类
看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方法我们也知道是为了能让cla ...
- mssql函数demo
ALTER FUNCTION [dbo].[f_GetCookType] (@saleDate datetime)RETURNS varchar(6)ASBEGIN declare @cookType ...
- spring占位符解析器---PropertyPlaceholderHelper
一.PropertyPlaceholderHelper 职责 扮演者占位符解析器的角色,专门用来负责解析路劲中or名字中的占位符的字符,并替换上具体的值 二.例子 public class Prope ...