コード例 #1
0
ファイル: producer.c プロジェクト: Cortys/KMS
int main() {
	int shmId = createSharedMemory();

	if(shmId == -1) {
		perror("Could not create shared memory");
		return 1;
	}

	Buffer* buffer;

	if(sharedBufferInit(shmId, &buffer, BUFFERSIZE)) {
		perror("Could not initialize buffer");
		return 1;
	}

	fprintf(stderr, "%d", shmId);

	sigset_t sigset;
	int sig;

	sigemptyset(&sigset);
    sigaddset(&sigset, SIGUSR1);
	sigprocmask(SIG_BLOCK, &sigset, NULL);

	printf("[%s] Producer is ready. Waiting for SIGUSR1 to PID=%d.\n", getTime(), getpid());

	sigwait(&sigset, &sig);

	generateData(buffer);

	destroySharedMemory(shmId);

	return 0;
}
コード例 #2
0
ファイル: clean.c プロジェクト: DrewFitz/OS-Assignment-5
int main(int argc, char const *argv[])
{
	if (argc != 2)
	{
		printf("usage: %s <data file>\n", argv[0]);
		exit(4);
	}

	int semaphores = getSemaphores();
	semaphoreWait(semaphores, WRITER);

	FILE* outputFile;
	outputFile = fopen(argv[1], "w");
	if (outputFile == 0)
	{
		printf("error: couldn't open output file");
		exit(5);
	}

	StudentInfo* basePointer = getSharedMemoryPointer();
	StudentInfo* currentPointer = basePointer;

	for (int i = 0; i < MAX_STUDENTS; ++i)
	{
		fprintf(outputFile, "%s\n", currentPointer->studentName);
		fprintf(outputFile, "%s\n", currentPointer->studentID);
		fprintf(outputFile, "%s\n", currentPointer->address);
		fprintf(outputFile, "%s\n", currentPointer->telephoneNumber);
		
		currentPointer++;
	}

	fclose(outputFile);

	detachSharedMemoryPointer(basePointer);

	destroySharedMemory();
	destroyReadCount();

	semaphoreSignal(semaphores, WRITER);

	destroySemaphores(semaphores);
	return 0;
}
コード例 #3
0
bool CrashHandlerImpl::initialize() {
	Autolock autoLock(&m_Lock);
	
	bool initialized = true;
	
	m_CrashHandlerPath = platform::getHelperExecutable(m_CrashHandlerApp);
	
	if(!createSharedMemory()) {
		return false;
	}
	
	fillBasicCrashInfo();
	
	if(!registerCrashHandlers()) {
		destroySharedMemory();
		initialized = false;
	}

	return initialized;
}
コード例 #4
0
bool CrashHandlerImpl::initialize() {
	Autolock autoLock(&m_Lock);
	
	bool initialized = true;
	
	fs::path local_path = fs::path(getExecutablePath());
	if(!local_path.empty()) {
		local_path = local_path.parent() / m_CrashHandlerApp;
		if(fs::exists(local_path)) {
			m_CrashHandlerPath = local_path;
		}
	}
	if(m_CrashHandlerPath.empty()) {
		local_path = m_CrashHandlerApp;
		if(fs::exists(local_path)) {
			m_CrashHandlerPath = local_path;
		}
	}
	if(fs::libexec_dir && m_CrashHandlerPath.empty()) {
		local_path = fs::path(fs::libexec_dir) / m_CrashHandlerApp;
		if(fs::exists(local_path)) {
			m_CrashHandlerPath = local_path;
		}
	}
	
	if(!createSharedMemory()) {
		return false;
	}
	
	fillBasicCrashInfo();
	
	if(!registerCrashHandlers()) {
		destroySharedMemory();
		initialized = false;
	}

	return initialized;
}
コード例 #5
0
int main(int argc, char *argv[]) {
	num_thrd = atoi(argv[4]);

	FILE *fr1, *fr2, *fw; //File pointers
	//int **mat1, **mat2, **solution; //To hold the matrix from the input file
	int rc1, rc2; //To hold the number of rows and columns
	int toStdOut = 1; //This is a flag variable. 0 for file output,1 for std out
	char line[charCount]; //for extract a char of lines

	//Check for the number of command line arguments
	if (argc != 5) {
		//Print output to standard output
		toStdOut = 0;
	}

	//To read a file use the fopen() function to open it
	fr1 = fopen(argv[1], "r");

	//If the file fails to open the fopen() returns a NULL
	if (fr1 == NULL) {
		printf("Cannot open %s. Program terminated...", argv[1]);
		exit(1);
	}

	// Similar to the above method read the second file
	fr2 = fopen(argv[2], "r");
	if (fr2 == NULL) {
		printf("Cannot open %s. Program terminated...", argv[2]);
		exit(1);
	}

	//get matrice sizes and check if they are equal so they can be multiplied
	fgets(line, 10000, fr1);
	rc1 = matSize(line);
	fgets(line, 10000, fr2);
	rc2 = matSize(line);
	fclose(fr1);
	fclose(fr2);

	fr1 = fopen(argv[1], "r");

	//If the file fails to open the fopen() returns a NULL
	if (fr1 == NULL) {
		printf("Cannot open %s. Program terminated...", argv[1]);
		exit(1);
	}

	// Similar to the above method read the second file
	fr2 = fopen(argv[2], "r");
	if (fr2 == NULL) {
		printf("Cannot open %s. Program terminated...", argv[2]);
		exit(1);
	}

	if (rc1 == rc2) {
		rc = rc1;
		//memory allocation for arrays

		//-----------------------------------------------------------
		//load up both matricies
		if ((structId = shmget(IPC_PRIVATE, sizeof(sharedStruct),
		IPC_CREAT | 0666)) < 0) {
			perror("smget returned -1\n");
			error(-1, errno, " ");
			exit(-1);
		}

		if ((Cms = sharedMemoryAttach()) == (sharedStruct *) -1) {
			perror("Process shmat returned NULL\n");
			error(-1, errno, " ");
		}

		matrixLoad(fr1, Cms->matrix1);
		matrixLoad(fr2, Cms->matrix2);

		pthread_mutexattr_t attr;
		pthread_mutexattr_init(&attr);
		pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
		pthread_mutex_init(&Cms->lock, &attr);

		base_pid = getpid();
		for (int i = 1; i < num_thrd; i++) {
			if (getpid() == base_pid)
				fork();
			else {
				if ((Cms = sharedMemoryAttach()) == (sharedStruct *) -1) {
					perror("Process shmat returned NULL\n");
					error(-1, errno, " ");
				}
				break;
			}
		}

		matrixMultiply(Cms);

		if (getpid() == base_pid) {
			for (int i = 1; i < num_thrd; i++) {
				wait(NULL);
			}
		} else {
			sharedMemoryDetach();
		}

		fclose(fr1);
		fclose(fr2);
	} else {
		printf("Program terminated. Both matrices need to be the same size");
		exit(1);
	}

	//Once your output matrix is made, check if it is a standard output
	//or output to file
	if (getpid() == base_pid) {
		if (toStdOut == 1) {
			//Create the output file
			fw = fopen(argv[3], "w");
			if (fw == NULL) {
				printf("File not created. Program terminated...");
				exit(1);
			}

			matrixToFile(fw);
			fclose(fw);

		} else {
			printMatrix(Cms->solution);

		}
	}

	pthread_mutex_destroy(&Cms->lock);
	sharedMemoryDetach();
	destroySharedMemory();

	return 0;
}
コード例 #6
0
void CrashHandlerImpl::shutdown() {
	Autolock autoLock(&m_Lock);
	
	unregisterCrashHandlers();
	destroySharedMemory();
}
コード例 #7
0
ファイル: XLibRenderer.c プロジェクト: TheCrazyT/jmf
JNIEXPORT jboolean JNICALL
Java_com_sun_media_renderer_video_XLibRenderer_xlibSetOutputSize(JNIEnv *env,
								  jobject blitter,
								  jint outWidth,
								  jint outHeight)
{
    /* We assume that this function is called only if there is a size change */
    XlibBlitter *xblitter = (XlibBlitter*) GetIntField(env, blitter, "blitter");
    int inWidth = xblitter->inWidth;
    int inHeight = xblitter->inHeight;
    int stride   = xblitter->inStride;

    awtLock(env, xblitter);
    
    /* Get rid of the old image and arrays */
    if (xblitter->ximage) {
	/* Clean up shared memory */
	if (xblitter->shmActive) {
	    destroySharedMemory(xblitter);
	}
	xblitter->ximage->data = NULL;
	XDestroyImage(xblitter->ximage);
    }

    if (xblitter->scaledData) {
	free(xblitter->scaledData);
	xblitter->scaledData = NULL;
    }
    
    if (xblitter->xincs) {
	free(xblitter->xincs);
	xblitter->xincs = NULL;
    }

    if (xblitter->yincs) {
	free(xblitter->yincs);
	xblitter->yincs = NULL;
    }
    
    xblitter->outWidth = outWidth;
    xblitter->outHeight = outHeight;

    if (  outWidth != xblitter->inWidth ||
	  outHeight != xblitter->inHeight) {
	/* We need to scale */
	xblitter->xincs = (char *) malloc(outWidth * sizeof(char));
	xblitter->yincs = (char *) malloc(outHeight * sizeof(char));

	{
	    int x, y, oldValue, newValue, xtotal;
	    oldValue = 0; xtotal = 0;
	    for (x = 1; x < xblitter->outWidth; x++) {
		newValue = (int)((double)(x * xblitter->inWidth) / outWidth);
		xblitter->xincs[x-1] = newValue - oldValue;
		xtotal += newValue - oldValue;
		oldValue = newValue;
	    }
	    xblitter->xincs[x - 1] = xblitter->inStride - xtotal;
	    oldValue = 0;
	    for (y = 1; y < xblitter->outHeight; y++) {
		newValue = (int)((double)(y * inHeight) / outHeight);
		xblitter->yincs[y-1] = newValue - oldValue;
		oldValue = newValue;
	    }
	}

	stride = outWidth;
    } else {
	/* No scaling required */
	xblitter->xincs = NULL;
	xblitter->yincs = NULL;
	xblitter->scaledData = NULL;
    }

    /* Try to allocate a shared memory image */
    if (xblitter->shmAvailable) {
	createSharedMemory(xblitter, outWidth, outHeight);
    }
    
    /* If a shared memory image could not be created and we need to scale, use XLib */
    if (xblitter->shmActive == 0) {
	if ( outWidth != xblitter->inWidth || outHeight != xblitter->inHeight) {
	    xblitter->scaledData = (void *) malloc(outWidth * outHeight *
						   (xblitter->bitsPerPixel >> 3));
	}

	xblitter->ximage = XCreateImage(xblitter->display, xblitter->visual,
					xblitter->depth, ZPixmap, 0,
					(char *)0, outWidth, outHeight,
					xblitter->bitsPerPixel,
					stride * (xblitter->bitsPerPixel >> 3));
    }