Code example for the signal amount operation in the operating system (Linux + Windows)

xiaoxiao2021-03-06  43

There is a producer process, there are two consumer processes. Producers produce 100 numbers of 1-100. Two consumers take a number from shared memory.

/ * Windows section * /

// thread.cpp: Defines the entry point of the console application. // ----------- Windows producers, consumer example program ------------------- // ----- --------- Author: zhangwei ---------------------------------- // - ------------ Date 12/10/2004 -------------------------------- - # include "stdafx.h"

/ / Raising data as a warehouse, you can put up to five data int Array [5];

// Record the number of INT Pointer for generating data;

// Record the location of the obtained data int Pointerget;

// Used to save the data and int SUM;

// critical area object critical_section csarray;

// Handle, save the Full semaphore Handle Hfulle;

// Handle, save the EMPTY semaphore;

// Producer function DWORD WINAPI PRODUCER (LPVOID LPPARAM) {INT i = 0; Pointer = 0; While (i <100) {WaitForsingleObject (Hempty, Infinite); EntercriticalSection (& CSARRAY); Array [(Pointer )% 5] = I 1; LeavecriticalSection (& CSARRAY); ReleaseSemaphore (HFULL, 1, NULL); i ;} return 0;}

// consumer functions ADWORD WINAPI ConsumerA (LPVOID lpParam) {while (1) {WaitForSingleObject (hFull, INFINITE); EnterCriticalSection (& csArray); sum = array [(pointerget )% 5]; printf ( "ConsumerA get% D / N ", array [(Pointerget -1)% 5]); if (Pointerget == 100) Printf (" THE SUM IS% D ", SUM); LeavecriticalSection (& CSARRAY); ReleaseSemaphore (Hempty, 1, Null) } Return 0;}

// consumer functions BDWORD WINAPI ConsumerB (LPVOID lpParam) {while (1) {WaitForSingleObject (hFull, INFINITE); EnterCriticalSection (& csArray); sum = array [(pointerget )% 5]; printf ( "ConsumerB get% D / N ", array [(Pointerget -1)% 5]); if (Pointerget == 100) Printf (" THE SUM IS% D ", SUM); LeavecriticalSection (& CSARRAY); ReleaseSemaphore (Hempty, 1, Null) } Return 0;}

// main function void main () {HANDLE hThreadProducer, hThreadConsumerA, hThreadComsumerB; sum = 0; pointerget = 0; InitializeCriticalSection (& csArray); hFull = CreateSemaphore (NULL, 0,5, NULL); hEmpty = CreateSemaphore (NULL, 5, 5, null);

hThreadProducer = CreateThread (NULL, 0, Producer, NULL, 0, NULL); hThreadConsumerA = CreateThread (NULL, 0, ConsumerA, NULL, 0, NULL); hThreadComsumerB = CreateThread (NULL, 0, ConsumerB, NULL, 0, NULL) ; _Getch ();

/ * Linux section * /

#include #include #include #include #include #include #include #include

#define Maxsem 5

// Declare three signal lights idint fullid; int EmptyId; int main () {struct sembuf p, v ;; union semun arg;

// Declare shared memory INT * Array; int * sum; int * set; int * get;

// Mapping shared memory array = (int *) mmap (null, sizeof (int) * 5, prot_read | prot_write, map_shared | map_anonymous, -1,0); sum = (int *) mmap (null, sizeof (int) , Prot_read | prot_write, map_shared | map_anonymous, -1,0); get = (int *) mmap (null, sizeof (int), prot_read | prot_write, map_shared | map_anonymous, -1,0); set = (int *) MMAP (NULL, SIZEOF (int), prot_read | prot_write, map_shared | map_anonymous, -1,0); * SUM = 0; * get = 0; * set = 0; // Generate signal light fullid = Semget (IPC_Private, 1, IPC_CREAT | 00666); EMPTYID = SEMGET (IPC_Private, 1, IPC_CREAT | 00666); Mutxid = Semget (IPC_Private, 1, IPC_CREAT | 00666); // Assign arg.val = 0 for signal lights; if (Semctl (fullid, 0, SetVal, Arg) == -1) PERROR ("Semctl SetVal Error");

Arg.val = maxsem; if (SemctL (EmptyID, 0, SetVal, Arg) == -1) PERROR ("Semctl SetVal Error");

arg.val = 1; if (SemCTL (MUTXID, 0, SetVal, Arg) == -1) PERROR ("setctl setval error"); // Initialization P, V operation v.SEM_NUM = 0; v.sem_op = 1 ; V.SEM_FLG = SEM_UNDO; P.SEM_NUM = 0; p. Sem_op = -1; p. Sem_flg = SEM_UNDO;

// Producer process if (fork () == 0) {

INT i = 0; While (i <100) {

Semop (EmptyID, & P, 1); Semop (Mutxid, & P, 1); Array [* (SET)% MaxSem] = i 1; Printf ("Producter% D / N", array [(* set)% MAXSEM ]); (* SET) ;

Semop (Mutxid, & V, 1); Semop (Fullid, & V, 1); i ;} Sleep (10); Printf ("Producter Is Over"); Exit (0);} else {// Consumera Process IF (Fork () == 0) {while (1) {

Semop (Fullid, & P, 1); Semop (Mutxid, & P, 1);

IF (* get == 100) Break; * SUM = Array [(* get)% MAXSEM];

Printf ("THE COMSUMERA GET NUMBER% D / N", Array [(* get)% MAXSEM]);

(* GET) ; if (* get == 100) Printf ("THE SUM IS% D / N", * SUM); SEMOP (Mutxid, & V, 1); Semop (EmptyID, & V, 1); SLEEP (1);

} Printf ("Consumera Is Over"); exit (0);

} else {// connumer B process IF (fork () == 0) {while (1) {

Semop (Fullid, & P, 1); Semop (Mutxid, & P, 1);

IF (* get == 100) BREAK;

* SUM = Array [(* get)% MAXSEM];

Printf ("THE COMSUMERB GET NUMBER% D / N", array [(* get)% MaxSem]); (* get) ; if (* get == 100) Printf ("the sum is% d / n" * SUM); Semop (Mutxid, & V, 1); SEMOP (EmptyID, & V, 1); SLEEP (1);

} Printf ("consumerb is over"); exit (0);}}} // Sleep (20); return 0;}

转载请注明原文地址:https://www.9cbs.com/read-47616.html

New Post(0)