UNIX環境高級編程學習之第十五章進程間通信 - 通過消息隊列實現進程間通信
/* User:Lixiujie * Date:20100825 * Desc:通過消息隊列實現進程間通信 * File:MsgQueue.c * gcc MsgQueue.c -o MsgQueue1 * 把SEND_TYPE修改爲2 RECV_TYPE修改爲1 * gcc MsgQueue.c -o MsgQueue2 */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #define SEND_TYPE 1 #define RECV_TYPE 2 typedef struct MsgBuf{ long lType; char szData[512]; }Msg; /* 取當前文件路徑 */ char* getCurrentFilePath(char *szCurrentFilePath){ char szBuf[256] = { 0x00 }; strcpy(szBuf, __FILE__); if (strstr(szBuf, "/") == NULL){ memset(szBuf, 0x00, sizeof(szBuf)); strcpy(szBuf, getenv("PWD")); strcat(szBuf, "/"); strcat(szBuf, __FILE__); } strcpy(szCurrentFilePath, szBuf); return szCurrentFilePath; } /* 發送信息線程 */ void *SendMsgThread(void *pData){ int mqID = *(int*)pData; int ret; while (1){ Msg msg; memset(&msg, 0x00, sizeof(msg)); msg.lType = SEND_TYPE; sprintf(msg.szData, "%ul:Hello world!/n", getpid()); ret = msgsnd(mqID, &msg, sizeof(msg.szData), IPC_NOWAIT); if (-1 == ret){ perror("SendMsgThread() msgsnd() is failed!/n"); } sleep(3); } return (void*)0; } /* 接收信息線程 */ void *RecvMsgThread(void *pData){ int mqID = *(int*)pData; int ret; while (1){ Msg msg; memset(&msg, 0x00, sizeof(msg)); msg.lType = RECV_TYPE; ret = msgrcv(mqID, (void*)&msg, sizeof(msg.szData), msg.1Type, 0); if (-1 == ret){ perror("RecvMsgThread() msgrcv() is failed!/n"); } printf("PID=%ul RECV DATA:%s/n",getpid(), msg.szData); } return (void*)0; } int main(int argc, char *argv[]) { int ret; key_t mqKey; char szCurrentFilePath[256] = { 0x00 }; getCurrentFilePath(szCurrentFilePath); /* 產生key */ mqKey = ftok(szCurrentFilePath, 1); if (-1 == mqKey){ perror("main() ftok() is failed!/n"); exit(1); } /* 定義消息隊列ID */ int mqID; /* 產生或獲取消息隊列 */ mqID = msgget(mqKey, IPC_CREAT|0666); if (-1 == mqID){ perror("main() msgget() is failed!/n"); exit(1); } pthread_t pth[2]; ret = pthread_create(&pth[0], NULL, SendMsgThread, &mqID); if (ret != 0){ perror("main() pthread_create() is failed!/n"); exit(1); } ret = pthread_create(&pth[1], NULL, RecvMsgThread, &mqID); if (ret != 0){ perror("main() pthread_create() is failed!/n"); exit(1); } pthread_detach(pth[0]); pthread_detach(pth[1]); while (1){ char szCMD[64] = { 0x00 }; gets(szCMD); if (strcmp(szCMD, "quit") == 0){ exit(1); }else if (strcmp(szCMD, "exit") == 0){ exit(1); } sleep(1); } return 0; } /* 刪除消息隊列 */ int rmMQ(int mqID){ reutnr msgctl(mqID, IPC_RMID, NULL); }
UNIX環境高級編程學習之第十一章線程-線程的創建、退出、等待、取消、分離 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #i
UNIX環境高級編程學習之第五章標準IO-實現查看所有用戶 #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(i
UNIX環境高級編程學習之第六章系統數據文件和信息-修改第四章實現的Shell的“ls -l”功能, 加入顯示文件的用戶名和組名 // 只能查看目錄中的所有文件屬性 #include <stdio.h> #include <stdlib.
UNIX環境高級編程學習之第十章信號-信號集的操作,讓進程阻塞SIGQUIT信號 /* File: sigprocmask.c Desc: 信號集的操作,讓進程阻塞SIGQUIT信號 */ #include <stdio.h> #in
UNIX環境高級編程學習之第八章進程控制-用父子進程實現打開kate,返回kate關閉狀態 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unis
UNIX環境高級編程學習之第三章文件IO-文件讀操作 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include
UNIX環境高級編程學習之第十章信號-信號的基本操作(綁定/忽略/默認/發送) /* File: signal.c Desc: 信號基本操作 */ #include <stdio.h> #include <stdlib.h> #inc
到現在,我Unix,數據庫的掃盲基本完成了,就差網絡這一塊了。從今天開始網絡掃盲。上週五開始看HTTPD的實現,我們的httpd是AA(AA是我們SBE的元老之一)開發的,只要是AA開發的東西,學習一遍都很有收穫。因爲AA是一位非常優秀的