Shared Memory Control

The shmcti system call permits the user to perform a number of generalized control operations on an existing shared memory segment and on the system shared memory data structure (see Table 8.4). Table 8.4. Summary of the shmcti System Call. Summary nt Shmctl(int shmid, int cmd, struct shmid_ds *buf) Success Failure Sets errno There are three arguments for the shmcti system call. The first, shmid, is a valid shared memory segment identifier generated by a prior shmget system call. The second...

Explanation

A signal was received by the process before data was sent. The socket is set to non-blocking, and no connections are pending. Insufficient memory to perform operation. Argument references location outside user address space. toien argument contains an incorrect value. Local end of a connection-oriented socket is closed. The socket argument is a file descriptor, not a socket descriptor. Socket type requires message to be sent to be atomic (all sent at once) and the message to send is too long....

Exercise

Modify Program 3.11 to use thewait4 library function. After each child terminates, have the parent process display the number of page faults the child process incurred. A page fault occurs when a program requests data that is not currently in memory. To satisfy the request the operating system must locate the data and load it into memory. As loading data from a device takes time and slows down processing the fewer page faults generated the better. Processes are generated by the fork system...

A Unix Domain Datagram Socket Example

Our UNIX domain datagram socket example is somewhat similar in function to the stream socket example presented in Section 10.4. In this example, the server creates a datagram socket (SOCK_DGRAM) in the UNIX domain and binds it to an address (file name). The client also creates a datagram socket and binds it to an address (using a different file name, unique to each client process). The client and server use the sendto and recvfrom network calls for communication. The client generates 10...

The first call to semget provided the system limits have not been reached creates a set of three semaphores The

Create a second set of three semaphores. The call uses the same key value as the first but includes the specification IPC_EXCL. With the IPC_EXCL flag set and the previous successful creation of the semaphore set using the same key value, this invocation of semget will fail. The third call to semget creates a three-semaphore set used by specifying IPC_PRIVATE instead of using the ftok key value. The semaphore identifier generated for this set will be private to this process. When the program is...

The use of the sigprocmask system call is shown in Program

* Demonstration of the sigprocmask call 7 void signal_catcher(int) < --1 struct sigaction new_action < - 2 sigprocmask(SIG_BLOCK, & new_signals, NULL) new_action.sa_handler signal_catcher 20 if (sigaction(SIGUSR2, & new_action, NULL) -1) cout Waiting for signal endl cout Done endl cout Received signal n will release SIGUSR1 endl sigprocmask(SIG_UNBLOCK, & new_signals, NULL) cout SIGUSR1 released endl (1) Empty (clear) the set of signals. (2) Add the SIGUSR1 signal to this set. The...

Top

A second primitive interprocess communication technique involves the use of signals. As previously indicated, signals occur asynchronously (with no specified timing or sequencing). Signals, which can be sent by processes or by the kernel, serve as notification that an event has occurred. Signals are generated when the event first occurs and are considered to be delivered when the process takes action on the signal. The delivery of most signals can be blocked so the signal can be acted upon...

D4 Viewing and Interpreting Profile Data

The profile data stored in the gmon.out file is analyzed and displayed using the gprof utility. If called with no options or command-line arguments, gprof assumes that the executable it is working with is a.out and that the profile data for this executable is in the file gmon.out. Also, unless directed otherwise, gprof generates and displays a fully annotated flat profile and a call graph using the collected data. Like most utilities, gprof supports an extensive set of options. We will examine...

Linking Object Code

Code from library files, predefined or user-defined, is combined with object code from the source program at compile time on an as-needed basis. When programming in C C++, additional library files containing the object code for system calls and library functions not contained in the standard library can be specified at compile time. This is done by using the -I compiler option, followed by the library name without the lib prefix and the a extension. For example, the compilation command...

Semaphore Control

