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

营销型网站制作方法一个vps主机放两个网站 速度

营销型网站制作方法,一个vps主机放两个网站 速度,架设仿冒网站挂马,做网站那个程序好LinuxC高级编程——线程间同步 宗旨#xff1a;技术的学习是有限的#xff0c;分享的精神是无限的。 1、 互斥锁mutex 多个线程同时访问共享数据时可能会冲突。对于多线程的程序#xff0c;访问冲突的问题是很普遍的#xff0c;解决的办法是引入互斥锁#xff08;Mutex技术的学习是有限的分享的精神是无限的。 1、 互斥锁mutex 多个线程同时访问共享数据时可能会冲突。对于多线程的程序访问冲突的问题是很普遍的解决的办法是引入互斥锁Mutex Mutual Exclusive Lock获得锁的线程可以完成“读-修改-写”的操作然后释放锁给其它线程没有获得 锁的线程只能等待而不能访问共享数据这样“读-修改-写”三步操作组成一个原子操作要么都执行要么都不执行不会执行到中间被打断也不会在其它处理器上并行做这个操作。Mutex用pthread_mutex_t类型的变量表示可以这样初始化和销毁 #includepthread.h int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_init(pthread_mutex_t *restrict mutex,constpthread_mutexattr_t *restrict attr); pthread_mutex_t mutex PTHREAD_MUTEX_INITIALIZER; 返回值成功返回0失败返回错误号。 pthread_mutex_init函数对Mutex做初始化参数attr设定Mutex的属性如果attr为NULL则表示缺省属性用pthread_mutex_init函 数初始化的Mutex可以用pthread_mutex_destroy销毁。如果Mutex变量是静态分配的全局变量 或static变量也可以用宏定义PTHREAD_MUTEX_INITIALIZER来初始化相当于用pthread_mutex_init初始化并且attr参数为NULL。 Mutex的加锁和解锁操作可以用下列函数 #includepthread.h int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); 返回值成功返回0失败返回错误号。 一个线程可以调用pthread_mutex_lock获得Mutex如果这时另一个线程已经调用pthread_mutex_lock获得了该Mutex则当前线程需要挂起等待直到另一个线程调用pthread_mutex_unlock释放Mutex当前线程被唤醒才能获得该Mutex并继续执行。 如果一个线程既想获得锁又不想挂起等待可以调用pthread_mutex_trylock如果Mutex已经被另一个线程获得这个函数会失败返回EBUSY而不会使线程挂起等待。  #include stdio.h #include stdlib.h #include pthread.h#define NLOOP 5000 int counter; /* incremented by threads */pthread_mutex_t counter_mutex PTHREAD_MUTEX_INITIALIZER;void *doit(void *);int main(int argc, char **argv) {pthread_t tidA, tidB;pthread_create(tidA, NULL, doit, NULL);pthread_create(tidB, NULL, doit, NULL);/* wait for both threads to terminate */pthread_join(tidA, NULL);pthread_join(tidB, NULL);return 0; } void *doit(void *vptr) {int i, val;for (i 0; i NLOOP; i){pthread_mutex_lock(counter_mutex);val counter;printf(%x: %d\n, (unsigned int)pthread_self(), val 1);counter val 1;pthread_mutex_unlock(counter_mutex);}return NULL; } 2、 条件变量 线程A需要等某个条件成立才能继续往下执行现在这个条件不成立线程A就阻塞等待而线程B在执行过程中使这个条件成立了就唤醒线程A继续执行。 在pthread库中通过条件变量Condition Variable 来阻塞等待一个条件或者唤醒等待这个条件的线程。 Condition Variable用pthread_cond_t类型的变量表示可以这样初始化和销毁 #include pthread.h int pthread_cond_destroy(pthread_cond_t *cond); int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t*restrict attr); pthread_cond_t cond PTHREAD_COND_INITIALIZER; 返回值成功返回0失败返回错误号。 和Mutex的初始化和销毁类似pthread_cond_init函数初始化一个Condition Variable attr参数 为NULL则表示缺省属性 pthread_cond_destroy函数销毁一个Condition Variable。如果Condition Variable是静态分配的也可以用宏定义PTHEAD_COND_INITIALIZER初始化相当于 用pthread_cond_init函数初始化并且attr参数为NULL。 Condition Variable的操作可以用下列函数 #include pthread.h int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t*restrict mutex, const struct timespec *restrict abstime); int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrictmutex); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_signal(pthread_cond_t *cond); 返回值成功返回0失败返回错误号。 可见一个ConditionVariable总是和一个Mutex搭配使用的。一个线程可以调 用pthread_cond_wait在一个Condition Variable上阻塞等待这个函数做以下三步操作 1.  释放Mutex   2. 阻塞等待   3. 当被唤醒时重新获得Mutex并返回. pthread_cond_timedwait函数还有一个额外的参数可以设定等待超时如果到达了abstime所指定的 时刻仍然没有别的线程来唤醒当前线程就返回ETIMEDOUT。一个线程可以调 用pthread_cond_signal唤醒在某个Condition Variable上等待的另一个线程也可以调用pthread_cond_broadcast唤醒在这个Condition Variable上等待的所有线程。  #include stdlib.h #include pthread.h #include stdio.hstruct msg {struct msg *next;int num; };struct msg *head; pthread_cond_t has_product PTHREAD_COND_INITIALIZER; pthread_mutex_t lock PTHREAD_MUTEX_INITIALIZER;void *consumer(void *p) {struct msg *mp;for (;;){pthread_mutex_lock(lock);while (head NULL){pthread_cond_wait(has_product, lock);}mp head;head mp-next;pthread_mutex_unlock(lock);printf(Consume %d\n, mp-num);free(mp);sleep(rand() % 5);} } void *producer(void *p) {struct msg *mp;for (;;){mp malloc(sizeof(struct msg));mp-num rand() % 1000 1;printf(Produce %d\n, mp-num);pthread_mutex_lock(lock);mp-next head;head mp;pthread_mutex_unlock(lock);pthread_cond_signal(has_product);sleep(rand() % 5);} }int main(int argc, char *argv[]) {pthread_t pid, cid;srand(time(NULL));pthread_create(pid, NULL, producer, NULL);pthread_create(cid, NULL, consumer, NULL);pthread_join(pid, NULL);pthread_join(cid, NULL);return 0; } 3、 信号量 Mutex变量是非0即1的可看作一种资源的可用数量初始化时Mutex是1表示有一个可用资源加锁时获得该资源将Mutex减到0表示不再有可用资源解锁时释放该资源将Mutex重新加到1表示又有了一个可用资源。信号量Semaphore和Mutex类似表示可用资源的数量和Mutex不同的是这个数量可以大于1. #include semaphore.h int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem); int sem_post(sem_t * sem); int sem_destroy(sem_t * sem); semaphore变量的类型为sem_t sem_init()初始化一个semaphore变量 value参数表示可用资源 的数量 pshared参数为0表示信号量用于同一进程的线程间同步本节只介绍这种情况。在用 完semaphore变量之后应该调用sem_destroy()释放与semaphore相关的资源。 调用sem_wait()可以获得资源使semaphore的值减1如果调用sem_wait()时semaphore的值已 经是0则挂起等待。如果不希望挂起等待可以调用sem_trywait()。调用sem_post()可以释放资 源使semaphore的值加1同时唤醒挂起等待的线程。 #include stdlib.h #include pthread.h #include stdio.h #include semaphore.h#define NUM 5 int queue[NUM]; sem_t blank_number, product_number;void *producer(void *arg) {int p 0;while (1){sem_wait(blank_number);queue[p] rand() % 1000 1;printf(Produce %d\n, queue[p]);sem_post(product_number);p (p 1) % NUM;sleep(rand() % 5);} }void *consumer(void *arg) {int c 0;while (1){sem_wait(product_number);printf(Consume %d\n, queue[c]);queue[c] 0;sem_post(blank_number);c (c 1) % NUM;sleep(rand() % 5);} }int main(int argc, char *argv[]) {pthread_t pid, cid;sem_init(blank_number, 0, NUM);sem_init(product_number, 0, 0);pthread_create(pid, NULL, producer, NULL);pthread_create(cid, NULL, consumer, NULL);pthread_join(pid, NULL);pthread_join(cid, NULL);sem_destroy(blank_number);sem_destroy(product_number);return 0; }
http://wiki.neutronadmin.com/news/386307/

