union member{ int a; char b; }x; int sum=0; //全局变量sum,x int m; //m is an variate to store x.a. char n; //n is an variate to store x.b. _Bool flag,conditions; //flag distinguishes int(1) and char(0);conditions functions like "pthread_cond_signal(&cond)" from Input to Sum but also from Sum to Sum.
void *Input(void* arg) { printf("Please input an integer or a character:\n"); //printf("**pthread 1 starts.\n"); while(1) { if(scanf("%d",&x.a)) { m=x.a; flag=1; conditions=1; //printf("**pthread 1 signal conditions to 2.\n"); } else { scanf("%c",&x.b); n=x.b; flag=0; conditions=1; //printf("**pthread 1 signal conditions to 2.\n"); }
//printf("**flag in pthread2 is %d\n",flag); if(flag) { for(i=0,sum=0;i<=m;i++) { sum=sum+i; } printf("the sum from 0 to %d is %d\n",m,sum); conditions=0; sleep(1); printf("Please input an integer or a character:\n"); } else { //printf("**judging the type of character.\n"); switch(n) { case 'p': { printf("Please input an integer or a character:\n"); while((!flag)&&(n=='p')) { sleep(1); } //conditions=1; break; };//stop Sum, use loop case 'e': { printf("exit.\n"); exit(0); break; };//exit sum.exe default: { printf("error.\n"); printf("Please input an integer or a character:\n"); setbuf(stdin, NULL); break; };//child process continue. } //printf("**pthread 2 ends.\n"); } } }