Sairam all,

Linux Man pages say
> "LinuxThreads currently does not support process-shared
> semaphores, thus!sem_init! always returns with error !ENOSYS! if
> |pshared| is not zero."
>
> I have a shared memory area I need to lock(using semaphore). Is there a Linux preferred
> method to share semaphores across processes?

Please send me the code too if possible

Sairam

Hi,

I've used Semaphores before & I would love to help you but I don't really understand your problem.

Sairam all,
Sorry I didn't frame the question properly I think.

Problem:
> Now I have two processes (created by fork system call) and now they try to access same memory area. I want only one process to access memory at any point in time. I want to achieve this by using Semaphore.
But to do so I need a global semaphore that can be used by both processes.

For instance initially sem_init system call can be used with third parameter value being set to 1.And so first process gets access and after entering critical region it calls sem_wait which makes semaphore value 0.So second process cannot access , and after first process leaves the shared memory semaphore value is set to 1 by calling sem_post system call.

This is how I want to use a shared semaphore. But........
Linux Man pages say
>"LinuxThreads currently does not support process-shared
> semaphores, thus!sem_init! always returns with error !ENOSYS! if
> |pshared| is not zero."

Does it mean that two processes cannot share a single semaphore.

So what should I do.If the sem_init woks well on SUSE Linux what should be the SECOND argument.

Please do explain and if possible send me C code snippets .
Thanking u.
Sairam

Interprocess Communications in Linux: The nooks & crannies. By John Shapley.

This book will help you to understand Semaphores & shared memory, & how to organize the access.

This is to create a semaphore set of 2.

semKey = ftok(".", 'S');
   /* Create the Semaphore that organizes the Attacks */
   if((semid = semget((int) pid, 2, IPC_CREAT | IPC_EXCL | 0660)) == -1){
      perror("semget: IPC_CREAT | 0660");
      exit(1);
   }

   /* Initialize Semaphore set as shown in array */
   arg.array = array;                              //    array = {1,0}
   if(semctl(semid, 0, SETALL, arg) == -1){
      perror("semctl: SETALL");
      exit(2);
   }

This is how the first process would acquire the semaphore, note for this process myNum = 0, for the other process it would equal 1, I usually pass myNum as a parameter when creating the process.

/* Gain access to Semaphore  */
   if((semid = semget((int) gpid, 2, 0)) == -1){ 
     perror("semget -- child -- obtaining combat semaphore");
     exit(1);
   }
  
   /* Acquire my  Semaphore */
   acquire.sem_num = myNum;        
   if(semop(semid, &acquire, 1) == -1 ){
      perror("semop -- child -- waiting to acquire the combat");
      exit(2);	
   }

After that do whatever operations you want restricted by semaphore access.

To release the Semaphore to the next process:

/* Release Semaphore to the other Process*/
    release.sem_num = (myNum+1)%2;  
    if(semop(semid, &release, 1) == -1){
      perror("semop -- child -- indicating combat has been required");
      exit(3);
    }

You need to have this in your header file if you have one, the important thing is it needs to be global for all the processes

struct sembuf acquire = {0, -1, SEM_UNDO}, 
                       release = {0,  1, SEM_UNDO};

Sorry I took long to reply, but I'm new here & I've been trying to figure out how to make the code highlighted as you can see I didn't get how I could do it lol.

Hope this helps.

commented: nice effort +9

I've been trying to figure out how to make the code highlighted as you can see I didn't get how I could do it lol.

FYI

[code=c] stuff here will be syntax-highlighted

[/code]

:)

FYI

[code=c] stuff here will be syntax-highlighted

[/code]

:)

I actually tried that, along with several other combinations, but it wouldn't work, maybe that day something was wrong lol. Either way thanks for the reply :).

Member Avatar for dmachop

Here's the code you've asked for...........

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#define semkey ((key_t)27835)
#define shmkey ((key_t)27834)

int main()
{
struct sembuf sema;
int shmem,semid,prsid,n;
char *shptr,str[30],str1[50]={"Inside child:"};
shmem=shmget(shmkey,50,IPC_CREAT|0666);
if(shmem==-1)
printf("error");
shptr=shmat(shmem,0,IPC_CREAT|0666);
semid=semget(semkey,1,IPC_CREAT|0666);
semctl(semid,0,SETVAL,1);
prsid=fork();
if(prsid>0)
{
printf("\nEnter message:");
scanf("%s",str);
strcpy(shptr,str);
semctl(semid,0,SETVAL,0);
wait(NULL);
}
if(prsid==0)
{
sema.sem_num=0;
sema.sem_op=0;
sema.sem_flg=0;
semop(semid,&sema,1);
write(1,str1,14);
write(1,shptr,strlen(shptr)+1);
printf("\n");
}
if(prsid<0)
printf("Unable to fork()");
}

Hope it helps you.................
Any doubts ask me.........

SNIP

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.