重点难点就是内存的管理,我们在全局作用域初始化了一个子线程参数数组(有点线程池的味道了),以避免子线程回调函数的参数指向的内存被回收 子线程数组的大小就是我们的并发服务器能够支持的最高并发量,如果超过了,就让客户端进行等待,直到线程数组出现空闲的子线程 其实我们一直都在创建子线程,但是通过子线程回调函数的参数数组将子线程的数量限制在了指定的范围内
gcc 1.c -o 1 -lpthread
#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>
int main()
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == fd) {
perror("socket");
exit(-1);
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr.s_addr);
server_addr.sin_port = htons(9999);
int ret = connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (-1 == ret) {
perror("connect");
exit(-1);
}
char* data = "你好,我是客户端!";
char recv_buf[1024];
int i = 0;
while(1) {
sprintf(recv_buf, "data %d\n", i++);
write(fd, recv_buf, strlen(recv_buf)+1);
sleep(1);
int len = read(fd, recv_buf, sizeof(recv_buf));
if (-1 == len) {
perror("read");
exit(-1);
} else if (len > 0) {
printf("收到服务器端的消息:%s\n", recv_buf);
} else if (0 == len) {
printf("服务器关闭\n");
break;
}
sleep(1);
}
close(fd);
return 0;
}
|