DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Interprocess communication

Example program

``semop system call example'' is a menu-driven program. It allows all possible combinations of using the semop system call to be exercised.

From studying this program, you can observe the method of passing arguments and receiving return values. The user-written program requirements are pointed out.

This program begins (lines 5-9) by including the required header files as specified on the shmop(S) manual page. Note that in this program errno is declared as an external variable; therefore, the sys/errno.h header file does not have to be included.

Variable and structure names have been chosen to be as close as possible to those in the synopsis. Their declarations are self explanatory. These names make the program more readable and are perfectly valid since they are local to the program.

The variables declared for this program and what they are used for are as follows:


sembuf[10]
used as an array buffer (line 14) to contain a maximum of ten sembuf type structures; ten is the standard value of the tunable parameter SEMOPM, the maximum number of operations on a semaphore set for each semop system call

sops
used as a pointer (line 14) to the sembuf array for the system call and for accessing the structure members within the array

string[8]
used as a character buffer to hold a number entered by the user

rtrn
used to store the return value from the system call

flags
used to store the code of the IPC_NOWAIT or SEM_UNDO flags for the semop system call (line 59)

sem_num
used to store the semaphore number entered by the user for each semaphore operation in the array

i
used as a counter (line 31) for initializing the structure members in the array, and used to print out each structure in the array (line 78)

semid
used to store the desired semaphore set identifier for the system call

nsops
used to specify the number of semaphore operations for the system call; must be less than or equal to SEMOPM

First, the program prompts for a semaphore set identifier that the system call is to perform operations on (lines 18-21). semid is stored in the semid variable (line 22).

A message is displayed requesting the number of operations to be performed on this set (lines 24-26). The number of operations is stored in the nsops variable (line 27).

Next, a loop is entered to initialize the array of structures (lines 29-76). The semaphore number, operation, and operation command (flags) are entered for each structure in the array. The number of structures equals the number of semaphore operations (nsops) to be performed for the system call, so nsops is tested against the i counter for loop control. Note that sops is used as a pointer to each element (structure) in the array, and sops is incremented just like i. sops is then used to point to each member in the structure for setting them.

After the array is initialized, all of its elements are printed out for feedback (lines 77-84).

The sops pointer is set to the address of the array (lines 85, 86). sembuf could be used directly, if desired, instead of sops in the system call.

The system call is made (line 88), and depending upon success or failure, a corresponding message is displayed. The results of the operation(s) can be viewed by using the semctl GETALL control command.

The example program for the semop system call follows. We suggest that you name the source program file semop.c and the executable file semop.

 1    /*This is a program to illustrate
 2     *the semaphore operations, semop(),
 3     *system call capabilities.
 4     */

5 /*Include necessary header files.*/ 6 #include <stdio.h> 7 #include <sys/types.h> 8 #include <sys/ipc.h> 9 #include <sys/sem.h> 10 /*Start of main C language program*/ 11 main() 12 { 13 extern int errno; 14 struct sembuf sembuf[10], *sops; 15 char string[8]; 16 int retrn, flags, sem_num, i, semid; 17 unsigned nsops;

18 /*Enter the semaphore ID.*/ 19 printf("\nEnter the semid of\n"); 20 printf("the semaphore set to\n"); 21 printf("be operated on = "); 22 scanf("%d", &semid); 23 printf("\nsemid = %d", semid);

24 /*Enter the number of operations.*/ 25 printf("\nEnter the number of semaphore\n"); 26 printf("operations for this set = "); 27 scanf("%d", &nsops); 28 printf("\nsops = %d", nsops);

29 /*Initialize the array for the 30 number of operations to be performed.*/ 31 for(i = 0, sops = sembuf; i < nsops; i++, sops++) 32 {

33 /*This determines the semaphore in 34 the semaphore set.*/ 35 printf("\nEnter the semaphore\n"); 36 printf("number (sem_num) = "); 37 scanf("%d", &sem_num); 38 sops->sem_num = sem_num; 39 printf("\nThe sem_num = %d", sops->sem_num);

40 /*Enter a (-)number to decrement, 41 an unsigned number (no +) to increment, 42 or zero to test for zero. These values 43 are entered into a string and converted 44 to integer values.*/ 45 printf("\nEnter the operation for\n"); 46 printf("the semaphore (sem_op) = "); 47 scanf("%s", string); 48 sops->sem_op = atoi(string); 49 printf("\nsem_op = %d\n", sops->sem_op);

50 /*Specify the desired flags.*/ 51 printf("\nEnter the corresponding\n"); 52 printf("number for the desired\n"); 53 printf("flags:\n"); 54 printf("No flags = 0\n"); 55 printf("IPC_NOWAIT = 1\n"); 56 printf("SEM_UNDO = 2\n"); 57 printf("IPC_NOWAIT and SEM_UNDO = 3\n"); 58 printf(" Flags = "); 59 scanf("%d", &flags);

60 switch(flags) 61 { 62 case 0: 63 sops->sem_flg = 0; 64 break; 65 case 1: 66 sops->sem_flg = IPC_NOWAIT; 67 break; 68 case 2: 69 sops->sem_flg = SEM_UNDO; 70 break; 71 case 3: 72 sops->sem_flg = IPC_NOWAIT | SEM_UNDO; 73 break; 74 default: /* Invalid Input */ 75 exit(-1); 76 } 77 printf("\nFlags = 0%o\n", sops->sem_flg); 78 }

79 /*Print out each structure in the array.*/ 80 for(i = 0; i < nsops; i++) 81 { 82 printf("\nsem_num = %d\n", sembuf[i].sem_num); 83 printf("sem_op = %d\n", sembuf[i].sem_op); 84 printf("sem_flg = 0%o\n", sembuf[i].sem_flg); 85 printf(" "); 86 }

87 sops = sembuf; /*Reset the pointer to 88 sembuf[0].*/

89 /*Do the semop system call.*/ 90 retrn = semop(semid, sops, nsops); 91 if(retrn == -1) { 92 printf("\nSemop failed, error = %d\n", errno); 93 } 94 else { 95 printf ("\nSemop was successful\n"); 96 printf("for semid = %d\n", semid);

97 printf("Value returned = %d\n", retrn); 98 } 99 }

semop system call example


Next topic: Shared memory
Previous topic: Using semop

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005