
linux-应用程序
psvoldemort
这个作者很懒,什么都没留下…
展开
-
自定义getMode --- 将mode_t 转换为相应的字符串
自定义getMode --- 将mode_t 转换为相应的字符串/* * getMode.c * * Created on: 2011-11-15 * Author: snape */#include #include #include #include /* * mode_t 是一个八进制数 * 0777 ---- 111 111 111 *原创 2011-11-15 10:36:53 · 2264 阅读 · 0 评论 -
IPC--消息队列 message queue(消息队列的创建,信息的发送和接收)
注意 :在消息得发送和接受时,可能会重置errno的值,如果发送和接受函数返回-1,要检查errno来确定具体的出错信息消息的发送#include #include #include #include #include #include #define KEY 0x1234/* * step 1 : 自定义msgbuf结构(存放消息的类型和数据本身),类比与msg.h原创 2011-11-13 22:12:38 · 733 阅读 · 0 评论 -
IPC--信号量 Semaphore 生产者 消费者模型 (semget semop semctl)
/* * producer.c * * Created on: 2011-11-19 * Author: snape */#include #include #include #include int main(int argc, char **argv) { void say(int, char*); int semid; struct sembuf b原创 2011-11-19 22:18:33 · 693 阅读 · 0 评论 -
IPC--共享内存 Share Memory 实例(shmget shmat shmdt shmctl)
/* * writeShareM.c * * Created on: 2011-11-20 * Author: snape */#include #include #include #include int main(int argc, char **argv) { void say(int, char *); int shmid; int i = 0;原创 2011-11-20 11:44:04 · 1427 阅读 · 0 评论 -
多线程 undefined reference to 'pthread_create' 解决办法
使用pthread.h中得函数时undefined reference to 'pthread_create'undefined reference to 'pthread_join'问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_a转载 2011-11-20 15:45:58 · 502 阅读 · 0 评论 -
进程退出(exit)处理函数 atexit
/* * atexit.c * * Created on: 2011-11-22 * Author: lc */#include #include #include #include /* * exit handler * 终止处理程序 * 使用atexit函数对函数的退出绑定终止处理程序 * * 该程序(函数)在函数exit后被调用(main函数ret原创 2011-11-22 18:57:42 · 660 阅读 · 0 评论 -
线程退出处理函数 pthread_cleanup_push pthread_cleanup_pop
/* * cleanup.c * * Created on: 2011-11-22 * Author: lc */#include #include #include #include /* * pthread_cleanup_push * pthread_cleanup_pop函数 * 为线程退出提供处理程序(函数) * * 函数cleanup会在以原创 2011-11-22 19:01:19 · 830 阅读 · 0 评论 -
多线程 读写锁 实例--同步操作双向链表(unix环境高级编程)
对双向链表得 insert append remove find原创 2011-11-22 21:13:42 · 1625 阅读 · 0 评论 -
多线程 一个mutex互斥量实例(unix环境高级编程)
/* * mutex.c * * Created on: 2011-11-23 * Author: lc */#include #include #include #include #include #include struct foo { int count; pthread_mutex_t mutex;};struct foo * fp;st原创 2011-11-22 19:37:49 · 583 阅读 · 0 评论 -
select 实例程序
/* * select.c * * Created on: 2011-11-7 * Author: lc */#include #include #include #include #include int main() { int count, maxfd; char buf[7]; int fds[2]; fd_set s原创 2011-11-06 21:01:32 · 488 阅读 · 0 评论 -
daemon 实例程序
/* * daemon.c * * Created on: 2011-11-9 * Author: lc */#include #include #include #include #include #include #include #define MAXFD 65535int main(int argc, char **arg原创 2011-11-08 20:30:53 · 581 阅读 · 0 评论 -
daemon 守护进程
fork出的子进程几乎会copy父进程的全部资源,包括 1. 控制终端 2.进程组ID3.session ID4.umask 掩码5.工作目录6.打开的文件 fd 要使子进程完全原创 2011-11-08 19:34:37 · 345 阅读 · 0 评论 -
syslog 系统日志服务 --记录daemon出错信息
daemon出错信息 不能通过perror显示在stderr,因为daemon没有控制终端 ,使用syslog 记录openlogsyslogcloselog/var/log/message 系统日志文件只能由root权限查看 /* * syslog.c * * Created on: 2011-11-9 * Author: lc */原创 2011-11-08 21:15:03 · 1165 阅读 · 0 评论 -
通信格式的约定(两种方式)
/* * string_type.c * * Created on: 2011-11-10 * Author: lc *///父子进程使用管道进行通信时,由于读写的字符串长度未知,//所以要获得有用的信息,就要对读的string和写的string规格进行规定//1. 规定读写字符串的长度,是定长(规定长度)//2. 规定前4个字符是字符串长度,后段字符是消息信息(原创 2011-11-10 14:56:35 · 500 阅读 · 0 评论 -
zombie 僵死进程(zombie进程会在进程表---ps -aux 中Defunct)
/* * zombie.c * * Created on: 2011-11-9 * Author: lc */#include #include #include #include //产生一个zombie进程//原因:子进程先于父进程退出,但是,父进程没有调用wait函数接受子进程退出状态,//导致子进程成为了一个僵尸进程---即,只在进程资源表中占位,已原创 2011-11-09 21:29:53 · 493 阅读 · 0 评论 -
IPC-- key关键字的产生 ftok函数
ftok函数 系统建立IPC通讯 (消息队列、信号量和共享内存) 时必须指定一个ID值。通常情况下,该id值通过ftok函数得到。编辑本段ftok原型 头文件: #include #include 如下: key_t ftok( char * fname, int id ) fname就是你指定的转载 2011-11-20 11:58:17 · 860 阅读 · 0 评论 -
全局跳转 setjmp longjmp
#include int setjmp(jmp_buf env) void longjmp(jmp_buf env, int val); setjmp 函数首次调用成功返回0(失败返回-1),以后得调用会返回longjmp第二个参数得值/* * jump.c * * Created on: 2011-11-18 * Author: snape */原创 2011-11-19 14:05:17 · 507 阅读 · 0 评论 -
信号分类与详解 (4) 定时器信号
SIGALRM时钟定时信号, 计算的是实际的时间或时钟时间. alarm函数使用该信号. SIGVTALRM虚拟时钟信号. 类似于SIGALRM, 但是计算的是该进程占用的CPU时间.(setitimer函数) SIGPROF类似于SIGALRM/SIGVTALRM, 但包括该进程用的CPU时间以及系统调用的时间.原创 2011-11-16 10:31:58 · 666 阅读 · 0 评论 -
IPC--消息队列 message queue --msgctl函数(删除指定msqid的消息队列)
msgctl 系统调用对 msgqid 标识的消息队列执行 cmd 操作,系统定义了 3 种 cmd 操作: IPC_STAT , IPC_SET , IPC_RMID ,意义分别如下:IPC_STAT : 该命令用来获取消息队列对应的 msqid_ds 数据结构,并将其保存到 buf 指定的地址空间。IPC_SET : 该命令用来设置消息队列的属性,要设置的属性存储在原创 2011-11-14 17:39:22 · 3563 阅读 · 1 评论 -
信号处理函数 signal
【signal系统调用】 功能描述: 为指定的信号安装新的处理句柄。信号处理句柄可能是用户指定的函数,SIG_IGN 或 SIG_DFL。当信号到达时,如果其处理句柄是SIG_DFL,那么会以默认的方式处理信号;如果其处理句柄是SIG_IGN,那么信号会被忽略;最后,如果处理句柄是用户指定的函数,此时先将信号处理方式重置为SIG_DFL,接着有可能阻塞处理中的信号,最后是调用信号处转载 2011-11-16 11:15:55 · 628 阅读 · 0 评论 -
时间 日期 -- 系统时间 time_t 结构,time difftime 函数
/* * sys_time.c * * Created on: 2011-11-15 * Author: snape */#include #include int main(int argc, char **argv) { time_t start, end; time(&start); sleep(5); time(&end); fprintf(st原创 2011-11-15 21:00:05 · 553 阅读 · 0 评论 -
信号分类与详解 (1) 软硬件异常信号 和 其他信号
软硬件异常信号其他信号SIGCHLD or SIGCLD子进程结束时, 父进程会收到这个信号。如果父进程没有处理这个信号,也没有等待(wait)子进程,子进程虽然终止,但是还会在内核进程表中占有表项,这时的子进程称为僵尸进程。这种情 况我们应该避免(父进程或者忽略SIGCHILD信号,或者捕捉它,或者wait它派生的子原创 2011-11-16 10:39:22 · 379 阅读 · 0 评论 -
IPC--消息队列 message queue --msgctl函数(get 指定msqid的消息队列)
2. msgctl(msqid,IPC_STAT,&buf); get消息队列的信息/* * print_msq.c * * Created on: 2011-11-15 * Author: snape */#include #include #include #include #include #include #include void原创 2011-11-15 11:34:00 · 704 阅读 · 0 评论 -
IPC--消息队列 message queue --msgctl 函数(创建 删除 查看 综合运用)
/* * msghandler.c * * Created on: 2011-11-15 * Author: snape */#include #include #include #include #include #include //usage : msghandler KEY [c|d]// KEY指定消息队列得关键字 c为创建 d为删除int ma原创 2011-11-15 12:30:06 · 895 阅读 · 0 评论 -
信号介绍
Unix信号列表1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGUSR1转载 2011-11-16 09:36:49 · 1123 阅读 · 0 评论 -
信号分类与详解 (2) 终止进程类信号
1. 软硬件异常类信号(多数的该类信号得默认处理是core-dump)2. 终止进程类信号 1) SIGHUP ‘SIGHUP’是一个信号,它按照惯例意味着“终端线路被挂断” SIGHUP信号与控制终端UNIX中进程组织结构为 session (会话)包含一个前台进程组及一个或多个后台进程组,一个进程组包含多原创 2011-11-16 08:58:55 · 904 阅读 · 0 评论 -
信号分类与详解 (3) 挂起进程类信号
SIGCONT(continue)让一个停止(stopped)的进程继续执行. 本信号不能被阻塞. 可以用一个handler来让程序在由stopped状态变为继续执行时完成特定的工作. 例如, 重新显示提示符 SIGSTOP停止(stopped)进程的执行. 注意它和terminate以及interrupt的区别:该进程还未结束, 只是暂停执行. 本信号不能被阻塞, 处理或原创 2011-11-16 10:21:12 · 1142 阅读 · 0 评论 -
waitpid wait的status参数 以及相应的宏函数 详解
wait 和 waitpid 的 int* 类型的参数用两个字节记录 wait 的 status参数 * 高8位 记录进程调用exit退出的状态(正常退出) * 低8位 记录进程接受到的信号 (非正常退出)如果正常退出(exit) ---高8位是退出状态号,低8位是0如果非正常退出(signal)----高八位是0,低8位是siganl id例如 /* * w原创 2011-11-17 15:48:16 · 2354 阅读 · 0 评论 -
信号的发送 kill raise
1. 函数说明: kill和raise是用来发送信号的: kill把信号发送给进程或进程组; raise把信号发送给(进程)自身. 他们的原型如下: #include int kill(pid_t pid, int signo); int raise(int signo); 成功则返回0, 出错则返回-1 从原型上可以转载 2011-11-17 16:33:36 · 359 阅读 · 0 评论 -
waitpid wait 综述
一)系统调用wait1)概述wait函数的原型为:pid_t wait(int *status)当进程退出时,它向父进程发送一个SIGCHLD信号,默认情况下总是忽略SIGCHLD信号,此时进程状态一直保留在内存中,直到父进程使用wait函数收集状态信息,才会清空这些信息.用wait来等待一个子进程终止运行称为回收进程.当父进程忘了用wait()函数等待已终止的子进程时,子转载 2011-11-17 15:39:28 · 365 阅读 · 0 评论 -
fcntl使用
and_ttfcntl使用功能描述:根据文件描述词来操作文件的特性。#include #include int fcntl(int fd, int cmd); int fcntl(int fd, int cmd, long arg); int fcntl(int fd, int cmd, struct flock *lock);转载 2014-03-13 19:07:53 · 816 阅读 · 0 评论