Posix条件变量:与互斥锁共同解决生产者与消费者关系 #include <unistd.h>#include <sys/types.h>#include <pthread.h>#include <semaphore.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <...
Posix线程:生产者与消费者问题 #include <unistd.h>#include <sys/types.h>#include <pthread.h>#include <semaphore.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <...
程序、进程和线程 程序:完成特定功能的一系列有序指令的集合 一个可执行文件 代码段+数据段进程:程序的一次动态执行过程 代码段+数据段+堆栈段+PCB一个进程只对应一个程序一个程序可以有很多进程线程:程序的一个可执行路线进程是资源竞争的基本单位线程是程序执行的最小单位线程共享进程数据,例如进程各种ID,地址空间(4GB),信号处理器表格,文件描述符表但也拥有自己的一部分数据...
Posix共享内存 #include <unistd.h>#include <sys/types.h>#include <sys/mman.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include &a
Posix消息队列 //发送消息#include &lt;unistd.h&gt;#include &lt;sys/types.h&gt;#include &lt;fcntl.h&gt;#include &lt;sys/stat.h&gt;#include &lt;mqueue.h&gt;#include &lt;stdlib
程序员面试宝典(2) 进程是资源(CPU、内存等)分配的基本单位,它是程序执行时的一个实例。程序运行时系统就会创建一个进程,并为它分配资源,然后把该进程放入进程就绪队列,进程调度器选中它的时候就会为它分配CPU时间,程序开始真正运行。线程是程序执行时的最小单位,它是进程的一个执行流,是CPU调度和分派的基本单位,一个进程可以由很多个线程组成,线程间共享进程的所有资源,每个线程有自己的堆栈和局部变量。线程由CP...
system v信号量函数 /*semget函数功能 用来创建和访问一个消息队列原型 int semget(key_t key, int nsems, int semflg)参数 key 信号集的名字nsems 信号集中信号量的个数semflg 由九个权限标志构成,他们的用法和创建文件时使用的mode模式标志一样返回值:成功返回一个非负整数,即信号集的标识码;失败返回-1*//*semctl函数功能 用于...
system v共享内存函数 /*shmget函数功能:用来创建共享内存int shmget(ket_t key, size_t size, int shmflg);参数key:这个共享内存段名字size:共享内存大小shmflg:由九个权限标志构成,他们的用法和创建文件时使用的mode模式标志是一样的返回值:成功返回一个非负整数,即该共享内存段的标识码;失败返回-1*//*shmat函数功能:将共享内存...
共享内存mmap函数 /*mmap函数 作用:将文件或设备空间映射到共享内存区void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);参数addr:要映射的起始地址。通常指定为NULL,让内核自动选择len:映射到进程地址空间的字节数prot:映射区保护方式 PROT_READ/页面可读 PROT_WRIT...
system v消息队列实现回射客户/服务器 /*可能存在死锁状态:当客户端发送数据给服务器, 服务器回射消息时,消息队列被多个客户端同时发送的大量消息填满, 服务器无法回射,形成死锁/* server.c */#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#in...
system v消息队列msgrcv函数 /*msgrcv函数从消息队列接受一个消息*///第一个参数为消息队列的标示符msgid//第二个参数为指针,只想准备发送的消息//第三个为指针,指向消息长度//第四个为实现接受优先级的简单方式//第五个为控制着没有相应类型的消息可供接受时将要发生的事情//成功返回实际放到接受缓冲区里去的字符个数,失败返回-1#include &lt;unistd.h&gt;#include &l...
system v消息队列msgsnd函数 /*msgsnd函数将一个消息添加到消息队列*///第一个参数为消息队列的标示符msgid//第二个参数为指针,只想准备发送的消息//第三个为指针,指向消息长度//第四个为控制着当前消息队列满或到达系统上限时将要发生的事情#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#i...
system v消息队列msgctl函数 /*msgctl函数创建消息队列*///第一个参数为消息队列的标示符msgid//第二个参数为执行动作,,,IPC_RMID//第三个为消息队列结构体#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include &l...
system v消息队列msgget函数 /*msgget函数创建消息队列*/#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <stdio.h>#include <errno....
system v消息队列msgget函数 /*msgget函数创建消息队列*/#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <stdio.h>#include <errno....
UDP组播 /*server*/#include &amp;lt;stdio.h&amp;gt;#include &amp;lt;stdlib.h&amp;gt;#include &amp;lt;sys/types.h&amp;gt;#include &amp;lt;sys/socket.h&amp;gt;#include &amp;lt;string.h&
socket出错处理封装函数 /* socket出错处理封装函数 wrap.h*/#ifndef __WRAP_H_#define __WRAP_H_void perr_exit(const char *s);int Accept(int fd, struct sockaddr* sa, socklen_t *salenptr);void Bind(int fd, const struct sockaddr*...
socket出错处理封装函数 /* socket出错处理封装函数 wrap.h*/#ifndef __WRAP_H_#define __WRAP_H_void perr_exit(const char *s);int Accept(int fd, struct sockaddr* sa, socklen_t *salenptr);void Bind(int fd, const struct sockaddr*...
socket编程多线程模型 /*问题1.调整进程内的最大文件描述符上限2.线程如有共享数据,考虑线程同步3.服务与客户端线程退出时,退出处理4.系统负载,随着链接客户端增加,导致其他线程不能及时得到CPU*//*server.c*/#include &lt;stdio.h&gt;#include &lt;string.h&gt;#include &lt;netinet/in.h&gt;#inclu
线程属性设置简例 #include <stdio.h>#include <pthread.h>#include <string.h>#include <stdlib.h>int print_ntimes(char *str){ sleep(1); printf("%s", str); return 0;}void *th_fun(void* ...