
linux系统编程
发如雪Jay
你猜
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
linux之通过线程属性设置线程的分离状态
7.通过线程属性设置线程的分离状态 a.初始化线程属性 int pthread_attr_init(pthread_attr_t *attr); 成功:0;失败:错误号 b.销毁线程属性所占用的资源 int pthread_attr_destroy(pthread_attr_t *attr); 成功:0;失败:错误号 c.设置线程属性,分离 or 非分离 int pthread_attr_setdetachstate(pthread_a...原创 2021-08-22 21:37:15 · 407 阅读 · 0 评论 -
linux之终止线程的三种方法
代码:pthrd_endof3.c#include <stdio.h>#include <unistd.h>#include <pthread.h>#include <stdlib.h>//方式1void *tfn1(void *arg){ printf("thread 1 returning\n"); return (void*)111; //以return的方式终止该线程}//方式2void *tfn2(void *arg){原创 2021-08-22 21:33:56 · 2720 阅读 · 0 评论 -
linux之使用 pthread_join 函数将循环创建的多个子线程回收
代码:pthrd_loop_join.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>int var = 100;void *tfn(void *arg){ int i; i = (int)arg; sleep(i); if(i==1){ var = 333; printf("I'm %dth pthread,原创 2021-08-22 21:29:32 · 640 阅读 · 0 评论 -
linux之使用 pthread_detach 函数实现线程分离
5.pthread_detach函数 作用:实现线程分离,好处:自动清理PCB 原型: int pthread_detach(pthread_t thread); 成功:0;失败:错误号 注意:一般情况下,线程终止后,其终止状态一直保留到其它线程调用 pthread_join 获取它的状态为止。但是线程也 可以被置为 detach 状态,这样的线程一旦终止就立刻回收它占用的所有资源,而不保留终止状态。不能对一个已经 处于 detach 状态的线程调用 pthread_j...原创 2021-08-22 21:27:03 · 1168 阅读 · 0 评论 -
linux之pthread_join回收子线程
4.pthread_join函数 作用:阻塞等待线程退出,获取线程退出状态。其作用,对应进程中 waitpid() 函数。 int pthread_join(pthread_t thread, void **retval); 成功:0;失败:错误号 参数: thread:线程ID; retval:存储线程结束状态。 对比记忆: 进程中:main 返回值、exit 参数-->int;等待子进程结束 wait 函数参数--&g...原创 2021-08-22 21:23:14 · 432 阅读 · 0 评论 -
linux之循环创建多个子线程
代码:more_pthrd.c#include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>void * thrd_func(void *arg){ int i = (int)arg; //sleep(i); printf("%dth thread: thread id = %lu, pid =原创 2021-08-22 21:12:57 · 657 阅读 · 0 评论 -
linux之创建单个进程
1.pthread_self函数 作用:获取线程ID,其作用对应进程中getpid()函数 原型:pthread_t pthread_self(void); 返回值:成功:0;失败:无返回值 线程 ID:pthread_t类型,本质:在 Linux 下为无符号整数(%lu),其他系统中可能是结构体实现2.pthread_create函数 作用:创建一个新线程,其作用,对应进程中 fork() 函数。 原型:int pthread_create(pthread_...原创 2021-08-22 21:09:48 · 314 阅读 · 0 评论 -
linux之父子进程交替打印数字
代码:#include <unistd.h>#include <signal.h>#include <stdio.h>#include <stdlib.h>//本程序:让父子进程交替打印数字,也是icp通信的一种int n=0,flag=0;void sys_err(char * str){ perror(str); exit(1);}//子进程信号处理函数void do_sig_child(int num){ printf原创 2021-08-22 21:01:32 · 1777 阅读 · 3 评论 -
linux之借助SIGCHLD信号回收子进程
代码:sigchild.c#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <sys/types.h>#include <sys/wait.h>#include <signal.h>void sys_err(char *str){ perror(str); exit(1)原创 2021-08-22 20:57:50 · 489 阅读 · 0 评论 -
linux之sigpending函数的简单使用
原型:int sigpending(sigset_t *set);参数:set是传出参数返回值:成功:0;失败:-1,设置 errno代码:sigpending.c#include <unistd.h>#include <signal.h>#include <stdio.h>void sig_alrm(){ /*nothing to do*/}unsigned int mysleep(unsigned int nsecs){ str原创 2021-08-22 20:51:24 · 867 阅读 · 0 评论 -
linux之sigaction函数的简单使用
1.sigaction函数原型:int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);参数:act:传入参数,新的处理方式。oldact:传出参数,旧的处理方式。作用:修改信号处理动作(通常在 Linux 用其来注册一个信号的捕捉函数)返回值:成功:0;失败:-1,设置 errnostruct sigaction...原创 2021-08-14 22:09:41 · 1208 阅读 · 0 评论 -
linux之pause函数的使用
用pause函数和alarm函数实现sleep函数的效果,代码pause.c#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <signal.h>#include <errno.h>//本程序:用pause函数和alarm函数实现sleep函数的效果void catch_sigalrm(int signo){ ; //因为为了使调用pause函数原创 2021-08-14 21:58:24 · 1336 阅读 · 0 评论 -
linux之signal函数的简单使用
1.signal函数 原型:sighandler_t signal(int signum, sighandler_t handler); 作用:注册一个信号捕捉函数2.代码:signal2.c#include <stdio.h>#include <signal.h>#include <stdlib.h>#include <errno.h>typedef (*sighandler_t) (int);vo...原创 2021-08-14 21:46:48 · 662 阅读 · 0 评论 -
linux之信号集操作函数的简单使用
1.信号集操作函数介绍 自定义信号集: sigset_t set; // typedef unsigned long sigset_t; (1) int sigemptyset(sigset_t *set); 将某个信号集清 0 成功:0;失败:-1 (2) int sigfillset(sigset_t *set); 将某个信号集...原创 2021-08-14 21:39:31 · 417 阅读 · 0 评论 -
linux之setitimer函数结合signal函数实现alarm函数的作用
1.alarm函数a.原型:unsigned int alarm(unsigned int seconds);b.返回值:返回 0 或上一个 alarm 剩余的秒数,无失败。c.作用:设置定时器(闹钟)。在指定 seconds 后,内核会给当前进程发送 14)SIGALRM 信号。进程收到该信号,默认动作终止。2.signal 函数:原型:sighandler_t signal(int signum, sighandler_t handler);作用:注册一个信号捕捉函数:3.s原创 2021-08-08 19:37:39 · 627 阅读 · 0 评论 -
linux之setitimer函数实现alarm函数的作用
1.alarm函数a.原型:unsigned int alarm(unsigned int seconds);b.返回值:返回 0 或上一个 alarm 剩余的秒数,无失败。c.作用:设置定时器(闹钟)。在指定 seconds 后,内核会给当前进程发送 14)SIGALRM 信号。进程收到该信号,默认动作终止。2.setitimer函数原型:int setitimer(int which, const struct itimerval *new_value, struct itimerv原创 2021-08-08 19:19:41 · 347 阅读 · 0 评论 -
linux之父进程使用kill函数杀死子进程
父进程循环创建5个子进程,并且父进程杀死第三个创建的子进程。代码:kill.c#include <stdlib.h>#include <stdio.h>#include <signal.h>#include <sys/types.h>#include <unistd.h>void sys_err(const char * str){ perror(str); exit(1);}int main(){ //int原创 2021-08-08 17:32:08 · 5392 阅读 · 0 评论 -
llinux之mmap用于非血缘关系进程间通信
代码:mmap_w.c#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h>#include <fcntl.h>#include <unistd.h>#include <string.h>struct Student{ int id; c.原创 2021-08-07 22:03:45 · 146 阅读 · 0 评论 -
linux之mmap用于父子进程间通信
1.mmap函数 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); 参数: addr: 指定映射区的首地址。通常传NULL,表示让系统自动分配 length: 共享内存映射区的大小(<= 文件的实际大小) prot: 共享内存映射区的读写属性。PROT_READ、PROT_WRITE、PROT_...原创 2021-08-07 21:57:27 · 208 阅读 · 0 评论 -
linux之通过一个文件实现非血缘关系进程间通信
原理:打开的文件是内核中的一块缓冲区。多个无血缘关系的进程,可以同时访问该文件。test1.c与test2.c实现了通过一个文件 完成两个非血缘关系进程之间的通信代码:test1.c#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <errno.h>#include <pthread.h>#include <sys/stat.h>#原创 2021-08-07 21:51:35 · 646 阅读 · 0 评论 -
linux之通过一个文件实现父子进程间通信
代码:fork_shared_fd.c#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <sys/wait.h>void sys_err(const c原创 2021-08-07 21:45:15 · 515 阅读 · 0 评论 -
linux之通过FIFO完成两个非血缘关系进程间的通信
1.FIFO 常被称为命名管道,以区分管道(pipe)。管道(pipe)只能用于“有血缘关系”的进程间。但通过 FIFO,不相关的进程也能交换数据。2.FIFO 是 Linux 基础文件类型中的一种。但,FIFO 文件在磁盘上没有数据块,仅仅用来标识内核中一条通道。各进程可以打开这个文件进行 read/write,实际上是在读写内核通道,这样就实现了进程间通信。3.创建方式: a.命令:mkfifo 管道名b.库函数:int mkfifo(const char ...原创 2021-07-31 17:45:16 · 714 阅读 · 0 评论 -
linux之pipe函数实现ls | wc –l
1.原型:int pipe(int pipefd[2]);2.返回值:成功:0;失败:-1,设置 errno3.函数调用成功返回 r/w 两个文件描述符。无需 open,但需手动 close。规定:fd[0] → r; fd[1] → w4.管道创建成功以后,创建该管道的进程(父进程)同时掌握着管道的读端和写端。5.利用pipe函数实现 ls | wc –l。假定父进程实现 ls,子进程实现 wc6.ls | wc –l的作用是统计该路径下文件的个数代码:#include &l原创 2021-07-30 21:51:36 · 675 阅读 · 0 评论 -
linux之pipe函数实现兄弟进程间通信
1.原型:int pipe(int pipefd[2]);2.返回值:成功:0;失败:-1,设置 errno3.函数调用成功返回 r/w 两个文件描述符。无需 open,但需手动 close。规定:fd[0] → r; fd[1] → w4.管道创建成功以后,创建该管道的进程(父进程)同时掌握着管道的读端和写端。5.利用pipe函数实现兄弟进程间通信代码:#include <stdio.h>#include <unistd.h>#include <原创 2021-07-30 21:46:18 · 667 阅读 · 0 评论 -
linux之pipe函数实现父子进程间通信
1.原型:int pipe(int pipefd[2]);2.返回值:成功:0;失败:-1,设置 errno3.函数调用成功返回 r/w 两个文件描述符。无需 open,但需手动 close。规定:fd[0] → r; fd[1] → w4.管道创建成功以后,创建该管道的进程(父进程)同时掌握着管道的读端和写端。5.利用pipe函数实现父子进程间通信:父进程写书据,子进程读数据代码#include <stdio.h>#include <unistd.h>原创 2021-07-30 21:40:54 · 2816 阅读 · 0 评论 -
linux之execlp函数简单使用
linux之execlp函数简单使用1.execlp函数:加载一个进程,借助PATH 环境变量#include <unistd.h>extern char **environ;int execlp(const char *file, const char arg, … / (char *) NULL */);a.参数1为要加载的程序的名字(可执行文件名),参数2为argv[0],参数3为argv[1]…,最后一个参数一定是NULLb.特别说明:第二个参数(argv[0])可以写任何原创 2021-07-26 17:36:10 · 9183 阅读 · 0 评论 -
linux之waitpid函数的简单使用
linux之waitpid函数waitpid函数:成功:返回清理掉的子进程 ID;失败:-1(无子进程)原型:pid_t waitpid(pid_t pid, int *wstatus, int options);a.作用同 wait,但可指定 pid 进程清理,可以不阻塞b.参数 pid:> 0(子进程id号) 回收指定 ID 的子进程-1 回收任意子进程(相当于 wait)c.如果参数3为WNOHANG,且子进程正在运行,则返回值为0WNOHANG表示非阻塞,(轮询)d.参数3为原创 2021-07-26 17:23:30 · 843 阅读 · 0 评论 -
linux之wait函数
linux之wait函数wait函数:成功:清理掉的子进程 ID;失败:-1 (没有子进程)#include <sys/types.h>#include <sys/wait.h>pid_t wait(int *wstatus);wstatus是传出参数,a.wait函数有3个功能:(1)阻塞等待子进程退出(2)回收子进程残留资源(3)获取子进程结束状态(退出原因)。b.可使用 wait 函数传出参数 status 来保存进程的退出状态。借助宏函数来进一步判断进程终原创 2021-07-23 22:04:02 · 7367 阅读 · 0 评论 -
linux之environ(环境变量)
linux之environ环境变量操作函数1.getenv函数:在当前环境变量中查找环境变量name,若存在返回name,若不存在返回NULL#include <stdlib.h>char *getenv(const char *name);2.setenv函数:设置环境变量,设置成功,则返回0,否则返回-1#include <stdlib.h>int setenv(const char *name, const char *value, int overwrite);原创 2021-07-23 21:54:46 · 1476 阅读 · 0 评论 -
linux之fork函数创建单个多个子进程
linux之fork函数1.fork函数:创建子进程,通过fork函数的返回值区分父子进程#include <sys/types.h>#include <unistd.h>pid_t fork(void);返回值有2个:父进程执行到fork函数,创建了子进程,然后两个进程同时对fork做返回(1)返回子进程的pid(进程id号,非负正数) -----------父进程(2)返回0------------------------------------- 子进程2.ge原创 2021-07-23 21:48:47 · 1325 阅读 · 0 评论