LinuxÒÁµéÔ°ÂÛ̳'s Archiver

yzli840324 ·¢±íÓÚ 2006-10-23 18:48

ÐÂÊÖÇóÖú£º¸ßÊÖÇë½ø

[font=ËÎÌå][font=ËÎÌå]ÐÂÊÖÇóÖú£º[/font][/font]
1I*{4\%l0H9G+Fu [font=ËÎÌå][font=ËÎÌå]cÓïÑÔ³ÌÐò[/font][/font]@$w uz x#r V4k&R
[font=ËÎÌå][font=ËÎÌå][/font][/font][font=ËÎÌå][font=ËÎÌå][font=ËÎÌå]¡°ÀûÓÃpthread±àдһ¸ö¶àÏ̳߳ÌÐòÀ´Ä£ÄâÁ½¸öÈËʹÓÃÁ½¸ö×Ô¶¯È¡¿î»úÀ´·ÃÎÊͬһ¸ö¹²ÏíÕʺŵÄÇé¿ö¡£¡°[/font]~v(`5|Ymc
[/font][/font]

sof_ljh ·¢±íÓÚ 2006-10-24 19:47

ÕâÓÐʲôÄѵģ¬Äãphtread_create´´ÔìÁ½¸öỊ̈߳¬ÉèÖÃÒ»¸öpthread_mutex¾ÍÐÐÁË£¬Ã¿Ò»¸öÏ߳̽øÈ¥phtread_lock(mutex),¾ÍÐа¢¡£.G;R2u"u4q%N)z5a/E%G}w
ÆäÓàµÄ×Ô¼º¸øÉϱßÌù´úÂë¾ÍÐа¢

yzli840324 ·¢±íÓÚ 2006-10-29 20:26

ÎҵĴúÂëΪʲô²»ÐУ¿CsAo {;Pe;~4oJ-V
³öÏÖ´íÎó£ºW!V(ej1O%Bu G
Abort : A memory image file is created as "core"
b9J9dv/q5l-gD.Y
DkWGx.aI4E #include"pthread.h"-n g&V)F^3Vh$eq-l2J
#include"stdio.h"
[T)CQ_ I #include"stdlib.h"*O8e6R.v__
pthread_mutex_t mutex;
2e(qB|O%\ void *function();#K'g)dYz7rF
double balance=500.0;?5IT8uB4g[
int main()K3T?rh
{&H^D})?:j*[7y'Q8J
int i,num_threads=2,a[2];
s5V.mp c |"{ v pthread_t p_threads[2]; ?z5r-B ^0y#{
pthread_attr_t attr;
1j5K7Ra#GU,x%F pthread_init(); w$t1X k_
pthread_mutex_init(&mutex,NULL);
3_VdF-w&D!D pthread_attr_init(&attr);(dqK7aa A
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);0lLS(`8e9` }
for(i=0;i<num_threads;i++)_1nI,S(x@b
if(pthread_create(&p_threads[i],NULL,function,NULL)!=0)4{:|7K5`&cH+l
perror("Pthread_create fails");wVp#cu{a
_5vq;b1c.G1]q
(Te#l LeU6} h1S
for(i=0;i<num_threads;i++)Y}s9u#m n Vf
{if ( pthread_join (p_threads[i], NULL ) )8UK p*~i3WwZ
    perror("error joining thread.");
9Lys3e*?;S abort();
+@1Iq4M8k/B2HD }S7glp9J9X
exit(0);L%ane~ Eo [
}2c'G3i#d$b Pc
_ {*_T$TR E
void *function()
i`Y SU8f z!R%U {
}7n2D H&~L?9ag2G char c;
7JOX3j5Z2E,T9? double amount1,amount2;}l c+Y&g3m
pthread_mutex_lock(&mutex);
o @yX;ay,A5^ printf("choose your service:\n");;cY @(Kb0[\
printf(" enter 1 deposit :\n");\J-bi)c r
printf(" enter 2 withdraw: \n");
0Wr&l7v5JmG scanf("%c",&c);oH(ZKvsJ-w|] W.B
switch(c)
P^Y4Dv)B-S {
"CV2w`*H` case '1':II9^+M&o([~
printf(" enter the amount deposit:\n");
'I.p4HH?:h scanf("%lf",&amount1);
?&yom8x Y i~x&I if(amount1<0)
)Q j9TG+{'|*f printf("error:deposit amount is invalidate");,Q;UtG3z Z
else{
Do7N6\[ |`kO balance=balance+amount1;
p/j-u%w e5S+x }AQ7o w3a/]r6E
printf("Available: %lf\n",balance);
!EC.p6QMP1H%wX [9Rgn break;:@7}laJw:U
case '2':`n#R _|3[
printf("enter the amount withdraw:\n");
1z? C z3YuL q&y0@e0W scanf("%lf",&amount2);)x`/}2S/[+`ilX
if(amount2<0)
A$GUTF'[ Hd printf("error: withdraw amount is invalidate");9{AP7V:]*h|
else{Ibv t X1r8ZJ"u
if(amount2>balance){
RJK3qJ$MW"Uk"~ printf("error:insufficient funds");
+l'{!c `$V%dx }
'pC7` c:qsu,e/Mn else{
,l+n@!t3uu balance=balance-amount2;M8I B o$xq
     }
,sdqQFFl 6Siq9R8Agke [.O
    }Y~!~[t.U1@R5U
printf("Available: %lf\n",balance);
3Oyg/g zT1^5\4p B }U/w_PV']0a
pthread_mutex_unlock(&mutex);
/Zz+@ ] @ E;I,n#Z wA return;FC.M"g+K-m
}

sof_ljh ·¢±íÓÚ 2006-11-2 17:17

perror²»ÊÇ¿ÉÖØÈ뺯Êý£¡ÄãÔÚ±àд¶àÏß³Ìʱ²»ÄÜÅжÏÊÇ·ÇÕý³£½áÊøµÄ£¡

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.