免费网站怎么注册,手机企业管理app软件,无代码编程的应用场景,怎样设计个人网站1.思维导图
2.有一个隧道#xff0c;长1000m#xff0c;有一辆高铁#xff0c;每秒100米#xff1b;有一辆快车#xff0c;每秒50米#xff1b;要求模拟这两列火车通过隧道的场景。
1程序代码#xff1a;
#include stdio.h
#include string.h
#i…1.思维导图
2.有一个隧道长1000m有一辆高铁每秒100米有一辆快车每秒50米要求模拟这两列火车通过隧道的场景。
1程序代码
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
#include wait.h
#include signal.h
#include sys/socket.h
#include arpa/inet.h
#include sys/socket.h
#include sys/ipc.h
#include sys/sem.h
#include semaphore.h
#include sys/msg.h
#include sys/shm.h
#include sys/un.htypedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;#define LENGTH 1000 //隧道的长度typedef struct
{const char *name;int speed;int position;
}Train;void *simulate_train(void *arg)
{Train *train (Train *)arg;while (train-position LENGTH) {train-position train-speed; // 火车前进if (train-position LENGTH)train-position LENGTH; // 防止超出隧道长度printf(%s正在 %d 米处\n, train-name, train-position);sleep(1); }printf(%s已出隧道\n, train-name);return NULL;
}
int main(int argc, const char *argv[])
{pthread_t high_speed_thread, fast_train_thread;// 初始化两列火车Train high_speed_train {高铁, 100, 0};Train fast_train {快车, 50, 0};// 创建线程模拟火车运行pthread_create(high_speed_thread, NULL, simulate_train, high_speed_train);pthread_create(fast_train_thread, NULL, simulate_train, fast_train);// 等待两列火车完成运行pthread_join(high_speed_thread, NULL);pthread_join(fast_train_thread, NULL);printf(两列火车都已出隧道\n);return 0;
}2运行效果 3.有一条隧道长1000m有一辆高铁每秒100米有一辆快车每秒50米有一辆慢车每秒25米模拟它们通过隧道的场景要求高铁最先过隧道快车其次慢车最后。
1程序代码
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
#include wait.h
#include signal.h
#include sys/socket.h
#include arpa/inet.h
#include sys/socket.h
#include sys/ipc.h
#include sys/sem.h
#include semaphore.h
#include sys/msg.h
#include sys/shm.h
#include sys/un.htypedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;#define LENGTH 1000 // 隧道长度typedef struct
{const char *name; int speed; int position; int order; //火车顺序
} Train;pthread_mutex_t mutex;
pthread_cond_t cond;
int current_order 1; // 当前允许进入隧道的火车顺序void *simulate_train(void *arg)
{Train *train (Train *)arg;pthread_mutex_lock(mutex);while (train-order ! current_order) {pthread_cond_wait(cond, mutex);}pthread_mutex_unlock(mutex);while (train-position LENGTH) {train-position train-speed; // 火车前进if (train-position LENGTH) {train-position LENGTH; // 防止超出隧道长度}printf(%s正在%d米处\n, train-name, train-position);sleep(1); // 模拟每秒前进}printf(%s已出隧道\n, train-name);pthread_mutex_lock(mutex);current_order;pthread_cond_broadcast(cond);pthread_mutex_unlock(mutex);return NULL;
}int main(int argc, const char *argv[])
{pthread_t id1,id2,id3;// 初始化三列火车Train high_speed_train {高铁, 100, 0, 1};Train fast_train {快车, 50, 0, 2};Train slow_train {慢车, 25, 0, 3};// 创建线程模拟火车运行pthread_create(id1, NULL, simulate_train, high_speed_train);pthread_create(id2, NULL, simulate_train, fast_train);pthread_create(id3, NULL, simulate_train, slow_train);// 等待所有线程完成pthread_join(id1, NULL);pthread_join(id2, NULL);pthread_join(id3, NULL);printf(所有火车都已出隧道\n);return 0;
}2运行效果 4.使用条件变量实现一个生产消费模型pv模型。生产者线程每秒生成2个苹果消费者线程每3秒消费5~9个苹果。
1程序代码
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include pthread.h
#include semaphore.h
#include wait.h
#include signal.h
#include sys/socket.h
#include arpa/inet.h
#include sys/socket.h
#include sys/ipc.h
#include sys/sem.h
#include semaphore.h
#include sys/msg.h
#include sys/shm.h
#include sys/un.htypedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;#define MAX_APPLES 100 //定义仓库最大容量
int apple_count 0; //当前苹果的数量//使用条件变量
pthread_mutex_t mutex;
pthread_cond_t cond;//生产者线程
void* producer(void* arg)
{while(1){pthread_mutex_lock(mutex);//生产苹果if(apple_count2 MAX_APPLES){apple_count 2;printf(生产者生产2个苹果目前苹果总数为:%d\n,apple_count);}else{printf(仓库已满生产者等待生产中...\n);}//唤醒消费者线程pthread_cond_signal(cond);pthread_mutex_unlock(mutex);sleep(1);}return NULL;
}//消费者线程
void* consumer(void* arg)
{while(1){pthread_mutex_lock(mutex);int consumption rand() % 5 5; //随机消费5~9个苹果while(apple_count consumption){printf(消费者想要消费 %d 个苹果但是只剩 %d 个苹果等待...\n,consumption,apple_count);pthread_cond_wait(cond,mutex); //等待生产者生产}//消费苹果apple_count - consumption;printf(消费者消费 %d 个苹果剩余苹果 %d个\n,consumption,apple_count);pthread_mutex_unlock(mutex);sleep(3);}return NULL;
}int main(int argc, const char *argv[])
{pthread_t id_producer,id_consumer;pthread_create(id_producer,0,producer,0);pthread_create(id_consumer,0,consumer,0);pthread_join(id_producer,NULL);pthread_join(id_consumer,NULL);return 0;
}
2运行效果