Title: Linux Process Communication - Sample Sample
I. Related knowledge semaphore: an integer; greater than or equal to 0 Times can be used for the number of resource entities used by the process; less than 0 times, the table is waiting for the number of processes in the critical area; the initial value of the signal amount used for mutually exclusive should be greater than 0; can only be changed by the P, V primitive operation; the semaphore element is composed of: 1, indicating the value of the semaphore element; 2, the final operation of the semaphore output ID 3, the number of processes of the semaphore element value 1 4. Wait for the number of processes of the semaphore element value of 0; Second, the main function 1.1 Create a signal amount int semget (key_t key, // identifies the keyword of the quantity, there are three methods: 1, use IPC - private to let the system Produce, // 2, pick a random number, 3, using FTOK from the file path name to generate int NSEMES, // signal quantity concentration element INT FLAG // ipc_creat; IPC_EXCL only creates successfully when the quantity set does not exist) : Return the semaphore: Return -1 1.2 Use the FTOK function to generate a keyword key_t ftok according to the file path name; the path name must have the corresponding permissions 1.3 Control SemCTL (int SMID, // Signal set of handle INT SEMNUM, // signal quantity set of INT cmd, // command / * Union SENUM ARG * / ... //) Success: Return the corresponding value failed: Return -1 command details Description: CMD: IPC_rmid Delete A Signal IPC_EXCL Only the value setVal setting the amount of the specified seminated value of the specified semaphore GetPID gets the value of the value of the specified semaphore GetPid for the value of the amount of the specified semaphum. The last process ID GetNCNT that manipulates this element gets the number of processes that are waiting for element to become 1 Getzcnt gets the number of processes for waiting elements to zero. Union SENUM is defined as follows: Union SENUM {Int Val; struct semid_ds * buf; unsigned short * array;} AGC The semid_ds is defined as follows: struct semid_ds {struct IPC_pem sem_pem; // Operation Pemission Struct time_t sem_otime; // last semop () time time_t sem_ctime; // last time changed by semctl () struct sem * sembase; // ptr to first semaphore in array struct sem_queue * sem_pending; // pending operations struct sem_queue * sem_pending_last; / / Last Pending Operations Struct Sem_undo * undo; // undo requests on this arrays; // number inst sem_nsems; // Number of semaphores in set}; 1.4 pair signal amount 1 or -1 or test is 0 int Semop (int SMID, Struct Sembuf * SOPS, // Point to the number of element operations in an array of element Operation arrays in an array) Structure SEMBUF defines sembuf {short int SEM_NUM; // Semaphore Number Short Int SEM_OP;
// Semaphore Operaion Short Int Sem_flg // Operation Flag}; 3, Example: 2.1 Server #include
#define SEGSIZE 1024 # define READTIME 1union semun {int val; struct semid_ds * buf; unsigned short * array;} arg; // the amount of signal generated int sem_creat (key_t key) {union semun sem; int semid; sem.val = 0 Semid = Semget (key, 1, ipc_creat | 0666); if (-1 == semid) {Printf ("Create Semaphore Error / N"); exit (-1);} Semctl (SEMID, 0, SETVAL, SEM ); Return SemID;} // Delete Semual Void Del_Sem (int SMID) {UNION SEMUN SEM; SEM.VAL = 0; SemCTL (SEMID, 0, IPC_RMID, SEM);}
// Pint P (INT SEMID) {struct sembuf SOPS = {0, 1, IPC_NOWAIT}; Return (SemoP (SemID, & Sops, 1));} // Vint V (int SEMID) {structure sembuf SOPS = {0 , -1, IPC_nowait}; RETURN (SemoP (SemID, & Sops, 1));} int main () {key_t key; int shm; char; char msg [7] = "-data-"; char I; struct semid_ds buf; key = ftok ("/", 0); shmid = shmget (key, segsize, ipc_creat | 0604); if (-1 == shmid) {Printf ("Create Shared Memory Error / N") Return -1;} shm = (char *) SHMAT (SHMID, 0, 0); IF (-1 == (int) shm) {Printf ("attach shared memory error / n"); return -1;} SEMID = SEM_CREAT (KEY); for (i = 0; i <= 3; i ) {Sleep (1); P (SEMID); SLEEP (READTIME); MSG [5] = '0' i; memcpy (SHM , MSG, SIZEOF (MSG)); SLEEP (58); V (SemID);} SHMDT (SHM); SHMCTL (SHMID, IPC_RMID, & BUF); DEL_SEM (SEMID); RETURN 0; // GCC -O SHM SHM. C -G} 2.2 Client #include
#define segsize 1024 # Define Readtime 1Union semion {int val; struct semid_ds * buf; unsigned short * array;} arg;
// Printing program execution time void out_time (void) {static long start = 0; time_tTM; if (0 == start) {TM = Time (null); start = (long) Tm; Printf ("now start .. ./N ");} Printf (" SECOND:% LD / N ", (" TIME (NULL)) - Start);} // Create a semaphore INT new_SEM (key_t key) {UNION SEMUN SEM; int SEMID ; Sem.val = 0; SemId = Semget (KEY, 0, 0); if (-1 == Semid) {Printf ("Create Semaphore Error / N"); exit (-1);} returnid;}
// Waiting for the signal to become 0void WAIT_V (int STRUCT SEMBUF SOPS = {0,0,0}; Semop (SemID, & SOPS, 1);
INT main (void) {key_t key; int shmID, char * shm; char msg [100]; char i; key = ftok ("/", 0); shmid = shmget (key, segsize, 0); if (-1 == shmid) {Printf ("Create Shared Memory Error / N); RETURN-1;} SHM = (Char *) Shmat (SHMID, 0, 0); if (-1 == (int) SHM ) {Printf ("attach shared memory error / n"); return -1;} semid = new_sem (key); for (i = 0; i <3; i ) {Sleep (2); Wait_V (SEMID) PRINTF ("Message Get IS:% S / N", SHM 1); OUT_TIME ();} SHMDT (SHM); Return 0; // GCC -O SHMC shmc.c -g}