The method of inter-process communication under Linux--signal volume

Source: Internet
Author: User
Tags semaphore

The signal volume here is mainly used between different processes.
Requires four operations

    1. P operation
    2. V Operation
    3. The semaphore is initialized with the SEMCTL call command parameter setting Setval, which must be done before the semaphore is used.
    4. And you can delete the semaphore ID by semctl Call command set to Ipc_rmid:

Because the union Semun is not defined in the library, it needs to be defined by itself.

Three functions to use the semaphore

    1. Semget used to create a new semaphore or to obtain a key with an existing semaphore
    2. Semop used to change the semaphore value
    3. Semctl is used to directly control semaphore information, such as SEMOP must first use SEMCTL to give an initial value
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <sys/sem.h>Unionsemun{intValstructSemid_ds *buf;unsigned  Short int*Array;structSeminfo *__buf;};Static intSet_semvalue (void);Static voidDel_semvalue (void);Static intSemaphore_p (void);Static intSemaphore_v (void);Static intsem_id;Static intSet_semvalue (void){UnionSemun sem_union; Sem_union.val=1; Semctl (sem_id,0, setval,sem_union);}Static voidDel_semvalue (void){UnionSemun sem_union; Semctl (sem_id,0, ipc_rmid,sem_union);}Static intSemaphore_p (void){structSembuf Sem_b; sem_b.sem_num=0; sem_b.sem_op=-1; Sem_b.sem_flg=sem_undo;//auto ReleaseSemop (Sem_id,&sem_b,1);}Static intSemaphore_v (void){structSembuf Sem_b; sem_b.sem_num=0; sem_b.sem_op=1; Sem_b.sem_flg=sem_undo;//auto ReleaseSemop (Sem_id,&sem_b,1);}//main FunctionintMainintargcChar*agrv[]) {intIintPause_time;CharOp_char=' O '; Srand ((unsigned int) getpid ()); Sem_id=semget ((key_t)1234,1,0666| Ipc_creat);if(argc>1) {Set_semvalue (); Op_char=' X '; Sleep2); } for(i=0;i<Ten; i++) {semaphore_p ();printf("%c", Op_char);        Fflush (stdout); Pause_time=rand ()%3; Sleep (Pause_time);printf("%c", Op_char);        Fflush (stdout);        Semaphore_v (); Pause_time=rand ()%2;    Sleep (Pause_time); }printf("\n%d-finished\n", Getpid ());if(argc>1) {Sleep (Ten);    Del_semvalue (); }Exit(0);}

[Email protected]:~/mytest./sem1 1 &
[1] 12839
[Email protected]:~/mytest ./sem1
Ooxxooxxooxxooxxooxxxxooxxooxxooxxooxxoo

12839-finished
12840-finished
[Email protected]:~/mytest$

The method of inter-process communication under Linux--signal volume

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.