linux多线程编译命令 linux shell 多线程执行程序

在linux下用c语言实现用多进程同步方法演示“生产者-消费者”问题

这个问题需要的知识主要包括:

创新互联建站长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为宽城企业提供专业的成都做网站、成都网站制作,宽城网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

1 多进程间进行通信;

2 使用同步信号量(semaphore)和互斥信号量(mutex)进行数据保护。

参考代码如下,可以参照注释辅助理解:

#include stdio.h

#include stdlib.h

#include unistd.h

#include pthread.h

#include semaphore.h

#define N 2   // 消费者或者生产者的数目

#define M 10 // 缓冲数目

int in = 0;   // 生产者放置产品的位置

int out = 0; // 消费者取产品的位置

int buff[M] = {0}; // 缓冲初始化为0, 开始时没有产品

sem_t empty_sem; // 同步信号量, 当满了时阻止生产者放产品

sem_t full_sem;   // 同步信号量, 当没产品时阻止消费者消费

pthread_mutex_t mutex; // 互斥信号量, 一次只有一个线程访问缓冲

int product_id = 0;   //生产者id

int prochase_id = 0; //消费者id

/* 打印缓冲情况 */

void print()

{

int i;

for(i = 0; i  M; i++)

   printf("%d ", buff[i]);

printf("\n");

}

/* 生产者方法 */ 

void *product()

{

int id = ++product_id;

while(1)

{

   // 用sleep的数量可以调节生产和消费的速度,便于观察

   sleep(1);

   //sleep(1);

  

   sem_wait(empty_sem);

   pthread_mutex_lock(mutex);

  

   in = in % M;

   printf("product%d in %d. like: \t", id, in);

  

   buff[in] = 1;  

   print();  

   ++in;

  

   pthread_mutex_unlock(mutex);

   sem_post(full_sem);  

}

}

/* 消费者方法 */

void *prochase()

{

int id = ++prochase_id;

while(1)

{

   // 用sleep的数量可以调节生产和消费的速度,便于观察

   sleep(1);

//sleep(1);

  

   sem_wait(full_sem);

   pthread_mutex_lock(mutex);

  

   out = out % M;

   printf("prochase%d in %d. like: \t", id, out);

  

   buff[out] = 0;

   print();

   ++out;

  

   pthread_mutex_unlock(mutex);

   sem_post(empty_sem);

}

}

int main()

