多进程/多线程
多进程服务器
步骤
服务器使用父进程 fork 创建子进程来和客户端进行通信,父进程负责取出连接请求。并且父进程接收子进程退出信号,通过信号处理函数回收子进程
步骤:
1.首先屏蔽子进程退出信号
2.使用socket函数,获取一个socket文件描述符
3.使用setsockopt端口复用
4.使用bind函数允许客户端的哪些ip可以访问服务器
5.使用listen监听客户端连接
6.使用accept从已连接的客户端队列中取出一个文件描述符,与它通信
7.使用fork函数创建一个子进程去与上面的文件描述符通信
代码
#include "socketwrap.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <arpa/inet.h>
// 信号处理函数
void waitchild(int signo)
{
pid_t wpid;
while (1)
{
wpid = waitpid(-1, NULL, WNOHANG);
if (wpid > 0)
{
printf("child exit, wpid==[%d]\n", wpid);
}
else if (wpid == 0 || wpid == -1)
{
break;
}
}
}
int main()
{
// 阻塞SIGCHLD信号
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, NULL);
int sigbol = 1;
int sfd = Socket(AF_INET, SOCK_STREAM, 0);
// 设置端口复用
int opt = 1;
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
struct sockaddr_in soaddr;
bzero(&soaddr, sizeof(soaddr));
soaddr.sin_family = AF_INET;
soaddr.sin_port = htons(9999);
soaddr.sin_addr.s_addr = htonl(INADDR_ANY);
Bind(sfd, (struct sockaddr *)&soaddr, sizeof(soaddr));
//监听-listen
Listen(sfd, 128);
struct sockaddr_in clientsocket;
socklen_t clilen;
char sIP[16];
while (1)
{
clilen = sizeof(clientsocket);
bzero(&clientsocket, clilen);
int cfd = Accept(sfd, (struct sockaddr *)&clientsocket, &clilen);
/* */
int pid = fork();
if (pid == 0)
{
// 子进程
close(sfd);
char buff[64];
printf("current pid is [%d],father is [%d]\n", getpid(), getppid());
while (1)
{
memset(buff, 0x00, sizeof(buff));
int n = Read(cfd, buff, sizeof(buff));
if (n == 0)
{
return 0;
}
else if (n < 0)
{
perror("child read error");
return -1;
}
printf("child [%d] recv data from [%s:%d]:[%s]\n", getpid(), inet_ntop(AF_INET, &clientsocket.sin_addr.s_addr, sIP, sizeof(sIP)), ntohs(clientsocket.sin_port), buff);
for (int i = 0; i < n; i++)
{
buff[i] = toupper(buff[i]);
}
n = Write(cfd, buff, n);
if (n <= 0)
{
perror("child write error");
return -1;
}
}
}
else if (pid > 0)
{
// 父进程
close(cfd);
if (sigbol == 1)
{
sigbol = 0;
// 注册SIGCHLD信号处理函数
struct sigaction act;
act.sa_handler = waitchild;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGCHLD, &act, NULL);
// 解除对SIGCHLD信号的阻塞
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
continue;
}
else
{
perror("fork error");
close(sfd);
return -1;
}
}
return 0;
}
多线程
线程是轻量级的线程(LWP:light weight process)
线程是最小执行单位,进程是最小分配资源单位。一个进程可以有多个线程,一个进程可以理解为只有一个线程的进程。
每个线程都有自己独立的pcb,它们有独立的线程id和线程号,线程id是程序员使用,线程号是系统使用,同一个进程的线程,它们的进程id是一样的,共享进程空间。
可以使用命令 ps -Lf pid
来查看线程号。
不管是fork创建子进程还是pthread_create 创建线程,底层实现都是调用同一个内核函数clone。区别就是复制原始进程的地址空间,那么就会创建一个子进程。如果共享原始进程的地址空间,那么就会创建一个线程。系统内核是不区分进程和线程的,因为它们都有自己独立的pcb,只有在用户层面的概念上区分。因此可以看出线程相关的 pthread_ 系列函数是库函数而不是系统调用。
一、线程创建和回收
循环创建线程
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void *threadMethod(void *num)
{
printf("this thread num is [%d], pid is [%d],tid is [%ld]\n", *(int *)num, getpid(), pthread_self());
sleep(30);
}
int main()
{
int arr[5];
pthread_t thread[5];
int i;
int ret;
for (i = 0; i < 5; i++)
{
arr[i] = i;
ret = pthread_create(&thread[i], NULL, threadMethod, &arr[i]);
if (ret != 0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
}
printf("main thread, pid is [%d], tid is [%ld]\n", getpid(), pthread_self());
sleep(30);
return 0;
}
多线程使用pthread_exit退出线程,并用pthread_join接收退出状态
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
int * nums;
void* thread_function(void* arg) {
// 线程执行的代码
int thread_id = *(int *)arg;
nums[thread_id] = thread_id + 5;
printf("Thread %d exiting,pid is [%d],tid is [%ld]\n", thread_id,getpid(),pthread_self());
pthread_exit(&nums[thread_id]);
}
int main() {
nums = (int *)malloc(sizeof(int)*5);
pthread_t threads[5];
int indexes[5] = {1, 2, 3, 4, 5}; // 线程索引,用于打印
for(int i = 0; i < 5; i++) {
int ret = pthread_create(&threads[i], NULL, thread_function, &indexes[i]);
if (ret) {
printf("Thread creation error: %d\n", ret);
return -1;
}
}
void* exit_status;
for(int i = 0; i < 5; i++) {
int ret = pthread_join(threads[i], &exit_status);
if (ret) {
printf("Thread join error: %d\n", ret);
return -1;
}
printf("Thread %d exited with status: %d\n", i, *(int *)exit_status);
}
return 0;
}
//输出
Thread 1 exiting,pid is [1913],tid is [140703346046720]
Thread 2 exiting,pid is [1913],tid is [140703337654016]
Thread 3 exiting,pid is [1913],tid is [140703329261312]
Thread 4 exiting,pid is [1913],tid is [140703320868608]
Thread 5 exiting,pid is [1913],tid is [140703312475904]
Thread 0 exited with status: 6
Thread 1 exited with status: 7
Thread 2 exited with status: 8
Thread 3 exited with status: 9
Thread 4 exited with status: 10
二、线程属性
pthread_create函数中的线程属性参数设置步骤
//1.定义线程属性变量
pthread_attr_t attr;
//2.线程属性初始化,成功返回0,失败返回失败编号
int pthread_attr_init (pthread_attr_t* attr);
//3.线程属性变量添加分离属性,其中detachstate可以设置为
//PTHREAD_CREATE_DETACHED(分离)和PTHREAD_CREATE_JOINABLE(非分离)
//成功返回0,失败返回失败编号
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
//4.创建线程后释放变量,成功返回0,失败返回失败编号
int pthread_attr_destroy(pthread_attr_t *attr);
三、线程分离
使用pthread_detach函数实现线程分离,线程的退出后不需要手动去回收资源,设置线程分离有两种方法,一个是使用pthread_detach函数,另一个是在pthread_create函数中添加分离属性。
方式一:使用pthread_detach函数实现线程分离
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void *threadMethod(void *num)
{
printf("this thread pid is [%d],tid is [%ld]\n", getpid(), pthread_self());
}
int main()
{
pthread_t thread;
int ret = pthread_create(&thread, NULL, threadMethod, NULL);
if (ret != 0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
ret = pthread_detach(thread);
if (ret != 0)
{
printf("pthread_detach error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid is [%d], tid is [%ld]\n", getpid(), pthread_self());
sleep(1);
return 0;
}
方式二:pthread_create函数中添加分离属性
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
void *threadMethod(void *num)
{
printf("this thread pid is [%d],tid is [%ld]\n", getpid(), pthread_self());
}
int main()
{
int ret;
pthread_attr_t attr;
ret = pthread_attr_init(&attr);
if (ret != 0)
{
printf("pthread_attr_init error, [%s]\n", strerror(ret));
return -1;
}
ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if (ret != 0)
{
printf("pthread_attr_setdetachstate error, [%s]\n", strerror(ret));
return -1;
}
pthread_t thread;
ret = pthread_create(&thread, &attr, threadMethod, NULL);
pthread_attr_destroy(&attr);
if (ret != 0)
{
printf("pthread_create error, [%s]\n", strerror(ret));
return -1;
}
printf("main thread, pid is [%d], tid is [%ld]\n", getpid(), pthread_self());
sleep(1);
return 0;
}
//输出
main thread, pid is [2063], tid is [139936574068480]
this thread pid is [2063],tid is [139936565679872]
标签:include,attr,int,ret,C语言,线程,pthread,进程,多线程
From: https://blog.csdn.net/qq_44653106/article/details/144807348