Linux multi-process + semaphore + shared memory programming

Source: Internet
Author: User

This program implements data communication between two processes that are not related to each other through shared memory.

At the same time, the signal volume is used to guarantee the read and write synchronization of two processes: the receiver cannot read the data while the sender is writing the shared memory, and the sender cannot write the data when the receiver reads the data.

1. Fork Create Child process

2, using two yuan signal, synchronous read-write end

Fork_shm.c

#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include "send_recv.h" int main (void) { printf ("fork test!\n");p id_t pid;if ((Pid=fork ()) ==-1) printf ("Fork Error"), else if (pid==0) {printf ("in the process\n ");p rintf (" The Father process ' s ppid is%d\n ", getppid ()); Fork_recv ();} Else{//sleep (1);p rintf ("In the parent process\n");p rintf ("The son process's PID is%d\n", getpid ()); Fork_send ();} return 0;}

Send_recv.c

#include "fork_recv.c" #include "fork_send.c" int fork_recv (); int fork_send ();

Fork_send.c

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/shm.h> #include <sys/ipc.h> #include <sys/sem.h> #include <string.h>int fork_send () {int Running=1;int shid;int semid;int value;void *sharem=null;struct sembuf sem_b;sem_b.sem_num = 0;SEM_B.SEM_FLG = SEM_UNDO; if ((Semid=semget (key_t) 123456,1,0666| Ipc_creat) ==-1) {perror ("Semget"); exit (exit_failure);}        if (Semctl (semid, 0, setval, 0) = =-1) {printf ("SEM init error"); if (Semctl (semid,0,ipc_rmid,0)!=0) {perror ("Semctl"); exit (exit_failure);} Exit (exit_failure);} Shid=shmget (key_t) 654321, (size_t) 2048,0600| Ipc_creat), if (shid==-1) {perror ("Shmget"); exit (exit_failure);} Sharem=shmat (shid,null,0); if (sharem==null) {perror ("Shmat"); exit (exit_failure);} while (running) {if ((Value=semctl (semid, 0, Getval) ==0) {printf ("Write Data operate\n");p rintf ("Please input something : \ n "); scanf ("%s ", Sharem), Sem_b.sem_op = 1;if (Semop (Semid, &sem_b, 1) = =-1) {fprintf (stderr, "Semaphore_p failed\n"); exit (exit_failure);}} if (strcmp (Sharem, "end") ==0) running--;} SHMDT (sharem); return 0;}


Fork_recv.c

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include < string.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h>int fork_recv () {int Running=1;char *shm_p=null;int shmid;int semid;int value;struct sembuf sem_b;sem_b.sem_num = 0;SEM_B.SEM_FLG = SEM_UNDO; if ((Semid=semget (key_t) 123456,1,0666| Ipc_creat) ==-1) {perror ("Semget"); exit (exit_failure);} Shmid=shmget (key_t) 654321, (size_t) 2048,0600| Ipc_creat), if (shmid==-1) {perror ("Shmget"); exit (exit_failure);} Shm_p=shmat (shmid,null,0); if (shm_p==null) {perror ("Shmat"); exit (exit_failure);} while (running) {if ((Value=semctl (semid, 0, Getval) ==1) {printf ("read Data operate\n"); sem_b.sem_op = -1;if (Semop ( Semid, &sem_b, 1) = =-1) {fprintf (stderr, "semaphore_p failed\n"); exit (exit_failure);} printf ("%s\n", shm_p);} if (strcmp (shm_p, "end") ==0) running--;} SHMDT (shm_p); if (Shmctl (shmid,ipc_rmid,0)!=0) {perror ("Shmctl"); exit (exit_failure);} if (Semctl (semid,0,ipc_rmid,0)! =0) {perror ("Semctl"); exit (exit_failure);} return 0;}


Linux multi-process + semaphore + shared memory programming

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.