Linux C 用链表简单实现消息队列

001 #include"stdio.h"  

002 #include"pthread.h"  

003 #include"string.h"  

004 #include"stdlib.h"  

005 #include"semaphore.h"  

006 #include<unistd.h>  

007    

008 #define Success 0  

009 #define Failure -1  

010    

011 struct Node  

012 {  

013     int iStatus;  

014     int iFlag;  

015     char acBuf[128];  

016     struct Node *next;  

017 };  

018    

019 sem_t semSend;  

020 sem_t semRevi;  

021 pthread_mutex_t testlock;  

022    

023    

024 // 工作链表  

025 struct Node *WorkingHead = NULL;  

026 struct Node *WorkingTail = NULL;  

027 int g_WorkingNodeNum = 4;  

028    

029 // 空闲链表  

030 struct Node *SleepingHead = NULL;  

031 struct Node *SleepingTail = NULL;  

032 int g_SleepingNodeNum = 0;  

033    

034 //初始化工作链表  

035  void InitWorkList()  

036 {  

037     static int iNum = 1;  

038     if(1 == iNum)  

039     {  

040         struct Node *p = (struct Node *)malloc(sizeof(struct Node));  

041         p->iStatus = 'W';  

042         p->iFlag = 1;  

043         strcpy(p->acBuf,"message");  

044         WorkingHead = WorkingTail = p;  

045         WorkingTail->next = NULL;  

046         iNum++;  

047     }  

048     else 

049     {  

050         struct Node *p = (struct Node *)malloc(sizeof(struct Node));  

051         p->iStatus = 'W';  

052         p->iFlag = 1;  

053         strcpy(p->acBuf,"message");  

054         WorkingTail->next = p;  

055         WorkingTail = p;  

056         WorkingTail->next = NULL;  

057     }  

058 }  

059    

060 // 遍历链表(WorkingList,SleepingList)  

061 void AccessList(struct Node *head)  

062 {  

063     struct Node *pst = head;  

064     int i = 1;  

065     while(NULL != pst)  

066     {  

067         printf("节点 %d : Status  %c  iFlag %d  %s \n",i,pst->iStatus,pst->iFlag,pst->acBuf);  

068         i++;  

069         pst=pst->next;  

070     }  

071 }  

072    

073 // 删除工作链表中的一个节点到空闲链表  

074 void *DelWtoS()  

075 {  

076     struct Node *Wpst;  

077     struct Node * pTemp;  

078        

079     while(1)  

080     {     

081         sem_wait(&semRevi);  

082         Wpst = WorkingHead;  

083        

084         while(Wpst->next != NULL)  

085          {  

086             pTemp = Wpst;  

087             Wpst = Wpst->next;  

088          }  

089      

090         pthread_mutex_lock(&testlock);  

091        

092         pTemp->next = NULL;  

093    

094         g_WorkingNodeNum--;  

095        

096     // 读取工作链表中的最后一个节点的信息,并实施删除  

097         printf("the end node message is %s\n",Wpst->acBuf);  

098     //   

099        

100         if (g_SleepingNodeNum == 0)  

101             {  

102                 Wpst->iStatus = 'S';  

103                 Wpst->iFlag = -1;  

104                 SleepingTail = Wpst;  

105                 SleepingHead = SleepingTail;  

106                 SleepingTail->next = NULL;  

107                 g_SleepingNodeNum++;  

108                 printf("add it to SleepList\n");  

109             }  

110         else 

111             {  

112                 Wpst->iStatus = 'S';  

113                 Wpst->iFlag = -1;  

114                 SleepingTail->next = Wpst;  

115                 SleepingTail = Wpst;  

116                 SleepingTail->next = NULL;   

117                 g_SleepingNodeNum++;  

118                 printf("add it to SleepList\n");  

119             }  

120         printf("node %d be added\n",g_SleepingNodeNum);  

121        

122         printf("-------------------Workinglist----------------- \n");  

123         AccessList(WorkingHead);  

124         printf("-------------------Sleepinglist-----------------\n");  

125         AccessList(SleepingHead);  

126    

127         pthread_mutex_unlock(&testlock);      

128         sem_post(&semSend);  

129        

130         sleep(5);  

131     }  

132     return NULL;      

133 }  

134    

135 // 删除空闲链表的一个节点到工作链表  

136 void *DelStoW()  

