user-0 提问: 关于用C写简易用户级线程库的问题

回复:
user: user-0
content: /*  * types used by thread library  */ /*  * types used by thread library  */ #include <stdio.h> #include <stdlib.h> #include <ucontext.h> #include <sys/mman.h>  struct TCB {   int thread_id;   int thread_priority;   ucontext_t thread_context;   struct TCB *next; };  typedef struct TCB TCB;  struct Queue {   TCB *front;   TCB *rear; };  typedef struct Queue Queue;  //utility functions  // Create new TCB  TCB* createTCB(ucontext_t *context,int threadId, int threadPriority) {   TCB *newT = (TCB *)malloc(sizeof(TCB));   newT->thread_context = *context;   newT->thread_id = threadId;   newT->thread_priority = threadPriority;   return newT; }  // Initialize a queue void initQueue(Queue *cq) {   cq->front = cq->rear = NULL; }  // Insert a new TCB into a Queue void Enqueue(Queue *cq, TCB *newT) {   newT->next = NULL;   if(cq->front == NULL && cq->rear == NULL){ 	cq->front = cq->rear = newT; 	return;   }   cq->rear->next = newT;   cq->rear = newT; }  // Remove the front TCB from a Queue void Dequeue(Queue *cq) { 	TCB *tmp = cq->front;  	if (cq->front == NULL) { 		printf("Access Error: Queue is Empty...\n"); 		return; 	}  	if (cq->front == cq->rear) { 		cq->front = cq->rear = NULL; 	} 	else { 		cq->front = cq->front->next; 	} 	free(tmp); }    #include "t_lib.h"  Queue *runningQueue; Queue *readyQueue; ucontext_t *newT; ucontext_t *mainT; TCB *current;  void t_init() {   runningQueue = (Queue *)malloc(sizeof(Queue));   readyQueue = (Queue *)malloc(sizeof(Queue));      initQueue(runningQueue);   initQueue(readyQueue);   mainT = (ucontext_t *) malloc(sizeof(ucontext_t));    getcontext(mainT);    /* let mainT be the context of main thread */    Enqueue(runningQueue,createTCB(mainT,0,1)); }  void t_shutdown()  { 	free(runningQueue); 	free(readyQueue); 	free(mainT); 	free(newT); 	free(current); }  int t_create(void (*fct)(int), int id, int pri) { 	size_t size = 0x10000; 	newT = (ucontext_t *) malloc(sizeof(ucontext_t)); 	 	getcontext(newT);  /* let newT be the context of new thread */ 	newT->uc_stack.ss_sp = malloc(size);  /* new statement */   newT->uc_stack.ss_size = size;   newT->uc_stack.ss_flags = 0;   newT->uc_link = &runningQueue->front->thread_context;  	makecontext(newT, (void (*)(void)) fct, 1, id); 	 	Enqueue(readyQueue,createTCB(newT,id,pri)); //	setcontext(newT);  // switch to new one }  void t_terminate()  { 	setcontext(&readyQueue->front->thread_context); 	Dequeue(runningQueue); //	runningQueue->front = readyQueue->front; //	Dequeue(readyQueue); }  void t_yield()  {  		current = (TCB *)malloc(sizeof(TCB));     current = runningQueue->front;          Enqueue(readyQueue, current);          runningQueue->front = readyQueue->front;          Dequeue(readyQueue);     swapcontext(&readyQueue->rear->thread_context, &runningQueue->front->thread_context);  }

