题目
Imagine that you need to transfer N people from one riverbank to another using a boat that holds a maximum of 3 people. Write a program that would model this situation. Each person should be represented as a thread(use Pthread library)
and the boat as a shared resource. You must use semaphores for synchronization.
Each person/thread needse to board the boat, cross the river and leave the boat.
For the sake of simplicity, we can neglect the time needed for boarding and leavingthe boat as well as the time that an empty boat needs to return after transferring people. In order to model river crossing please use the sleep function for about 3-5
seconds. Extensive program output when a person/thread boards the boat, crossesriver and leaves the boat is required. The number of people to transfer(N) is a command line argument.
For this question please submit the C language code file along with the Makefile used to compile the program and anything else necessary to run it.
Please identify the student name and student id in the first line of the code file.
(as a comment) Comments in the code explaining the program flow are necessary.
In addition, please attach screenshots of the code execution for at least 3
values of N.
All the code must work on the Linux operating system. Here is a useful resource on threads programming in Linux: Pthread library.
译文:想象一下,你需要用一艘最多能容纳3人的船把N个人从一个河岸转移到另一个河岸。编写一个程序来模拟这种情况。每个人都应该表示为一个线程(使用Pthread库和船作为共享资源。您必须使用信号量进行同步,每个人/线程都需要登船、过河和离开船。为了简单起见,我们可以忽略登船和离船所需的时间,以及空船在转移人员后需要返回的时间。为了模拟过河,请使用睡眠功能约3-5秒。当一个人/线程登上船,穿过河和离开船时,需要广泛的程序输出。要转移的人数(N)是一个命令行参数。对于这个问题,请提交C语言代码文件,以及用来编译程序的Makefile和运行它所需的任何其他文件请在代码文件的第一行注明学生姓名及学号。(作为注释)代码中解释程序的注释是必要的。另外,请附上至少3个N值的代码执行截图。所有代码都必须在Linux操作系统上工作。下面是一个关于Linux线程编程的有用资源:Pthread库。
代码
C文件
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <semaphore.h>
#include <unistd.h>
#define MAXX 1001
int num = 0,num_crossed=0;
pthread_t Thread_boat;
sem_t sem[MAXX];
pthread_t person[MAXX];
void* person_func(void* arg) {
int* ii = (int*)arg;
int no=*ii;
free(ii);
sem_wait(&sem[no]);
printf("NO.%d has reached the other side.\r\n", no+1);
return ((void*)0);
}
int main(int argc, char* argv[]) {
if (argc!=2) {
printf("The number of arguments is wrong!\r\n");
return -1;
}
for (int i = 0; i < strlen(argv[1]); i++) {
//To get the number
num *= 10;
num += (argv[1][i] - '0');
}
printf("There are %d people to cross the river.\r\n",num);
for (int i = 0; i < num; i++) {
//Create one thread per person
int* ii=calloc(1,sizeof(int));
*ii=i;
pthread_create(&person[i], NULL, person_func, (void*)ii);
sem_init(&sem[i], 0, 0);
}
while (num_crossed < num) {
int idx = 1;
for (idx; idx <= 3 && idx + num_crossed <= num;) {
printf("NO.%d boarded the boat.\r\n", num_crossed + idx);
idx++;
}
sleep(2);
idx-=1;
for (int i = 0; i < idx; i++) {
sem_post(&sem[i+ num_crossed]);
}
sleep(2);
num_crossed += idx;
}
sleep(2);
printf("The end!\r\n");
return 0;
}
makefile
boat : boat.o #boat 就是我们要生成的目标
# boat.o 是生成此目标的先决条件
gcc -o boat main.o -lpthread
boat.o : main.c
gcc -c main.c
clean :
rm boat.o