اكواد خاصه بببرمجه نظم التشغيل c++

semaphore.c

//***********************************************************************
//
// Name:        semaphore.c
//
// Author:      Poonam Hajgude
//
// Description: Main program creates three semaphores and two threads 
//              to solve producer & Consumer problem. 
//
// Created:     March 21st, 2004.
// 
//***********************************************************************

#include <windows.h>
#include <stdio.h>
#include<conio.h>

const INT numBufSegs=10;

char pData, cData;
char Buffer[10];
char alphaBet[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

INT pindeX=0,cindeX=0,i=0,oktr=0;

HANDLE pHandle,cHandle,full,empty,mutex;
DWORD threadID;

void Producer()
{
	printf("Producer thread startting
");
	while(1)
	{
		//Produce Data
		pData=alphaBet[i++];
		WaitForSingleObject(empty,INFINITE);
		WaitForSingleObject(mutex,INFINITE);
		//add data to buffer
		Buffer[pindeX++]=pData;
		pindeX = pindeX % numBufSegs;
		ReleaseSemaphore(mutex,1,0);
		ReleaseSemaphore(full,1,0);
	}
	printf("Producer thread existing
");
}

void Consumer()
{
	printf("Consumer thread startting
");
	printf("Data are
");
	for(oktr=0;oktr<26;oktr++)
		{
		WaitForSingleObject(full,INFINITE);
		WaitForSingleObject(mutex,INFINITE);
		//move data from buffer
		cData=Buffer[cindeX++];
		cindeX=cindeX % numBufSegs;
		ReleaseSemaphore(mutex,1,0);
		ReleaseSemaphore(empty,1,0);
		printf("%c ",cData);
		}
	printf("
Consumer thread existing
");
	TerminateThread(pHandle,1);
}



int main(void) 
{
  full=CreateSemaphore(0,0,numBufSegs,0);
  if(full==0)
	  printf("Can not create full Semaphore
");

  empty=CreateSemaphore(0,numBufSegs,numBufSegs,0);
  if(empty==0)
	  printf("Can not create empty Semaphore
");

  mutex=CreateSemaphore(0,1,1,0);
  if(full==0)
	  printf("Can not create mutex Semaphore
");

  //Create threads
  pHandle=CreateThread(0,0,(LPTHREAD_START_ROUTINE) Producer,0,0,&threadID);
  if(pHandle==0)
	  printf("Can not create thread for Producer
");

  cHandle=CreateThread(0,0,(LPTHREAD_START_ROUTINE) Consumer,0,0,&threadID);
  if(cHandle==0)
	  printf("Can not create thread for Consumer
");

  //Wait for Consumer thread to finish first
  WaitForSingleObject(cHandle,INFINITE);
  printf("Consumer thread termination
");

  //Wait for Producer thread to finish 
  WaitForSingleObject(pHandle,INFINITE);
  printf("Producer thread termination
");

  CloseHandle(full);
  CloseHandle(empty);
  CloseHandle(mutex);
  CloseHandle(pHandle);
  CloseHandle(cHandle);
  exit(1);
  
}

#include <windows.h>
#include <stdio.h>

#define SHMSIZE			16

HANDLE hMapFile;		// handle to the mapping file
enum threads { thread_a, thread_b };	// two threads

void threadfunc(LPVOID lpParm)
{
	int *threadid;
	LPVOID lpMapAddress;
	char *pData;
	int ix;

	threadid = (int *)lpParm;
	
	/**
	 * Create a map view of the mapping object.
	 */
	lpMapAddress = MapViewOfFile(hMapFile,				// handle to mapping object 
								FILE_MAP_ALL_ACCESS,    // read/write permission 
								0,                      // max. object size 
								0,                      // size of hFile 
								0);                     // map entire file 
	 
	if (lpMapAddress == NULL) 
	{ 
		fprintf(stderr, "Could not map view of file.
");
	}

	pData = (char *) lpMapAddress;
	for (ix=0; ix<SHMSIZE; ix++) {
		if (*threadid == thread_a) {
			printf("Thread %d is writing a 'A' on iteration %d ...
", thread_a, ix);
			*pData++ = 'A';
			Sleep(500);
		}
		else if (*threadid == thread_b) {
			printf("Thread %d is writing a 'B' on iteration %d ...
", thread_b, ix);
			*pData++ = 'B';
			Sleep(1000);
		}
	}

	printf("Thread %d exited.
", *threadid);
}

int main(void) {
	int ix;
	int thread_a_id, thread_b_id;
	HANDLE threadA, threadB;
	LPVOID lpMapAddress;
	char *pData;

	/**
	 * Create a mapping object.
	 * The allocated memory is backed by the Windows memory paging system.
	 */
	hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,    // current file handle 
								NULL,                              // default security 
								PAGE_READWRITE,                    // read/write permission 
								0,                                 // max. object size 
								SHMSIZE,                                 // size of hFile 
								NULL);							// name of mapping object 
	 
	if (hMapFile == NULL) 
	{ 
		fprintf(stderr, "Could not create file mapping object.
"); 
		return -1;
	}

	thread_a_id = thread_a;
	thread_b_id = thread_b;

	threadA = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadfunc,&thread_a_id,0,NULL);
	if (threadA == NULL) {
		printf("Creating the thread A failed.
");
		return -1;
	}

	threadB = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadfunc,&thread_b_id,0,NULL);
	if (threadB == NULL) {
		printf("Creating the thread B failed.
");
		return -1;
	}

	/**
	 * The main function also create a map of view to the
	 * shared memory by the two threads.
	 */
	lpMapAddress = MapViewOfFile(hMapFile,				// handle to mapping object 
								FILE_MAP_ALL_ACCESS,    // read/write permission 
								0,                      // max. object size 
								0,                      // size of hFile 
								0);                     // map entire file 
	 
	if (lpMapAddress == NULL) 
	{ 
		fprintf(stderr, "Could not map view of file.
");
		return -1;
	}

	pData = (char *) lpMapAddress;
	WaitForSingleObject(threadA, INFINITE);
	
	/**
	 * Prints out what the shared memory contains after the first thread exits.
	 */
	fprintf(stdout, "Reading the shared memory in the main function (1st time) ...
");
	for (ix=0; ix<SHMSIZE; ix++) {
		putchar(*pData++);
	}
	fprintf(stdout, "
");

	pData = (char *) lpMapAddress;
	WaitForSingleObject(threadB, INFINITE);

	/**
	 * Prints out what the shared memory contains after the second thread exits.
	 */
	fprintf(stdout, "Reading the shared memory in the main function (2nd time) ...
");
	for (ix=0; ix<SHMSIZE; ix++) {
		putchar(*pData++);
	}
	fprintf(stdout, "
");

	CloseHandle(hMapFile);
	CloseHandle(threadA);
	CloseHandle(threadB);
}