Linux Programming
文章平均质量分 66
duyiwuer2009
这个作者很懒,什么都没留下…
展开
-
popen+pclose 执行 shell 命令
在《system() 执行 shell 命令》中,我们介绍了 system 执行 shell 命令的方法,system 返回值比较混乱,难以理解,而且 popen 在处理子进程标准输出上会很方便。注意:管道只能处理标准输出,不能处理标准错误输出。popen 和 pclose 的实现与 system 类似,多了一步创建管道的操作。popen 成功返回 FILE 句柄,失败返回 N原创 2016-02-18 21:53:46 · 10502 阅读 · 0 评论 -
Introduction to Endianness(大端小端介绍)
Introduction to EndiannessMichael Barr12/28/2001 10:35 AM ESTFrom: http://www.eetimes.com/discussion/beginner-s-corner/4023889/Introduction-to-Endianness译文:http://blog.csdn.net/xbt746/arti转载 2012-04-12 20:04:29 · 1068 阅读 · 0 评论 -
fork()引起的printf()输出多次的问题
#include #include #include #include int main(){ pid_t pid; printf("before fork - with line break\n"); printf("before fork - no line break"); //fflush(stdout); if( (pid = fork()) < 0 ) {原创 2012-09-26 12:18:14 · 3136 阅读 · 0 评论 -
改变进程名(change the process name or title)
BSD 中可以用 setproctitle() system call 改变进程名,但是 Linux 没有这样的 system call.当用 ps or top查看时能看到改变之后的进程名。#include #include #include #include #include #include extern char **environ;int main(int原创 2012-12-28 15:30:34 · 4906 阅读 · 0 评论 -
Linux kernel 重要版本发布日期
By September 1991, Linux version 0.01 was released on theFTP server (ftp.funet.fi) of the Finnish University and ResearchNetwork (FUNET). It had 10,239 lines of code. InOctober 1991, Linux version 0原创 2012-12-20 23:58:28 · 1171 阅读 · 0 评论 -
常见协议头结构体定义
环境说明:Intel 小端存储Linux Ubuntu-Server 2.6.38-8-generic-pae #42-Ubuntu SMP Mon Apr 11 05:17:09 UTC 2011 i686 i686 i386 GNU/Linux● Ethernet header#include /* This is a name for the 48 bit et原创 2012-10-21 21:31:57 · 2535 阅读 · 1 评论 -
poll()例程(包含 bind,listen,accept,setsockopt 的用法)
Using poll() instead of select()The poll() API is part of the Single Unix Specification and the UNIX® 95/98 standard. The poll() API performs the same API as the existing select() API. The only原创 2012-10-20 17:12:42 · 2788 阅读 · 0 评论 -
Linux kernel 发布日期列表
LinuxVersionsThis is a list oflinks to every changelog.3.xLinux 3.5 Released 21 Jul, 2012 (62 days)Linux 3.4 Released 20 May, 2012 (63 days)Linux 3.3 Released 18 Mar, 2012 (74 days)Linux转载 2012-10-16 08:56:46 · 2013 阅读 · 0 评论 -
编写守护进程
[1] Clear file creation mask. The file mode creation mask that's inherited could be set to deny certain permissions.If the daemon process is going to create files, it may want to set specific perm原创 2012-09-16 19:31:29 · 912 阅读 · 0 评论 -
调用exec()后进程在何处终止
调用exec()后进程在何处终止#include #include #include #include int main(){ int a = 1; int pid; char buff[] = "before fork"; if(write(1, buff, sizeof(buff) - 1) != (sizeof(buff) - 1)) { perror("wr原创 2012-09-24 10:13:05 · 1436 阅读 · 0 评论 -
获取主机名、IP——gethostent(),gethostbyname(),gethostbyaddr()
1、原理:查询 /etc/hosts 等文件及 DNS or NIS 服务器The domain name queries carried out by gethostbyname() and gethostbyaddr() use a combination of any or all of the name server named(8), a broken out line fro原创 2012-08-13 21:21:20 · 8649 阅读 · 1 评论 -
getaddrinfo(),gethostbyname(),getnetbyaddr(),getservbyname(),getprotobyname()对比总结
1. gethostbyname()原理:查询 /etc/hosts 等文件及 DNS or NIS 服务器The domain name queries carried out by gethostbyname() and gethostbyaddr() use a combination of any or all of the name server named(8), a b原创 2012-09-22 21:55:06 · 2645 阅读 · 0 评论 -
获取网络号、网络名——getnetent(),getnetbyaddr(),getnetbyname()
struct netent *getnetent(void);struct netent *getnetbyname(const char *name);struct netent *getnetbyaddr(uint32_t net, int type);void setnetent(int stayopen);void endnetent(void);/* Description原创 2012-08-15 21:39:45 · 4655 阅读 · 0 评论 -
判断处理器器是大端(Big-Endian)还是小端( Little-Endian)
#include #define BIG_ENDIAN 1#define LITTLE_ENDIAN 0int getEndianness(){ int a = 0x12345678; char *p; p = (char *)(&a); if(0x78 == *p) { return LITTLE_ENDIAN; } return BIG_ENDIAN;}原创 2013-04-24 23:37:04 · 1359 阅读 · 0 评论 -
system() 执行 shell 命令
实现原理strace 跟踪结果:clone(child_stack=0, flags=CLONE_PARENT_SETTID|SIGCHLD, parent_tidptr=0x7fff936fc388) = 15661wait4(15661, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 15661实际的过程:1. 父进程 -原创 2015-12-16 15:11:24 · 6090 阅读 · 0 评论 -
getopt()
#include #include using namespace std;#define OPTSTR "1abc:d:2::"int main(int argc, char *argv[]){ int iRet = getopt(argc, argv, OPTSTR); int count = 0; cout << "optstring: " << O原创 2013-11-28 20:20:32 · 1167 阅读 · 0 评论 -
主机名、服务与地址的映射——getaddrinfo(),getnameinfo(),gai_strerror()
/* Structure to contain information about address of a service provider. */struct addrinfo{ int ai_flags; /* Input flags. */ int ai_family; /* Protocol family for socket. */ int ai_sockt原创 2012-08-16 22:27:11 · 17430 阅读 · 1 评论 -
多线程、多进程写同一日志情况下的日志库中 I/O 的选型
文件描述符与 inode 相关背景知识出自《The Linux Programming Interface》多线程有上面的背景知识可知,多线程情况下写同一文件用的是同一个【文件偏移量】,因此只要单条写日志操作是原子操作,就不会出现日志混乱的情况。系统 I/O系统 I/O write() 不带应用层缓冲(进程级别缓冲),因此只要保证单条日志操作之调用一次 writ原创 2015-01-29 12:13:16 · 3821 阅读 · 0 评论 -
防止全局变量、头文件重复包含与 extern 的使用
C语言中经常会出现文件重复包含而导致全局变量重复定义,下面以一个例子说明-----MakefileCC = gccOBJS = main.o errhandle.o strcopy.oCFLAGS = -Wall -std=c99main: ${OBJS} # or $(OBJS), ${OBJS} is shell style ${CC} ${CFLAGS} -o $@ ${原创 2012-08-17 21:25:57 · 2913 阅读 · 0 评论 -
调用 fork 两次避免僵尸进程
Avoid zombie processes by calling fork twice/* * Avoid zombie processes by calling fork twice. * APUE-2e 程序清单8-5 */#include #include #include #include #include "sig_wait_proc.h"int main(原创 2012-09-05 20:19:38 · 4999 阅读 · 2 评论 -
避免产生僵尸进程的N种方法(zombie process)
注意:1、如果僵尸进程已经产生,当其父进程终止时,僵尸进程还是会消失。避免产生僵尸进程的5种方法:1、推荐方法:fock twice, 用孙子进程去完成子进程的任务(http://blog.csdn.net/duyiwuer2009/article/details/7948040)2、wait(), 但是会使父进程阻塞3、signal(SIGCHLD,SIG_IGN原创 2012-09-10 22:28:12 · 8272 阅读 · 1 评论 -
GDB 调试段错误
原文:http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.htmlWe are going to use gdb to figure out why the following program causes a segmentation fault. The program is meant to read in a line of t转载 2013-11-28 20:27:36 · 1116 阅读 · 0 评论 -
Linux C socket编程中几个常见的结构体定义
● sockaddr#include struct sockaddr{ sa_family_t sa_family; char sa_data[14];};● in_addr_t#include /* Internet address. */typedef uint32_t in_addr_t;struct in_addr{ in_addr_t s_addr;原创 2012-08-13 16:32:28 · 2745 阅读 · 1 评论 -
获取协议名、协议号——getprotoent(),getprotobyname(),getprotobynumber()
/* Description of data base entry for a single service. */struct protoent{ char *p_name; /* Official protocol name. */ char **p_aliases; /* Alias list. */ int p_proto; /* Protocol numb原创 2012-08-16 10:21:59 · 4089 阅读 · 0 评论 -
进程内存布局
一个由 C/C++编译的程序占用的内存(memory)分为以下几个部分:1. 程序代码区(.text) - 存放函数体的二进制代码 。2. 文字常量区(.rodata) - 常量字符串就是放在这里的,程序结束后由系统释放(rodata—read only data)。3. 全局区/静态区(static) - 全局变量 和 静态变量的存储是放原创 2012-09-19 00:31:48 · 9722 阅读 · 0 评论 -
进程组、会话、控制终端
process group - 进程组session - 会话controlling terminal - 控制终端#include #include #include #include #include #include #include intmain(){ int i, fd0, fd1, fd2; pid_t pid; struct原创 2012-09-16 15:17:12 · 964 阅读 · 0 评论 -
普通用户为什么能修改自己的密码
/etc/passwd 文件用来存储所有用户的信息,任何用户都可以修改自己的密码,显然,普通用户在执行 /usr/bin/passwd 命令时修改了 /etc/passwd 文件(并不是说可以使用 vi 编辑器修改),但是,通过查看 /etc/passwd 文件的权限,发现普通用户对此文件仅有读权限,是什么原因导致普通用户可以修改 /etc/passwd 文件呢?这是因为 /u原创 2013-05-08 23:28:36 · 2641 阅读 · 0 评论 -
操作系统中常见术语
参考:《FreeBSD操作系统设计与实现》(The Design and Implementation of the FreeBSD Operating System)附录-术语表原创 2012-09-22 09:58:34 · 1686 阅读 · 0 评论 -
关于SIGHUP
1. SIGHUP, http://en.wikipedia.org/wiki/SIGHUPSignals have always been a convenient method of inter-process communication (IPC), but in early implementations there wereno user-definable signals (s原创 2012-09-21 19:16:02 · 2782 阅读 · 0 评论 -
perror() 和 strerror()
#include /* perror() */#include #include /* strerror() */int main(){ fprintf(stderr, "EACCES: %s\n", strerror(EACCES)); errno = ENOENT; perror("ENOENT"); return 0;}/*$ ./a.outEACCES: Per原创 2012-09-16 16:02:43 · 598 阅读 · 0 评论 -
Linux 和 Windows 中 fopen 打开方式的区别
The mode string can also include the letter 'b' either as a last characteror as a character between the characters in any of the two-characterstrings described above. This is strictly for compati原创 2012-05-10 20:49:24 · 3556 阅读 · 0 评论 -
GNU C Compiler Internals/GNU C Compiler Architecture(GNU C编译器内部结构/架构)
tag: cc1, cpp, as, ld, collect2http://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/GNU_C_Compiler_Architecturehttp://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/GNU_C_Compiler_Archi原创 2012-01-19 20:03:51 · 916 阅读 · 0 评论 -
UNIX下的文件权限解析
http://www.hudong.com/wiki/SUID转载 2011-11-30 20:15:58 · 600 阅读 · 0 评论 -
APUE-3.1 测试能否对标准输入设置偏移量(测试stdin,重定向文件,管道,FIFO)
/* 3-1 测试能否对标准输入设置偏移量(测试stdin,重定向文件,管道,FIFO) */#include #include #include #include int main(){ if(lseek(STDIN_FILENO, 0, SEEK_CUR) == -1) { perror("cannot seek"); exit(1); } else { pr原创 2011-11-22 20:26:00 · 858 阅读 · 0 评论 -
APUE-3.2 创建一个具有空洞的文件
/* 3-2 创建一个具有空洞的文件 */#include #include #include /* S_IRUSR,etc.*/#include #include #include int main(void){ char buf1[] = "abcdefghij"; char buf2[] = "ABCDEFGHIJ"; int fd; if( (fd = open(原创 2011-11-22 20:27:23 · 733 阅读 · 0 评论 -
APUE-3.4 对于指定的描述符打印文件标志
/* 3-4 对于指定的描述符打印文件标志 */#include #include #include #include #include int main(int argc, char *argv[]){ int val; if(argc != 2) { printf("usage: a.out \n"); exit(1); } if( (val = fcntl(a原创 2011-11-22 20:29:23 · 951 阅读 · 0 评论 -
dup,dup2实现stdout重定向
/* dup,dup2实现stdout重定向 */#include #include #include #include #include #include int main(void){ int fd, tempfd; char buf[] = "Am I in the stdout or in the file?\n"; if( (fd = open("tempfile"原创 2011-11-22 20:30:09 · 3743 阅读 · 0 评论 -
fopen() 的打开模式
对于Windows平台,若要以二进制形式打开文件,则需添加'b'(UNIX/Linux平台不区分文本文件和二进制文件,http://blog.csdn.net/duyiwuer2009/article/details/7555137)。'b'的添加规则如下:This additional "b" character can either be appended at the end of th原创 2012-05-13 15:32:19 · 1084 阅读 · 0 评论 -
Linux socket-多进程面向连接的服务器客户端程序
总结:1.、TCP的地址复用(address reuse)问题2、HOST_NAME_MAX 的处理方法3、多进程socket编程中 close() 和 shutdown() 的问题4、gethostname() 的问题5、getaddrinfo(hostname, "ruptime", &hint, &ailist) 中 "ruptime" 服务的问题("Servname n原创 2012-09-28 11:25:02 · 3305 阅读 · 0 评论 -
socket()参数中的地址族、套接字类型、协议号的取值
int socket(int domain, int type, int protocol);1、domain(地址族)和 type(套接字类型)的取值可以见 man socket2、关于 protocol(协议号)的取值可以见 netinet/in.h(可以结合 /etc/protocols看)也可以看这里:socket()--Create Socket, http://publib原创 2012-09-29 23:47:32 · 1711 阅读 · 0 评论