user: user-1
content: 仅供参考：//循环向a函数每次发送200个字节长度（这个是固定的）的buffer, //a函数中需要将循环传进来的buffer，组成240字节（也是固定的）的新buffer进行处理， //在处理的时候每次从新buffer中取两个字节打印 #ifdef WIN32     #pragma warning(disable:4996) #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32     #include <windows.h>     #include <process.h>     #include <io.h>     #define  MYVOID             void     #define  vsnprintf          _vsnprintf #else     #include <unistd.h>     #include <sys/time.h>     #include <pthread.h>     #define  CRITICAL_SECTION   pthread_mutex_t     #define  MYVOID             void * #endif //Log{ #define MAXLOGSIZE 20000000 #define MAXLINSIZE 16000 #include <time.h> #include <sys/timeb.h> #include <stdarg.h> char logfilename1[]="MyLog1.log"; char logfilename2[]="MyLog2.log"; static char logstr[MAXLINSIZE+1]; char datestr[16]; char timestr[16]; char mss[4]; CRITICAL_SECTION cs_log; FILE *flog; #ifdef WIN32 void Lock(CRITICAL_SECTION *l) {     EnterCriticalSection(l); } void Unlock(CRITICAL_SECTION *l) {     LeaveCriticalSection(l); } void sleep_ms(int ms) {     Sleep(ms); } #else void Lock(CRITICAL_SECTION *l) {     pthread_mutex_lock(l); } void Unlock(CRITICAL_SECTION *l) {     pthread_mutex_unlock(l); } void sleep_ms(int ms) {     usleep(ms*1000); } #endif void LogV(const char *pszFmt,va_list argp) {     struct tm *now;     struct timeb tb;      if (NULL==pszFmt||0==pszFmt[0]) return;     vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);     ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,logstr);     flog=fopen(logfilename1,"a");     if (NULL!=flog) {         fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);         if (ftell(flog)>MAXLOGSIZE) {             fclose(flog);             if (rename(logfilename1,logfilename2)) {                 remove(logfilename2);                 rename(logfilename1,logfilename2);             }         } else {             fclose(flog);         }     } } void Log(const char *pszFmt,...) {     va_list argp;      Lock(&cs_log);     va_start(argp,pszFmt);     LogV(pszFmt,argp);     va_end(argp);     Unlock(&cs_log); } //Log} #define ASIZE    200 #define BSIZE    240 #define CSIZE      2 char Abuf[ASIZE]; char Cbuf[CSIZE]; CRITICAL_SECTION cs_HEX ; CRITICAL_SECTION cs_BBB ; struct FIFO_BUFFER {     int  head;     int  tail;     int  size;     char data[BSIZE]; } BBB; int No_Loop=0; void HexDump(int cn,char *buf,int len) {     int i,j,k;     char binstr[80];      Lock(&cs_HEX);     for (i=0;i<len;i++) {         if (0==(i%16)) {             sprintf(binstr,"%03d %04x -",cn,i);             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         } else if (15==(i%16)) {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);             sprintf(binstr,"%s  ",binstr);             for (j=i-15;j<=i;j++) {                 sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');             }             Log("%s\n",binstr);         } else {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         }     }     if (0!=(i%16)) {         k=16-(i%16);         for (j=0;j<k;j++) {             sprintf(binstr,"%s   ",binstr);         }         sprintf(binstr,"%s  ",binstr);         k=16-k;         for (j=i-k;j<i;j++) {             sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');         }         Log("%s\n",binstr);     }     Unlock(&cs_HEX); } int GetFromRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {     int lent,len1,len2;      lent=0;     Lock(cs);     if (fbuf->size>=len) {         lent=len;         if (fbuf->head+lent>BSIZE) {             len1=BSIZE-fbuf->head;             memcpy(buf     ,fbuf->data+fbuf->head,len1);             len2=lent-len1;             memcpy(buf+len1,fbuf->data           ,len2);             fbuf->head=len2;         } else {             memcpy(buf     ,fbuf->data+fbuf->head,lent);             fbuf->head+=lent;         }         fbuf->size-=lent;     }     Unlock(cs);     return lent; } MYVOID thdB(void *pcn) {     char        *recv_buf;     int          recv_nbytes;     int          cn;     int          wc;     int          pb;      cn=(int)pcn;     Log("%03d thdB              thread begin...\n",cn);     while (1) {         sleep_ms(10);         recv_buf=(char *)Cbuf;         recv_nbytes=CSIZE;         wc=0;         while (1) {             pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);             if (pb) {                 Log("%03d recv %d bytes\n",cn,pb);                 HexDump(cn,recv_buf,pb);                 sleep_ms(1);             } else {                 sleep_ms(1000);             }             if (No_Loop) break;//             wc++;             if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);         }         if (No_Loop) break;//     } #ifndef WIN32     pthread_exit(NULL); #endif } int PutToRBuf(int cn,CRITICAL_SECTION *cs,struct FIFO_BUFFER *fbuf,char *buf,int len) {     int lent,len1,len2;      Lock(cs);     lent=len;     if (fbuf->size+lent>BSIZE) {         lent=BSIZE-fbuf->size;     }     if (fbuf->tail+lent>BSIZE) {         len1=BSIZE-fbuf->tail;         memcpy(fbuf->data+fbuf->tail,buf     ,len1);         len2=lent-len1;         memcpy(fbuf->data           ,buf+len1,len2);         fbuf->tail=len2;     } else {         memcpy(fbuf->data+fbuf->tail,buf     ,lent);         fbuf->tail+=lent;     }     fbuf->size+=lent;     Unlock(cs);     return lent; } MYVOID thdA(void *pcn) {     char        *send_buf;     int          send_nbytes;     int          cn;     int          wc;     int           a;     int          pa;      cn=(int)pcn;     Log("%03d thdA              thread begin...\n",cn);     a=0;     while (1) {         sleep_ms(100);         memset(Abuf,a,ASIZE);         a=(a+1)%256;         if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop         send_buf=(char *)Abuf;         send_nbytes=ASIZE;         Log("%03d sending %d bytes\n",cn,send_nbytes);         HexDump(cn,send_buf,send_nbytes);         wc=0;         while (1) {             pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);             Log("%03d sent %d bytes\n",cn,pa);             HexDump(cn,send_buf,pa);             send_buf+=pa;             send_nbytes-=pa;             if (send_nbytes<=0) break;//             sleep_ms(1000);             if (No_Loop) break;//             wc++;             if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);         }         if (No_Loop) break;//     } #ifndef WIN32     pthread_exit(NULL); #endif } int main() { #ifdef WIN32     InitializeCriticalSection(&cs_log);     InitializeCriticalSection(&cs_HEX );     InitializeCriticalSection(&cs_BBB ); #else     pthread_t threads[2];     int threadsN;     int rc;     pthread_mutex_init(&cs_log,NULL);     pthread_mutex_init(&cs_HEX,NULL);     pthread_mutex_init(&cs_BBB,NULL); #endif     Log("Start===========================================================\n");      BBB.head=0;     BBB.tail=0;     BBB.size=0;  #ifdef WIN32     _beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);     _beginthread((void(__cdecl *)(void *))thdB,0,(void *)2); #else     threadsN=0;     rc=pthread_create(&(threads[threadsN++]),NULL,thdA,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);     rc=pthread_create(&(threads[threadsN++]),NULL,thdB,(void *)2);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1); #endif      if (!access("No_Loop",0)) {         remove("No_Loop");         if (!access("No_Loop",0)) {             No_Loop=1;         }     }     while (1) {         sleep_ms(1000);         if (No_Loop) break;//         if (!access("No_Loop",0)) {             No_Loop=1;         }     }     sleep_ms(3000);     Log("End=============================================================\n"); #ifdef WIN32     DeleteCriticalSection(&cs_BBB );     DeleteCriticalSection(&cs_HEX );     DeleteCriticalSection(&cs_log); #else     pthread_mutex_destroy(&cs_BBB);     pthread_mutex_destroy(&cs_HEX);     pthread_mutex_destroy(&cs_log); #endif     return 0; }

