Unix shared storage and semaphore routines
Routine description: execute server, two client, store content in server, read content by two client.
Three passes are synchronized using semaphores server.c---#include # include "sem_shm.h" int main (int argc,char** argv) {int ret;int semque_id / / Open a semaphore semque_id = open_semque (); / / initialize the semaphore set_sem (semque_id, 1); ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the semaphore is:% d\ n", ret); fputs ("set shared memory\ n", stdout); / / Open a shared storage int id_shm = open_shm (); int count = 3 While (count--) {ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the semaphore is:% d\ n", ret); v_sem (semque_id); fputs ("perform V operation\ n", stdout); ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the semaphore is:% d\ n", ret); fputs ("input send content\ n", stdout); char buf [100] Memset (buf,100,0); scanf ("% s", buf); / / get the first valid address for shared storage char * addr = (char *) shmat (id_shm, 0P0); if ((void*) addr = = (void*)-1) {perror ("shmat\ n"); exit (1);} memset (addr,100,0); strncpy (addr,buf,100); fputs ("content sent complete\ n", stdout); printf ("\ n") Fputs ("perform P operation\ n", stdout); p_sem (semque_id); sleep (1);} fputs ("whether to delete shared memory to exit y or n\ n", stdout); if (getchar () = ='y') {shmctl (id_shm, IPC_RMID, NULL);} return 0 }-client.c---#include # include # include # Include # include "sem_shm.h" int main (int argc Char** argv) {int ret Int semque_id = open_semque (); int shm_id = open_shm (); ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the signal set is:% d\ n", ret); while (1) {fputs ("execute operation V\ n", stdout); v_sem (semque_id); ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the signal set is:% d\ n", ret) Fputs ("read server content\ n", stdout); char * addr = (char *) shmat (shm_id, 0, SHM_RDONLY); if ((void*) addr = = (void*)-1) {perror ("shmat\ n"); exit (1);} fflush (stdout); fputs (addr,stdout); printf ("\ n"); fputs ("execute operation P\ n", stdout); p_sem (semque_id); ret = semctl (semque_id, 0, GETVAL) Printf ("the value of the first semaphore in the signal set is:% d\ n", ret); sleep (1);} / / semctl (semque_id, 0, IPC_RMID); return 0 }-sem_shm.c---#include "sem_shm.h "# include # include int open_semque (void) {key_t key = ftok (". / " 100) If (key = =-1) {perror ("ftok\ n"); exit (1);} int semque_id = semget (key, 1 ~ IPC encrypted creat | 0666); if (semque_id = =-1) {perror ("msgget\ n"); exit (1);} return semque_id;} void set_sem (int semque_id, int val) {union semun sem_union;sem_union.val = val;if (semctl (semque_id, 0, SETVAL, sem_union) =-1) {perror ("semctl_set\ n") Exit (1);}} int open_shm () {key_t key = ftok (".. /", 100); if (key = =-1) {perror ("ftok\ n"); exit (1);} / 0666 means that none of the three user group other users have the right to execute int id_shm = shmget (key, SHM_SIZE, IPC_CREAT | 0666); if (- 1 = id_shm) {perror ("shmget\ n"); exit (1);} return id_shm } void p_sem (int semque_id) {struct sembuf arry [1]; arry [0] .sem _ num = 0Tracerry [0] .sem _ op = 1Tracing Arry [0] .sem _ flg = SEM_UNDO;int ret = semop (semque_id, arry, 1); if (ret) {perror ("semop\ n"); exit (1);} void v_sem (int semque_id) {struct sembuf arry [1]; arry [0] .sem _ num = 0usarry [0] .sem _ op =-1 / / arry [0] .sem _ flg = IPC_NOWAIT;//arry [0] .sem _ flg = SEM_UNDO;int ret = semop (semque_id, arry, 1); if (ret) {perror ("semop\ n"); exit (1) }-sem_shm.h---#ifndef SEM_SHM_H # define SEM_SHM_H#include # define SHM_SIZE 100int open_semque (void) Void set_sem (int, int); void v_sem (int); void p_sem (int); int open_shm (); union semun {int val;struct semid_ds buf;unsigned short * array;} # endif / / end SEM_SHM_H---makefile---CC=gccTarget0=serverTarget1=clientwarn=-Wall # .PHONY: all clean # command: make all or make cleanclean:rm-f $(obj) main server client * ~ * gch * .o#all: $(Target0) $(Target1) $(Target0): server.o sem_shm.o$ (CC)-o $@ $^ ${warn} server.o:server.c$ (CC)-c $