* The exchange of PID in the message queue are only between 2 processes only, so no one else reading these messages. What do you mean by remove the message queue? I never really had in mind the fact that deleting a message from the queue after it have been read, I thought that after being read, he removed himself from
* With IPC_NOWAIT, it gives me my printf error and move on, but, it succeeded well to send its PID to the other!
* This is the process code "code" and the "choice" (I put that passage in question, and other functions are not useful blabla) for which the printf is everywhere, as the multiprocess debugging with GDB is not trivial, I screwed the printf around to debug by hand!
One last thing before looking at the heart of the matter: the ipcs are created by a separate process, that is why these 2 processes are only attached to different IPCs they need
I have highlighted the passage in which process 1 is blocked waiting (or return -1 with IPC_NOWAIT) but I leave the variable declarations in case there would need.
CODE.C
Code:
int main ()
{
int PIDchoice, t sem_computer, sem_phrase, sem_letter, BAL, numeroPhrase, letterCodage;
char sentence [65];
char conditionArret [] = "stop";
char conditionArret2 [] = "STOP";
struct msgbuf
{
long mtype;
char mtext [65];
} message;
message.mtype = 1;
signal (SIGUSR1, signal_ignore) / * deviation signal * /
// sem_computer = semget (cle_computer, 1.0); / * semaphore count * attachment /
sem_phrase = semget (cle_phrase, 1.0); / * semaphore phrase attachment * /
sem_letter = semget (cle_letter, 1.0); / * semaphore commitment letter * /
t = open ( "TUBE", O_RDONLY); / * attachment to the tube called * /
BAL = msgget (cle_BAL, 0666); / * attachment to the mailbox * /
// up (sem_computer) / * increment counter * /
// exchange PID
printf ( "early exchange PID \ n");
sprintf (message.mtext, "% d", getpid ());
printf ( "% s is my PID \ n", message.mtext);
if ((msgsnd (BAL, & message, sizeof (message.mtext), 0 ))==- 1)
printf ( "ERROR SENDING PID");
printf ( "PID sent \ n");
if ((msgrcv (BAL, & message, 100.2, IPC_NOWAIT ))==- 1)
printf ( "ERROR RECEIVING PID \ n");
printf ( "% s is the PIDchoice \ n", message.mtext);
PIDchoice = atoi (message.mtext);
printf ( "end exchange PID \ n \ n");
// end exchange PID
here is the snippet of the exchange process by PID (no prob with him, and he receives a message without sending Show me any error message):
Want.This
Code:
int main ()
{
int sem_computer, sem_letter, sem_phrase, BAL, PIDcode, numeroPhrase, letterCode;
char conditionArret [] = "stop";
struct msgbuf
{
long mtype;
char mtext [65];
} message;
message.mtype = 2 / * choice: message type 2 * /
srand (time (NULL)) / * initialize random counter * /
signal (SIGUSR1, signal_ignore) / * deviation signal SIGUSR1 * /
// sem_computer = semget (cle_computer, 0666); / * semaphore count * attachment /
sem_letter = semget (cle_letter, 1.0); / * semaphore commitment letter * /
sem_phrase = semget (cle_phrase, 1.0); / * semaphore phrase attachment * /
BAL = msgget (cle_BAL, 0666); / * attachment to the letter box * /
// semop (...& up (sem_computer)) / * increment counter * /
/ exchange * PID * /
printf ( "early exchange PID \ n");
msgrcv (BAL, & message, 100,1,0);
printf ( "% s is the PID code \ n", message.mtext);
PIDcode = atoi (message.mtext);
printf ( "PID received \ n");
sprintf (message.mtext, "% d", getpid ());
printf ( "% s is my PID \ n", message.mtext);
// if ((msgsnd (BAL, & message, sizeof (message.mtext), 0 ))==- 1)
// printf ( "ERROR SENDING");
printf ( "FIN exchange PID \ n");
/ * end exchange PID * /
Bookmarks