user: user-2
content: 代码不全 另外代码要用格式插入

user: user-0
content: 我第一次来这边问问题 还不会用

user: user-0
content: 不是的，就只是简单的在linux上写一个等同于pthread库作用的用户级库

user: user-2
content: ucos吗?

user: user-0
content: This is main()#0 created single thread[1]... This is main()#0 created double thread[2]... This is main()#0 created triple thread[5]... This is main()#0 created quad thread[15]... This is main()[0]... this is thread #1 [0]... this is double thread #2, created single thread #3. this is double thread #2, created single thread #4. this is triple thread #5, created double thread #6. this is triple thread #5, created double thread #9. this is triple thread #5, created double thread #12. this is quad thread #15, created single thread #16. this is quad thread #15, created single thread #17. this is quad thread #15, created single thread #18. this is quad thread #15, created single thread #19. This is main()[1]... this is thread #1 [1]... this is thread #3 [0]... this is thread #4 [0]... this is 'double' thread 2 [0]... this is double thread #6, created single thread #7. this is double thread #6, created single thread #8. this is double thread #9, created single thread #10. this is double thread #9, created single thread #11. this is double thread #12, created single thread #13. this is double thread #12, created single thread #14. this is 'triple' thread 5 [0]... this is thread #16 [0]... this is thread #17 [0]... this is thread #18 [0]... this is thread #19 [0]... this is 'quad' thread #15 [0]... This is main()[2]... this is thread #1 [2]... this is thread #3 [1]... this is thread #4 [1]... this is 'double' thread 2 [1]... this is thread #7 [0]... this is thread #8 [0]... this is 'double' thread 6 [0]... this is thread #10 [0]... this is thread #11 [0]... this is 'double' thread 9 [0]... this is thread #13 [0]... this is thread #14 [0]... this is 'double' thread 12 [0]... this is 'triple' thread 5 [1]... this is thread #16 [1]... this is thread #17 [1]... this is thread #18 [1]... this is thread #19 [1]... this is 'quad' thread #15 [1]... This is main()[3]... this is thread #1 [3]... this is thread #3 [2]... this is thread #4 [2]... this is 'double' thread 2 [2]... this is thread #7 [1]... this is thread #8 [1]... this is 'double' thread 6 [1]... this is thread #10 [1]... this is thread #11 [1]... this is 'double' thread 9 [1]... this is thread #13 [1]... this is thread #14 [1]... this is 'double' thread 12 [1]... this is 'triple' thread 5 [2]... this is thread #16 [2]... this is thread #17 [2]... this is thread #18 [2]... this is thread #19 [2]... this is 'quad' thread #15 [2]... This is main()[4]... this is thread #1 [4]... this is thread #3 [3]... this is thread #4 [3]... this is 'double' thread 2 [3]... this is thread #7 [2]... this is thread #8 [2]... this is 'double' thread 6 [2]... this is thread #10 [2]... this is thread #11 [2]... this is 'double' thread 9 [2]... this is thread #13 [2]... this is thread #14 [2]... this is 'double' thread 12 [2]... this is 'triple' thread 5 [3]... this is thread #16 [3]... this is thread #17 [3]... this is thread #18 [3]... this is thread #19 [3]... this is 'quad' thread #15 [3]... This is main()[5]... this is thread #1 [5]... this is thread #3 [4]... this is thread #4 [4]... this is 'double' thread 2 [4]... this is thread #7 [3]... this is thread #8 [3]... this is 'double' thread 6 [3]... this is thread #10 [3]... this is thread #11 [3]... this is 'double' thread 9 [3]... this is thread #13 [3]... this is thread #14 [3]... this is 'double' thread 12 [3]... this is 'triple' thread 5 [4]... this is thread #16 [4]... this is thread #17 [4]... this is thread #18 [4]... this is thread #19 [4]... Thread 15 is done... This is main()[6]... this is thread #1 [6]... this is thread #3 [5]... this is thread #4 [5]... this is 'double' thread 2 [5]... this is thread #7 [4]... this is thread #8 [4]... this is 'double' thread 6 [4]... this is thread #10 [4]... this is thread #11 [4]... this is 'double' thread 9 [4]... this is thread #13 [4]... this is thread #14 [4]... this is 'double' thread 12 [4]... this is 'triple' thread 5 [5]... this is thread #16 [5]... this is thread #17 [5]... this is thread #18 [5]... this is thread #19 [5]... This is main()[7]... this is thread #1 [7]... this is thread #3 [6]... this is thread #4 [6]... this is 'double' thread 2 [6]... this is thread #7 [5]... this is thread #8 [5]... this is 'double' thread 6 [5]... this is thread #10 [5]... this is thread #11 [5]... this is 'double' thread 9 [5]... this is thread #13 [5]... this is thread #14 [5]... this is 'double' thread 12 [5]... Thread 5 is done... this is thread #16 [6]... this is thread #17 [6]... this is thread #18 [6]... this is thread #19 [6]... This is main()[8]... this is thread #1 [8]... this is thread #3 [7]... this is thread #4 [7]... this is 'double' thread 2 [7]... this is thread #7 [6]... this is thread #8 [6]... this is 'double' thread 6 [6]... this is thread #10 [6]... this is thread #11 [6]... this is 'double' thread 9 [6]... this is thread #13 [6]... this is thread #14 [6]... this is 'double' thread 12 [6]... this is thread #16 [7]... this is thread #17 [7]... this is thread #18 [7]... this is thread #19 [7]... This is main()[9]... this is thread #1 [9]... this is thread #3 [8]... this is thread #4 [8]... Thread 2 is done... this is thread #7 [7]... this is thread #8 [7]... this is 'double' thread 6 [7]... this is thread #10 [7]... this is thread #11 [7]... this is 'double' thread 9 [7]... this is thread #13 [7]... this is thread #14 [7]... this is 'double' thread 12 [7]... this is thread #16 [8]... this is thread #17 [8]... this is thread #18 [8]... this is thread #19 [8]... This is main()[10]... Thread 1 is done... this is thread #3 [9]... this is thread #4 [9]... this is thread #7 [8]... this is thread #8 [8]... Thread 6 is done... this is thread #10 [8]... this is thread #11 [8]... Thread 9 is done... this is thread #13 [8]... this is thread #14 [8]... Thread 12 is done... this is thread #16 [9]... this is thread #17 [9]... this is thread #18 [9]... this is thread #19 [9]... This is main()[11]... Thread 3 is done... Thread 4 is done... this is thread #7 [9]... this is thread #8 [9]... this is thread #10 [9]... this is thread #11 [9]... this is thread #13 [9]... this is thread #14 [9]... Thread 16 is done... Thread 17 is done... Thread 18 is done... Thread 19 is done... This is main()[12]... Thread 7 is done... Thread 8 is done... Thread 10 is done... Thread 11 is done... Thread 13 is done... Thread 14 is done... This is main()[13]... This is main()[14]... This is main()[15]... Begin shutdown... Done with shutdown...

