微网站需要什么技术,廊坊seo排名优化网站,wordpress 多站点 固定链接,做属于公司的网站有什么好处1.要求定义一个全局变量char buf1234567#xff0c;创建两个线程#xff0c;不考虑退出条件。
a.A线程循环打印buf字符串#xff0c;
b.B线程循环倒置buf字符串#xff0c;即buf中本来存储1234567#xff0c;倒置后buf中存储7654321.B线程中不打印!!
c.倒置…1.要求定义一个全局变量char buf1234567创建两个线程不考虑退出条件。
a.A线程循环打印buf字符串
b.B线程循环倒置buf字符串即buf中本来存储1234567倒置后buf中存储7654321.B线程中不打印!!
c.倒置不允许使用辅助数组。
d要求A线程打印出来的结果只能为1234567或者 7654321不允许出现76345217234567等乱序情况
e.不允许使用sleep函数
f.分析出现错误的原因。
错误原因主进程和分支进程是并发执行的无法预测谁先谁后。
#include stdio.h
#include pthread.h
#include unistd.hchar buf[] 1234567;
int len sizeof(buf);void* overtrun(void* arg) //void* arg (void*)buf
{int i0,jlen;char temp;while(1){for(i0,jlen-2; i j; i,j--){temp ((char*)arg)[i];((char*)arg)[i] ((char*)arg)[j];((char*)arg)[j] temp;}pthread_exit(NULL);} return arg;
}int main(int argc, const char *argv[])
{
// char* pb buf;pthread_t tid;pthread_create(tid, NULL, overtrun, (void*)buf);printf(%d\n,len);while(1){int i,j;for(i0; ilen-1; i){printf(%c,buf[i]);}putchar(10); }pthread_join(tid,NULL);return 0;
} 2.完成图片拷贝要求一个线程拷贝一半另一个线程拷贝另一半。
#include stdio.h
#include pthread.h
#include unistd.h
#include sys/types.h
#include sys/stat.htypedef struct
{FILE* fop;FILE* fop_w;long size;
}Info;void* copy(void* arg) //void* arg (void*)msg
{char b;int count0,i0;fseek( ((Info*)arg)-fop, (((Info*)arg)-size/2),SEEK_SET);fseek( ((Info*)arg)-fop_w, (((Info*)arg)-size/2),SEEK_SET);for(i; i (((Info*)arg)-size/2); i){fscanf( ((Info*)arg)-fop, %c, b);fprintf( ((Info*)arg)-fop_w,%c,b);count;// printf(分支线程复制:%d\n,count);}printf(分支线程复制了%d个\n,count);pthread_exit(NULL);return arg;
}int main(int argc, const char *argv[])
{Info msg;msg.fop fopen(./cq.jpg,r);if(msg.fop NULL){perror(fop);return -1;}msg.fop_w fopen(./f.jpg,w);if(msg.fop_w NULL){perror(fop_w);return -1;}fseek(msg.fop, 0, SEEK_END);msg.size ftell(msg.fop);printf(大小:%ld\n,msg.size);pthread_t tid;pthread_create(tid, NULL, copy, (void*)msg);char c;int count0,i0;printf(开始\n);fseek(msg.fop, 0,SEEK_SET);fseek(msg.fop_w, 0,SEEK_SET);for(i; imsg.size/2; i){fscanf(msg.fop,%c,c);fprintf(msg.fop_w,%c,c);count;// printf(主线程复制:%d\n,count);}printf(主线程复制了%d个\n,count);pthread_join(tid,NULL);if(fclose(msg.fop_w) 0){perror(fclose_fop_w);return -1;}if(fclose(msg.fop) 0 ){perror(fclose_fop);return -1;}return 0;
}