shell中的基本语法
/
if [ ]; then
elif [ ]; then
else
fi
/
case $a in
1)
2)
*)
esac
/
while [ ]
do
done
编写程序1+2+。。+10将结果输入到文件
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define Address "/home/chenyue/文档/0619/a.txt"
int main()
{
int result = 0;
for(int i = 1; i < 10; i++)
{
result += i;
}
int fp;
fp = open(Address, O_RDWR, 0777);
char buf[16] = {0};
sprintf(buf, "%d\n", result);
write(fp, buf, strlen(buf));
close(fp);
return 0;
}
STM32位带操作// addr为首地址,bitnum为绑定的位序号
#define BitBAND(addr, bitnum)
(addr & 0XF0000000) + 0X2000000 + ((addr & 0XFFFFF)<< 5 ) + (bitnum << 2)
用互斥锁和信号量 编写生产者-消费者模型
/*
*mutex=1;
*lock mutex=0;
*unlock mutex=1;
*车位:共享资源 [管理员:互斥锁:串行---信号量:并行 ]车:线程
*互斥锁:每次只能够放一辆车
*信号量:可以运行多个线程同时访问共享资源
*sem_init();
*sem_wait();===
*sem_post(sem,**)//解锁
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
sem_t produce_sem;
sem_t consume_sem;
//线程同步
pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
typedef struct node
{
int data;
struct node *next;
}Node;
//指针
Node *head=NULL;
/*
producer:
consumer:
*/
void *consumer(void *arc){
while(1){
sem_wait(&consume_sem);//初始化为0则阻塞
//阻塞到post时候进行唤醒
//吃东西访问共享数据删除节点
Node *pdel= head;
head = head->next;
printf("-------consumer:%#X spend %d\n",pthread_self(),pdel->data);
free(pdel);
//将占有的资源还给生产者???
sem_post(&produce_sem);//++
sleep(rand()% 3);
}
pthread_exit((void *)0);
}
void *producer(void *arc){
while(1){
sem_wait(&produce_sem);//相当于资源--
//创建节点
Node *pnew =(Node *)malloc(sizeof(Node));
//插入节点初始化
pnew->data = rand() % 1000; //0-999
//头插法
pnew->next = head;
head = pnew;
printf("-------producer:%#X produce %d\n",pthread_self(),pnew->data);
pthread_mutex_unlock(&mylock);
//通知消费者唤醒一个线程
sem_post(&consume_sem);//++
//pthread_cond_broadcast(&cond);//唤醒所有线程
// pthread_cond_signal(&cond);
//printf("lmj\n");
sleep(rand()%3);
}
//通知消费者
pthread_exit((void *)1);
}
int main(int argc, char const *argv[])
{
pthread_t tid1,tid2;
//消费者资源为0
sem_init(&consume_sem,0,0);
sem_init(&produce_sem,0,4);//最多5个
pthread_create(&tid1,NULL,producer,NULL);
pthread_create(&tid2,NULL,consumer,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
sem_destory(consume_sem);
sem_destory(&produce_sem)
//sleep(10);
return 0;
}