Linux Production consumer model __linux

Source: Internet
Author: User
Tags mutex semaphore

This paper mainly analyzes the synchronization and mutual exclusion mechanism between multiple producers, multiple consumers and multiple resources.

In the operating system, we've learned about the synchronous mutex model between producer and consumer.

Producers:

{

Production of a product;

P (empty);

P (mutex);

product into the buffer;

V (mutex);

V (full);

}

Consumers

{

P (full);

P (mutex);

Remove the product from the buffer;

V (mutex);

V (empty);

}

In real programming implementations, the IPC mechanism is used to create shared memory areas for variables used jointly by producers and consumers and to map the shared memory area to the process's address space so that the address space can be directly

to operate.

Moreover, in this model, three semaphores are used, so the semaphore set mechanism in Linux is used.

#include <shm.h> int main () {int num;
	int shmid_goods,shmid_index,semid;
	char* Shmaddr=null;
	int *indexaddr=null;

	int is_noexist=0;
	num=10; Create the shared memory area of the goods and link to the process user space.
	Size is 10 bytes.
				if ((Shmid_goods=createshm (".", ' s ', num)) ==-1 {if (errno==eexist) {if (Shmid_goods=openshm (".", ' s ')) ==-1) {
			Exit (1);
		        } else {perror ("Create shared Memory failed\n");
		Exit (1);
		}//Returns the identifier of the shared memory area if ((Shmaddr=shmat (Shmid_goods, (char*) 0,0) = = = (char*)-1) {perror ("Attach shared Memory error\n");
	Exit (1);
    /* Create a shared memory that holds the index number of the producer and consumer in buffer.
	The index points to the index value of the production/consumption buffer.
				*/if (SHMID_INDEX=CREATESHM (".", ' Z ', 2)) ==-1) {if (errno==eexist) {if (Shmid_index=openshm (".", ' Z ')) ==-1) {
			Exit (1);
		        } else {perror ("Create shared Memory failed\n");
		Exit (1);
	} else {is_noexist=1; //attach the shared memory to the current process if (Indexaddr=shmat (Shmid_index, (int*) 0,0)) = = (int*)-1) {perror (" Attach shared MEmory error\n ");
	Exit (1);
		/* index initialized to 0/if (is_noexist) {indexaddr[0]=0;
	indexaddr[1]=0;
	/* This function is used to generate a semaphore set and to set the semaphore value through a semaphore set identifier Mutex:1 empty:10 full:0
	The first two parameters are used to generate a key. */if (Semid=createsem (".", ' t ', 3,0)) ==-1) {if (errno==eexist) {if (Semid=opensem (".", ' t ')) ==-1) {exit (1)
			;
			} else {perror ("Semget error:");
		Exit (1);
	        } else {Union Semun arg;
         	Seting value for mutex semaphore arg.val=1;
	        	if (Semctl (semid,0,setval,arg) ==-1) {perror ("Setting semaphore value failed\n");
            	return-1;
            	}//set value for synchronous semaphore Arg.val=num;
            	The NUM means that's producer can continue to produce NUM products if (semctl (semid,1,setval,arg) ==-1)
	         	{perror ("Setting semaphore value failed\n");
         	return-1; //the semaphore ' s value is default//the default value ' 0 ' MeAns That's consumer is not with any product now} int goods=0;
		while (1) {p (semid,1);
		Sleep (3);
		P (semid,0);
		Producer is producing a product goods=rand ()%10;
		Put the cargo in the buffer number of indexaddr[0] into the goods shmaddr[indexaddr[0]]=goods;
		printf ("producer:%d produces a product[%d]:%d\n", Getpid (), indexaddr[0],goods);
		The index value is +1 at the same time.
		indexaddr[0]= (indexaddr[0]+1)%10;
		V (semid,0);
		Sleep (3);
	V (semid,2);
 }

}



#include <shm.h> int main () {int num;
	int shmid_goods,shmid_index,semid;
	char* Shmaddr=null;
	int *indexaddr=null;

	int is_noexist=0;
	num=10; Create the shared memory area of the goods and link to the process user space.
	Size is 10 bytes.
				if ((Shmid_goods=createshm (".", ' s ', num)) ==-1 {if (errno==eexist) {if (Shmid_goods=openshm (".", ' s ')) ==-1) {
			Exit (1);
		        } else {perror ("Create shared Memory failed\n");
		Exit (1);
		}//Returns the identifier of the shared memory area if ((Shmaddr=shmat (Shmid_goods, (char*) 0,0) = = = (char*)-1) {perror ("Attach shared Memory error\n");
	Exit (1);
    /* Create a shared memory that holds the index number of the producer and consumer in buffer.
	The index points to the index value of the production/consumption buffer.
				*/if (SHMID_INDEX=CREATESHM (".", ' Z ', 2)) ==-1) {if (errno==eexist) {if (Shmid_index=openshm (".", ' Z ')) ==-1) {
			Exit (1);
		        } else {perror ("Create shared Memory failed\n");
		Exit (1);
	} else {is_noexist=1; //attach the shared memory to the current process if (Indexaddr=shmat (Shmid_index, (int*) 0,0)) = = (int*)-1) {perror (" Attach shared MEmory error\n ");
	Exit (1);
		/* index initialized to 0/if (is_noexist) {indexaddr[0]=0;
	indexaddr[1]=0;
	/* This function is used to generate a semaphore set and to set the semaphore value through a semaphore set identifier Mutex:1 empty:10 full:0
	The first two parameters are used to generate a key. */if (Semid=createsem (".", ' t ', 3,0)) ==-1) {if (errno==eexist) {if (Semid=opensem (".", ' t ')) ==-1) {exit (1)
			;
			} else {perror ("Semget error:");
		Exit (1);
	        } else {Union Semun arg;
         	Seting value for mutex semaphore arg.val=1;
	        	if (Semctl (semid,0,setval,arg) ==-1) {perror ("Setting semaphore value failed\n");
            	return-1;
            	}//set value for synchronous semaphore Arg.val=num;
            	The NUM means that's producer can continue to produce NUM products if (semctl (semid,1,setval,arg) ==-1)
	         	{perror ("Setting semaphore value failed\n");
         	return-1; //the semaphore ' s value is default//the default value ' 0 ' MeAns That's consumer is not with any product now} int goods=0;
		while (1) {p (semid,1);
		Sleep (3);
		P (semid,0);
		Producer is producing a product goods=rand ()%10;
		Put the cargo in the buffer number of indexaddr[0] into the goods shmaddr[indexaddr[0]]=goods;
		printf ("producer:%d produces a product[%d]:%d\n", Getpid (), indexaddr[0],goods);
		The index value is +1 at the same time.
		indexaddr[0]= (indexaddr[0]+1)%10;
		V (semid,0);
		Sleep (3);
	V (semid,2);
 }

}



#include <shm.h> int main (int argc,char **argv) {int num;
	int shmid_goods,shmid_index,semid;
	char* Shmaddr=null;
	int* Indexaddr=null;

	int is_noexist=0;
	num=10;
	Create a shared memory as goods buffer//Note that the same shared area of the production consumer uses the same parameters,//So the same key value is Shancheng.
				if ((Shmid_goods=createshm (".", ' s ', num)) ==-1 {if (errno==eexist) {if (Shmid_goods=openshm (".", ' s ')) ==-1) {
			Exit (1);
		        } else {perror ("Create shared Memory failed\n");
		Exit (1); }//attach the shared memory to the current process if (Shmaddr=shmat (Shmid_goods, (char*) 0,0)) = = (char*)-1) {Perro
		R ("Attach Shared Memory error\n");
	Exit (1); //create a shared memory as index if (Shmid_index=createshm (".", ' Z ', 2)) ==-1) {if (errno==eexist) {if (shmid_
			Index=openshm (".", ' Z ')) ==-1) {exit (1);
		        } else {perror ("Create shared Memory failed\n");
		Exit (1);
	} else {is_noexist=1; }//attach the shared memory to the current process if (indexadDr=shmat (Shmid_index, (int*) 0,0)) = = (int*)-1) {perror ("Attach shared Memory error\n");
	Exit (1);
		} if (is_noexist) {indexaddr[0]=0;
	indexaddr[1]=0;
			//create a semaphore set including 3 semaphores if (Semid=createsem (".", ' t ', 3,0)) ==-1) {if (errno==eexist) {
			if ((Semid=opensem (".", ' t ')) ==-1) {exit (1);
			} else {perror ("Semget error:");
		Exit (1);
	        } else {Union Semun arg;
         	Seting value for mutex semaphore arg.val=1;
	        	if (Semctl (semid,0,setval,arg) ==-1) {perror ("Setting semaphore value failed\n");
            	return-1;
            	}//set value for synchronous semaphore Arg.val=num;
            	The NUM means that's producer can continue to produce NUM products if (semctl (semid,1,setval,arg) ==-1)
	         	{perror ("Setting semaphore value failed\n");
         	return-1; //the semaphore ' s value is default//the dEfault value ' 0 ' means that's the consumer is not with any product now} int goods=0;
		while (1) {p (semid,2);
		Sleep (1);
		P (semid,0);
		Take out a cargo.
		GOODS=SHMADDR[INDEXADDR[1]];
		printf ("consumer:%d consumes a product[%d]:%d\n", Getpid (), indexaddr[1],goods);
		Index value plus one indexaddr[1]= (indexaddr[1]+1)%num;
		V (semid,0);
		Sleep (1);
	V (semid,1);
 }

}



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.