The semget system call (Table 7.3) is used to create or gain access to a set of semaphores. The semcti system call allows the user to perform a variety of generalized control operations on the system semaphore structure, on the semaphores as a set, and on individual semaphores. Additional manipulative operations on specific semaphores within a set are covered in the following section on semaphore operations (Section 7.4). Table 7.3. Summary of the semcti System Call. < sys types.h> Manual...

Info

10 fork() cout hee endl fork() cout ha endl fork() cout ho endl return 0 assuming all fork system calls are successful, how many lines of output will be produced Is it ever possible for a ho to be output before ahee Why is this Would the number of hees, has and hos be different if the endl was left out of each of the cout statements Why Processes generate child processes for a number of reasons. In a Linux environment, there are several long-lived processes, which run continuously in the...

Mutex Variables

One of the easiest methods to ensure coordination is to use a mutual exclusion lock, or mutex. Conceptually, a mutex can be thought of as a binary semaphore with ownership whose state either allows (0, unlocked) or prohibits (nonzero, locked) access. Unlike a binary semaphore, any thread within the scope of a mutex can lock a mutex, but only the thread that locked the mutex should unlock it. Again, while it should not be done, unlocking a mutex that a thread did not lock does not generate an...

Introduction

Conceptually, a semaphore is a data structure that is shared by several processes. Semaphores are most often used to synchronize operations when multiple processes access a common, non-shareable resource. By using semaphores, we attempt to avoid starvation (which occurs when a process is habitually denied access to a resource it needs) and deadlock (which occurs when two or more processes each hold a resource that the other needs while waiting for the other process to release its resource)....

J A reentrant function supports access by multiple threads and maintains the integrity of its data across consecutive

Iinux ar t usr lib libc.a grep'_r .o' pr-n-2-t 28 efgcvt_r.o 59 nscd_gethst_r.o 29 qefgcvt_r.o 60 getutent_r.o 30 hsearch_r.o 61 getutid_r.o 31 getspent_r.o 62 getutline_r.o As shown, reentrant functions have _r appended to their non-reentrant name. The object code library for POSIX thread functions must be linked in at compile time using the compiler switch -ipthread. Additionally, _posix_c_source should be defim if strict compliance to POSIX standards is required. For example, L J On...

Message Queue Operations

Message queues are used to send and receive messages. An actual message, from the system's standpoint, is defined by the msgbuf structure found in the header file long int mtype * type of received sent message 7 char mtext 1 * text of the message 7 This structure is used as a template for the messages to be sent to and received from the message queue. The first member of the msgbuf structure is the message type. The message type, mtype, is a long integer value and is normally greater than 0....

Constant Protocol Family

PF_ATMPVC Access to raw ATM PVCs PF_AX25 Amateur radio AX.25 protocol PFJNET6 IPv6 Internet protocols PF_NETLINK Kernel user interface device PF_PACKET Low-level packet interface PFJJNIX, PF_LOCAL Local communication PF_X25 ITU-T X.25 ISO-8208 protocol x25(7) When a socket call is made in a program in some, but not all, development settings, the socket library must be specifically linked at compile time using the option -isocket. When creating a socket, insufficient memory available. Cannot...

Appendix B Unix Error Messages

Errors generated by the failure of a system call or library function, can be displayed using the perror or strerror library function calls (see Managing Failures Section 1.5, Managing System Call Failures). For example, the error messages returned by strerror on a Linux system can be displayed in their entirety using Program B.1. Program B.1 Displaying strerror messages. for (int err 0 err < sys_nerr ++err) cout err ' t' strerror(err) endl As the output of the program will fill more than one...

Program 1016 Internet domain connectionoriented server using MSGOOB

Internet domain, connection-oriented SERVER - MSG PEEK include local_sock.h include < time.h> void signal_catcher(int) int orig_sock, Original socket in server new_sock New socket from connect socklen_t clntjen Length of client address struct sockaddrjn Internet addr client & server clnt_adr, serv_adr int len, i, Misc counters, etc. urg, mark Flag reception of OOB msg and to note its location in the stream. Catch when child terminates if (signal(SIGCHLD , signal_catcher) SIG_ERR)...

