Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
38
Добавлен:
06.02.2018
Размер:
1.27 Кб
Скачать
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <sys/sem.h>

void main()
{
	char *pm, *pm1, book[]="abcdefghijklmnopqrstvxyz";
	int hmem, hsem;
	struct sembuf sop_lock[1] = {0, -1, 0};
	struct sembuf sop_unlock[1] = {0, 1, 0};
	printf("Begin work 1\n");
	hmem=shmget(1801197, 4000, IPC_CREAT | 0600);
	if (hmem == -1)
	{
		printf("Error AllocSharedMem with:");
		getchar();
		exit(0);
	}
	hsem = semget(1801196, 1, 0600 | IPC_CREAT);
	if(hsem == -1)
	{
		printf("Error create semaphore:");
		getchar();
		exit(0);
	}
	semop(hsem, &sop_unlock[0], 1);
	pm=shmat(hmem, NULL, 0);
	if (pm==NULL) 
	{
		printf("shmat");
		exit(3);
	}
	sleep(10);
	semop(hsem, &sop_lock[0], 1);
	printf("Text has been loaded\n");
	strcpy(pm, "This text has been loaded");
	semop(hsem, &sop_unlock[0], 1);
	sleep(10);
	pm1 = malloc(1000);
	if(pm1 == NULL)
	{
		printf("Error malloc");
		getchar();
		exit(0);	
	}
	for(int i = 0; i < 24; i++)
	{
		printf("Trying to write, offset %d, \'%c\'...\n",(399*i + i), book[i]);
		*(pm1 + 399*i + i) = book[i];
		printf("\'%c\' write in adress = %p, offset = %d\n\n",book[i],(pm1+399*i + i),(399 * i + i));
		sleep(2);

	}
	getchar();
	
	exit(0);
}
Соседние файлы в папке линукс