本文目錄一覽:
- 1、C語言多線程編程為什麼要用pthread
- 2、c語言編程問題題目,給定3條線的長度,求出用這3條線組成的三角形的面積
- 3、在C語言的多線程編程中一般volatile應該用在什麼地方?
- 4、C語言程序編程
- 5、c語言多進程編程
C語言多線程編程為什麼要用pthread
3個線程使用的都是同一個info 代碼 Info_t *info = (Info_t *)malloc(sizeof(Info_t));只創建了一個info pthread_create(threads[i],NULL,calMatrix,(void *)info); 三個線程使用的是同一個 我把你的代碼改了下
c語言編程問題題目,給定3條線的長度,求出用這3條線組成的三角形的面積
#include stdio.h
#include math.h
int main()
{
int a,b,c;
double area,p;
while(scanf(“%d%d%d”,a,b,c)==3)
{
if(a+bcb+caa+cb)
{
p=(a+b+c)/2.0;
area=sqrt(p*(p-a)*(p-b)*(p-c));
}
else
{
area=-1;
}
printf(“%.0f\n”,area);
}
return 0;
}
在C語言的多線程編程中一般volatile應該用在什麼地方?
一般用在多線程程序中,由於某個變量可能被多個線程修改,因此,修飾為volatile,使其每次讀取,都是從存儲volatile變量的地址中去取,而不是取寄存器中的值。
volatile int a;
你的這種設想,都是最好要用volatile的地方,只要這個變量被反覆頻繁的修改,最好用volatile
C語言程序編程
10.
#includestdio.h
main()
{
int i,x,y,z;
printf(“所有的水仙花數為:\n”);
for(i=100;i1000;i++)
{
x=i/100;y=i/10%10;z=i%10;
if(i==x*x*x+y*y*y+z*z*z)
printf(“%5d”,i);
}
printf(“\n”);
}
c語言多進程編程
多進程這個詞用得比較少,聽過來有點不熟悉。你這個程序在linux下應該很容易實行,就是個進程間通信的問題,管道、消息隊列、共享內存都可以,可以找找相關資料。昨天失言不好意思。
三個源文件分別為1.c、2.c、3.c一個頭文件share.h。
share.h:
//共享的內存,兩個數組
typedef struct{
int a[2];
int b[2];
int id;
}share_use;
1.c:
#includeunistd.h
#includestdio.h
#includestdlib.h
#includestring.h
#includesys/types.h
#includesys/ipc.h
#includesys/shm.h
#include”share.h”
int main(){
void *shared_memory = (void *)0;
share_use *share_stuff;
int shmid;
shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//創建共享內存
if(shmid==-1){
fprintf(stderr,”共享內存創建失敗!\n”);
exit(1);
}
shared_memory = shmat(shmid, (void *)0,0);//讓進程可以訪問共享內存
if(shared_memory == (void *)-1){
fprintf(stderr,”啟用共享內存失敗!\n)”;
exit(1);
}
printf(“Memory attached at %X\n”,(int)shared_memory);
share_stuff = (share_use *)shared_memory;
share_stuff-id=0;
share_stuff-a[0]=1;
share_stuff-a[1]=2;
while(1){
if(share_stuff-id)
exit(0);
sleep(10);
}
}
2.c:
#includeunistd.h
#includestdio.h
#includestdlib.h
#includestring.h
#includesys/types.h
#includesys/ipc.h
#includesys/shm.h
#include”share.h”
int main(){
void *shared_memory = (void *)0;
share_use *share_stuff;
int shmid;
shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//創建共享內存
if(shmid==-1){
fprintf(stderr,”共享內存創建失敗!\n”);
exit(1);
}
shared_memory = shmat(shmid, (void *)0,0);//讓進程可以訪問共享內存
if(shared_memory == (void *)-1){
fprintf(stderr,”啟用共享內存失敗!\n”);
exit(1);
}
printf(“Memory attached at %X\n”,(int)shared_memory);
share_stuff = (share_use *)shared_memory;
share_stuff-b[0]=share_stuff-a[0]*100;
share_stuff-b[1]=share_stuff-a[1]*100;
while(1)
{
if(share_stuff-id)
exit(0);
sleep(10);
}
}
3.c:
#includeunistd.h
#includestdio.h
#includestdlib.h
#includestring.h
#includesys/types.h
#includesys/ipc.h
#includesys/shm.h
#include”share.h”
int main(){
void *shared_memory = (void *)0;
share_use *share_stuff;
int shmid;
shmid=shmget((key_t)1234,sizeof(share_use),0666|IPC_CREAT);//創建共享內存
if(shmid==-1){
fprintf(stderr,”共享內存創建失敗!\n”);
exit(1);
}
shared_memory = shmat(shmid, (void *)0,0);//讓進程可以訪問共享內存
if(shared_memory == (void *)-1){
fprintf(stderr,”啟用共享內存失敗!\n”);
exit(1);
}
printf(“Memory attached at %X\n”,(int)shared_memory);
share_stuff = (share_use *)shared_memory;
printf(“共享內存中有元素:%d , %d”,share_stuff-b[0],share_stuff-b[1]);
share_stuff-id=1;
return 0;
}
linux或unix環境下編譯
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/158322.html