day27 多线程编译

作业

第一题:
创建2个线程,
1#线程:负责通过文件IO向文件中写入数据
2#线程:负责从该文件中读取数据
使用互斥锁实现:一定是先写入数据,再读取数据

#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 m1;
int flag=0;

void* task(void* arg)
{
	pthread_mutex_lock(&m1);
	int fd=open("1.txt",O_RDONLY);
	while(1)
	{
		char str;
		int res=read(fd,&str,1);
		if(res == 0){break;}
		printf("%c",str);
	}
	pthread_mutex_unlock(&m1);
	close(fd);
	flag=1;
}

int main(int argc, const char *argv[])
{
	//初始化互斥锁
	pthread_mutex_init(&m1,0);
	pthread_mutex_lock(&m1);

	pthread_t id;
	pthread_create(&id,0,task,0);
	pthread_detach(id);
	

	int fd=open("1.txt",O_WRONLY | O_CREAT | O_TRUNC,0666);
	while(1)
	{
		char x;
		int res=read(STDIN_FILENO,&x,1);
		if(res == 0 || x == '#'){break;};
		write(fd,&x,1);
	}
	pthread_mutex_unlock(&m1);
	close(fd);
	while(1)
	{
		if(flag == 1){break;}
	}
	return 0;
}

第二题:
创建5个线程,使用互斥锁安排这5个线程同步运行:123451234512345…

#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 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\n");
		sleep(1);
		pthread_mutex_unlock(&m3);
	}
}

void* task3(void* arg)
{
	while(3)
	{
		pthread_mutex_lock(&m3);
		printf("3\n");
		sleep(1);
		pthread_mutex_unlock(&m4);
	}
}

void* task4(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&m4);
		printf("4\n");
		sleep(1);
		pthread_mutex_unlock(&m5);
	}
}

void* task5(void* arg)
{
	while(1)
	{
		pthread_mutex_lock(&m5);
		printf("5\n");
		sleep(1);
		pthread_mutex_unlock(&m1);
	}
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&m1,0);
	pthread_mutex_init(&m2,0);
	pthread_mutex_init(&m3,0);
	pthread_mutex_init(&m4,0);
	pthread_mutex_init(&m5,0);

	pthread_mutex_lock(&m2);
	pthread_mutex_lock(&m3);
	pthread_mutex_lock(&m4);
	pthread_mutex_lock(&m5);

	pthread_t id2,id3,id4,id5;
	pthread_create(&id2,0,task2,0);
	pthread_create(&id3,0,task3,0);
	pthread_create(&id4,0,task4,0);
	pthread_create(&id5,0,task5,0);

	while(1)
	{
		pthread_mutex_lock(&m1);
		printf("1\n");
		sleep(1);
		pthread_mutex_unlock(&m2);
	}
	return 0;
}

第三题
有2条隧道,一条快速隧道,一条普通隧道。有5列火车,3列复兴号,2列绿皮扭扭车
要求,复兴号2条隧道都能走,绿皮车只能走普通隧道
模拟火车过隧道的场景

#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 m1;
pthread_mutex_t m2;

int a=0;
int b=0;

void use_a(char* name)
{
	a++;
	printf("%s使用了快速隧道a,a使用次数为%d\n",name,a);
}

void use_b(char* name)
{
	b++;
	printf("%s使用了普通隧道b,b使用次数为%d\n",name,b);
}

void* task2(void* arg)
{
	while(1)
	{
		char name[10]="复兴号2";
		pthread_mutex_lock(&m1);
		use_a(name);
		sleep(1);
		pthread_mutex_unlock(&m1);
		
		pthread_mutex_lock(&m2);
		use_b(name);
		sleep(1);
		pthread_mutex_unlock(&m2);
	}

}

void* task3(void* arg)
{
	while(1)
	{
		char name[10]="复兴号3";
		pthread_mutex_lock(&m1);
		use_a(name);
		sleep(1);
		pthread_mutex_unlock(&m1);
		
		pthread_mutex_lock(&m2);
		use_b(name);
		sleep(1);
		pthread_mutex_unlock(&m2);
	}

}

void* task4(void* arg)
{
	while(1)
	{
		char name[10]="绿皮1";
		pthread_mutex_lock(&m2);
		use_b(name);
		sleep(1);
		pthread_mutex_unlock(&m2);
	}

}

void* task5(void* arg)
{
	while(1)
	{
		char name[10]="绿皮2";
		pthread_mutex_lock(&m2);
		use_b(name);
		sleep(1);
		pthread_mutex_unlock(&m2);
	}

}

int main(int argc, const char *argv[])
{
	pthread_mutex_init(&m1,0);
	pthread_mutex_init(&m2,0);

	pthread_t id2,id3,id4,id5;
	pthread_create(&id2,0,task2,0);
	pthread_create(&id3,0,task3,0);
	pthread_create(&id4,0,task4,0);
	pthread_create(&id5,0,task5,0);
	while(1)
	{
		char name[10]="复兴号1";
		pthread_mutex_lock(&m1);
		use_a(name);
		sleep(1);
		pthread_mutex_unlock(&m1);
		
		pthread_mutex_lock(&m2);
		use_b(name);
		sleep(1);
		pthread_mutex_unlock(&m2);
	}


	return 0;
}

思维导图

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力帮您写一个符合要求的代码,以下是代码示例: ``` #include <stdio.h> #include <stdlib.h> #include "ThostFtdcMdApi.h" class CtpMdSpi : public CThostFtdcMdSpi { public: CtpMdSpi(CThostFtdcMdApi* pMdApi) : m_pMdApi(pMdApi) {} virtual void OnFrontConnected() { printf("CTP Front connected.\n"); m_pMdApi->ReqUserLogin(&m_loginReq, 0); } virtual void OnFrontDisconnected(int nReason) { printf("CTP Front disconnected.\n"); } virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { if (pRspInfo->ErrorID == 0) { printf("CTP User logged in. Trading day: %s\n", pRspUserLogin->TradingDay); m_pMdApi->SubscribeMarketData(m_instruList, 1); } else { printf("CTP User login failed. Error ID: %d\n", pRspInfo->ErrorID); } } virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) { printf("CTP New tick received. Instrument ID: %s, Last price: %f\n", pDepthMarketData->InstrumentID, pDepthMarketData->LastPrice); } private: CThostFtdcMdApi* m_pMdApi; char m_instruList[10] = "cu1907.SHF"; // 监听的合约代码 CThostFtdcReqUserLoginField m_loginReq = { "your_broker_id", "your_user_id", "your_password" }; }; int main() { CThostFtdcMdApi* pMdApi = CThostFtdcMdApi::CreateFtdcMdApi(""); CtpMdSpi spi(pMdApi); pMdApi->RegisterSpi(&spi); pMdApi->RegisterFront("tcp://ctp_md_address:port"); pMdApi->Init(); while (true) { // 等待消息处理 Sleep(1000); } pMdApi->Release(); return 0; } ``` 请注意将代码中的变量值替换为您自己的实际值,然后编译运行即可。当有新的行情到达时,会输出相应的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值