当前位置: 首页 > news >正文

ppt电子商务网站建设广州seo成功案例

ppt电子商务网站建设,广州seo成功案例,有没有做书签的网站,网站建设多少钱Linux环境中实现并发TCP/IP服务器。多线程在解决方案中提供了并发性。由于并发性,它允许多个客户端同时连接到服务器并与服务器交互。 Linux多线程编程概述 许多应用程序同时处理多项杂务。服务器应用程序处理并发客户端;交互式应用程序通常在处理后台…

Linux环境中实现并发TCP/IP服务器。多线程在解决方案中提供了并发性。由于并发性,它允许多个客户端同时连接到服务器并与服务器交互。

Linux多线程编程概述

许多应用程序同时处理多项杂务。服务器应用程序处理并发客户端;交互式应用程序通常在处理后台计算时处理用户输入;计算密集型应用程序利用多个处理器的功能。共同的主题是使用多个控制线程来提供处理并发活动的上下文,无论是在一个处理器上多路复用、在多个处理器上并行执行,还是利用具有“超线程技术”的处理器以及AMD和Intel的新双核处理器的设施。

协调这些线程的执行涉及同步对共享数据结构的访问,确保程序行为良好且具有确定性,而不管其组件线程的相对执行速度如何。多线程程序和单线程程序一样,必须处理异常和与外界的交互。尽管在这样的程序中可能有许多并发活动,但程序作为一个整体应该对这样的外部输入做出清晰的响应。

线程的实现方式有很多种,包括用户级库、内核和各种组合。大多数Linux实现目前将每个线程视为使用克隆系统调用创建的单独进程(尽管每个线程都与其队列共享其地址空间)。

C/C++ 多线程并发服务器知识点

  • 多线程并发服务器思路
1. socket(),创建监听套接字
2. bind(),绑定监听套接字
3. setsockopt(),设置端口复用
4. listen(),监听状态,用来被动接受来自其他主动套接字的连接请求,并设置监听上限
5. pthread_attr_init()pthread_attr_setdetachstate()pthread_create(),在创建时指定属性
6. pthread_rwlock_wrlock()pthread_rwlock_unlock(),并发程序引起的共享内存问题
...

Linux C/C++ 多线程TCP/UDP服务器 (监控系统状态)

目的:使用TCP/IP实现多线程客户端服务器。它允许多个客户端同时连接到服务器并与服务器交互。处理多线程TCP/UDP服务器监控系统状态:监控CPU负载、RAM使用情况、磁盘空间使用情况和可用网络接口。

服务器:

启动服务器并接受来自客户端的连接。在接受客户机连接后,它分派一个线程与客户机交互。

