gcc 1.c -o 1 -lpthread
ctrl+f搜索服务端代码和客户端代码获取代码
该方法也可以实现并发服务器
服务端代码
#include <sys/mman.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int asda =5;
int tickets =100;
pthread_mutex_t mutex;
pthread_mutex_t mutex1;
pthread_mutex_t mutex2;
void *sig_handler(void* arg) {
while(1){
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&mutex);
if(tickets>0){
printf("子线程%ld正在卖地%d张哦票\n",pthread_self(),100-tickets+1);
tickets--;
}else {pthread_mutex_unlock(&mutex);break;}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
pthread_mutex_t asdasdasdasdasdas;
int numm =1;
void *sig_handler1(void* arg) {
while(1){
pthread_mutex_lock(&asdasdasdasdasdas);
numm++;
printf("++谢谢,tid:%ld,nummm:%d\n",pthread_self(),numm);
pthread_mutex_unlock(&asdasdasdasdasdas);
usleep(1);
}
return NULL;
}
void *sig_handler12232(void* arg) {
while(1){
pthread_mutex_lock(&asdasdasdasdasdas);
printf("++读读,tid:%ld,nummm:%d\n",pthread_self(),numm);
pthread_mutex_unlock(&asdasdasdasdasdas);
usleep(1);
}
return NULL;
}
#include<sys/time.h>
#include<time.h>
void sig_handler2(int num){
time_t tm2 = time(NULL);
struct tm* loc = localtime(&tm2);
char* str =asctime(loc);
int fd =open("time.txt",O_RDWR|O_CREAT|O_APPEND,0664);
write(fd,str,strlen(str));
close(fd);
}
#include<sys/shm.h>
pthread_mutex_t mutext;
pthread_cond_t cond;
#include <semaphore.h>
sem_t psem;
sem_t csem;
struct Node {
int num;
struct Node* next;
};
struct Node* head = NULL;
void* producer (void* arg) {
while(1) {
sem_wait(&psem);
pthread_mutex_lock(&mutex);
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->next = head;
head = new_node;
new_node->num = rand() % 1000;
printf("add node, num %d, tid: %ld\n", new_node->num, pthread_self());
pthread_mutex_unlock(&mutex);
sem_post(&csem);
}
return NULL;
}
void* consumer (void* arg) {
while(1) {
sem_wait(&csem);
pthread_mutex_lock(&mutex);
struct Node* tmp = head;
head = head->next;
printf("delete node, num: %d, tid: %ld\n", tmp->num, pthread_self());
free(tmp);
pthread_mutex_unlock(&mutex);
sem_post(&psem);
}
return NULL;
}
#include<arpa/inet.h>
#include<bits/socket.h>
void recyle_child(int arg) {
while(1) {
int ret = waitpid(-1, NULL, WNOHANG);
if (-1 == ret) {
printf("所有的子进程回收完毕\n");
break;
} else if (ret == 0) {
break;
} else if (ret > 0) {
printf("子进程%d被回收了\n", ret);
}
}
}
#include<errno.h>
#include<ctype.h>
#include<ctype.h>
#include<ctype.h>
#include<sys/select.h>
#include<poll.h>
#include<sys/epoll.h>
int main()
{
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == lfd) {
perror("socket");
exit(-1);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(9999);
int ret = bind(lfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (-1 == ret) {
perror("bind");
exit(-1);
}
ret = listen(lfd, 128);
if (-1 == ret) {
perror("listen");
exit(-1);
}
int epfd = epoll_create(100);
struct epoll_event epev;
epev.events = EPOLLIN;
epev.data.fd = lfd;
epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &epev);
struct epoll_event epevs[1024] = {0};
while(1){
int trett =epoll_wait(epfd, epevs,1024,-1);
if (-1 ==trett) {
perror("epoll_wait");
exit(-1);
}
printf("epoll_wait函数的返回值 ret = %d\n", trett);
for (int i = 0; i < trett; i++)
{
int curfd = epevs[i].data.fd;
if(curfd==lfd) {
struct sockaddr_in client_addr;
int len = sizeof(client_addr);
int cfd = accept(lfd, (struct sockaddr*)&client_addr, &len);
printf("新的客户端进来了\n");
char client_ip[16];
inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ip, sizeof(client_ip));
unsigned short client_port = ntohs(client_addr.sin_port);
printf("客户端IP:%s\n端口:%d\n", client_ip, client_port);
if (-1 == cfd) {
if (EINTR == errno ) {
continue;
}
perror("accept");
exit(-1);
}
epev.events=EPOLLIN;
epev.data.fd=cfd;
epoll_ctl(epfd, EPOLL_CTL_ADD,cfd,&epev);
} else {
char buf[1024] = {0};
int len = read(curfd, buf, sizeof(buf));
if (-1 == len) {
perror("read");
exit(-1);
} else if (0 == len) {
printf("客户端断开连接了\n");
epoll_ctl(epfd, EPOLL_CTL_DEL,curfd,NULL);
close(curfd);
} else if (len > 0) {
printf("读取到数据:%s\n", buf);
write(curfd, buf, strlen(buf)+1);
}
}
}
}
close(lfd);
close(epfd);
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)