Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* basic semaphore operations, UNIX-native */
- /* pointer arithmetics:
- *
- * push(pointer, v) INDICATES *(pointer++) = v
- * pop(pointer) INDICATES *(--pointer)
- * (*++v)[0] INDICATES (**++v)
- * INDICATES first char in v
- * INDICATES name of string/vector v
- * likewise, *v[0] INDICATES **v
- * and *v[n] INDICATES **(v + n)
- * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
- * char **argv INDICATES pointer to char pointer
- * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
- * int *v[len] INDICATES vector "v" of "len" pointers to int elements
- * void *funct() INDICATES function "funct" that returns a pointer-to-void
- * void (*funct)() INDICATES pointer to a function "funct" that returns void
- *
- */
- /* useful characters: [] # */
- # include <stdio.h>
- # include <stdlib.h> // for exit
- # include <sys/ipc.h>
- # include <sys/shm.h> // for shmdet
- # include <sys/sem.h>
- # include <fcntl.h> // for O_RDWR
- # define BUFFER 256
- # define RSIZE 20 // reply size
- # define exception(x) { puts(x); exit(1); }
- /* semaphore buffer */
- typedef struct {
- unsigned short num;
- short op;
- short flag;
- } sembuf;
- /* unsigned semaphore */
- struct sem_uns {
- int val;
- unsigned short * array;
- };
- int main(int argc, char * argv[])
- {
- /* type of basic semaphore operations:
- *
- * wait(sem_descr, sem_num)
- * {
- * struct sembuf operations[1] = {{ sem_num, -1, 0 }};
- * return sem_operation(sem_descr, operations, 1);
- * }
- *
- * signal(...) does the same, but with +1 in operations[].
- */
- /* SEE MEMSHARE.C
- *
- * >> Sample at: pg. 45
- * >> One function for the memory writer, one for the reader, both in a do-while(!quit)
- * >>
- * >> writer() contains shmat(), puts("please input"), scanf() and strcpy() (last two in do-while)
- * >> reader() contains shmat(), printf() (in do-while)
- * >>
- * >> main() contains: shmget(), and then
- * >> if(fork()) wait(&status); else writer(shm_descriptor);
- * >> if(fork()) wait(&status); else reader(shm_descriptor);
- * >>
- * >> ...And then shmctl(IPC_RMID).
- *
- * The difference with semaphores:
- * 1) sem_num/op/flag = 0/-1/0 & semop(ID, &op, 1) AFTER the writer() loop (= signal())
- * 2) sem_num/op/flag = 0/0/0 & semop(ID, &op, 1) BEFORE the reader() loop (= wait())
- *
- * The main() uses shmget, semget, semctl (when necessary) and forks just like in MESSAGES.C
- * Then removes the memory (shmctl) and semaphore (semctl) and quits
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement