i want do make synchronization of accesing to mapped memory using semaphores just to demonstrate how semaphores work . point is : when i use fork ill ignore proces with pid == 0 and in PID != 0 i want to do following : this proces taht i will create using fork will either read from mapped memmory or write to mapped memmory. so if i open 2 terminals and on all terminal ill create N proceses accesing same mapped memmory i want to synchronize it with semaphores .. i have following code that i wrote and cant fix it bcs im kinda new to C programmin and id need some help advice or whatever can anyone help me ?

Code:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <time.h>
#include <sys/sem.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#define FILE_LENGTH 0x400
#define SEM_RESOURCE_MAX 1
#define SEMMSL 10

void opensem(int *sid, key_t key);

union semun {
	int val;
	struct semid_ds *buf;
	unsigned short int *array;
};

int createsem(int *sid, key_t key, int members){
	int cntr;
	struct semid_ds mysemds;
	union semun semopts;
	
	if (members > SEMMSL) {
		printf("uz mame max semaforov\n");
		return 1;
	}


	printf("idem vytvorit novy semafor set s %d clenmi \n",members);
	
	if ((*sid = semget(key, members, IPC_CREAT | IPC_EXCL | 0666)) == -1 ) {
		fprintf(stderr,"semafor set uz existuje\n");
		return 1;
	}

	semopts.val = SEM_RESOURCE_MAX;
	for (cntr = 0 ; cntr < members; cntr++)
		semctl(*sid, cntr, SETVAL, semopts);

	return 0;
}


void removesem(int sid) {

	semctl(sid,0,IPC_RMID,0);
	printf("semafor odstraneny\n");
}

int getval(int sid, int member){
	int semval;
	semval = semctl(sid,member, GETVAL,0);
	return(semval);

}

void dispval(int sid, int member) {
	int semval;
	semval = semctl(sid, member, GETVAL, 0);
	printf("semval pre clena %d je %d\n", member, semval);
	 	
}

void locksem(int sid, int member) {
	struct sembuf sem_lock = {member, -1, SEM_UNDO};
	
	if ((semop(sid, &sem_lock, 1)) == -1) {
		fprintf(stderr,"Lock semaforu sa dodrbal\n");
		exit(1);
	}
	else {
		printf("semafor resource sa znizil o 1 (lockol som ho)\n");
	}
	dispval(sid,member);
	
}

void unlocksem(int sid, int member) {
	struct sembuf sem_unlock = {member, 1, SEM_UNDO};
	int semval;
	
	semval = getval(sid, member);
	if (semval == SEM_RESOURCE_MAX ) {
		fprintf(stderr, "semafor je zavrety \n");
		exit(1);
	}
	if ((semop(sid,&sem_unlock,1)) == -1 ) {
		fprintf(stderr,"Unlock zlyhal\n");
		exit(1);

	}
	else {
		printf("semafor resource sa zvysil o 1 (unlockol som ho) \n");
		dispval(sid,member);
	}
}

void opensem(int *sid, key_t key)
{
        /* Open the semaphore set - do not create! */
        if((*sid = semget(key, 0, 0666)) == -1)
        {
                printf("Semaphore set does not exist!\n");
                exit(1);
        }
}

int main(int argc, char *argv[])
{
       // initialize semaphores
	//system("bash kill_sem.sh");
	key_t	key;
	int	semset_id;
	char	c;
	int	fd;

	int 	i;
	int 	pid;
	char* 	file_memory, *s;
	key = ftok("~",'s');
	fd = open("mp.txt",O_RDWR | O_CREAT , S_IRUSR | S_IWUSR);
	
	lseek(fd, FILE_LENGTH  + 1,SEEK_SET);
	write(fd, "", 1);
	lseek(fd, 0, SEEK_SET);
	file_memory = mmap(0, FILE_LENGTH, PROT_WRITE,MAP_SHARED,fd,0);
	close(fd);

	if ( createsem(&semset_id, key, 1) == 1) {
		//opensem(&semset_id,key);
	}
	int tmp_len = 0;
	//s = file_memory;

	//sprintf((char*)file_memory,"%d\n",65);
	//munmap(file_memory,FILE_LENGHT);
       // create childs
	
	s = file_memory;
	//locksem(semset_id, 0);
       	for ( i  = 0;   i<= 5 ; i++) {
		//locksem(semset_id, 0);
		//char* buffer;
		pid = fork();
		if ( pid == 0 ) {
		}
               	else { // ak druhy proces zapis pid.
			if ( getval(semset_id,0) == 1 ) {
	   			locksem(semset_id, 0);
				printf("moj proces ma PID %d\n",pid);
    				sprintf((char *)(s),"%d\n",pid);
				printf("- IF >>>>>");
				//removesem(semset_id);
				munmap(file_memory, FILE_LENGTH);
				unlocksem(semset_id,0);
				break;
				sleep(1);
			} else { 
				printf("- ELSE >>>>>");
				locksem(semset_id, 0);
	       			for ( s = file_memory; *s != '\n'; s++){
                        		putchar(*s);
                        		putchar('\n');
                		} // for 
				printf("- ELSE >>>>>");
				unlocksem(semset_id, 0);
				munmap(file_memory, FILE_LENGTH);
				sleep(1);
	       		} //else
			break;	
		} // for
		sleep(1);
        	//munmap(file_memory, FILE_LENGTH);
	}
       
	//removesem(semset_id);
	//munmap(file_memory, FILE_LENGTH);

       return EXIT_SUCCESS;
}
thx for any help