嵌入式程序解析

上传人:最**** 文档编号:117080165 上传时间:2019-11-18 格式:DOC 页数:9 大小:102.01KB
返回 下载 相关 举报
嵌入式程序解析_第1页
第1页 / 共9页
嵌入式程序解析_第2页
第2页 / 共9页
嵌入式程序解析_第3页
第3页 / 共9页
嵌入式程序解析_第4页
第4页 / 共9页
嵌入式程序解析_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《嵌入式程序解析》由会员分享,可在线阅读,更多相关《嵌入式程序解析(9页珍藏版)》请在金锄头文库上搜索。

1、多线程:#include #include #include #include #include int task1(int *cnt)while(*cnt 5)sleep(1);(*cnt)+;printf(task1 cnt = %d.n, *cnt);return (*cnt);int task2(int *cnt)while(*cnt 5)sleep(2);(*cnt)+;printf(task2 cnt = %d.n, *cnt);return (*cnt);int main(int argc, char *argv)int result;int t1 = 0;int t2 = 0;

2、int rt1, rt2;pthread_t thread1, thread2;/* create the first thread. */result = pthread_create(&thread1, PTHREAD_CREATE_JOINABLE, (void *)task1, (void *)&t1);if(result)perror(pthread_create: task1.n);exit(EXIT_FAILURE);/* create the second thread. */result = pthread_create(&thread2, PTHREAD_CREATE_JO

3、INABLE, (void *)task2, (void *)&t2);if(result)perror(pthread_create: task2.n);exit(EXIT_FAILURE);pthread_join(thread1, (void *)&rt1);pthread_join(thread2, (void *)&rt2);printf(total %d times.n, t1+t2);printf(return value of task1: %d.n, rt1);printf(return value of task2: %d.n, rt2);exit(EXIT_SUCCESS

4、);TCP(sever):#include #include #include #include #include #include #include #include #define PORT 5000 / The port which is communicate with server#define BACKLOG 10#define LENGTH 512 / Buffer length int main () int sockfd; / Socket file descriptor int nsockfd; / New Socket file descriptor int num; i

5、nt sin_size; / to store struct size char sdbufLENGTH; / Send buffer struct sockaddr_in addr_local; struct sockaddr_in addr_remote; char sendstr16= 123456789 abcde; /* Get the Socket file descriptor */ if( (sockfd = socket(AF_INET, SOCK_STREAM, 0) = -1 ) printf (ERROR: Failed to obtain Socket Despcri

6、tor.n); return (0); else printf (OK: Obtain Socket Despcritor sucessfully.n); /* Fill the local socket address struct */ addr_local.sin_family = AF_INET; / Protocol Family addr_local.sin_port = htons(PORT); / Port number addr_local.sin_addr.s_addr = INADDR_ANY; / AutoFill local address bzero(&(addr_

7、local.sin_zero), 8); / Flush the rest of struct /* Blind a special Port */ if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr) = -1 ) printf (ERROR: Failed to bind Port %d.n,PORT); return (0); else printf(OK: Bind the Port %d sucessfully.n,PORT); /* Listen remote connect/calling

8、*/ if(listen(sockfd,BACKLOG) = -1) printf (ERROR: Failed to listen Port %d.n, PORT); return (0); else printf (OK: Listening the Port %d sucessfully.n, PORT); while(1) sin_size = sizeof(struct sockaddr_in); /* Wait a connection, and obtain a new socket file despriptor for single connection */ if (nso

9、ckfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size) = -1) printf (ERROR: Obtain new Socket Despcritor error.n); continue; else printf (OK: Server has got connect from %s.n, inet_ntoa(addr_remote.sin_addr); /* Child process */ if(!fork() printf(You can enter string, and press exit to en

10、d the connect.n); while(strcmp(sdbuf,exit) != 0) scanf(%s, sdbuf); if(num = send(nsockfd, sdbuf, strlen(sdbuf), 0) = -1) printf(ERROR: Failed to sent string.n); close(nsockfd); exit(1); printf(OK: Sent %d bytes sucessful, please enter again.n, num); close(nsockfd); while(waitpid(-1, NULL, WNOHANG) 0); TCP(client):#include #include #include #include #include #include #include #include #define PORT 5000 / The port which is communicate with server#define LENGTH 256 / Buffer lengthint main(int argc, char *argv) int sockfd; / Socket file descri

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号