|
|
``semget system call example'' is a menu-driven program. It allows all possible combinations of using the semget 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 4-8) by including the required header files as specified by the semget(2) manual page. Note that the sys/errno.h header file is included as opposed to declaring errno as an external variable; either method will work.
Variable 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:
The program begins by prompting for a hexadecimal key, an octal operation permissions code, and the control command combinations (flags) which are selected from a menu (lines 15-32). All possible combinations are allowed even though they might not be viable. This allows observing the errors for invalid combinations.
Next, the menu selection for the flags is combined with the operation permissions; the result is stored in opperm_flags (lines 36-52).
Then, the number of semaphores for the set is requested (lines 53-57); its value is stored in nsems.
The system call is made next; the result is stored in the semid (lines 60, 61).
Since the semid variable now contains a valid semaphore set identifier or the error code (-1), it is tested to see if an error occurred (line 63). If semid equals -1, a message indicates that an error resulted and the external errno variable is displayed (line 65). Remember that the external errno variable is only set when a system call fails; it should only be examined immediately following system calls.
If no error occurred, the returned semaphore set identifier is displayed (line 69).
The example program for the semget system call follows. We suggest that you name the source program file semget.c and the executable file semget.
1 /*This is a program to illustrate 2 *the semaphore get, semget(), 3 *system call capabilities.*/4 #include <stdio.h> 5 #include <sys/types.h> 6 #include <sys/ipc.h> 7 #include <sys/sem.h> 8 #include <errno.h>
9 /*Start of main C language program*/ 10 main() 11 { 12 key_t key; /*declare as long integer*/ 13 int opperm, flags, nsems; 14 int semid, opperm_flags;
15 /*Enter the desired key*/ 16 printf("\nEnter the desired key in hex = "); 17 scanf("%x", &key);
18 /*Enter the desired octal operation 19 permissions.*/ 20 printf("\nEnter the operation\n"); 21 printf("permissions in octal = "); 22 scanf("%o", &opperm);
23 /*Set the desired flags.*/ 24 printf("\nEnter corresponding number to\n"); 25 printf("set the desired flags:\n"); 26 printf("No flags = 0\n"); 27 printf("IPC_CREAT = 1\n"); 28 printf("IPC_EXCL = 2\n"); 29 printf("IPC_CREAT and IPC_EXCL = 3\n"); 30 printf(" Flags = "); 31 /*Get the flags to be set.*/ 32 scanf("%d", &flags);
33 /*Error checking (debugging)*/ 34 printf ("\nkey =0x%x, opperm = 0%o, flags = %d\n", 35 key, opperm, flags); 36 /*Incorporate the control fields (flags) with 37 the operation permissions.*/ 38 switch (flags) 39 { 40 case 0: /*No flags are to be set.*/ 41 opperm_flags = (opperm | 0); 42 break; 43 case 1: /*Set the IPC_CREAT flag.*/ 44 opperm_flags = (opperm | IPC_CREAT); 45 break; 46 case 2: /*Set the IPC_EXCL flag.*/ 47 opperm_flags = (opperm | IPC_EXCL); 48 break; 49 case 3: /*Set the IPC_CREAT and IPC_EXCL 50 flags.*/ 51 opperm_flags = (opperm | IPC_CREAT | IPC_EXCL); 52 break; 53 default: /* Invalid Input */ 54 exit(-1); 55 }
56 /*Get the number of semaphores for this set.*/ 57 printf("\nEnter the number of\n"); 58 printf("desired semaphores for\n"); 59 printf("this set (25 max) = "); 60 scanf("%d", &nsems);
61 /*Check the entry.*/ 62 printf("\nNsems = %d\n", nsems);
63 /*Call the semget system call.*/ 64 semid = semget(key, nsems, opperm_flags);
65 /*Perform the following if the call is unsuccessful.*/ 66 if(semid == -1) 67 { 68 printf("The semget call failed, error number = %d\n", errno); 69 } 70 /*Return the semid upon successful completion.*/ 71 else 72 printf("\nThe semid = %d\n", semid); 73 exit(0); 74 }
semget system call example