// int msgctl(int msqid, int cmd, struct msqid_ds *buf); // http://man7.org/linux/man-pages/man2/msgctl.2.html // // Controls the queue with IPC_SET, IPC_INFO and IPC_RMID via msgctl(2). When no // argument is passed, it'll return the information about the queue from // IPC_INFO. // // TODO: IPC_SET is currently not supported. static VALUE sysvmq_stats(int argc, VALUE *argv, VALUE self) { struct msqid_ds info; VALUE info_hash; VALUE cmd; sysvmq_t* sysv; // Optional argument handling if (argc > 1) { rb_raise(rb_eArgError, "Wrong number of arguments (0..1)"); } // Default to IPC_STAT cmd = argc == 1 ? argv[0] : INT2FIX(IPC_STAT); TypedData_Get_Struct(self, sysvmq_t, &sysvmq_type, sysv); // TODO: Does FIX2INT actually perform this check already? Check_Type(cmd, T_FIXNUM); while (msgctl(sysv->id, FIX2INT(cmd), &info) < 0) { if (errno == EINTR) { rb_thread_wait_for(polling_interval); continue; } rb_sys_fail("Failed executing msgctl(2) command."); } // Map values from struct to a hash // TODO: Add all the fields // TODO: They are probably not ints.. info_hash = rb_hash_new(); rb_hash_aset(info_hash, ID2SYM(rb_intern("count")), INT2FIX(info.msg_qnum)); rb_hash_aset(info_hash, ID2SYM(rb_intern("maximum_size")), INT2FIX(info.msg_qbytes)); // TODO: Can probably make a better checker here for whether the struct // actually has the member. // TODO: BSD support? #ifdef __linux__ rb_hash_aset(info_hash, ID2SYM(rb_intern("size")), INT2FIX(info.__msg_cbytes)); #elif __APPLE__ rb_hash_aset(info_hash, ID2SYM(rb_intern("size")), INT2FIX(info.msg_cbytes)); #endif return info_hash; }
void destroy(){ /*acaba com tudo*/ printf("Acabando com o programa...\n"); free(buffer->request_list); if(sem_close(sem_write)==-1){ perror("Write Semaphore close"); exit(0); } if(sem_close(sem_read)==-1){ perror("Read Semaphore close"); exit(0); } if(sem_close(sem_config)==-1){ perror("Config Semaphore close"); exit(0); } if(sem_close(sem_buffer)==-1){ perror("Config Semaphore close"); exit(0); } if(sem_close(sem_full)==-1){ perror("Config Semaphore close"); exit(0); } if(sem_close(sem_empty)==-1){ perror("Config Semaphore close"); exit(0); } shmctl(shm_id,IPC_RMID,NULL); shmctl(buff_id,IPC_RMID,NULL); msgctl(queue, IPC_RMID, 0); close(socket_conn); exit(0); }
int main(int ac, char **av) { int lc; char *msg; int i; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); } setup(); /* global setup */ /* The following loop checks looping state if -i option given */ for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset tst_count in case we are looping */ tst_count = 0; /* loop through the test cases */ for (i = 0; i < TST_TOTAL; i++) { TEST(msgctl(*(TC[i].queue_id), TC[i].ipc_cmd, TC[i].buf)); if (TEST_RETURN != -1) { tst_resm(TFAIL, "msgctl() call succeeded " "on expected fail"); continue; } TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == TC[i].error) { tst_resm(TPASS | TTERRNO, "expected failure"); } else { tst_resm(TFAIL | TTERRNO, "unexpected error"); tst_resm(TINFO, "expected error is - %d : %s", TC[i].error, strerror(TC[i].error)); } } } cleanup(); tst_exit(); }
void knDestroyQueue(knQueue_t* que) { if(que == NULL) { return; } #ifdef _SW_Q_ linkedlist_delete(que->queue, knReleaseEventNode); #else msgctl(que->fd, IPC_RMID, 0); #endif KnOSDeleteMutex(que->lock); KnOSDeleteSemaphore(que->event); KnFree(que); }
int main(void) { pthread_t receiverId; struct my_msgbuf buf; char *ptr = (char *)malloc(sizeof(char) * 200); int msqid; key_t key; if ((key = ftok("isense", 'B')) == -1) { perror("ftok"); exit(1); } if ((msqid = msgget(0x2423207, 0644 | IPC_CREAT)) == -1) { perror("msgget"); exit(1); } printf("MSG Q ID IS %d\n", msqid); printf("Enter lines of text, ^D to quit:\n"); buf.mtype = 2; /* we don't really care in this case */ printf("Address of ptr is %p\n", ptr); buf.mtext = (void *)ptr; printf("Address of mtext is %p\n", buf.mtext); pthread_create( &receiverId, NULL, (void* (*) (void *))receiver, NULL ); while(fgets(ptr, (sizeof(char) * 200), stdin) != NULL) { int len = strlen(ptr); /* ditch newline at end, if it exists */ if (ptr[len-1] == '\n') { ptr[len-1] = '\0'; } if (msgsnd(msqid, &buf, sizeof(buf.mtext), 0) == -1) /* +1 for '\0' */ { perror("msgsnd"); } } if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(1); } return 0; }
void interrupt(){ printf("NAGLE ZAKONCZONO PROGRAM\n"); Game_message game_message; game_message.mtype=PODDAJSIE; msgsnd(game_queue_id,&game_message, sizeof(game_message.game_data),0); msgsnd(output_queue_id,&game_message, sizeof(game_message.game_data),0); sleep(1); msgctl(game_queue_id,IPC_RMID,0); kill(0,SIGKILL); }
ATF_TC_BODY(msgrcv_nonblock, tc) { struct msg msg = { MSG_MTYPE_1, { 'a', 'b', 'c' } }; const ssize_t n = 10; int id, sta; ssize_t i; pid_t pid; id = msgget(MSG_KEY, IPC_CREAT | 0600); ATF_REQUIRE(id != -1); for (i = 0; i < n; i++) { ATF_REQUIRE(msgsnd(id, &msg, sizeof(struct msg), IPC_NOWAIT) == 0); } pid = fork(); ATF_REQUIRE(pid >= 0); if (pid == 0) { while (i != 0) { if (msgrcv(id, &msg, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT) == -1) _exit(EXIT_FAILURE); i--; } _exit(EXIT_SUCCESS); } (void)sleep(2); (void)kill(pid, SIGKILL); (void)wait(&sta); if (WIFSIGNALED(sta) != 0 || WTERMSIG(sta) == SIGKILL) atf_tc_fail("msgrcv(2) blocked with IPC_NOWAIT"); if (WIFEXITED(sta) == 0 && WEXITSTATUS(sta) != EXIT_SUCCESS) atf_tc_fail("msgrcv(2) failed"); ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0); }
int main(void) { key_t key = 2222; struct mymesg my_msg; char recv_buf[512] = {0}; int msgid = 0; //创建消息队列 msgid = msgget(key, 0666 | IPC_CREAT); if (msgid == -1) { err_sys("msgget error"); } else { printf("msgid = %d\n", msgid); } //读取队列数据 while (1) { if (msgrcv(msgid, recv_buf, 512, 0, 0) == -1) { err_sys("msgrcv error"); } if (memcmp(recv_buf, "q", 1) == 0) { printf("接收到“q”,退出!\n"); break; } printf("recv msg = %s\n", recv_buf); } //撤销队列 if (msgctl(msgid, IPC_RMID, 0) == -1) { err_sys("msgclt error"); } exit(0); }
int restore_queue(struct msgque_data *msgque) { int fd, ret, id, i; char buf[32]; fd = open("/proc/sys/kernel/msg_next_id", O_WRONLY); if (fd == -1) { printf("Failed to open /proc/sys/kernel/msg_next_id\n"); return -errno; } sprintf(buf, "%d", msgque->msq_id); ret = write(fd, buf, strlen(buf)); if (ret != strlen(buf)) { printf("Failed to write to /proc/sys/kernel/msg_next_id\n"); return -errno; } id = msgget(msgque->key, msgque->mode | IPC_CREAT | IPC_EXCL); if (id == -1) { printf("Failed to create queue\n"); return -errno; } if (id != msgque->msq_id) { printf("Restored queue has wrong id (%d instead of %d)\n", id, msgque->msq_id); ret = -EFAULT; goto destroy; } for (i = 0; i < msgque->qnum; i++) { if (msgsnd(msgque->msq_id, &msgque->messages[i].mtype, msgque->messages[i].msize, IPC_NOWAIT) != 0) { printf("msgsnd failed (%m)\n"); ret = -errno; goto destroy; }; } return 0; destroy: if (msgctl(id, IPC_RMID, 0)) printf("Failed to destroy queue: %d\n", -errno); return ret; }
/* NOTE: o timeout is always considered zero o this function always returns the maximum message size, whether there are messages at the port or not...see port_buffer_size() */ ssize_t port_buffer_size_etc(port_id port, uint32 flags, bigtime_t timeout) { struct msqid_ds info; if(msgctl((int)port, IPC_STAT, &info) == -1) { switch(errno) { case EACCES: case EFAULT: case EIDRM: case EINVAL: case EPERM: return (ssize_t)B_BAD_PORT_ID; } } return (ssize_t)(info.msg_qbytes); }
int main(int argc, char* argv[]) { int nb_fils, i; key_t cle; if (argc < 2) { printf("error syntaxe : %s nbProcessus \n", argv[0]); exit(1); } /*------------------------------------------------------*/ /* creation msg */ /*------------------------------------------------------*/ char code = 0; code = getpid() & 255; if (code == 0) code |= 1; if ((cle = ftok(argv[0], code)) == -1) { perror("ftok"); exit(-1); } msg_id = msgget(cle, IPC_CREAT | IPC_EXCL | 0600); /*------------------------------------------------------*/ /* creation processus */ /*------------------------------------------------------*/ nb_fils = atoi(argv[1]); nfork(nb_fils); /*------------------------------------------------------*/ /* traitement liberation msg */ /*------------------------------------------------------*/ /* attendre la fin de tous les fils*/ for(i = 0 ; i < nb_fils; i++){ wait(NULL); } int result = 0; for (i = 0; i < nb_fils; i++) { msgrcv(msg_id, &msg, sizeof(int), sizeof(int), 0); printf("pere msg%d recu ==> %d \n", i, msg.val_rand); result += msg.val_rand; } printf("pere %d : somme %d \n", getpid(),result); msgctl(msg_id, IPC_RMID, NULL); return EXIT_SUCCESS; }
/* * ipc_cleanup() - * * Destroy the semaphore set and message queue. Use with caution, this * code does not check if there is a daemon running. */ int ipc_cleanup(char *archive_dir) { int rc = 0; if (ipc_generate_keys(archive_dir) < 0) return -1; if ((semid = semget(semkey, 0, 0)) >= 0) { if (semctl(semid, 0, IPC_RMID) < 0) { fprintf(stderr, "semctl() failed in ipc_cleanup(): %s\n", strerror(errno)); rc = -1; } else if (!opt_quiet) fprintf(stderr, "semaphore set removed\n"); rc = 1; } else if (!opt_quiet) fprintf(stderr, "no semaphore set found\n"); if ((msgid = msgget(msgkey, 0)) >= 0) { if (msgctl(msgid, IPC_RMID, NULL) < 0) { fprintf(stderr, "msgctl() failed in ipc_cleanup(): %s\n", strerror(errno)); rc = -1; } else if (!opt_quiet) fprintf(stderr, "message queue removed\n"); if (rc >= 0) rc |= 2; } else if (!opt_quiet) fprintf(stderr, "no message queue found\n"); return rc; }
int main(int argc, char const *argv[]) { int msg_id; //Id de la file pour l'envoie des messages au serveur struct msg_struct msg; //Structure de donnée qui contiendra les messages (reçu et émis) if (argc != 2) { //On vérifie le nombre d'argument printf("Usage: %s chaine", argv[0]); exit(-1); } /** PARTIE ENVOIE DE LA REQUETE AU SERVEUR **/ //On récupère l'id de la file de message principale (Envoie des chaines de caractère au serveur) msg_id = msgget(MSGKEY, 0666); if (msg_id == -1) { perror("Le serveur n'est pas pret !"); exit(-1); } //On affiche et on construit le message printf("CLIENT %d: message : %s\n", getpid(), argv[1]); msg.type = 42; msg.pid = getpid(); strncpy(msg.message, argv[1], TAILLE_MAX); //On utilise strNcpy qui permet de copier au maximum TAILLE_MAX caractère et donc permet d'éviter les débordements. //Envoi du message (et vérification de l'envoie) if (msgsnd(msg_id, &msg, sizeof(struct msg_struct), 0) == -1) { perror("Impossible d'envoyer le message"); exit(-1); } /** PARTIE ATTENTE PUIS RECEPTION DE LA REPONSE DU SERVEUR **/ //On récupère l'id de la file par lequel le serveur va nous envoyer la réponse msg_id = msgget(getpid(), 0666|IPC_CREAT); if (msg_id == -1) { perror("Problème file de réponse !"); exit(-1); } //Reception de la reponse msgrcv(msg_id, &msg, sizeof(struct msg_struct), 42, 0); //Affichage de la reponse printf("CLIENT: resultat recu depuis le serveur %d :\n\t %s\n", msg.pid, msg.message); //On supprime la file utilisé pour la réponse msgctl(msg_id, IPC_RMID, NULL); return 0; }
ATF_TC_BODY(msgrcv_basic, tc) { struct msg msg1 = { MSG_MTYPE_1, { 'a', 'b', 'c' } }; struct msg msg2 = { MSG_MTYPE_1, { 'x', 'y', 'z' } }; int id; id = msgget(MSG_KEY, IPC_CREAT | 0600); ATF_REQUIRE(id != -1); (void)msgsnd(id, &msg1, sizeof(struct msg), IPC_NOWAIT); (void)msgrcv(id, &msg2, sizeof(struct msg), MSG_MTYPE_1, IPC_NOWAIT); ATF_CHECK(msg1.buf[0] == msg2.buf[0]); ATF_CHECK(msg1.buf[1] == msg2.buf[1]); ATF_CHECK(msg1.buf[2] == msg2.buf[2]); ATF_REQUIRE(msgctl(id, IPC_RMID, 0) == 0); }
kernel_close_in() { register long ret; /*-------------------------------------------------------------------------* * There is only one input queue for all tasks. Closed only at Shutdown. *-------------------------------------------------------------------------*/ ret = msgctl(k_in, IPC_RMID, 0); if (ret < 0) { krash("kernel_close_in", "Close Input Queue", 0); } k_in = 0; return 0; }
int main (int argc, char **argv) { int qid; key_t key; if (argc != 2) err_quit ("Usage: msgrmq path"); if ((key = ftok (argv [1], 0x42)) == -1) err_msg ("ftok failed"); if ((qid = msgget (key, 0)) == -1) err_msg ("msgget failed"); if (msgctl (qid, IPC_RMID, NULL) == -1) err_msg ("msgctl failed"); return (0); }
/*----------------------------------------------------------------------------*/ status_t close_port(port_id port) { struct msqid_ds info; info.msg_perm.mode = 0444; /* read only */ if(msgctl((int)port, IPC_SET, &info) == -1) { switch(errno) { case EACCES: case EFAULT: case EIDRM: case EINVAL: case EPERM: return B_BAD_PORT_ID; } } return B_OK; }
/* ---------------------------------------------------------------- * 功 能: 取出消息队列的msqid_ds数据并返回 * 的机构中 * 输入参数: iMsgid 消息队列的标识符 * 输出参数: ptDs 消息队列的msqid_ds结构 * 返 回 值: 0 成功/-1 失败 * 作 者: * 日 期:2012/12/27 * 调用说明: * 修改日志:修改日期 修改者 修改内容简述 * ---------------------------------------------------------------- */ int GetMsgQueStat(int iMsgid, struct msqid_ds *ptDs) { if (iMsgid <= 0) { PRINT_LOG(ERROR_LVL, "GetMsgQueStat input-para[iMsgid=%d] error", iMsgid); return FAIL; } if (msgctl(iMsgid, IPC_STAT, ptDs) < 0) { PRINT_LOG(ERROR_LVL, "call msgctl IPC_STAT(msgid=%d) fail[%d-%s]", iMsgid, errno, strerror(errno)); return FAIL; } return SUCC; }
int launch_player(t_lemipc *lemipc) { while (lemipc->is_dead == FALSE) if (player_turn(lemipc) == EXIT_FAILURE) return (EXIT_FAILURE); lemipc->map[lemipc->pos] = 0; if (lemipc->is_alone == TRUE && player_is_alone(lemipc, TRUE) == TRUE) { shmdt(lemipc->map); usleep(SLEEP); semctl(lemipc->sem_id, 0, IPC_RMID); shmctl(lemipc->shm_id, IPC_RMID, NULL); msgctl(lemipc->msg_id, IPC_RMID, NULL); } else shmdt(lemipc->map); return (EXIT_SUCCESS); }
int main() { int msgid; msgid = msgget(KEY, IPC_CREAT | 0600); printf("IPC_CREAT = %d\n", IPC_CREAT); if (fork() == 0) { sleep(3); msgp.mtype = 1; strcpy(msgp.mtext, "Hi! I am child process!\n"); msgsnd (msgid, &msgp, TEXT_SIZE, 0); return 0; } else { msgrcv(msgid, &msgp, TEXT_SIZE, 0, 0); printf("parent receive mtext: %s\n", msgp.mtext); msgctl(msgid, IPC_RMID, NULL); } return 0; }
int msq_status(int msq_id) { struct msqid_ds buf = {0}; if (msgctl(msq_id, IPC_STAT, &buf) < 0) errReturn("msgctl", -1); printf("--------status---------\n"); printf("stime: %s", ctime(&buf.msg_stime)); printf("rtime: %s", ctime(&buf.msg_rtime)); printf("ctime: %s", ctime(&buf.msg_ctime)); printf("msg_qnum: %lu\n", buf.msg_qnum); printf("msg_qbytes: %lu\n", buf.msg_qbytes); printf("lspid: %hu\n", buf.msg_lspid); printf("lrpid: %hu\n", buf.msg_lrpid); printf("--------status---over-------\n"); return 0; }
void main(int argc, char const *argv[]) { int vetor[11], i, totalPrimos = 0, fila; struct msg msg; fila = msgget(KEY,0600|IPC_CREAT); srand(time(NULL)); for (i = 0; i < 11; i++) { vetor[i] = rand() % 10; } pid_t p1, p2; p1 = fork(); if (p1 > 0) { p2 = fork(); if (p2 > 0) { int cont = 0; msgrcv(fila,&msg,sizeof(msg.totalPrimos),0,0); cont = msg.totalPrimos; msgrcv(fila,&msg,sizeof(msg.totalPrimos),0,0); cont += msg.totalPrimos; wait(NULL); wait(NULL); msgctl(fila, IPC_RMID, NULL); printf("Total de primos = %d\n", cont); } else { for (i = (N/2); i < N; i++) { totalPrimos += primo(vetor[i]); } msg.totalPrimos = totalPrimos; msg.totalPrimos = 1; msgsnd(fila,&msg,sizeof(msg.totalPrimos),0); } } else { for (i = 0; i < (N/2); i++) { totalPrimos += primo(vetor[i]); } msg.totalPrimos = totalPrimos; msgsnd(fila,&msg,sizeof(msg.totalPrimos),0); } }
int main() { int running = 1; int msgid; struct my_msg_st some_data; long int msg_to_receive = 0; msgid = msgget( (key_t)1234, 0666 | IPC_CREAT); if (msgid == -1) { fprintf(stderr, "failed to get:\n"); exit(EXIT_FAILURE); } while (running) { if(msgrcv(msgid, (void *)&some_data, BUFSIZ,msg_to_receive,0) == -1) { fprintf(stderr, "failedto receive: \n"); exit(EXIT_FAILURE); } printf("You Wrote: %s", some_data.some_text); if(strncmp(some_data.some_text, "end", 3) == 0) { running = 0; } } if (msgctl(msgid, IPC_RMID, 0) == -1) { fprintf(stderr, "failed to delete\n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]){ key_t cle; int id_file; int nb_prod1 = atoi(argv[1]); int nb_prod2 = atoi(argv[2]); int nb_conso1 = atoi(argv[3]); int nb_conso2 = atoi(argv[4]); /* On vérifie de bien recevoir 4 arguments */ if(argc!=5){ perror("Il faut passer le nombre de producteurs et de consommateurs pour chaque type de message en paramètre"); exit(-1); } /* Crée une clé unique */ cle = ftok("./Makefile", ID_PROJET); /* Crée une file de message */ id_file = msgget(cle, IPC_EXCL | IPC_CREAT | 0666); /* Création de plusieurs consommateurs de type 1 */ fork_consommateur(nb_conso1, 1, id_file); /* Création de plusieurs consommateurs de type 2 */ fork_consommateur(nb_conso2, 2, id_file); /* Création de plusieurs producteurs de type 1 */ fork_producteur(nb_prod1,1,id_file); /* Création de plusieurs producteurs de type 2 */ fork_producteur(nb_prod2,2,id_file); printf("Pere en attente\n"); for(int i=0;i<nb_prod1+nb_prod2+nb_conso1+nb_conso2;i++) wait(NULL); printf("Tous les fils sont morts\n"); msgctl(id_file, IPC_RMID, 0); printf("File de messages detruite\n"); return 0; }
int main(int argc, char **argv) { int msgid; if (argc != 2) { fprintf(stdout, "<%s> usage: msgrmid <pathname>\n", __func__); exit(1); } msgid = msgget(ftok(argv[1], 0), 0); if (msgid == -1) { perror("msgget"); exit(2); } msgctl(msgid, IPC_RMID, NULL); return 0; }
int main(void){ key_t key; int i, msgid; struct msg_st buf; ssize_t len; pid_t pid; long arr[2] ={ONE, TWO}; key = ftok(KEYPATH, KEYPROJ); if(key < 0){ perror("ftok():"); exit(-1); } msgid = msgget(key, IPC_CREAT|0644); if(msgid < 0){ perror("msgget"); exit(-1); } for(i=0; i<2; i++){ pid = fork(); if(pid == 0){ while (1) { len = msgrcv(msgid, &buf, sizeof(buf)- sizeof(long), arr[i], 0); if (len < 0) { perror("msgrcv():"); exit(-1); } printf("mtype:%d,name:%s,math:%d,chinese:%d\n",\ arr[i], buf.name , buf.math, buf.chinese); } eixt(0); } } for(i=0; i<2; i++){ wait(NULL); } msgctl(msgid, IPC_RMID, NULL); exit(0); }
void ha2(int sig) { int i; while(1) { message msg; int numBytes=msgrcv(id,&msg,sizeof(long),par,IPC_NOWAIT); if(numBytes!=-1) { count++; } else break; } for(i=1;i<=n;i++) kill(children[i],SIGKILL); printf("total count: %d\n",count); fflush(stdin); if(msgctl(id,IPC_RMID,NULL)==-1)perror("msgctl remove"); exit(0); }
void msg_stat(int msgid,struct msqid_ds msg_info) { int ret; ret = msgctl(msgid,IPC_STAT,&msg_info); if(ret == -1) { marbit_send_log(ERROR,"msgctl error :"); //exit(EXIT_FAILURE); } // printf("\n"); //printf("current number of bytes on queue is %d\n",msg_info.msg_cbytes); // printf(" max of bytes on queue id %d\n",msg_info.msg_qbytes); //printf(" number of messages in queue is %d\n",msg_info.msg_qnum); //printf("last change time is %s\n",ctime(&msg_info.msg_ctime)); // printf("message uid is %d\n",msg_info.msg_perm.uid); // printf("message gid is %d\n",msg_info.msg_perm.gid); return; }
int main(void) { int msgid; struct msgbuf buf, sndbuf; int ret; char cmd[CMDSIZE]; msgid = msgget(IPC_PRIVATE, IPC_CREAT | 0666); if (msgid == -1) { perror("msgget()"); return 1; } snprintf(cmd, CMDSIZE, "echo %d > private", msgid); system(cmd); printf("key = %x, msgid = %d\n", IPC_PRIVATE, msgid); sndbuf.src = 1; while (1) { ret = msgrcv(msgid, &buf, MSGBUFSIZE, -5, 0); if (ret == -1) { if (errno == EINTR) { continue; } perror("msgrcv()"); break; } printf("from %d: %s\n", buf.src, buf.data); if (strncmp(buf.data, "quit", 4) == 0) { break; } sndbuf.dest = buf.src; ret = snprintf(sndbuf.data, DATASIZE, "ACK"); msgsnd(msgid, &sndbuf, sizeof(long) + ret + 1, 0); } /* fix me */ msgctl(msgid, IPC_RMID, NULL); return 0; }
int main() { int qid; key_t key; struct message msg; if((key=ftok(".",'a'))==-1) { perror("ftok"); exit(1); } if((qid=msgget(key,IPC_CREAT|0666))==-1) { perror("msgget"); exit(1); } printf("Open queue %d",qid); do { memset(msg.msg_text,0,BUFFER_SIZE); if(msgrcv(qid,(void*)&msg,BUFFER_SIZE,0,0)<0) { perror("msgrcv"); exit(1); } // printf("The message from process %d : %s",(int)msg.msg_type,msg.msg_text); printf("The message receive: type: %d \tcontent : %s",(int)msg.msg_type,msg.msg_text); }while(strncmp(msg.msg_text,"quit",4)); if((msgctl(qid,IPC_RMID,NULL))<0) { perror("msgctl"); exit(1); } exit(0); }