随笔分类 - Linux系统编程
摘要:chmod [option] mode file # 将文件file的权限修改为mode # option可以为-r 表示递归修改文件 chown [option] owner:gourp file # 修改文件file的所有者和组 sed -i 's/aaa/bbb/g' filename # 将
阅读全文
摘要:# server #include <stdio.h> #include <arpa/inet.h> #include <sys/un.h> #include <string.h> #include <unistd.h> int main() { int lfd = socket(AF_LOCAL,
阅读全文
摘要:# UDP通信 # server.c #include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> int main() { int lfd = socket
阅读全文
摘要:# select /* select阻塞函数,多次还是需要while,在新建客户端方面有没有都一样 因为accept多次也需要while 但在判断客户端是否有数据到来方面 使用了select就不需要创建多个线程或进程判断了,select可以批量判断 */ #include <stdio.h> #in
阅读全文
摘要:/* 多线程实现并发服务器 主线程负责接收 子线程负责处理 */ #include <stdio.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <un
阅读全文
摘要:# server /* 多进程实现并发服务器 主进程负责接收 子进程负责处理 */ #include <stdio.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #in
阅读全文
摘要:# TCP通信流程 # 四次挥手半关闭状态 # server /* #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); 功能:创建一个套接字 参数: domain
阅读全文
摘要:有几个线程就有几个除互斥信号之外的信号量,每个线程等待自己的信号量有位置, 并最后给其他信号量位置。初始时,生产者的值非0,消费者的值为0。 /* #include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int va
阅读全文
摘要:# 互斥锁 /* #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 功能:初始化一个互斥变量mutex 参数:
阅读全文
摘要:# pthread_join /* #include <pthread.h> int pthread_join(pthread_t thread, void **retval); 功能:和一个已经终止的线程进行连接 回收线程的资源 阻塞函数,调用一次只能回收一个线程 任何线程都可以wait其它线程一
阅读全文
摘要:拥有线程程序的编译需要加 -pthread gcc a.c -o a -pthread /* #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_ro
阅读全文
摘要:# 终端 # 进程组 # 会话 # 守护进程 // 创建一个会话,每隔2s获取系统时间,并将时间写入到磁盘文件中 #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <s
阅读全文
摘要:# write.c /* #include <sys/ipc.h> #include <sys/shm.h> int shmget(key_t key, size_t size, int shmflg); 作用:创建一个新的共享内存段,或获取一个既有共享内存段的标识 新创建的段会初始化为0 参数:
阅读全文
摘要:/* #include <signal.h> int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); 功能:检查或改变信号的处理方式 参数: signum:信号编号 act:处理方式 stru
阅读全文
摘要:# core文件使用 如果要使用core文件,首先将core设置文件大小 ulimit -a //查看各种文件大小限制 ulimit -c 1024 //将core文件大小设置为1024,c表示core文件,从-a的列表中可以看到 设置大小之后,再进行编译,如果不成功则会生成core文件 使用gdb
阅读全文
摘要:/* 内存映射: 是将磁盘文件数据映射到内存,用户通过修改内存就能修改磁盘文件 #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); 功能:将一个
阅读全文
摘要:# 父子进程之间示例 /* 有名管道(FIFO) 提供一个路径名与之关联,以FIFO的文件形式存在于文件系统中 读写操作和普通文件一样,常用于不存在关系的进程之间 注意事项: 读写进程只要有一端未打开,另一打开的一端就会阻塞在read或write处 当两端都打开,其中一端关闭时,另一端也停止 通过命
阅读全文
摘要:/* #include <stdlib.h> void exit(int status); #include <unistd.h> void _exit(int status); 参数: status:进程退出时的一个状态信息,由调用进程传给父进程 孤儿进程 父进程运行结束,但子进程还在运行,这样的
阅读全文
摘要:/* exec函数族 加载并运行可执行目标文件 fork调用一次,返回两次 exec调用一次,从不返回,只有出现错误时,才会返回-1到调用程序 fork后相同程序,不同进程;execve后相同进程,不同程序。 因此,通常fork一个子进程,然后再使用exec #include <unistd.h>
阅读全文

浙公网安备 33010602011771号