user: user-0
content: 第二个测试文件中run出来的结果是： This is main()#0 created single thread[1]... This is main()#0 created double thread[2]... This is main()#0 created triple thread[5]... This is main()#0 created quad thread[15]... This is main()[0]... this is thread #1 [0]... this is double thread #2, created single thread #3. this is double thread #2, created single thread #4. this is triple thread #5, created double thread #6. this is triple thread #5, created double thread #9. this is triple thread #5, created double thread #12. this is quad thread #15, created single thread #16. this is quad thread #15, created single thread #17. this is quad thread #15, created single thread #18. this is quad thread #15, created single thread #19. This is main()[1]... this is thread #1 [1]... this is thread #3 [0]... this is thread #4 [0]... this is 'double' thread 2 [0]... this is double thread #6, created single thread #7. this is double thread #6, created single thread #8. this is double thread #9, created single thread #10. this is double thread #9, created single thread #11. this is double thread #12, created single thread #13. this is double thread #12, created single thread #14. this is 'triple' thread 5 [0]... this is thread #16 [0]... this is thread #17 [0]... this is thread #18 [0]... this is thread #19 [0]... this is 'quad' thread #15 [0]... This is main()[2]... this is thread #1 [2]... this is thread #3 [1]... this is thread #4 [1]... this is 'double' thread 2 [1]... this is thread #7 [0]... this is thread #8 [0]... this is 'double' thread 6 [0]... this is thread #10 [0]... this is thread #11 [0]... this is 'double' thread 9 [0]... this is thread #13 [0]... this is thread #14 [0]... this is 'double' thread 12 [0]... this is 'triple' thread 5 [1]... this is thread #16 [1]... this is thread #17 [1]... this is thread #18 [1]... this is thread #19 [1]... this is 'quad' thread #15 [1]... This is main()[3]... this is thread #1 [3]... this is thread #3 [2]... this is thread #4 [2]... this is 'double' thread 2 [2]... this is thread #7 [1]... this is thread #8 [1]... this is 'double' thread 6 [1]... this is thread #10 [1]... this is thread #11 [1]... this is 'double' thread 9 [1]... this is thread #13 [1]... this is thread #14 [1]... this is 'double' thread 12 [1]... this is 'triple' thread 5 [2]... this is thread #16 [2]... this is thread #17 [2]... this is thread #18 [2]... this is thread #19 [2]... this is 'quad' thread #15 [2]... This is main()[4]... this is thread #1 [4]... this is thread #3 [3]... this is thread #4 [3]... this is 'double' thread 2 [3]... this is thread #7 [2]... this is thread #8 [2]... this is 'double' thread 6 [2]... this is thread #10 [2]... this is thread #11 [2]... this is 'double' thread 9 [2]... this is thread #13 [2]... this is thread #14 [2]... this is 'double' thread 12 [2]... this is 'triple' thread 5 [3]... this is thread #16 [3]... this is thread #17 [3]... this is thread #18 [3]... this is thread #19 [3]... this is 'quad' thread #15 [3]... This is main()[5]... this is thread #1 [5]... this is thread #3 [4]... this is thread #4 [4]... this is 'double' thread 2 [4]... this is thread #7 [3]... this is thread #8 [3]... this is 'double' thread 6 [3]... this is thread #10 [3]... this is thread #11 [3]... this is 'double' thread 9 [3]... this is thread #13 [3]... this is thread #14 [3]... this is 'double' thread 12 [3]... this is 'triple' thread 5 [4]... this is thread #16 [4]... this is thread #17 [4]... this is thread #18 [4]... this is thread #19 [4]... Thread 15 is done... This is main()[6]... This is main()[7]... this is thread #1 [6]... this is thread #3 [5]... this is thread #4 [5]... this is 'double' thread 2 [5]... this is thread #7 [4]... this is thread #8 [4]... this is 'double' thread 6 [4]... this is thread #10 [4]... this is thread #11 [4]... this is 'double' thread 9 [4]... this is thread #13 [4]... this is thread #14 [4]... this is 'double' thread 12 [4]... this is 'triple' thread 5 [5]... this is thread #16 [5]... this is thread #17 [5]... this is thread #18 [5]... this is thread #19 [5]... This is main()[8]... This is main()[9]... this is thread #1 [7]... this is thread #3 [6]... this is thread #4 [6]... this is 'double' thread 2 [6]... this is thread #7 [5]... this is thread #8 [5]... this is 'double' thread 6 [5]... this is thread #10 [5]... this is thread #11 [5]... this is 'double' thread 9 [5]... this is thread #13 [5]... this is thread #14 [5]... this is 'double' thread 12 [5]... Thread 5 is done... this is thread #16 [6]... this is thread #16 [7]... this is thread #17 [6]... this is thread #18 [6]... this is thread #19 [6]... This is main()[10]... This is main()[11]... this is thread #1 [8]... this is thread #3 [7]... this is thread #4 [7]... this is 'double' thread 2 [7]... this is thread #7 [6]... this is thread #8 [6]... this is 'double' thread 6 [6]... this is thread #10 [6]... this is thread #11 [6]... this is 'double' thread 9 [6]... this is thread #13 [6]... this is thread #14 [6]... this is 'double' thread 12 [6]... this is thread #16 [8]... this is thread #16 [9]... this is thread #17 [7]... this is thread #18 [7]... this is thread #19 [7]... This is main()[12]... This is main()[13]... this is thread #1 [9]... this is thread #3 [8]... this is thread #4 [8]... Thread 2 is done... this is thread #7 [7]... this is thread #7 [8]... this is thread #8 [7]... this is 'double' thread 6 [7]... this is thread #10 [7]... this is thread #11 [7]... this is 'double' thread 9 [7]... this is thread #13 [7]... this is thread #14 [7]... this is 'double' thread 12 [7]... Thread 16 is done... This is main()[14]... Segmentation fault 就会有分段错误，按道理应该输出

