#include<stdio.h> #include<pthread.h> #include<unistd.h> int tickets = 15; pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; void * sell_tickets1(void *arg){ while (1) { pthread_mutex_lock(&mutex1); if(tickets > 0) { printf("W1: %d tickets are left\n",tickets--); } else { pthread_mutex_unlock(&mutex1); break; } pthread_mutex_unlock(&mutex1); sleep(1); } printf("W1: 10 tickets were sold from window 1\n"); return NULL; } void * sell_tickets2(void *arg){ while (1) { pthread_mutex_lock(&mutex1); if(tickets > 0) { printf("W2: %d tickets are left\n",tickets--); } else { pthread_mutex_unlock(&mutex1); break; } pthread_mutex_unlock(&mutex1); sleep(2); } printf("W2: 5 tickets were sold from window 2\n"); return NULL; } int main() { pthread_t t1,t2; pthread_create(&t1, NULL, sell_tickets1, NULL); pthread_create(&t2, NULL, sell_tickets2, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; }
时间: 2024-01-15 19:03:14 浏览: 66
这段代码是一个经典的多线程编程例子,用于模拟两个售票窗口售卖15张票的过程。主要涉及到线程的创建、互斥锁的使用等。
在该程序中,tickets变量表示剩余票数,sell_tickets1和sell_tickets2是两个线程函数,分别表示两个售票窗口的售票过程。互斥锁mutex1用于保证线程间对tickets变量的访问不会出现竞争条件。具体实现中,每个线程在访问tickets变量之前都会先对mutex1进行加锁,然后进行售票操作,最后再释放锁。当剩余票数为0时,线程退出循环,释放锁。
该程序中使用了pthread库中的相关函数,如pthread_mutex_lock、pthread_mutex_unlock、pthread_create、pthread_join等。
需要注意的是,该程序在两个线程中分别使用了不同的睡眠时间,即sell_tickets1中睡眠1秒,sell_tickets2中睡眠2秒。这是为了模拟不同售票窗口的售票速度不同,同时也能更好地展示多线程编程带来的效果。
相关问题
#include "crc.h"#include <stdio.h>
这段代码包含了两个头文件:`crc.h` 和 `<stdio.h>`。`crc.h` 通常用于计算循环冗余校验(CRC)值,而 `<stdio.h>` 提供了标准输入/输出功能,如文件操作和基本的 I/O 流控制。
`#include "crc.h"` 引入了一个名为 `crc.h` 的库,可能包含了一些函数来计算 CRC 值,例如计算文件、数据块或字符串的校验和。
`#include <stdio.h>` 包含了标准输入输出函数,例如 `printf()` 和 `scanf()`,用于处理文本输入输出。
下面是一个简单的使用这两个头文件的示例,假设`crc.h`中有`crc32()`函数来计算CRC32校验值:
```c
#include "crc.h"
#include <stdio.h>
int main() {
char data[] = "Hello, World!";
uint32_t crc = crc32(0, data, strlen(data)); // 初始化为0,计算data的CRC值
printf("CRC-32 of the string: %u\n", crc); // 输出CRC值
return 0;
}
```
在这个例子中,我们计算了字符串 "Hello, World!" 的CRC-32值并打印出来。
#include <unistd .h>
`#include <unistd.h>`是C语言中的一个预处理指令,用于包含`unistd.h`头文件。`unistd.h`是一个标准C库头文件,提供了许多与操作系统交互的函数和符号常量。
这个头文件提供了许多与系统调用相关的函数,例如`fork()`、`exec()`和`pipe()`等。它还定义了一些常量,如文件描述符的标准输入、标准输出和标准错误的常量值。
以下是一个使用`unistd.h`头文件的简单示例,展示了如何使用`fork()`函数创建一个子进程:
```c
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
// 子进程
printf("Hello from child process!\n");
} else {
// 父进程
printf("Hello from parent process!\n");
}
return 0;
}
```
这个程序会创建一个子进程,并在子进程和父进程中分别输出不同的消息。`fork()`函数会返回两次,一次在父进程中返回子进程的ID,一次在子进程中返回0。通过判断返回值,可以在父进程和子进程中执行不同的代码。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)