Linux多線程同步之訊息佇列

來源:互聯網
上載者:User

訊息佇列是訊息的鏈表,存放在核心中並有訊息佇列標示符標示。

msgget用於建立一個新隊列或開啟一個現存的隊列。msgsnd將新訊息加入到訊息佇列中;每個訊息包括一個long型的type;和訊息緩衝;msgrcv用於從隊列中取出訊息;取訊息很智能,不一定先進先出

①msgget,建立一個新隊列或開啟一個現有隊列

#include

int msgget ( key_t key, int flag );

//成功返回訊息佇列ID;錯誤返回-1

②msgsnd: 發送訊息

#include

int msgsnd( int msgid, const void* ptr, size_t nbytes, int flag )

//成功返回0,錯誤返回-1

a:   flag可以指定為IPC_NOWAIT;  若訊息佇列已滿,則msgsnd立即出錯返回EABAIN;

若沒指定IPC_NOWAIT; msgsnd會阻塞,直到訊息佇列有空間為止

③msgrcv: 讀取訊息:

ssize_t msgrcv( int msgid, void* ptr, size_t nbytes, long type, int flag );

a. type == 0; 返回訊息佇列中第一個訊息,先進先出

b. type > 0    返回訊息佇列中類型為tpye的第一個訊息

c. type < 0    返回訊息佇列中類型 <=  |type| 的資料;若這種訊息有若干個,則取類型值最小的訊息

訊息佇列建立步驟:

#define   MSG_FILE "."

struct msgtype {

long mtype;

char buffer[BUFFER+1];

};

if((key=ftok(MSG_FILE,'a'))==-1)

{

fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));

exit(1);

}

if((msgid=msgget(key, IPC_CREAT | 0666/*PERM*/))==-1)

{

fprintf(stderr,"Creat Message  Error:%s\n", strerror(errno));

exit(1);

}

msg.mtype = 1;

strncpy(msg.buffer, argv[1], BUFFER);

msgsnd(msgid, &msg, sizeof(struct msgtype), 0);

msgrcv(msgid, &msg, sizeof(struct msgtype), 1, 0);

範例程式碼:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define   MSG_FILE "."

#define   BUFFER 255

#define   PERM S_IRUSR|S_IWUSR

#define IPCKEY 0x111

struct msgtype {

long mtype;

char buffer[BUFFER+1];

};

void* thr_test( void* arg ){

struct msgtype msg;

int msgid;

msgid =  *((int*)arg);

printf("msqid = %d  IPC_NOWAIT = %d\n", msgid, IPC_NOWAIT);

time_t tt = time(0)+8;

//while( time(0) <= tt )

//{

msgrcv(msgid, &msg, sizeof(struct msgtype), 1, 0);

fprintf(stderr,"Server Receive:%s\n", msg.buffer);

msg.mtype = 2;

msgsnd(msgid, &msg, sizeof(struct msgtype), 0);

//}

pthread_exit( (void*)2 );

}

int main(int argc, char **argv)

{

struct msgtype msg;

key_t key;

int msgid;

pthread_t tid;

if(argc != 2)

{

fprintf(stderr,"Usage:%s string\n", argv[0]);

exit(1);

}

/*

char path[256];

sprintf( path, "%s/", (char*)getenv("HOME") );

printf( "path is %s\n", path );

msgid=ftok( path, IPCKEY );

*/

if((key=ftok(MSG_FILE,'a'))==-1)

{

fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));

exit(1);

}

if((msgid=msgget(key, IPC_CREAT | 0666/*PERM*/))==-1)

{

fprintf(stderr,"Creat Message  Error:%s\n", strerror(errno));

exit(1);

}

pthread_create( &tid, NULL, thr_test, &msgid );

fprintf(stderr,"msid is :%d\n", msgid);

msg.mtype = 1;

strncpy(msg.buffer, argv[1], BUFFER);

msgsnd(msgid, &msg, sizeof(struct msgtype), 0);

exit(0);

}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.