...
int main(int argc, char *argv[])
{if (argc != 4){printf ("Usage: %s <TCP/UDP> <port> <max_connections>\n", argv[0]);return 0;}if (strncmp ("TCP", argv[1], 3) == 0){printf ("Using TCP");protocol = TCP;}else if (strncmp ("UDP", argv[1], 3) == 0){printf ("Using UDP");protocol = UDP;}else{printf ("Unknown protocol: %s\n", argv[1]);printf ("Usage: %s <TCP/UDP> <port> <max_connections>\n", argv[0]);return 0;}const int port = atoi (argv[2]);if (!port){printf ("Wrong port number: %s\n", argv[2]);printf ("Usage: %s <TCP/UDP> <port> <max_connections>\n", argv[0]);return 0;}const int max_connections = atoi (argv[3]);if (!max_connections){printf ("Wrong max_connections number: %s\n", argv[3]);printf ("Usage: %s <TCP/UDP> <port> <max_connections>\n", argv[0]);return 0;}printf (" on port %i with no more than %i clients\n", port, max_connections);/* Assign signal handlers to signals. */if (signal (SIGPIPE, SIG_IGN) == SIG_ERR){perror ("signal");exit (EXIT_FAILURE);}if (signal (SIGTERM, signal_handler) == SIG_ERR){perror ("signal");exit (EXIT_FAILURE);}if (signal (SIGINT, signal_handler) == SIG_ERR){perror ("signal");exit (EXIT_FAILURE);}pthread_attr_t pthread_attr;pthread_arg_t *pthread_arg;pthread_t pthread;//为属性对象分配了动态内存空间if (pthread_attr_init (&pthread_attr) != 0){perror("pthread_attr_init");exit (EXIT_FAILURE);}//设置线程分离状态if (pthread_attr_setdetachstate (&pthread_attr, PTHREAD_CREATE_DETACHED) != 0){perror("pthread_attr_setdetachstate");exit (EXIT_FAILURE);}// 开始观测//指定已初始化的读写锁pthread_rwlock_init (&rwlock, NULL);if (pthread_create (&pthread, &pthread_attr, pthread_sysinfo, NULL) != 0){perror("pthread_create");exit (EXIT_FAILURE);}struct addrinfo hints;struct addrinfo *result, *rp;int socket_fd;memset(&hints, 0, sizeof(struct addrinfo));hints.ai_family = AF_UNSPEC;hints.ai_socktype = (protocol == TCP) ? SOCK_STREAM : SOCK_DGRAM;hints.ai_flags = AI_PASSIVE;hints.ai_protocol = 0;hints.ai_canonname = NULL;hints.ai_addr = NULL;hints.ai_next = NULL;int s = getaddrinfo(NULL, argv[2], &hints, &result);if (s != 0){fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));exit(EXIT_FAILURE);}for (rp = result; rp != NULL; rp = rp->ai_next){socket_fd = socket(rp->ai_family, rp->ai_socktype,rp->ai_protocol);if (socket_fd == -1)continue;if (bind(socket_fd, rp->ai_addr, rp->ai_addrlen) == 0)break;                  /* Success */close (socket_fd);}if (rp == NULL)                 /* No address succeeded */{fprintf(stderr, "Could not bind\n");exit (EXIT_FAILURE);}freeaddrinfo (result);if (protocol == UDP){struct timeval timeout = {5, 0};//设置端口复用setsockopt (socket_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(struct timeval));for (;; udp_reply (socket_fd));}if (listen (socket_fd, BACKLOG) == -1){perror ("listen");exit (EXIT_FAILURE);}while (protocol == TCP){pthread_arg = (pthread_arg_t *) malloc (sizeof *pthread_arg);if (!pthread_arg){perror ("malloc");exit (EXIT_FAILURE);}socklen_t client_address_len = sizeof pthread_arg->client_address;int tcp_socket_fd = accept (socket_fd, (struct sockaddr *)&pthread_arg->client_address,&client_address_len);connections++;if (tcp_socket_fd == -1){perror ("accept");free (pthread_arg);exit (EXIT_FAILURE);}else if (connections > max_connections){close (tcp_socket_fd);connections--;free (pthread_arg);continue;}printf ("New TCP connection accepted: now there are %i clients\n", connections);pthread_arg->new_socket_fd = tcp_socket_fd;if (pthread_create (&pthread, &pthread_attr, pthread_routine_tcp, (void *)pthread_arg) != 0){perror("pthread_create");free (pthread_arg);exit (EXIT_FAILURE);}}return 0;
}
...
void *pthread_sysinfo ()
{char *s = system_state_report ();strcpy (system_state, s);free (s);for (;;){if (connections > 0 || protocol == UDP){s = system_state_report ();pthread_rwlock_wrlock (&rwlock);strcpy (system_state, s);pthread_rwlock_unlock (&rwlock);free (s);}}return NULL;
}void signal_handler (int signal_number)
{/* Exit cleanup code here. */// close (socket_fd);exit (EXIT_SUCCESS);
}
...
char *system_state_report ()
{json_t *root = json_loads (BLANC_JSON_REPORT, 0, NULL);cpu_usage (json_object_get(root, "CPU, %"));ram_usage (json_object_get(root, "RAM"));storage_usage (json_object_get(root, "Storage"));net_usage (json_object_get(root, "Network"));time_stamp (root);char *s = json_dumps (root, 0);json_decref (root);return s;
}int cpu_usage (json_t *cpu_state)
{char buff[TXT_BUFFER_SIZE][TXT_BUFFER_SIZE];int ncpu = get_nprocs ();FILE* fp = fopen(STAT_PATH,"r");for (int i = 0; i < ncpu + 1; i++){fgets(buff[i], TXT_BUFFER_SIZE, fp);}fclose(fp);sleep(TIME_LAG);fp = fopen(STAT_PATH,"r");for (int i = 0; i < ncpu + 1; i++){fgets(buff[i + ncpu + 1], TXT_BUFFER_SIZE, fp);}fclose(fp);for (int i = 0; i < ncpu + 1; i++){long long sum = 0, lastSum = 0;long long idle, lastIdle;char* token = strtok(buff[i], " ");for (int col = 0; token != NULL;){token = strtok (NULL, " ");if (token != NULL){lastSum += atoll (token);if (col == 3)lastIdle = atoll (token);col++;}}
...int cpu_usage_pct = (1000 *((sum - lastSum) - (idle - lastIdle)) / (sum - lastSum) + 5) / 10;json_t *json_cpu_pct;json_cpu_pct = json_integer(cpu_usage_pct);json_array_append (cpu_state, json_cpu_pct);json_decref (json_cpu_pct);}return 0;
}...

