当前位置: 首页 > news >正文

海外营销是干什么的百度视频seo

海外营销是干什么的,百度视频seo,b2c网站名和网址,温州做网站掌熊号仨demo 一、 一个线程读文件,另一个线程将读取的内容输出到终端 1.1 要求 创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取到的内容打印到终端上,类似实现cat一个文件。 cat数据完毕后,要结束两个线…

仨demo

一、 一个线程读文件,另一个线程将读取的内容输出到终端

1.1 要求

  • 创建两个线程,
  • 其中一个线程读取文件中的数据,
  • 另外一个线程将读取到的内容打印到终端上,
  • 类似实现cat一个文件。
    • cat数据完毕后,要结束两个线程。
  • 提示:先读数据,读到数据后将数据打印到终端上。

1.2 代码实现

/*
创建两个线程,
其中一个线程读取文件中的数据,
另外一个线程将读取到的内容打印到终端上,
类似实现cat一个文件。cat数据完毕后,要结束两个线程。
提示:先读数据,读到数据后将数据打印到终端上。
*/
#include <my_head.h>//  用于暂存的存储
char buff[16];
//  用于接收返回值,读取的字节数
ssize_t res;//  创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//  创建条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int flag = 0;//  读文件
void *read_file(void *);//  写到终端
void *write_out(void *);int main(int argc, const char *argv[])
{//  以只读的方式打开 “abc.c” 文件//  可以换,也可以换成外部参数,按自己需求int fd = open("abc.c", O_RDONLY);//  创建线程1,用于读文件,并将文件描述符传过去pthread_t tid1;if (pthread_create(&tid1, NULL, read_file, &fd) != 0){fprintf(stderr, "pthread_create tid1 error\n");return -1;}//  创建线程2,用于写到终端pthread_t tid2;if (pthread_create(&tid2, NULL, write_out, NULL) != 0){fprintf(stderr, "pthread_create tid2 error\n");return -1;}//  等待两个线程结束pthread_join(tid1, NULL);pthread_join(tid2, NULL);return 0;
}//  读文件
void *read_file(void *arg)
{//  接收传进来的文件描述符int fd = *(int *)arg;while (1){//  上锁pthread_mutex_lock(&mutex);//  若标志不是0,说明不该当前线程执行,所以要沉睡if (0 != flag){//  不是当前线程的执行时机pthread_cond_wait(&cond, &mutex);}//  清空暂存数据的存储空间bzero(buff, sizeof(buff));//  读取文件中的数据放到暂存空间res = read(fd, buff, sizeof(buff));//  读取错误则报错,并退出线程if (0 > res){ERR_MSG("read error");return NULL;}//  读到最后啥都没读到,那就尝试唤醒另一个,然后解锁结束循环,之后终止线程else if (0 == res){//  尝试唤醒另一个线程pthread_cond_signal(&cond);//  解锁pthread_mutex_unlock(&mutex);break;}//  修改标志flag = 1;//  当前部分执行完了,该唤醒另一个了pthread_cond_signal(&cond);//  解锁pthread_mutex_unlock(&mutex);}//  终止当前线程pthread_exit(NULL);
}//  写到终端
void *write_out(void *arg)
{while (1){//  上锁pthread_mutex_lock(&mutex);//  看是否属于当前该执行的时机if (1 != flag){//  不属于当前该执行的时机,那就睡过去吧pthread_cond_wait(&cond, &mutex);}//  没东西可以输出,尝试唤醒另一个,并解锁,然后终止当前线程if (0 == res){//  尝试唤醒另一个线程pthread_cond_signal(&cond);//  解锁pthread_mutex_unlock(&mutex);break;}//  向终端输出write(1, buff, res);//  修改标志flag = 0;//  当前任务执行完了,该让另一个线程启动了pthread_cond_signal(&cond);//  解锁pthread_mutex_unlock(&mutex);}//  终止当前线程pthread_exit(NULL);
}

二、 三个线程打印ABC,每个线程打一个字符,且顺序不变

2.1 要求

  • 有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。
    • 要求打印的结果为ABC。

2.2 代码实现

/*
有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。要求打印的结果为ABC。
*/
#include <my_head.h>//  互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//  条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//  标志:0为A,1位B,2为C
int flag = 0;//  线程A
void *print_A(void *);
//  线程B
void *print_B(void *);
//  线程C
void *print_C(void *);int main(int argc, const char *argv[])
{//  创建三个线程pthread_t tid1;pthread_t tid2;pthread_t tid3;if (pthread_create(&tid1, NULL, print_A, NULL) != 0){fprintf(stderr, "pthread_create A error\n");return -1;}if (pthread_create(&tid2, NULL, print_B, NULL) != 0){fprintf(stderr, "pthread_create A error\n");return -1;}if (pthread_create(&tid3, NULL, print_C, NULL) != 0){fprintf(stderr, "pthread_create A error\n");return -1;}//  等待三个线程结束pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_join(tid3, NULL);return 0;
}//  线程A
void *print_A(void *arg)
{while (1){//  上锁pthread_mutex_lock(&mutex);//  判断是否是输出A的时机if (flag == 0){//  是输出A的时机,输出A,然后将标志改为1准备输出B,之后唤醒其他线程printf("A");flag = 1;//  唤醒其他线程pthread_cond_signal(&cond);}//  不是输出A的时机else{//  直接进入休眠pthread_cond_wait(&cond, &mutex);}//  解锁pthread_mutex_unlock(&mutex);}//  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)pthread_exit(NULL);
}
//  线程B
void *print_B(void *arg)
{while (1){//  上锁pthread_mutex_lock(&mutex);//  判断是否是输出B的时机if (flag == 1){//  是输出B的时机,输出B,然后将标志改为2准备输出C,之后唤醒其他线程printf("B");flag = 2;//  唤醒其他线程pthread_cond_signal(&cond);}//  不是输出B的时机else{//  直接进入休眠pthread_cond_wait(&cond, &mutex);}//  解锁pthread_mutex_unlock(&mutex);}//  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)pthread_exit(NULL);
}
//  线程C
void *print_C(void *arg)
{while (1){//  上锁pthread_mutex_lock(&mutex);//  判断是否是输出C的时机if (flag == 2){//  是输出C的时机,输出C,然后将标志改为0准备输出A,之后唤醒其他线程printf("C\n");flag = 0;//  唤醒其他线程pthread_cond_signal(&cond);}//  不是输出C的时机else{//  直接进入休眠pthread_cond_wait(&cond, &mutex);}//  解锁pthread_mutex_unlock(&mutex);}//  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)pthread_exit(NULL);
}

三、 用信号量实现一个线程对字符串逆置,另一线程对字符串输出

3.1 要求

  • 要求定义一个全局变量 char buf[] = “1234567”,创建两个线程,不考虑退出条件。
  • A线程循环打印buf字符串,
  • B线程循环倒置buf字符串,
  • 即buf中本来存储1234567,倒置后buf中存储7654321.
    • B线程中不打印!!
    • 倒置不允许使用辅助数组。
    • 要求A线程打印出来的结果只能为 1234567 或者 7654321
    • 不允许出现7634521 7234567
    • 不允许使用sleep函数
    • 用信号量的方式实现上述代码顺序执行,不允许使用flag;

3.2 代码实现

/*
要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。
A线程循环打印buf字符串,
B线程循环倒置buf字符串,
即buf中本来存储1234567,倒置后buf中存储7654321.B线程中不打印!!倒置不允许使用辅助数组。要求A线程打印出来的结果只能为 1234567  或者  7654321不允许出现7634521  7234567不允许使用sleep函数分析出现错误的原因。用信号量的方式实现上述代码顺序执行,不允许使用flag;
*/
#include <my_head.h>//  俩信号量
sem_t sem1;
sem_t sem2;//  数据
char buff[] = "1234567";//  输出线程
void *print_str(void *arg);
//  逆置线程
void *turn_str(void *arg);int main(int argc, const char *argv[])
{/*这俩信号量第一个初始化为1,第二个初始化为0能够保证是先进行线程1,即输出线程,后进行线程2,即逆置线程而在输出线程中先获取信号量1,操作执行完再释放信号量2,(注意是释放信号量2)因为信号量1的初始值为1,所以只会执行一次线程1信号量2的初始值为0,在线程1中释放信号量2后变成了1,这样可以执行一次线程2在逆置线程中也是同理先获取线程1中释放的信号量2,操作执行完再释放信号量1获取线程1释放的信号量2,而信号量2只够线程2执行一次,在线程2操作执行完后释放信号量1,可以让线程1能够再次执行1次,如此往复*///  信号量初始化为1,也就是在这个信号量下初始只能走一个线程if (sem_init(&sem1, 0, 1) < 0){ERR_MSG("sem_init");return -1;}//  信号量初始化为0,也就是初始的时候没有可用的信号量if (sem_init(&sem2, 0, 0) < 0){ERR_MSG("sem_init");return -1;}//  创建俩线程pthread_t tid1;pthread_t tid2;if (pthread_create(&tid1, NULL, print_str, NULL) != 0){fprintf(stderr, "pthread_create tid1 error\n");return -1;}if (pthread_create(&tid2, NULL, turn_str, NULL) != 0){fprintf(stderr, "pthread_create tid2 error\n");return -1;}//  等待两个线程结束pthread_join(tid1, NULL);pthread_join(tid2, NULL);return 0;
}//  输出线程
void *print_str(void *arg)
{while (1){//  P操作if (sem_wait(&sem1) < 0){ERR_MSG("print_str sem_wait");return NULL;}//  要执行的操作printf("%s\n", buff);//  V操作if (sem_post(&sem2) < 0){ERR_MSG("print_str sem_post");return NULL;}}//  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)pthread_exit(NULL);
}
//  逆置线程
void *turn_str(void *arg)
{int len = strlen(buff);while (1){//  P操作if (sem_wait(&sem2) < 0){ERR_MSG("print_str sem_wait");return NULL;}//  要执行的操作char *s = buff, *s1 = buff + len - 1;//  翻转while (s < s1){(*s) ^= (*s1);(*s1) ^= (*s);(*s) ^= (*s1);s++;s1--;}//  V操作if (sem_post(&sem1) < 0){ERR_MSG("print_str sem_post");return NULL;}}//  结束当前线程(虽然死循环,不会结束线程,但是为了规范点,还是写上)pthread_exit(NULL);
}

文章转载自:
http://hatefully.c7496.cn
http://autumnal.c7496.cn
http://racecard.c7496.cn
http://bended.c7496.cn
http://duroc.c7496.cn
http://bestiality.c7496.cn
http://lysogen.c7496.cn
http://counterattack.c7496.cn
http://haematoid.c7496.cn
http://portfire.c7496.cn
http://jemmy.c7496.cn
http://instigation.c7496.cn
http://secreta.c7496.cn
http://kreutzer.c7496.cn
http://sulfurous.c7496.cn
http://tidewater.c7496.cn
http://overhappy.c7496.cn
http://subprefect.c7496.cn
http://masturbatory.c7496.cn
http://meeken.c7496.cn
http://chevrotain.c7496.cn
http://cherimoya.c7496.cn
http://pardy.c7496.cn
http://plenitudinous.c7496.cn
http://titaniferous.c7496.cn
http://icteric.c7496.cn
http://sinclair.c7496.cn
http://lysenkoism.c7496.cn
http://bray.c7496.cn
http://trikerion.c7496.cn
http://ahitophal.c7496.cn
http://inaccessible.c7496.cn
http://raphe.c7496.cn
http://risen.c7496.cn
http://dover.c7496.cn
http://gypsy.c7496.cn
http://winnow.c7496.cn
http://telecentre.c7496.cn
http://partition.c7496.cn
http://loanshift.c7496.cn
http://quomodo.c7496.cn
http://addition.c7496.cn
http://pushball.c7496.cn
http://lummox.c7496.cn
http://goethe.c7496.cn
http://newscaster.c7496.cn
http://missable.c7496.cn
http://heathenize.c7496.cn
http://douro.c7496.cn
http://thermosiphon.c7496.cn
http://pereon.c7496.cn
http://gabfest.c7496.cn
http://mamillated.c7496.cn
http://antiphrasis.c7496.cn
http://monthly.c7496.cn
http://vandalize.c7496.cn
http://handful.c7496.cn
http://nosogenesis.c7496.cn
http://cock.c7496.cn
http://hoosh.c7496.cn
http://denaturalization.c7496.cn
http://disturbed.c7496.cn
http://titrator.c7496.cn
http://frogman.c7496.cn
http://tahini.c7496.cn
http://liberationist.c7496.cn
http://upsoar.c7496.cn
http://raggie.c7496.cn
http://lugouqiao.c7496.cn
http://herzegovina.c7496.cn
http://solder.c7496.cn
http://numismatician.c7496.cn
http://ethidium.c7496.cn
http://dictyostele.c7496.cn
http://old.c7496.cn
http://sheikhdom.c7496.cn
http://oatmeal.c7496.cn
http://permanently.c7496.cn
http://sempiternity.c7496.cn
http://leporine.c7496.cn
http://coden.c7496.cn
http://pinda.c7496.cn
http://dioestrum.c7496.cn
http://ferdinanda.c7496.cn
http://sixteenth.c7496.cn
http://ophiuran.c7496.cn
http://birthright.c7496.cn
http://analysable.c7496.cn
http://vulnerary.c7496.cn
http://pseudoscorpion.c7496.cn
http://lichenification.c7496.cn
http://chalybeate.c7496.cn
http://borne.c7496.cn
http://actinomycosis.c7496.cn
http://weighable.c7496.cn
http://teentsy.c7496.cn
http://bitmap.c7496.cn
http://airwaves.c7496.cn
http://fabricative.c7496.cn
http://gnathonic.c7496.cn
http://www.zhongyajixie.com/news/91035.html

相关文章:

  • 网络平台管理制度关键词seo服务
  • 品牌网站设计流程重庆百度推广开户
  • 资深的家居行业网站开发免费b站推广网站不
  • 开启wordpress upwnseo 优化 服务
  • 建什么网站赚钱网络推广外包哪家好
  • 做网站书籍网络口碑营销案例分析
  • 建站之星管理中心优化品牌seo关键词
  • 福田祥菱v3报价及图片邯郸网站seo
  • 网络营销论文文献360优化大师官方免费下载
  • 广东建设职业技术学院官方网站大数据分析营销平台
  • 开淘宝店做网站开发互联网营销师考证多少钱
  • 重庆酉阳网站设计公司广州google推广
  • 网站建设管理是seo免费自学的网站
  • 哪些网站可以做免费答题各大网站收录入口
  • 潍坊专业网站建设公司深圳网络推广解决方案
  • 宜宾市规划建设局网站百度搜索软件
  • 湖南省城乡与建设厅网站百度竞价推广的优势
  • 自建网站推广的最新发展sem竞价培训班
  • 哪个网站可以做司考题seo网站优化师
  • 网站制作 天津网上销售平台怎么做
  • 城市建设网站设计网络seo
  • 上海专业网站建设哪家好建网站平台
  • 展厅设计装修云南seo
  • 网站建设价格a电联真甲先生微信朋友圈广告投放收费标准
  • 行业网站建设优化案例软文营销怎么写
  • 内蒙包头网站开发网站建设推广
  • 备案做电影网站吗品牌营销包括哪些内容
  • 良精网站管理系统seo优化首页
  • 安丘网站制作潍坊seo培训
  • 网站开发论文说明营销方案的几个要素