1.
#include <stdio.h>
#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 mutex1;
pthread_mutex_t mutex2;
//用于写的线程
void*task1(void*arg)
{
pthread_mutex_lock(&mutex1);
//打开文件
int fd1=open("./1.c",O_WRONLY | O_TRUNC | O_CREAT,0644);
if(fd1<0)
{
perror("fd1 error");
return NULL;
}
//定义存储写入的容器
char write_buf[32]="0";
printf("请输入要输入的数据:");
scanf("%s",write_buf);
write(fd1,write_buf,strlen(write_buf));
//关闭文件
close(fd1);
pthread_mutex_unlock(&mutex2);
}
//用于读取的线程
void*task2(void*arg)
{
pthread_mutex_lock(&mutex2);
//打开文件
int fd2=open("./1.c",O_RDONLY);
if(fd2<0)
{
perror("fd2 error");
return NULL;
}
//定义接收的容器
while(1)
{
char read_buf[32]="0";
int rev=read(fd2,read_buf,strlen(read_buf));
if(rev==0)
{
break;
}
printf("%s\n",read_buf);
}
}
int main(int argc, const char *argv[])
{
//初始化互斥锁
pthread_mutex_init(&mutex1,0);
pthread_mutex_init(&mutex2,0);
pthread_mutex_lock(&mutex2);
//创建线程号
pthread_t id1,id2;
//创建两个线程
pthread_create(&id1,0,task1,0);//用于写的线程
pthread_create(&id2,0,task2,0);//用于读取的线程
//回收后线
pthread_join(id1, NULL);
pthread_join(id2, NULL);
//销毁互斥锁
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
return 0;
}
2.
#include <stdio.h>
#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 mutex1;
pthread_mutex_t mutex2;
pthread_mutex_t mutex3;
pthread_mutex_t mutex4;
pthread_mutex_t mutex5;
void* task1(void*arg)
{
while(1)
{
pthread_mutex_lock(&mutex1);
printf("A\n");
pthread_mutex_unlock(&mutex2);
}
}
void* task2(void*arg)
{
while(1)
{
pthread_mutex_lock(&mutex2);
printf("B\n");
pthread_mutex_unlock(&mutex3);
}
}
void* task3(void*arg)
{
while(1)
{
pthread_mutex_lock(&mutex3);
printf("C\n");
pthread_mutex_unlock(&mutex4);
}
}
void* task4(void*arg)
{
while(1)
{
pthread_mutex_lock(&mutex4);
printf("D\n");
pthread_mutex_unlock(&mutex5);
}
}
void* task5(void*arg)
{
while(1)
{
pthread_mutex_lock(&mutex5);
printf("E\n");
pthread_mutex_unlock(&mutex1);
}
}
int main(int argc, const char *argv[])
{
//创建线程号
pthread_t id1,id2,id3,id4,id5;
//初始化互斥锁
pthread_mutex_init(&mutex1,0);
pthread_mutex_init(&mutex2,0);
pthread_mutex_init(&mutex3,0);
pthread_mutex_init(&mutex4,0);
pthread_mutex_init(&mutex5,0);
//提前上锁保证task1拿到资源
pthread_mutex_lock(&mutex2);
pthread_mutex_lock(&mutex3);
pthread_mutex_lock(&mutex4);
pthread_mutex_lock(&mutex5);
//创建线程
pthread_create(&id1,0,task1,0);
pthread_create(&id2,0,task2,0);
pthread_create(&id3,0,task3,0);
pthread_create(&id4,0,task4,0);
pthread_create(&id5,0,task5,0);
//回收线程
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);
pthread_join(id4,NULL);
pthread_join(id5,NULL);
//毁坏互斥锁
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
pthread_mutex_destroy(&mutex3);
pthread_mutex_destroy(&mutex4);
pthread_mutex_destroy(&mutex5);
return 0;
}
#define _GNU_SOURCE
#include <myhead.h>
pthread_mutex_t quick;
pthread_mutex_t slow;
int qtunnel = 0, stunnel = 0;
typedef struct queue{
char qarr[5] = {0};
char sarr[3] = {0};
qlen = 0;
slen = 0;
}queue;
void *green1(void *agr){
while(1){
if(0 == stunnel){
pthread_mutex_lock(&slow);
stunnel = 1;
printf("K1 in stunnel\n");
usleep(3);
printf("K1 out stunnel\n");
stunnel = 0;
pthread_mutex_unlock(&slow);
}
}
}
void *green2(void *agr){
while(1){
if(0 == stunnel){
pthread_mutex_lock(&slow);
stunnel = 1;
printf("K2 in stunnel\n");
usleep(3);
printf("K2 out stunnel\n");
stunnel = 0;
pthread_mutex_unlock(&slow);
}
}
}
void *bullet1(void *agr){
while(1){
if(0 == qtunnel){
pthread_mutex_lock(&quick);
qtunnel = 1;
printf("G1 in qtunnel\n");
usleep(1);
printf("G1 out qtunnel\n");
qtunnel = 0;
pthread_mutex_unlock(&quick);
continue;
}else if(0 == stunnel){
pthread_mutex_lock(&slow);
stunnel = 1;
printf("G1 in stunnel\n");
usleep(1);
printf("G1 out stunnel\n");
stunnel = 0;
pthread_mutex_unlock(&slow);
}
}
}
void *bullet2(void *agr){
while(1){
if(0 == qtunnel){
pthread_mutex_lock(&quick);
qtunnel = 1;
printf("G2 in qtunnel\n");
usleep(1);
printf("G2 out qtunnel\n");
qtunnel = 0;
pthread_mutex_unlock(&quick);
continue;
}else if(0 == stunnel){
pthread_mutex_lock(&slow);
stunnel = 1;
printf("G2 in stunnel\n");
usleep(1);
printf("G2 out stunnel\n");
stunnel = 0;
pthread_mutex_unlock(&slow);
}
}
}
void *bullet3(void *agr){
while(1){
if(0 == qtunnel){
pthread_mutex_lock(&quick);
qtunnel = 1;
printf("G3 in qtunnel\n");
usleep(1);
printf("G3 out qtunnel\n");
qtunnel = 0;
pthread_mutex_unlock(&quick);
continue;
}else if(0 == stunnel){
pthread_mutex_lock(&slow);
stunnel = 1;
printf("G3 in stunnel\n");
usleep(1);
printf("G3 out stunnel\n");
stunnel = 0;
pthread_mutex_unlock(&slow);
}
}
}
int main(int argc, const char *argv[])
{
pthread_t G1;
pthread_t G2;
pthread_t G3;
pthread_t K1;
pthread_t K2;
pthread_mutex_init(&quick, 0);
pthread_mutex_init(&slow, 0);
pthread_create(&K1, 0, green1, 0);
pthread_create(&K2, 0, green2, 0);
pthread_create(&G1, 0, bullet1, 0);
pthread_create(&G2, 0, bullet2, 0);
pthread_create(&G3, 0, bullet3, 0);
pthread_detach(G1);
pthread_detach(G2);
pthread_detach(G3);
pthread_detach(K1);
pthread_detach(K2);
while(1){
printf("slowtunnel : %d\n", stunnel);
printf("quicktunnel: %d\n", qtunnel);
usleep(5);
}
return 0;
}