semctl()

2 views
Skip to first unread message

Hercules

unread,
Feb 27, 2006, 9:21:55 AM2/27/06
to linuxtalks
I want to lock the shared memory. my project also uses the same
location. It always uses the key vale 1256. Before my project going to
lock, I want to lock the memory by external program. Once it locked by
external program the try to lock the memory again by my project.
Deadlock situation occurs. That situation I want.
My program is (CHECK LINE NO 36. I am using the same key value. It I
give this key to shmget() it returns file already use. YES my project
uses the same location.

My question is how to lock a used shared memory by an external program.
what re the parameter shud i give in shmctl() to lock... I am not clear
shmid. bcause if i use shmget() it returns file exists error.

14 main()
15 { int i,j,k=5;
16 int pid;
17 int semid; /* semid of semaphore set */
18 key_t key = 1234; /* key to pass to semget() */
19 int semflg = IPC_CREAT | IPC_EXCL | 0666; /* semflg to pass
to semget() */
20 int nsems = 1; /* no of elements in the sem array nsems to
pass to semget() */
21 int nsops; /* number of operations to do */
22 struct sembuf *sops = (struct sembuf *)
malloc(2*sizeof(struct sembuf));
23 /* ptr to operations to perform */
24
25 /* set up semaphore */
26
27 (void) fprintf(stderr, "\nsemget: Setting up seamaphore:
semget(%#lx, %\
28 %#o)\n",key, nsems, semflg);
29 if ((semid = semget(key, nsems, semflg)) == -1) {
30 perror("semget: semget failed");
31 /* exit(1); */
32 } else
33 (void) fprintf(stderr, "semget: semget succeeded: semid
=\
34 %d\n", semid);
35
36 k=shmctl(1234,SHM_LOCK,0);
37 if (k == 0)
38 {
39 printf("%s \n", "Lock Succcessful ");
40 }
41 else
42 {
43 printf("%s error number is %d\n", "Lock unsuccessful",errno);
44 }
45
46
47 }

Reply all
Reply to author
Forward
0 new messages