當前位置:編程學習大全網 - 編程軟體 - 最近在學linux,在看UNIX高級環境編程時,其中有節講到:linux主線程用線程ID來控制工作線程處理任務

最近在學linux,在看UNIX高級環境編程時,其中有節講到:linux主線程用線程ID來控制工作線程處理任務

以下是多線程同步的例子

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);

}

  • 上一篇:各位大神說說學ssm好還是學ssh好
  • 下一篇:VBA語句求解釋,高手快來,謝謝了~~急急急
  • copyright 2024編程學習大全網