#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <pthread.h> #include <semaphore.h> void * pthread_odd_function(void * arg); void * pthread_even_function(void * arg); pthread_mutex_t work_mutex; pthread_cond_t work_cond; #define MAX_COUNT 10 int count = 0; int main(int argc, char const *argv[]) { pthread_t pthread_odd; pthread_t pthread_even; pthread_attr_t pthread_attr; int res; res = pthread_attr_init(&pthread_attr);//init pthread attribute,step 1 if (res != 0){ perror("pthread_attr_init failed!"); exit(EXIT_FAILURE); } res = pthread_cond_init(&work_cond,NULL); if (res != 0){ perror("pthread_cond_init failed!"); exit(EXIT_FAILURE); } res = pthread_mutex_init(&work_mutex,NULL); if (res != 0){ perror("pthread_mutex_init failed!"); exit(EXIT_FAILURE); } pthread_attr_setdetachstate(&pthread_attr,PTHREAD_CREATE_DETACHED);//design pthread attribute step 2 res = pthread_create(&pthread_odd,&pthread_attr,pthread_odd_function,NULL);//step 3 if (res != 0){ perror("pthread_create failed!"); exit(EXIT_FAILURE); } res = pthread_create(&pthread_even,&pthread_attr,pthread_even_function,NULL); if (res != 0){ perror("pthread_create failed!"); exit(EXIT_FAILURE); } while(count < MAX_COUNT) ; //wait the two sons threads finished pthread_mutex_destroy(&work_mutex); pthread_cond_destroy(&work_cond); pthread_exit(NULL); return 0; } void * pthread_odd_function(void *arg)//step 4 { pthread_mutex_lock(&work_mutex); while(count < MAX_COUNT){ if (count % 2 == 1){ printf("the odd count is : %d\n", count); ++count; pthread_cond_signal(&work_cond);//in order to release the thread of even } else pthread_cond_wait(&work_cond,&work_mutex);//the pthread is blocked,wait for the condition } pthread_mutex_unlock(&work_mutex); } void * pthread_even_function(void *arg)//step 5 { pthread_mutex_lock(&work_mutex); while(count < MAX_COUNT){ if (count % 2 == 0){ printf("the even count is : %d\n", count); ++count; pthread_cond_signal(&work_cond);//in order to release the thread of odd } else pthread_cond_wait(&work_cond,&work_mutex);//wait the condition satisfied } pthread_mutex_unlock(&work_mutex); }给我讲一下这段代码
时间: 2023-06-19 20:06:16 浏览: 80
这段代码是一个线程同步的例子,它创建了两个线程,一个线程打印出所有奇数,另一个线程打印出所有偶数。具体来说,代码中使用了互斥锁和条件变量来保证线程同步。互斥锁的作用是保护共享资源,只允许一个线程访问该资源。条件变量用于线程之间的通信,当一个线程需要等待某个条件满足时,它会进入阻塞状态,等待其他线程发送信号通知它条件已满足。
在主函数中,先初始化了互斥锁和条件变量,然后创建了两个线程,并使用pthread_attr_setdetachstate函数将线程属性设置为PTHREAD_CREATE_DETACHED,表示线程被创建后马上就进入分离状态,不需要等待其他线程回收资源。接着主线程进入一个循环,等待两个子线程执行完毕。
在子线程中,先获取互斥锁,进入while循环,判断count是否小于最大值,如果是,则进入条件判断。奇数线程打印出奇数并将count加1,然后调用pthread_cond_signal函数发送信号通知偶数线程可以执行了。偶数线程同样进入条件判断,打印出偶数,将count加1,然后调用pthread_cond_signal函数通知奇数线程可以执行了。如果条件不满足,则调用pthread_cond_wait函数将线程阻塞,等待其他线程发送信号通知它条件已满足。最后释放互斥锁,线程结束。
值得注意的是,条件变量的使用必须与互斥锁一起使用,否则可能会出现竞争条件,导致程序出现错误。
相关问题
虚拟机centos没有那个文件或目录 #include <winsock2.h>该怎么办
`#include <winsock2.h>` 是 Windows 平台的头文件,如果你在 CentOS 虚拟机上编译程序,应该使用 Linux 平台的头文件。具体来说,你需要包含 `<sys/socket.h>`、`<netinet/in.h>` 和 `<arpa/inet.h>` 这三个头文件来使用 Linux 平台的套接字 API。
另外,Linux 平台的套接字 API 在使用时需要链接 `libsocket` 库(或者 `libws2_32` 库,如果你在 Windows 平台上编译程序的话需要链接 `ws2_32.lib` 库)。你可以在编译时加上 `-lsocket` 参数来链接 `libsocket` 库,例如:
```
gcc -o server server.c -lsocket
```
以下是一个在 CentOS 上使用 Linux 平台的套接字 API 实现的服务器示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // 创建套接字
if (server_socket < 0) {
perror("socket");
return 1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(8000);
int iResult = bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)); // 绑定套接字
if (iResult < 0) {
perror("bind");
close(server_socket);
return 1;
}
iResult = listen(server_socket, SOMAXCONN); // 监听套接字
if (iResult < 0) {
perror("listen");
close(server_socket);
return 1;
}
printf("服务器已启动,等待连接...\n");
int counter = 0;
while (1) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len); // 接受客户端连接
if (client_socket < 0) {
perror("accept");
close(server_socket);
return 1;
}
counter++; // 每当有一个新的客户端连接时,计数器加1
printf("客户端 %s:%d 已连接,是今天的第 %d 个客户端。\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), counter);
char reply[1024];
sprintf(reply, "你是今天第%d个客户端", counter);
send(client_socket, reply, strlen(reply), 0); // 发送回复消息
close(client_socket);
}
close(server_socket);
return 0;
}
```
在 Linux 平台上,我们使用了 `socklen_t` 类型来表示套接字地址结构体的大小。另外,我们使用了 `close()` 函数来关闭套接字连接。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h>分别有什么作用
这些引用中包含了C语言的头文件,这些头文件中定义了一些函数和变量,可以在程序中使用。具体来说:
- #include <stdio.h>:定义了输入输出函数,例如printf和scanf。
- #include <stdlib.h>:定义了一些通用的函数,例如malloc和free,用于动态内存分配和释放。
- #include <string.h>:定义了一些字符串处理函数,例如strcpy和strcat。
- #include <unistd.h>:定义了一些系统调用函数,例如fork和execve。
- #include <sys/socket.h>:定义了一些网络编程相关的函数和数据结构,例如socket和sockaddr。
- #include <arpa/inet.h>:定义了一些网络字节序和主机字节序之间的转换函数,例如htonl和ntohs。
这些头文件中的函数和变量可以帮助程序员更方便地编写程序,提高程序的可读性和可维护性。