49 printf("\nEnter the semnum = "); 50 scanf("%d", &semnum); 51 /*Do the system call.*/ 52 retrn = semctl(semid, semnum, GETVAL, arg); 53 printf("\nThe semval = %d", retrn); 54 break; 55 case 2: /*Set a specified value.*/ 56 printf("\nEnter the semnum = "); 57 scanf("%d", &semnum); 58 printf("\nEnter the value = "); 59 scanf("%d", &arg.val); 60 /*Do the system call.*/ 61 retrn = semctl(semid, semnum, SETVAL, arg); 62 break; 63 case 3: /*Get the process ID.*/ 64 retrn = semctl(semid, 0, GETPID, arg); 65 printf("\nThe sempid = %d", retrn); 66 break; 67 case 4: /*Get the number of processes 68 waiting for the semaphore to 69 become greater than its current 70 value.*/ 71 printf("\nEnter the semnum = "); 72 scanf("%d", &semnum); 73 /*Do the system call.*/ 74 retrn = semctl(semid, semnum, GETNCNT, arg); 75 printf("\nThe semncnt = %d", retrn); 76 break;
77 case 5: /*Get the number of processes
78 waiting for the semaphore 79 value to become zero.*/ 80 printf("\nEnter the semnum = "); 81 scanf("%d", &semnum); 82 /*Do the system call.*/ 83 retrn = semctl(semid, semnum, GETZCNT, arg); 84 printf("\nThe semzcnt = %d", retrn); 85 break;
86 case 6: /*Get all of the semaphores.*/
87 /*Get the number of semaphores in 88 the semaphore set.*/ 89 arg.buf = & semid_ds; 90 retrn = semctl(semid, 0, IPC_STAT, arg); 91 if(retrn == -1) 92 goto ERROR; 93 length = arg.buf->sem_nsems; 94 /*Get and print all semaphores in the 95 specified set.*/ 96 arg.array = semvals; 97 retrn = semctl(semid, 0, GETALL, arg); 98 for (i = 0; i < length; i++) 99 { 100 printf("%d", semvals[i]); 101 /*Separate each 102 semaphore.*/ 103 printf(" "); 104 } 105 break;
106 case 7: /*Set all semaphores in the set.*/
107 /*Get the number of semaphores in 108 the set.*/ 109 arg.buf = & semid_ds; 110 retrn = semctl(semid, 0, IPC_STAT, arg); 111 if(retrn == -1) 112 goto ERROR; 113 length = arg.buf->sem_nsems; 114 printf("Length = %d\n", length); 115 /*Set the semaphore set values.*/ 116 printf("\nEnter each value:\n"); 117 for(i = 0; i < length ; i++) 118 { 119 scanf("%d", &c); 120 semvals[i] = c; 121 } 122 /*Do the system call.*/ 123 arg.array = semvals; 124 retrn = semctl(semid, 0, SETALL, arg); 125 break;
126 case 8: /*Get the status for the semaphore set.*/
127 /*Get and print the current status values.*/ 128 arg.buf = & semid_ds; 129 retrn = semctl(semid, 0, IPC_STAT, arg); 130 printf ("\nThe USER ID = %d\n", 131 arg.buf->sem_perm.uid); 132 printf ("The GROUP ID = %d\n", 133 arg.buf->sem_perm.gid); 134 printf ("The operation permissions = 0%o\n", 135 arg.buf->sem_perm.mode); 136 printf ("The number of semaphores in set = %d\n", 137 arg.buf->sem_nsems); 138 printf ("The last semop time = %d\n", 139 arg.buf->sem_otime); 140 printf ("The last change time = %d\n", 141 arg.buf->sem_ctime); 142 break;
143 case 9: /*Select and change the desired
144 member of the data structure.*/ 145 /*Get the current status values.*/ 146 arg.buf = & semid_ds; 147 retrn = semctl(semid, 0, IPC_STAT, arg.buf); 148 if(retrn == -1) 149 goto ERROR; 150 /*Select the member to change.*/ 151 printf("\nEnter the number for the\n"); 152 printf("member to be changed:\n"); 153 printf("sem_perm.uid = 1\n"); 154 printf("sem_perm.gid = 2\n"); 155 printf("sem_perm.mode = 3\n"); 156 printf("Entry = "); 157 scanf("%d", &choice); 158 switch(choice){
159 case 1: /*Change the user ID.*/
160 printf("\nEnter USER ID = "); 161 scanf ("%d", &uid); 162 arg.buf->sem_perm.uid = uid; 163 printf("\nUSER ID = %d\n", 164 arg.buf->sem_perm.uid); 165 break;
166 case 2: /*Change the group ID.*/
167 printf("\nEnter GROUP ID = "); 168 scanf("%d", &gid); 169 arg.buf->sem_perm.gid = gid; 170 printf("\nGROUP ID = %d\n", 171 arg.buf->sem_perm.gid); 172 break;
173 case 3: /*Change the mode portion of
174 the operation 175 permissions.*/ 176 printf("\nEnter MODE in octal = "); 177 scanf("%o", &mode); 178 arg.buf->sem_perm.mode = mode; 179 printf("\nMODE = 0%o\n", 180 arg.buf->sem_perm.mode); 181 break; 182 default: /* Invalid Input */ 183 exit(-1); 184 } 185 /*Do the change.*/ 186 retrn = semctl(semid, 0, IPC_SET, arg); 187 break; 188 case 10: /*Remove the semid along with its 189 data structure.*/ 190 retrn = semctl(semid, 0, IPC_RMID, arg); 191 break; 192 default: /* Invalid Input */ 193 exit(-1); 194 } 195 /*Perform the following if the call is unsuccessful.*/ 196 if(retrn == -1) 197 { 198 ERROR: 199 printf ("\nThe semctl call failed!, error number = %d\n", errno); 200 exit(0); 201 } 202 printf ("\n\nThe semctl system call was successful\n"); 203 printf ("for semid = %d\n", semid); 204 exit (0); 205 }