{

pthread_t id1[N];

pthread_t id2[N];

int i;

int ret[N];

// 初始化同步信号量

int ini1 = sem_init(empty_sem, 0, M); 

int ini2 = sem_init(full_sem, 0, 0);  

if(ini1  ini2 != 0)

{

   printf("sem init failed \n");

   exit(1);

//初始化互斥信号量 

int ini3 = pthread_mutex_init(mutex, NULL);

if(ini3 != 0)

{

   printf("mutex init failed \n");

   exit(1);

// 创建N个生产者线程

for(i = 0; i  N; i++)

{

   ret[i] = pthread_create(id1[i], NULL, product, (void *)(i));

   if(ret[i] != 0)

   {

printf("product%d creation failed \n", i);

exit(1);

   }

}

//创建N个消费者线程

for(i = 0; i  N; i++)

{

   ret[i] = pthread_create(id2[i], NULL, prochase, NULL);

   if(ret[i] != 0)

   {

printf("prochase%d creation failed \n", i);

exit(1);

   }

}

//销毁线程

for(i = 0; i  N; i++)

{

   pthread_join(id1[i],NULL);

   pthread_join(id2[i],NULL);

}

exit(0); 

}

在Linux下编译的时候,要在编译命令中加入选项-lpthread以包含多线程支持。比如存储的C文件为demo.c,要生成的可执行文件为demo。可以使用命令:

gcc demo.c -o demo -lpthread

程序中为便于观察,使用了sleep(1);来暂停运行,所以查看输出的时候可以看到,输出是每秒打印一次的。

Linux多线程编程

程序代码test.c共两个线程,一个主线程,一个读缓存区的线程:

#include pthread.h

#include stdio.h

#include stdlib.h

#include string.h

#include unistd.h

char globe_buffer[100];

void *read_buffer_thread(void *arg); //这里先声明一下读缓存的线程,具体实现写在后面了

int main()

{

int res,i;

pthread_t read_thread;

for(i=0;i20;i++)

globe_buffer[i]=i;

printf("\nTest thread : write buffer finish\n");

sleep(3);\\这里的3秒是多余,可以不要。

res = pthread_create(read_thread, NULL, read_buffer_thread, NULL);

if (res != 0)

{

printf("Read Thread creat Error!");

exit(0);

}

sleep(1);

printf("waiting for read thread to finish...\n");

res = pthread_join(read_thread, NULL);

if (res != 0)

{

printf("read thread join failed!\n");

exit(0);

}

printf("read thread test OK, have fun!! exit ByeBye\n");

return 0;

}

void *read_buffer_thread(void *arg)

{

int i,x;

printf("Read buffer thread read data : \n");

for(i=0;i20;i++)

{

x=globe_buffer[i];

printf("%d ",x);

globe_buffer[i]=0;//清空

}

printf("\nread over\n");

}

---------------------------------------------------------------------------------

以上程序编译:

gcc -D_REENTRANT test.c -o test.o –lpthread

运行这个程序:

$ ./test.o:

如何在Windows下编译Linux内核

内核配置完成,输入make命令即可开始编译内核。如果没有修改Makefile文件并指定ARCH和CROSS_COMPILE参数,则须在命令行中指定:

$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-

目前大多数主机都是多核处理器,为了加快编译进度,可以开启多线程编译,在make的时候加上“-jN”即可,N的值为处理器核心数目的2倍。例如对于I7 4核处理器,可将N设置为8:

$ make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- -j8

采用多线程编译的优点是能加快编译进度,。具体可以参照ZLG《嵌入式Linux开发教程(下册)》第1章。

linux多线程编程 -- __thread

__thread 是GCC内置的线程局部存储设施,存取效率可以和全局变量相比。

__thread 变量 每一个线程有一份独立实体 ,各个线程的值互不干扰。可以用来修饰那些带有全局性且值可能变,但是又不值得用全局变量保护的变量。

只能修饰 POD 类型(类似整型指针的标量,不带自定义的构造、拷贝、赋值、析构的类型,二进制内容可以任意复制memset,memcpy,且内容可以复原)

  不能修饰 class 类型,因为无法自动调用构造函数和析构函数,可以用于修饰全局变量,函数内的静态变量,不能修饰函数的局部变量或者class的普通成员变量,且__thread变量值只能初始化为编译期常量,即编译期间就能确定值。

场景说明:每个线程有一些需要保存的上下文信息,即可使用 __thread 变量

LINUX下多线程编译问题

你编译的时候有加多线程连接选项吗? 要加上 -lpthread 或者 -pthread (尽量选后者)

例如 gcc -pthread -o test main.cpp

另外你的线程创建的不对,函数指针不能强转类型(这里也不用转)

pthread_create(producter_t,NULL,(void*)producter_f,NULL);

pthread_create(consumer_t,NULL,(void*)consumer_f,NULL);

应该是

pthread_create(producter_t,NULL,producter_f,NULL);

pthread_create(consumer_t,NULL,consumer_f,NULL);

linux里面线程编译运行问题

gcc xxx.c -lpthread 其中的-l是指包含的lib库,具体写法可以man gcc看下

多线程函数除了要包含头文件pthread.h外还必须要包含lib库pthread

pthread_create是创建线程,但具体的线程里面做什么事是在void *create(void *arg)里,这个函数名是自己任意区的,但返回值和参数一般都是void*类型,因为pthread_create函数的定义就是这样

int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);


本文标题:linux多线程编译命令 linux shell 多线程执行程序
浏览地址:http://ybzwz.com/article/docsooc.html