一、作业
第一题: 创建2个线程
1#线程:负责通过文件IO向文件中写入数据
2#线程:负责从该文件中读取数据 使用互斥锁实现:一定是先写入数据,再读取数据
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.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 ma;
pthread_mutex_t mb;
void *task(void *arg)
{
pthread_mutex_lock(&mb);
//子线程
int fd1 = open("wst.txt", O_RDONLY);
while(1)
{
char st[32]={0};
int res = read(fd1,st,31);
if(res == 0){break;}
printf("%s",st);
}
printf("\n");
close(fd1);
pthread_mutex_unlock(&ma);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mb,0);//初始化互斥锁
pthread_mutex_lock(&mb);
pthread_mutex_init(&ma,0);
pthread_t id;
pthread_create(&id,0,task,0);
pthread_detach(id);
//主线程
pthread_mutex_lock(&ma);
int fd = open("wst.txt", O_WRONLY | O_TRUNC | O_CREAT, 0666);
char stu[32]={"good night everybody"};
write(fd,stu,32);
close(fd);
pthread_mutex_unlock(&mb);
sleep(2);
return 0;
}
运行结果:
![](https://i-blog.csdnimg.cn/direct/4bb82f3824a74e99800bc54d93abf0bd.png)
第二题: 创建5个线程,使用互斥锁安排这5个线程同步运行:123451234512345.....
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.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 m1;
pthread_mutex_t m2;
pthread_mutex_t m3;
pthread_mutex_t m4;
pthread_mutex_t m5;
void* task2(void* arg){
while(1){
pthread_mutex_lock(&m2);
printf("2");
fflush(stdout);
sleep(1);
pthread_mutex_unlock(&m3);
}
}
void* task3(void* arg){
while(1){
pthread_mutex_lock(&m3);
printf("3");
fflush(stdout);
sleep(1);
pthread_mutex_unlock(&m4);
}
}
void* task4(void* arg){
while(1){
pthread_mutex_lock(&m4);
printf("4");
fflush(stdout);
sleep(1);
pthread_mutex_unlock(&m5);
}
}
void* task5(void* arg){
while(1){
pthread_mutex_lock(&m5);
printf("5");
fflush(stdout);
sleep(1);
pthread_mutex_unlock(&m1);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&m2,0);
pthread_mutex_lock(&m2);
pthread_mutex_init(&m1,0);
pthread_mutex_init(&m3,0);
pthread_mutex_lock(&m3);
pthread_mutex_init(&m4,0);
pthread_mutex_lock(&m4);
pthread_mutex_init(&m5,0);
pthread_mutex_lock(&m5);
pthread_t id,id2,id3,id4,id5;
pthread_create(&id2,0,task2,0);
pthread_detach(id2);
pthread_create(&id3,0,task3,0);
pthread_detach(id3);
pthread_create(&id4,0,task4,0);
pthread_detach(id4);
pthread_create(&id5,0,task5,0);
pthread_detach(id5);
while(1){
pthread_mutex_lock(&m1);
printf("1");
fflush(stdout);
sleep(1);
pthread_mutex_unlock(&m2);
}
return 0;
}
运行结果:
![](https://i-blog.csdnimg.cn/direct/abe8d9c937db4079a7955d1fc031c5f2.png)
第三题 有2条隧道,一条快速隧道,一条普通隧道。有5列火车,3列复兴号,2列绿皮扭扭车 要求,复兴号2条隧道都能走,绿皮车只能走普通隧道 模拟火车过隧道的场景
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.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>
int stu[5]={1,2,3,4,5};
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
pthread_mutex_t mutex;
void *task(void *arg)
{
//子线程
for(int i=0; i<5; i++)
{
pthread_mutex_lock(&mutex);
sleep(1);
printf("我是普通通道!可以1,2,3复兴号通过也可以由4,5火车通过\n");
if(stu[i]<4)
{
printf("这是复兴号列车,允许通过!\n");
}
else
{
printf("这是普通火车,允许通过\n");
}
pthread_mutex_unlock(&mutex);
}
sleep(1);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex,0);
pthread_t id;
pthread_create(&id,0,task,0);
pthread_detach(id);
//主线程
for(int i=0; i<5; i++)
{
pthread_mutex_lock(&mutex);
printf("我是快速通道!只能由1,2,3开头复兴号通过\n");
sleep(1);
if(stu[i]<4)
{
printf("这是复兴号列车,允许通过!\n");
}
else
{
printf("这是普通火车,不允许通过,请返回!\n");
}
sleep(0.5);
pthread_mutex_unlock(&mutex);
}
sleep(2);
pthread_mutex_unlock(&mutex);
sleep(1);
return 0;
}
运行结果如下:![](https://i-blog.csdnimg.cn/direct/195287a49513405d8200b39287493f7a.png)