相关文章:

  • 做网站从设计到上线流程全国商务网站大全
  • 手机网站编程网页加速器免费永久
  • 在京东上怎样做网站自媒体申请注册
  • 设计软件网站定制开发建造自己的网站
  • 网站的投票 计数模块怎么做做网站公司排名多少钱
  • 网站建设着wordpress增加产品
  • 万网建网站教程一起买买买网站建设
  • 永兴县网站建设公司深圳谷歌推广公司
  • 郑州网站设计排行成都开发小程序的公司
  • 银川市住房和城乡建设网站设计师证
  • 网站设计模板含数据库wordpress缩略图幻灯展现
  • 校园加盟网站建设网络系统设计师是干什么的
  • 关于网站建设的网站网页制作0基础怎么学
  • 我要找个做网站的公司论坛网站建设教程
  • 求一个做美食视频的网站网站怎么注销备案号
  • 手机微信怎么建立公众号seo上海优化
  • wordpress 整站转移免费咨询服务协议
  • 哪个网站有激光打标业务做公司可以做网站
  • ec网站域名潍坊网站制作 熊掌号
  • 微信订阅号不认证可以做网站吗百度app免费下载
  • 珠海建站网站模板wordpress如何应用ssl
  • 网站自然排名怎么做网络营销的广告形式
  • 网站建设的税率是多少医疗网站建设哪家好
  • 中山市城乡建设局网站望牛墩做网站
  • 网站服务器维护需要多久xampp上安装wordpress
  • 如何创建一个和淘宝一样的网站ftp怎么连接网站
  • 安徽网站建设有限公司怎么查网站找谁做的
  • 茌平做创建网站公司网站备案 每年
  • 网站服务器升级一般多久seo公司 杭州
  • 网站设计需要多少钱温州网站开发公司