b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

linux下c语言多线程,网络通信简单聊天程序(Linux under the C language multi-thr

电脑杂谈  发布时间:2019-07-10 21:12:06  来源:网络整理

linux线程间通信_linux 多线程通信_linux线程通信

利用动态进程池或者线程池搭建网络服务器、客户端模型,实现网络基本通信(聊天,传文件等)。"msn messenger" 这个字眼是相当含糊的,因为微软用这个术语关系了几个不同部分的消息解决方案.你通过"msn messenger 网络"聊天,用来连接msn messenger 网络的最流行的程序是"msn messenger",而程序在msn messenger 网络中使用的语言则是"msn messenger 协议"。java线程之间的通信总是隐式进行 java并发模型—硬件视图 内存空间 共享对象 共享对象 共享对象 内存中的jvm 对象 程之间共享 线程1 线程2 处理器a 处理器b 处理器c 处理器d java并发模型—操作系统视图 jvm进程 hotspot vm中, java线程被 java线程 java线程 java线程 映射为本地操作系 统线程 linux kernel 操作系统内核 直接调度java 线程给可用的cpu处理器a 处理器b 处理器c 处理器d 编译器和处理器喜欢不择手段的冒险源代码 编译器优化 指令级并行 内存系统的 最终执行的 的重排序 的重排序 重排序 指令序列 编译器的 重排序 重排序 指令级并行的 处理器的 重排序 重排序 内存系统的 重排序 顺序一致性内存模型的原型结构 处理器a 处理器b 处理器c 处理器c a3 b2 d1 程序顺 a2 b1 c3 d2 序不变 a1 c2 每个内存 c1 单元一个 fifo队列内存单元 1 2 3 4 5 6 7 8 9 10 11 12 13 … … n顺序一致性内存模型的程序员视图 线程 线程 线程 线程 … 2 3 n 1 内存 顺序一致性内存模型的2 大特性 特性2 线程a 的程序顺序 操作的执行整体上无序,但两个线程 都只能看到这个执行顺序。

linux 多线程通信_linux线程通信_linux线程间通信

Application of select function to achieve asynchronous read and write operations。Said first server: first create a socket, and then bound, then enter an infinite loop, using the accept function, to accept the "connection" request, then call to create thread function, create a new thread, into the next cycle。 Thus, whenever a new "connection" is accepted, a new thread is created to implement 1 network communications to the N。 In the server program, thread with a buffer read and write, in order to avoid errors, then it is necessary to add the key code mutex lock work_mutex, see the specific code。

linux 多线程通信_linux线程间通信_linux线程通信

Server code1 #include<stdio。h>2 #include<stdlib。h>3 #include<string。h>4 #include<pthread。h>5 #include<sys/socket。h>6 #include<sys/un。h>7 #include<unistd。h>8 #include<semaphore。h> / / there is no binary semaphore can be deletedNine10 char buffer[1024]; / / read and write by Region11 sem_t bin_sem; / / binary semaphore not used, can be deleted12 void *pthread_function (void *arg); / / thread entrance function declaration13 pthread_mutex_t work_mutex; / / mutex statementFourteen15, int, main () {16 int result; / / integer variable to store the return value of a function call17, struct, sockaddr_un, server_address, client_address, //UNIX socket, server_address for server-side listening, and client_address for socket after client connection18 int client_len; / / accept connection, the client function of the length of the address stored in the19 int server_socketfd client_socketfd; / / server and client socket file descriptor20 pthread_t a_thread; / / thread ID mark21 pthread_attr_t thread_attr; / / thread attributes, can be seen behind, I commented, not used, can be deleted。

linux线程间通信_linux 多线程通信_linux线程通信

Twenty-two23 result = sem_init (&bin_sem, 0, 1); / / initialize a binary semaphore, due to the use of the mutex, so no use, can be deleted24 if (result = = 0) {25 PERROR ("sem_init");26 exit (EXIT_FAILURE);27}Twenty-eight29 result = pthread_mutex_init (&work_mutex, NULL); / / initialize the mutex30 if (result = = 0) {31 PERROR ("pthread_mutex_init");32 exit (EXIT_FAILURE);33}Thirty-four35 server_socketfd = socket (AF_UNIX, SOCK_STREAM, 0); / / create a socket connection, with TCP, for demonstration purposes only UNIX domain socket。

linux线程间通信_linux 多线程通信_linux线程通信

Thirty-six37 server_address。sun_family = AF_UNIX;38 strcpy (server_address。sun_path, server_socket);Thirty-nine40 unlink ("server_socket"); / / in bound before the previously existing in the current directory delete socketForty-one42 result = bind (server_socketfd (struct, sockaddr*) &server_address, sizeof (server_address)); / / binding43 If (result = = 0) {44 PERROR ("bind");45 exit (EXIT_FAILURE);46}Forty-seven48 result = listen (server_socketfd, 5); / / monitor, allows a maximum of 5 connection requests49 If (result = = 0) {50 PERROR ("listen");51 exit (EXIT_FAILURE);52}Fifty-three54 client_len = sizeof (client_address);55 while (1) {/ / to enter an infinite loop56 / printf ("If you want to quit please enter'quit'\n");"Printf" ("Do you want to accept a connectiong\n");58 memset (buffer,'\0', sizeof (buffer));59 fgets (buffer, sizeof (buffer), stdin);60 if ((strncmp ("quit", buffer, 4)) = = 0) break; * /6162 client _ socketfd = acceptance (server _ socketfd, (struct sockaddr *) & client _ address, & client _ len); / / 接受一个连接请求6364 / * result = pthread _ attr _ practice (& thread _ attr);65 if (result! = 0) {66 perror ("pthread _ attr _ practice");67 exit (exit _ failure);68}69 result = pthread _ attr _ setdetachstate (& thread _ attr, pthread _ create _ detached);70 if (result! = 0) {71 perror ("pthread _ attr _ setdetachstate");72 exit (exit _ failure);73} * /74 result = pthread _ create (& a _ thread, null, pthread _ function (void *) client _ socketfd); / / 成功接受一个请求后, 就会创建一个线程, 然后主线程又进入accept函数, 如果此时没有连接请求, 那么主线程会阻塞75 if (result! = 0) {76 perror ("pthread _ create");77 exit (exit _ failure);78}7980}81}8283 void * pthread _ function (void * arg) {/ / 线程入口函数, 每调用一次pthread _ create, 都会创建一个新的线程84 int fd = (int) arg; / / 把函数参数, 即连接成功后的套接字, 赋给fd。

85 int result;86 fd _ seen read _ fds; / / 文件描述符集合, 用于select函数87 int max _ fds; / / 文件描述符集合的最大数8889 printf ("% d id has connected! ! \ n ", fd);90 while (1) {9192 fd _ zero (& read _ fds); / / 清空集合93 fd _ seen (0, & read _ fds); / / 将标准输入放入的文件描述符集合, 这个用于读取标准输入, 即键盘的输入94 fd _ seen (fd, & read _ fds); / / 将连接后的客户文件描述符放入的文件描述符集合, 这个用于向客户端读取数据95 max _ fds = fd + 1;9697 / / sem _ wait (& bin _ sem);98 pthread _ mutex _ lock (& work _ mutex); / / 对关键区域上锁99 printf ("% d has get the lock \ n", fd);100 result = (select max _ fds, & read _ fds, (fd _ seen *) null, (fd _ seen *) null, (struct timeval *) null); / / 开始那些文件描述符出于可读状态101 if (result < 1) {102 printf ("select");103}104 if (fd _ isset (0, & read _ fds)) {/ / 如果标准输入处于可读状态, 说明键盘有所输入, 将输入的数据存放在buffer中, 然后向客户端写回, 如果输入 "quit" 将会退出一个聊天线程105 memset (buffer, '\ 0', sizeof (buffer)); / / 保险起见, 清零106 fgets (buffer, sizeof (buffer), stdin);107 if ((strncmp ("quit", buffer, 4)) = = 0) {108 printf ("you have terminaled the chat 'n');109 / / sem _ post (& bin _ sem);110 pthread _ mutex _ unlock (& work _ mutex);111 break;112}113 else {114 result = write (fd, buffer, sizeof (buffer));115 if (result = = - 1) {116 perror ("write");117 exit (exit _ failure);118}119}120}121 if (fd _ isset (fd, & read _ fds)) {/ / 如果客户套接字符可读,Well, the reads are stored in buffer, and then displayed, if the other party interrupts the chat, then result==0122 memset (buffer,'\0', sizeof (buffer));123 result = read (FD, buffer, sizeof (buffer));124 if (result = = -1) {125 PERROR ("read");126 exit (EXIT_FAILURE);127}128 else if (result = = 0) {"Printf" ("The other side has terminal the chat\n");130 / / sem_post (&bin_sem);131 pthread_mutex_unlock (&work_mutex);132 break;133}134 else{135 printf ("receive message:%s", "buffer");136}137}138 pthread_mutex_unlock (&work_mutex); / / unlock139 sleep (1); / / if not the line, the current thread will always occupy the buffer。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-111969-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...