Process Resource Limits

As system resources are finite, every process is restrained by certain operating system-imposed limits. At the command line, the ulimit command (which is actually a built-in command found in the Bourne shell bin sh ) provides the user with a means to display and modify current system limits available to the shell and the processes that The C shell ( bin csh) provides a somewhat similar built-in command called limit. The command ulimit-Ha displays the hard limits for the system. The hard limits...

Mutex Kind ConstantBehavior

Fast PTHREAD_MUTEX_FAST If the mutex is already locked by another thread, the calling thread blocks. If the thread that owns (locked) the mutex attempts to lock it a second time, the thread will deadlock The thread that unlocks the mutex is assumed to be the owner of the mutex. Unlocking a nonlocked mutex will result in undefined behavior. recursive PTHREAD_MUTEX_RECURSIVE The system will the record number of lock requests for the mutex. The mutex is unlocked only when an equal number of unlock...

Multithread Semaphores

The activities of threads may also be coordinated with semaphores. A semaphore is similar in function to a mutex. However, a semaphore can act as either a binary entity similar to a mutex or as a counting entity. Counting semaphores can be used to manage multiple resources. As they are somewhat more complex, semaphores are more system-intensive than mutexes. Semaphore concepts and operations were presented in some detail in Chapter 7. However, the Chapter 7 semaphore operations are System...

Message Queue Control

The ownership and access permissions, established when the message queue was created, can be examined and modified using the msgcti system call (see Table 6.7). The msgcti system call references the message queue indicated by the msqid argument. The value of the cmd argument is used to indicate the action that msgcti should take. The following defined constants actions can be specified Table 6.7. Summary of the msgget System Call. < sys types.h> Manual Section int msgget (int msqid,int cmd,...

Creating a Shared Memory Segment

The shmget system call is used to create the shared memory segment and generate the associated system data structure or to gain access to an existing segment. The shared memory segment and the system data structure are identified by a unique shared memory identifier that the shmget system call returns (see Table 8.1). Providing no system parameters are exceeded, the shmget system call creates a new shared memory segment if The value for its first argument, key, is the symbolic constant...

Using a File as Shared Memory

Most versions of Linux-UNIX also support the mmap system call, which can be used to map a file to a process's virtual memory address space. In many ways mmap is more flexible than its shared memory system call counterpart. Once a mapping has been established, standard system calls rather than specialized system calls can be used to manipulate the shared memory object (Table 8.11). Unlike memory, the contents of a file are nonvolatile and will remain available even after a system has been shut...

Condition Flag Set Action Taken by semop

Semval o Increment semzcnt for the semaphore and wait (block) semval o, then adjust semzcnt and return. semid is removed, then return -1 and seterrno to EIDRM. A signal is caught, then adjust semzcnt and set errno to EINTR. Constant pe rror Message Explanation While in a wait queue for the semaphore, a signal was received by the calling process. The value for nsops is greater than the system limit. The requested operation would cause the calling process to block, but IPC_NOWAIT was specified....

The u Area

In addition to the text, data, and stack segments, the operating system also maintains for each process a region called the u area (user area). The u area contains information specific to the process (e.g., open files, current directory, signal actions, accounting information) and a system stack segment for process use. If the process makes a system call (e.g., the system call to write in the function main in Program 1.1), the stack frame information for the system call is stored in the system...

We then use rpcgen with the a and c flags to generate the header file the client and server stub files the client and

Figure 9.20 Using rpcgen with the -a and -C flags. linux rpcgen -a -C fact.x linux Is -x fact_client.c fact_clnt.c fact.h fact_server.c fact_svc.c fact.x Makefile.fact As shown, passing rpcgen the protocol definition file with the -a and -c flags generates six files the header file, fact.h, and the RPC stub files, fact_cint.c and fact_svc.c, which are similar in content and nature to those in the previous example. The three new files created by rpcgen bear further investigation. The client...