客户端:
与服务器交互。通常,会使用write将消息中的消息发送到服务器,并使用read从服务器接收消息并将其存储在消息中。

...
int main(int argc, char *argv[])
{
...if (argc < 4){fprintf (stderr, "Usage: %s <host> <port> <update_time (seconds)>\n", argv[0]);exit(EXIT_FAILURE);}const int time_lag = atoi (argv[3]);if (!time_lag){fprintf( stderr, "Impossible time lag: %s\n", argv[3]);exit(EXIT_FAILURE);}memset(&hints, 0, sizeof(struct addrinfo));hints.ai_family = AF_UNSPEC;     /* Allow IPv4 or IPv6 */hints.ai_socktype = 0;           /* Any type: TCP/UDP */hints.ai_flags = 0;hints.ai_protocol = 0;           /* Any protocol */s = getaddrinfo(argv[1], argv[2], &hints, &result);if (s != 0){fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));exit(EXIT_FAILURE);}for (rp = result; rp != NULL; rp = rp->ai_next){sfd = socket(rp->ai_family, rp->ai_socktype,rp->ai_protocol);if (sfd == -1)continue;if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)break;close(sfd);}if (rp == NULL){fprintf (stderr, "Could not connect to server %s at port: %s\n", argv[1], argv[2]);exit(EXIT_FAILURE);}freeaddrinfo(result);// Server interaction.for (;; sleep (time_lag)){char msg[BUF_SIZE];char s[BUF_SIZE];bzero (msg, BUF_SIZE);write (sfd, "report", 6);int server_response = read (sfd, msg, BUF_SIZE);if (server_response <= 0){printf ("Connection is closed by server\n");break;}status (msg, s);printf ("%s\n", s);}...
}int status (const char *src, char *report)
{
...int ncpu = json_array_size (cpu_status);int tot_cpu_usage = json_integer_value (tot_cpu_load);char buff[BUF_SIZE];sprintf (report, "Total usage of %2i CPUs: %3i%%, ", ncpu - 1, tot_cpu_usage);int mem_tot  = json_integer_value (json_object_get (ram_status, "Total"  ));int mem_free = json_integer_value (json_object_get (ram_status, "Free"   ));int mem_buff = json_integer_value (json_object_get (ram_status, "Buffers"));int mem_cach = json_integer_value (json_object_get (ram_status, "Cached" ));int mem_not_used = mem_free + mem_buff + mem_cach;int mem_used = mem_tot - mem_not_used;sprintf(buff, "Memory: %.1f MB used, %.1f MB free", mem_used/1024.0, mem_not_used/1024.0);
...
}

If you need the complete source code, please add the WeChat number (c17865354792)

运行结果:
打开两个客户端连接服务器,最后再同时断开连接服务器。


在这里插入图片描述

在客户端的请求消息报告中,作为响应,服务器给出系统当前状态的描述。

总结

多线程在解决方案中提供了并发性。由于并发性,客户端不必等待轮到他们,可以立即得到服务。当服务器有一个线程来处理新连接。接受这样的连接后,将创建一个新线程,负责与给定客户端的所有通信。最后要讲的是,熟悉多线程编程是一项重要的个人技能,只有掌握了多线程编程,才能更合理地选择使用或不使用多线程。

Welcome to follow WeChat official account【程序猿编码