137 {  

138     struct Node * Spst;  

139     struct Node * pTemp;  

140        

141 // 用户输入时,从空闲链表删除一个节点,并添加到工作链表  

142     while(1)      

143     {  

144         sem_wait(&semSend);  

145         Spst=SleepingHead;  

146    

147         if ( 1 == g_SleepingNodeNum)  

148             {     

149                 pthread_mutex_lock(&testlock);  

150            

151                 Spst = SleepingHead;  

152                 SleepingHead = NULL;  

153                 g_SleepingNodeNum--;  

154    

155                 printf("input message:\n");  

156                 scanf("%s",Spst->acBuf);  

157    

158                 Spst->iStatus = 'W';  

159                 Spst->iFlag = 1;  

160    

161                 Spst->next = WorkingHead;  

162                 WorkingHead = Spst;  

163                 g_WorkingNodeNum++;  

164    

165                 printf("-------------------Workinglist----------------- \n");  

166                 AccessList(WorkingHead);  

167                 printf("-------------------Sleepinglst-----------------\n");  

168                 AccessList(SleepingHead);  

169    

170                 pthread_mutex_unlock(&testlock);  

171                 sem_post(&semRevi);   

172        

173                 sleep(5);  

174             }  

175    

176         else 

177             {  

178    

179                 while(Spst->next != NULL)  

180                     {  

181                         pTemp = Spst;  

182                         Spst = Spst->next;  

183                     }  

184        

185                 pthread_mutex_lock(&testlock);  

186                 g_SleepingNodeNum--;  

187                 pTemp->next = NULL;  

188        

189                 // 用户输入  

190                 printf("input message:\n");  

191                 scanf("%s",Spst->acBuf);  

192    

193                 Spst->iStatus = 'W';  

194                 Spst->iFlag = 1;  

195        

196                 Spst->next = WorkingHead;  

197                 WorkingHead = Spst;  

198                 g_WorkingNodeNum++;  

199    

200                 printf("-------------------Workinglist----------------- \n");  

201                 AccessList(WorkingHead);  

202                 printf("-------------------Sleepinglst-----------------\n");  

203                 AccessList(SleepingHead);  

204    

205                 pthread_mutex_unlock(&testlock);  

206                 sem_post(&semRevi);   

207        

208                 sleep(5);  

209             }  

210     }  

211     return NULL;  

212        

213 }  

214    

215 // function main()  

216 int main()  

217 {  

218     // 初始化工作链表WorkList;  

219     for (int i=0;i<4;i++)  

220      {  

221         InitWorkList();   

222      }    

223     printf("working 4 nodes be created:\n");  

224        

225     // 遍历链表  

226     printf("working nodes message:\n");  

227     AccessList(WorkingHead);     

228        

229     int err;  

230     pthread_t tid;  

231     sem_init(&semSend,0,0);  

232     sem_init(&semRevi,0,4);  

233     pthread_mutex_init(&testlock, NULL);  

234        

235     // 从工作链表中读取最后一个节点的message,并且删除节点  

236     err = pthread_create(&tid,NULL,DelWtoS,NULL);  

237     if(err=0)  

238         printf("can’t create thread:%s\n",strerror(err));     

239        

240     // 从空闲链表中删除一个节点添加个工作链表的头部供使用   

241     err = pthread_create(&tid,NULL,DelStoW,NULL);  

242     if(err=0)  

243         printf("can’t create thread:%s\n",strerror(err));  

244        

245     sleep(1000);  

246     sem_close(&semSend);  

247     sem_close(&semRevi);  

248     pthread_mutex_destroy(&testlock);      

249     return Success;  

250 }
[root@desktop97 桌面]# ./a.out 
working 4 nodes be created: 
working nodes message: 
节点 1 : Status  W  iFlag 1  message 
节点 2 : Status  W  iFlag 1  message 
节点 3 : Status  W  iFlag 1  message 
节点 4 : Status  W  iFlag 1  message 
the end node message is message 
add it to SleepList 
node 1 be added 
-------------------Workinglist----------------- 
节点 1 : Status  W  iFlag 1  message 
节点 2 : Status  W  iFlag 1  message 
节点 3 : Status  W  iFlag 1  message 
-------------------Sleepinglist----------------- 
节点 1 : Status  S  iFlag -1  message 
input message: 
1 
-------------------Workinglist----------------- 
节点 1 : Status  W  iFlag 1  1 
节点 2 : Status  W  iFlag 1  message 
节点 3 : Status  W  iFlag 1  message 
节点 4 : Status  W  iFlag 1  message 
-------------------Sleepinglst----------------- 
the end node message is message 
add it to SleepList 
node 1 be added 
-------------------Workinglist----------------- 
节点 1 : Status  W  iFlag 1  1 
节点 2 : Status  W  iFlag 1  message 
节点 3 : Status  W  iFlag 1  message 
-------------------Sleepinglist----------------- 
节点 1 : Status  S  iFlag -1  message 
input message: 
w 
-------------------Workinglist----------------- 
节点 1 : Status  W  iFlag 1  w 
节点 2 : Status  W  iFlag 1  1 
节点 3 : Status  W  iFlag 1  message 
节点 4 : Status  W  iFlag 1  message 
-------------------Sleepinglst----------------- 
the end node message is message 
add it to SleepList 
node 1 be added 
-------------------Workinglist----------------- 
节点 1 : Status  W  iFlag 1  w 
节点 2 : Status  W  iFlag 1  1 
节点 3 : Status  W  iFlag 1  message 
-------------------Sleepinglist----------------- 
节点 1 : Status  S  iFlag -1  message 
e 
input message: 
-------------------Workinglist----------------- 
节点 1 : Status  W  iFlag 1  e 
节点 2 : Status  W  iFlag 1  w 
节点 3 : Status  W  iFlag 1  1 
节点 4 : Status  W  iFlag 1  message 
-------------------Sleepinglst-----------------

你可能感兴趣的:(Linux C 用链表简单实现消息队列)