Title: Write two processes A and B, using shared memory technology, a writes a string to the shared memory, and B prints the string that is read from the shared memory on the screen.
//Create a shared memory area#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/ipc.h>#include<sys/shm.h>intMainintArgChar*args[]) { //Create a memory sharing area intShmid=0; Shmid=shmget (Ipc_private,sizeof(Char)*1024x768,0666); if(shmid==-1) {printf ("Shmget () is failed!\n"); return-1; } printf ("Create shared memory area successfully, shared memory sector identifier is%d\n", Shmid); return 0;}
//writing data to a shared memory area#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/shm.h>intMainintArgChar*args[]) { if(Arg <2) {printf ("Please enter a parameter! \ n"); return-1; } intShmid =0; intResid=0; Shmid= Atoi (args[1]); //process attach shared memory Area void* Shmbuf =NULL; Shmbuf= Shmat (Shmid,0,0); if(Shmbuf = =-1) {printf ("process A has failed to attach shared memory area! Error message:%s\n", Strerror (errno)); return-1; } //data write process private shared memory AreaRead (Stdin_fileno, Shmbuf,sizeof(Char) *1024x768); //close the private shared memory area of this processResid=SHMDT (SHMBUF); printf ("The return value of the SHMDT () function is%d\n", Resid); return 0;}
#include <stdio.h>#include<stdlib.h>#include<string.h>//read data from the shared memory area#include <unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/shm.h>intMainintArgChar*args[]) { if(Arg <2) {printf ("Please enter a parameter! \ n"); return-1; } intShmid =0; intResid =0; Shmid= Atoi (args[1]); //attach to shared memory area void* Shmbuf =NULL; Shmbuf= Shmat (Shmid,0, shm_rdonly); if(Shmbuf = =-1) {printf ("Shmat () is failed \ n"); return-1; } printf ("%s", SHMBUF); //closes the shared memory area of the current processResid =SHMDT (SHMBUF); printf ("The return value of the SHMDT () function is%d\n", Resid); return 0;}
//Close shared memory area#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/ipc.h>#include<sys/shm.h>intMainintArgChar*args[]) { if(arg<2) {printf ("Please enter a parameter! \ n"); return-1; } intResid=0; intShmid=0; Shmid=atoi (args[1]); Resid=shmctl (Shmid,ipc_rmid,0); printf ("The return value is%d\n", Resid); return 0;}
. Suffixes:.c. OCC=gccSRCS1=CREATESHM.CSRCS2=WRA.CSRCS3=RDB.CSRCS4=rmshm.cobjs1=$ (srcs1:.c=. O) OBJS2=$ (srcs2:.c=. O) OBJS3=$ (srcs3:.c=. O) OBJS4=$ (srcs4:.c=. O) EXEC1=firstsEXEC2=nextsEXEC3=lastsEXEC4=lasts2start:$ (OBJS1) $ (OBJS2) $ (OBJS3) $ (OBJS4) $ (CC)-o $ (EXEC1) $ (OBJS1) $ (CC)-o $ (EXEC2) $ (OBJS2) $ (CC)-o $ (EXEC3) $ (OBJS3) $ (CC)-o $ (EXEC4) $ (OBJS4) @echo"^_^-----OK-----^_^". C.O: $ (CC)-WALL-G-o [email protected]-C $<Clean:rm-F $ (OBJS1) RM-F $ (OBJS2) RM-F $ (OBJS3) RM-F $ (OBJS4) RM-F $ (EXEC1) RM-F $ (EXEC2) RM-F $ (EXEC3) RM-F $ (EXEC4)
Linux Linux Program Practice Five