user: user-0
content: 另外附上我写的功能方程： #include <stdio.h> #include <stdlib.h> #include <ucontext.h> #include <sys/mman.h>  struct TCB {   int thread_id;   int thread_priority;   ucontext_t thread_context;   struct TCB *next; };  typedef struct TCB TCB;  struct Queue {   TCB *front;   TCB *rear; };  typedef struct Queue Queue;  //utility functions  // Create new TCB  TCB* createTCB(ucontext_t *context,int threadId, int threadPriority) {   TCB *newT = (TCB *)malloc(sizeof(TCB));   newT->thread_context = *context;   newT->thread_id = threadId;   newT->thread_priority = threadPriority;   return newT; }  // Initialize a queue void initQueue(Queue *cq) {   cq->front = cq->rear = NULL; }  // Insert a new TCB into a Queue void Enqueue(Queue *cq, TCB *newT) {   newT->next = NULL;   if(cq->front == NULL && cq->rear == NULL){ 	cq->front = cq->rear = newT; 	return;   }   cq->rear->next = newT;   cq->rear = newT; }  // Remove the front TCB from a Queue void Dequeue(Queue *cq) { 	TCB *tmp = cq->front;  	if (cq->front == NULL) { 		printf("Access Error: Queue is Empty...\n"); 		return; 	}  	if (cq->front == cq->rear) { 		cq->front = cq->rear = NULL; 	} 	else { 		cq->front = cq->front->next; 	} 	free(tmp); }