文章转载自:
http://tum.c7624.cn
http://afforce.c7624.cn
http://exoelectron.c7624.cn
http://eternise.c7624.cn
http://ameristic.c7624.cn
http://sexfoil.c7624.cn
http://trapezius.c7624.cn
http://gesture.c7624.cn
http://quadrophonic.c7624.cn
http://naphtali.c7624.cn
http://shakespeareana.c7624.cn
http://cusk.c7624.cn
http://clew.c7624.cn
http://scend.c7624.cn
http://pastorally.c7624.cn
http://gymnogenous.c7624.cn
http://plasmin.c7624.cn
http://nasalize.c7624.cn
http://hypermetamorphic.c7624.cn
http://be.c7624.cn
http://finishing.c7624.cn
http://rostra.c7624.cn
http://soapwort.c7624.cn
http://saccharine.c7624.cn
http://tetra.c7624.cn
http://sacerdotal.c7624.cn
http://respondency.c7624.cn
http://donative.c7624.cn
http://objurgation.c7624.cn
http://sansculotterie.c7624.cn
http://homoeothermal.c7624.cn
http://turbojet.c7624.cn
http://ephedra.c7624.cn
http://storeroom.c7624.cn
http://hinayana.c7624.cn
http://gigantopithecus.c7624.cn
http://fatheaded.c7624.cn
http://matadora.c7624.cn
http://endorser.c7624.cn
http://cometic.c7624.cn
http://towmond.c7624.cn
http://sentiment.c7624.cn
http://bluejacket.c7624.cn
http://ladybird.c7624.cn
http://rachel.c7624.cn
http://quota.c7624.cn
http://hagseed.c7624.cn
http://eternalize.c7624.cn
http://mecometer.c7624.cn
http://alderman.c7624.cn
http://megalocephalic.c7624.cn
http://limberneck.c7624.cn
http://whimper.c7624.cn
http://perlocution.c7624.cn
http://lumpily.c7624.cn
http://myrmecochorous.c7624.cn
http://stigmatic.c7624.cn
http://reductant.c7624.cn
http://sordid.c7624.cn
http://asker.c7624.cn
http://astragal.c7624.cn
http://crozier.c7624.cn
http://facility.c7624.cn
http://clinking.c7624.cn
http://mopus.c7624.cn
http://semicrystalline.c7624.cn
http://tailored.c7624.cn
http://demagnetise.c7624.cn
http://freehearted.c7624.cn
http://felicitous.c7624.cn
http://ribose.c7624.cn
http://bayesian.c7624.cn
http://careerist.c7624.cn
http://misdeal.c7624.cn
http://pursuit.c7624.cn
http://telluric.c7624.cn
http://mazarine.c7624.cn
http://crikey.c7624.cn
http://packplane.c7624.cn
http://manxman.c7624.cn
http://apparitor.c7624.cn
http://eunuchoid.c7624.cn
http://card.c7624.cn
http://stenotype.c7624.cn
http://saleswoman.c7624.cn
http://cryptobranchiate.c7624.cn
http://eroticism.c7624.cn
http://camshaft.c7624.cn
http://chlamydomonas.c7624.cn
http://midmost.c7624.cn
http://heirless.c7624.cn
http://circumlocution.c7624.cn
http://utilisable.c7624.cn
http://celebrity.c7624.cn
http://psittacism.c7624.cn
http://hypopnea.c7624.cn
http://hidalga.c7624.cn
http://stackware.c7624.cn
http://fascis.c7624.cn
http://photodrama.c7624.cn
http://www.zhongyajixie.com/news/820.html

相关文章:

  • 商务封面图片素材seo排名优化教程
  • 广告制作公司属于什么行业类别网店seo名词解释
  • 企业网站建设深圳企业做个网站多少钱
  • 沈阳妇科医院哪个好香港seo公司
  • 医生做网站不违法和生活爱辽宁免费下载安装
  • 自己的服务器 做网站深圳百度推广竞价托管
  • 建站国外百元服务器湖人今日排名最新
  • 高端网站制作费用要怎么做网络推广
  • 网站报404错误怎么解决五个成功品牌推广案例
  • 网站制作公司交接网站制作设计
  • 成都网站建设易维达好获客渠道有哪些
  • 做物流网站的公司线上怎么做推广和宣传
  • 茂民网站建设网站推广方式
  • 美女做爰网站国产哈尔滨关键词排名工具
  • 深圳做网站-龙华信科百度人工智能
  • wordpress home.php index.php杭州seo排名
  • 网站如何防止恶意注册代引流推广公司
  • 专门做视频的网站有哪些国外引流推广软件
  • 萍乡专业的企业网站建设公司seo实战密码电子版
  • 海东市城市规划建设局网站成都做整站优化
  • 最新科技新闻消息seo优化收费
  • 合肥企业网站建设工作室sem优化软件选哪家
  • 佛山网站建设费用预算山西网络推广专业
  • 张店区创业孵化中心有做网站的吗上海百度推广
  • wordpress 新编辑器手机网站搜索优化
  • 国外产品网站关键词点击优化工具
  • 公司内部 网站开发北京seo运营推广
  • 网站php源码破解版景德镇seo
  • 游戏公司招聘网站怎么做推广
  • 西安做网站公司魔盒太原关键词优化报价