1:思维导图
2:有一个隧道,长1000m,有一辆高铁,每秒100米,有一辆快车,每秒50m,要求模拟这两列火车通过隧道的场景
#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.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_mutex_t m;
int task1()
{pthread_mutex_lock(&m);printf("高铁过去\n");for(int i=0;i<1000/100;i++){printf("-");sleep(1);fflush(stdout);}putchar(10);pthread_mutex_unlock(&m);
}
int task2()
{pthread_mutex_lock(&m);printf("快车过去\n");for(int i=0;i<1000/50;i++){printf("-");sleep(1);fflush(stdout);}putchar(10);pthread_mutex_unlock(&m);
}
void* thread_main(void* arg)
{task2();
}
int main(int argc, const char *argv[])
{pthread_mutex_init(&m,NULL);pthread_t id;pthread_create(&id,0,thread_main,0);task1();pthread_join(id,NULL);return 0;
}
3:有一个隧道,长1000m,有一辆高铁,每秒100米,有一辆快车,每秒50m,有一辆慢车每秒25m,要求模拟这两列火车通过隧道的场景,但是要求高铁最先过隧道,快车其次,慢车最后
#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.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_mutex_t H;
pthread_mutex_t K;
pthread_mutex_t M;
int task1()
{while(1){pthread_mutex_lock(&H);sleep(10);printf("高铁过去\n");pthread_mutex_unlock(&K);}
}
int task2()
{while(1){pthread_mutex_lock(&K);sleep(20);printf("快车过去\n");pthread_mutex_unlock(&M);}
}
int task3()
{while(1){pthread_mutex_lock(&M);sleep(40);printf("慢车过去\n");pthread_mutex_unlock(&H);}
}
void* thread_main_1(void* arg)
{task2();
}
void* thread_main_2(void* arg)
{task3();
}
int main(int argc, const char *argv[])
{pthread_mutex_init(&H,NULL);pthread_mutex_init(&K,NULL);pthread_mutex_init(&M,NULL);pthread_mutex_lock(&K);pthread_mutex_lock(&M);pthread_t id1,id2;pthread_create(&id1,0,thread_main_1,0);pthread_create(&id2,0,thread_main_2,0);task1();pthread_join(id1,NULL);pthread_join(id2,NULL);return 0;
}
4:使用条件变量实现一个生产者消费者模型(pv)模型
生产者线程:每秒生成2个苹果
消费者线程:没3秒消费 5~9个苹果
要求消费者在消费之前一定要有足够的苹果给消费
#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.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_cond_t cond1;
pthread_cond_t cond2;
pthread_mutex_t m;
int nump=0,numv=0;
int task1()
{while(1){pthread_mutex_lock(&m);pthread_cond_wait(&cond2,&m);while(nump-numv<0){nump+=2;sleep(1);}pthread_cond_signal(&cond1);pthread_mutex_unlock(&m);}
}
int task2()
{while(1){pthread_mutex_lock(&m);srand(time(0));sleep(3);numv=rand()%5+5;pthread_cond_signal(&cond2);pthread_cond_wait(&cond1,&m);printf("生产者共有%d个苹果\n",nump);printf("-----------------------------------------\n");nump-=numv;printf("消费者消费%d个苹果,生产者还剩%d个苹果\n",numv,nump);printf("-----------------------------------------\n");pthread_mutex_unlock(&m);}
}
void* task_main(void* arg)
{task2();
}
int main(int argc, const char *argv[])
{pthread_cond_init(&cond1,NULL);pthread_cond_init(&cond2,NULL);pthread_mutex_init(&m,NULL);pthread_t id;pthread_create(&id,0,task_main,0);task1();pthread_join(id,NULL);return 0;
}