Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

signals & message queues overview

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Hier ansehen

1 von 16 Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

Ähnlich wie signals & message queues overview (20)

Anzeige

Aktuellste (20)

signals & message queues overview

  1. 1. SIGNALS and MESSAGE QUEUES
  2. 2. SIGNALS • Interrupts that are sent to a process when an event happens. • Communication channel between user processes and from kernel to user process. • Like program attempting to access a nonexistent file.
  3. 3. Contd. • System can be of type:▫ ▫ ▫ ▫ ▫ Hardware Software I/O Process Resource
  4. 4. SENDING SIGNALS • A program can signal a different program using the kill() system call with prototype int kill(pid_t pid, int sig); • This will send the signal with number sig to the process with process ID pid. Signal numbers are small positive integers. • A user can send a signal from the command line using the kill command. Common uses are kill -9 N to kill the process with pid N
  5. 5. SEMANTICS • When two signals arrive shortly after each other, the second one will be lost if it arrives before the signal handler is called. • if it arrives after the signal handler is called, the default action will happen - this may very well kill the process.
  6. 6. RECEIVING SIGNALS • When a process receives a signal, a default action happens, unless the process has arranged to handle the signal. • For example, by default SIGHUP, SIGINT, SIGKILL will kill the process; SIGQUIT will kill the process and force a core dump; SIGSTOP, SIGTTIN will stop the process; SIGCONT will continue a stopped process; SIGCHLD will be ignored.
  7. 7. CONTD. • When a signal arrives, the process is interrupted, the current registers are saved, and the signal handler is invoked. When the signal handler returns, the interrupted activity is continued. • The three most common things to do in a signal handler are ▫ set a flag variable and return immediately ▫ throw away all the program was doing, and restart at some convenient point ▫ clean up and exit.
  8. 8. BLOCKING SIGNALS • Each process has a list of currently blocked signals. When a signal is blocked, it is not delivered but remains pending. • i.e. no signal handling routine is called
  9. 9. RETURNING FORM A SIGNAL HANDLER • When the program was interrupted by a signal, its status was saved, to be restored just before execution continues at the point of interruption. • the kernel arranges that the return from the signal handler causes a jump to a short code sequence (sometimes called trampoline) that executes a sigreturn() system call. • This system call takes care of everything
  10. 10. MESSAGE QUEUES • One process establishes a message queue that others may access. Often a server will establish a message queue that multiple clients can access. • Features of Message Queues ▫ A process generating a message may specify its type when it places the message in a message queue. ▫ Another process accessing the message queue can use the message type to selectively read only messages of specific type(s) in a first-in-first-out manner.
  11. 11. Messaging Methods
  12. 12. ATTRIBUTES • A conceptual view of a message queue: • The attributes of each element on the queue: - long integer type; - size of the data portion of the message (can be zero); * A message queue element then has one more field: - data (if the length is greater than zero) * Message Structure
  13. 13. SYSTEM QUEUE
  14. 14. msgid_ds • Fully describes message queue. struct msqid_ds { struct ipc_perm msg_perm; /*operation permission struct */ struct msg *msg_first; /* pointer to first message on q*/ struct msg *msg_last; /* point to last message on q */ ulong msg_cbytes; /* current # bytes on q */ ulong msg_qnum; /* # of message on q */ ulong msg_qbytes; /* max # of bytes on q */ pid_t msg_lspid; /* pid of last msgsnd */ pid_t msg_lrpid; /* pid of last msgrcv */ time_t msg_stime; /* last msgsnd time */ ............................ }; /* total 17 members */
  15. 15. msgsnd System Call place (send) message in the message queue. int msgsnd ( int msqid, const void *msgp, size_t msgsz, int msgflg) ▫ Returns: Success: 0; Failure: -1 ▫ Arguments  int msgid: valid message queue identifier  const void *msgp: address of the message to be sent  size_t msgsz: the size of the message to be sent.  int msgflg: Two possible values:  0: Block, if the message queue is full  IPC_NOWAIT : don’t wait if message queue is full
  16. 16. msgrcv System Call • retrieve message from the message queue. int msgrcv ( int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);  Return: Success: number of bytes actually received; Failure: -1 • Arguments: ▫ ▫ ▫ ▫ ▫ int msqid: the message queue identifier. void *msgp: a point to received message location (structure). size_t msgsz: the maximum size of the message in bytes. long msgtype: the type of the message to be retrieved. int msgflg: to indicate what action should be taken.  0: error if size of message exceeds msgsz  MSG_NOERROR: if size of message exceeds msgsz, accept msgsz bytes  IPC_NOWAIT: return –1 with errno set to ENOMSG

×