Linux Linux Program Practice Five

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.